0

I need to add foreign key to MySQL table with RESTRICT rule, and something really strange happens:

When I do:

ALTER TABLE smartphone_model_work 
ADD CONSTRAINT SmartphoneMW_Work
FOREIGN KEY(SmartphoneWorkID)
REFERENCES smartphone_work(SmartphoneWorkID)
ON DELETE RESTRICT;

It creates foreign key with "NO ACTION" rule, while I need "RESTRICT". But when I do:

ALTER TABLE smartphone_model_work 
ADD CONSTRAINT SmartphoneMW_Work
FOREIGN KEY(SmartphoneWorkID)
REFERENCES smartphone_work(SmartphoneWorkID)
ON DELETE CASCADE;

It creates "CASCADE" constraint as expected.

So how to create RESTRICT rule?

My MySQL version is 5.7.20

Thanks.

2
  • Form the MySql doc RESTRICT: Rejects the delete or update operation for the parent table. Specifying RESTRICT (or NO ACTION) is the same as omitting the ON DELETE or ON UPDATE clause. dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
    – nacho
    Commented Jul 25, 2019 at 15:36
  • Hmm... When I execute command: ALTER TABLE smartphone_model_work ADD CONSTRAINT SmartphoneMW_Work FOREIGN KEY(SmartphoneWorkID) REFERENCES smartphone_work(SmartphoneWorkID); It really creates RESTRICT rule. But why is that? Why only NOT pointing RESTRICT keyword creates RESTRICT rule?
    – dmitriybch
    Commented Jul 25, 2019 at 15:45

1 Answer 1

0

So, for whatever reason correct query is:

ALTER TABLE smartphone_model_work 
ADD CONSTRAINT SmartphoneMW_Work
FOREIGN KEY(SmartphoneWorkID)
REFERENCES smartphone_work(SmartphoneWorkID);

I.e. to create RESTRICT you should NOT use RESTRICT keyword. Looks like it's some kind of bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.