Bug #68286 | Cannot change FK constraint settings | ||
---|---|---|---|
Submitted: | 6 Feb 2013 5:42 | Modified: | 6 Feb 2013 15:01 |
Reporter: | Peter Laursen (Basic Quality Contributor) | Email Updates: | |
Status: | Verified | Impact on me: | |
Category: | MySQL Server: Parser | Severity: | S3 (Non-critical) |
Version: | 5.1, 5.5(.29), 2.6.7 | OS: | Windows (probably any) |
Assigned to: | CPU Architecture: | Any |
[6 Feb 2013 5:42]
Peter Laursen
[6 Feb 2013 7:56]
Valeriy Kravchuk
I'd say this is a documented limitation. Read http://dev.mysql.com/doc/refman/5.5/en/alter-table.html: "You cannot add a foreign key and drop a foreign key in separate clauses of a single ALTER TABLE statement. You must use separate statements."
[6 Feb 2013 8:48]
Peter Laursen
Why doesn't it return 1064 syntax error then? Changing category to 'parser' accordingly. The parser should catch this as an invalid statement, and a 1064 syntax error should be returned, I think.
[6 Feb 2013 15:00]
Peter Laursen
Let me add that I think a single ALTER TABLE is preferable for performance reasons (even with "online ALTER TABLE" as of 5.6).
[6 Feb 2013 15:01]
Matthew Lord
Verified in 5.6.10: mysql> select version(); +-----------+ | version() | +-----------+ | 5.6.10 | +-----------+ 1 row in set (0.00 sec) mysql> /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; Query OK, 0 rows affected (0.00 sec) mysql> mysql> CREATE TABLE `sometab` ( -> `inventory_id` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT, -> `film_id` SMALLINT(5) UNSIGNED NOT NULL, -> `store_id` TINYINT(3) UNSIGNED NOT NULL, -> `last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, -> PRIMARY KEY (`inventory_id`), -> KEY `idx_fk_film_id` (`film_id`), -> KEY `idx_store_id_film_id` (`store_id`,`film_id`), -> CONSTRAINT `fk_inventory_store` FOREIGN KEY (`store_id`) REFERENCES `store` (`store_id`), -> CONSTRAINT `fk_inventory_film` FOREIGN KEY (`film_id`) REFERENCES `film` (`film_id`) -> ) ENGINE=INNODB AUTO_INCREMENT=4582 DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.02 sec) mysql> mysql> /* Alter table in target */ mysql> ALTER TABLE `sometab` -> DROP FOREIGN KEY `fk_inventory_film` , -> ADD CONSTRAINT `fk_inventory_film` -> FOREIGN KEY (`film_id`) REFERENCES `film` (`film_id`) ON DELETE CASCADE ON UPDATE CASCADE , -> DROP FOREIGN KEY `fk_inventory_store` , -> ADD CONSTRAINT `fk_inventory_store` -> FOREIGN KEY (`store_id`) REFERENCES `store` (`store_id`) ON DELETE CASCADE ON UPDATE CASCADE ; ERROR 1826 (HY000): Duplicate foreign key constraint name 'test/fk_inventory_film' mysql> mysql> /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; Query OK, 0 rows affected (0.00 sec) I agree that this limitation is documented and thus should ideally be caught at the SQL layer before attempting to actually ALTER the table. I will copy this over to the developers so that they can take a closer look. Thank you for the great bug report, Peter!
[13 Apr 2019 13:10]
Dmitry Lenev
Posted by developer: Note that this limitation is still there in version 8.0.17-git. However, I don't agree that we should add check prohibiting such statements to parser, Instead, in my opinion, we should try to remove this limitation. There are no conceptual reasons behind it and we allow similar ALTER TABLE which remove and add the same object within the same statement for other types of objects like columns and keys.