MySQL method for defining foreign keys

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.