HomeХобби и стильRelated VideosMore From: Jiansen Lu

MySQL foreign key constraint

18 ratings | 13984 views
CREATE TABLE users ( user_id int NOT NULL, user_name varchar(45) NOT NULL, PRIMARY KEY (user_id) ) CREATE TABLE pictures ( pic_id int NOT NULL, user_id int NOT NULL, PRIMARY KEY (pic_id), CONSTRAINT fk_pictures FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE ON UPDATE CASCADE ) ALTER TABLE pictures DROP FOREIGN KEY fk_pictures We have four options: RESTRICT | CASCADE | SET NULL | NO ACTION CASCADE: automatically delete or update SET NULL: the foreign key table is set NULL RESTRICT: Rejects the delete or update operation NO ACTION: the same as RESTRICT Recommend for InnoDB Storage Engine. More about this video: http://jiansenlu.blogspot.com/2014/06/example-of-mysql-foreign-key-constraint.html
Html code for embedding videos on your blog
Text Comments (3)
Jered (1 month ago)
Thank you Very much :) you save my system w3schools sucks sometimes
David Ivan de Lange (1 month ago)
THANK YOU SO F*CKING MUCH! I've been struggeling with my code for a week now, while my code was correct, but the key relationship was set on Restricted instead of Cascade. You are my hero, mate! :,D
Paranolverizer (1 year ago)
so here in CONSTRAINT fk_pictures , fk_pictures is only a name??

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.