3

I have this piece of code to alter a tables relationship but I want to add cascade update and delete operations do I need to add cascade update cascade delete to this code

ALTER TABLE product_images
ADD FOREIGNKEY (product_id) REFERENCES products(product_id)

this is a one to many relationship I hope my code is right for a start. I have a products table and a product_images table which has many images for each product

Updated

ALTER TABLE product_images
ADD FOREIGN KEY (product_id) REFERENCES products(product_id)

2 Answers 2

2

Those referential actions need to be specified. RESTRICT is default.

2
  • I tried that piece of code and it says the syntax is wrong do you know another version of the code for it to create a relationship in mysql
    – ONYX
    Apr 12, 2013 at 1:54
  • @KDM the syntax is wrong because FOREIGNKEY needs to be FOREIGN KEY. Apr 12, 2013 at 1:54
0

I used this and it worked properly:

ALTER TABLE tb1
ADD CONSTRAINT relationship_name FOREIGN KEY fieldName REFERENCES tb2(fieldName);

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.