MySQL defines a foreign key method is the knowledge that everyone who learns MySQL needs to master, the following on the MySQL definition of FOREIGN KEY statement written in detail, for your reference.
Foreign keys bring a lot of benefits to MySQL, the following is to introduce you to the MySQL definition of FOREIGN KEY statement, and the MySQL definition of foreign keys in the process of error processing, for your reference to learn.
mysql> CREATE TABLE Categories (category_id tinyint (3) unsigned not NULL auto_increment, Nam e varchar (+) not NULL, PRIMARY KEY (category_id), Engine=innodb; Query OK, 0 rows affected (0.36 sec) mysql> INSERT into categories VALUES (1, ' SQL Server '), (2, ' Oracle '), (3, ' Post Gresql '), (4, ' MySQL '), (5, ' SQLite '); Query OK, 5 rows affected (0.48 sec) records:5 duplicates:0 warnings:0 mysql> CREATE TABLE members ( -member_id INT (one) UNSIGNED not NULL, name VARCHAR (+) NOT NULL, PRIMARY KEY (member_id) ) Engine=innodb; Query OK, 0 rows affected (0.55 sec) mysql> INSERT into members VALUES (1, ' Test '), (2, ' admin '); Query OK, 2 rows affected (0.44 sec) records:2 duplicates:0 warnings:0 mysql> CREATE TABLE articles ( -article_id INT (one) unsigned not NULL auto_increment, title varchar (255) is not NULL, Catego Ry_id tinyint (3) unsigned not NULL, member_id int (one) unsigned not NULL, INDEX (category_id), FOREIGN key (category_id) REFERENCES categories (category_id), CONSTRAINT fk_member FOREIGN KEY (mem BER_ID) REFERENCES members (member_id), PRIMARY KEY (article_id), Engine=innodb; Query OK, 0 rows affected (0.63 sec)
Note: For non-InnoDB tables, the FOREIGN key clause is ignored.
MySQL method for defining foreign keys