There is nothing unexpected in this behavior.
When you create `test_a_b` table, indexes on `a` and `b` columns are added automatically to comply with foreign key requirements. If you check the table structure at this point, it looks like this:
CREATE TABLE `test_a_b` (
|
`a` int(11) NOT NULL,
|
`b` int(11) NOT NULL,
|
KEY `ref_a` (`a`),
|
KEY `ref_b` (`b`),
|
CONSTRAINT `ref_a` FOREIGN KEY (`a`) REFERENCES `test_a` (`id`),
|
CONSTRAINT `ref_b` FOREIGN KEY (`b`) REFERENCES `test_b` (`id`)
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1
|
(note `ref_a` and `ref_b` keys).
Then, when you add the primary key, `ref_a` key is no longer needed and gets automatically dropped. The table looks like this:
CREATE TABLE `test_a_b` (
|
`a` int(11) NOT NULL,
|
`b` int(11) NOT NULL,
|
PRIMARY KEY (`a`,`b`),
|
KEY `ref_b` (`b`),
|
CONSTRAINT `ref_a` FOREIGN KEY (`a`) REFERENCES `test_a` (`id`),
|
CONSTRAINT `ref_b` FOREIGN KEY (`b`) REFERENCES `test_b` (`id`)
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1
|
It is expected and documented behavior (the automatic drop is better documented in MySQL manual, while MariaDB KB could use some additions).
But then, you're trying to drop the primary key. It would mean there would be no key on `a` column anymore, and foreign key needs it, hence the problem.
This behavior exists in MariaDB 5.5-10.3 and MySQL 5.6/5.7. MySQL 8.0 (at least 8.0.3 which I have handy), doesn't throw the error, it allows the PK to be dropped, but it also silently drops both foreign keys along with it, so it's hardly a proper fix. However, it hints that some attempt has been made to change the behavior, so I'm assigning it to marko to decide if we want it to be changed on MariaDB. If not, please re-assign it to greenman for extending documentation in regard to automatic key creation/removal.
Update: 8.0.12 is back to the old behavior, it also doesn't allow to drop the PK, so I guess it was just a flaw in an early version.
There is nothing unexpected in this behavior.
When you create `test_a_b` table, indexes on `a` and `b` columns are added automatically to comply with foreign key requirements. If you check the table structure at this point, it looks like this:
(note `ref_a` and `ref_b` keys).
Then, when you add the primary key, `ref_a` key is no longer needed and gets automatically dropped. The table looks like this:
It is expected and documented behavior (the automatic drop is better documented in MySQL manual, while MariaDB KB could use some additions).
But then, you're trying to drop the primary key. It would mean there would be no key on `a` column anymore, and foreign key needs it, hence the problem.
This behavior exists in MariaDB 5.5-10.3 and MySQL 5.6/5.7. MySQL 8.0 (at least 8.0.3 which I have handy), doesn't throw the error, it allows the PK to be dropped, but it also silently drops both foreign keys along with it, so it's hardly a proper fix. However, it hints that some attempt has been made to change the behavior, so I'm assigning it to marko to decide if we want it to be changed on MariaDB. If not, please re-assign it to greenman for extending documentation in regard to automatic key creation/removal.
Update: 8.0.12 is back to the old behavior, it also doesn't allow to drop the PK, so I guess it was just a flaw in an early version.