Change ondelete behavior on SQL table
I have previously done:
ALTER TABLE `cms__model__file_taggable_tag` ADD CONSTRAINT
cms__model__file_taggable_tag_id_cms__model__file_id FOREIGN KEY (id) REFERENCES
cms__model__file (id), ADD CONSTRAINT
cms_开发者_Python百科_model__file_taggable_tag_tag_id_taggable_tag_id FOREIGN KEY (tag_id)
REFERENCES taggable_tag (id) ON DELETE CASCADE ON UPDATE CASCADE;
but I meant to execute (forgot to add on update cascade constraint):
ALTER TABLE `cms__model__file_taggable_tag` ADD CONSTRAINT
cms__model__file_taggable_tag_id_cms__model__file_id FOREIGN KEY (id) REFERENCES
cms__model__file (id) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT
cms__model__file_taggable_tag_tag_id_taggable_tag_id FOREIGN KEY (tag_id)
REFERENCES taggable_tag (id) ON DELETE CASCADE ON UPDATE CASCADE;
How can I remove the previous constraints and apply the correct one?
Tried:
ALTER TABLE `cms__model__file_taggable_tag` DROP CONSTRAINT
cms__model__file_taggable_tag_id_cms__model__file_id
got:
#1064 - You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'CONSTRAINT
cms__model__file_taggable_tag_id_cms__model__file_id' at line 1
Use SHOW CREATE TABLE tableName
to check if MySQL has actually created the constraint and if it has changed (truncated) its name.
Then use DROP FOREIGN KEY
, not DROP CONSTRAINT
:
ALTER TABLE cms__model__file_taggable_tag
DROP FOREIGN KEY cms__model__file_taggable_tag_id_cms__model__file_id
Check the MySQL docs: ALTER TABLE and FOREIGN KEY Constraints
First,
ALTER TABLE tablename DROP CONSTRAINT constraintname
Then, put your corrected constraints back in.
Edit: Didn't notice you were using MySQL. I believe MySQL's syntax is
ALTER TABLE tablename DROP INDEX indexname
Replace the word ADD with DROP in your ALTER clause
精彩评论