Home  >  Article  >  Database  >  How to write foreign key constraints in mysql

How to write foreign key constraints in mysql

王林
王林Original
2020-07-08 11:14:5329761browse

Mysql foreign key constraint writing method: [[CONSTRAINT ] FOREIGN KEY field name REFERENCES primary key column 1]. A foreign key constraint is a special field of a table that is often used together with a primary key constraint.

How to write foreign key constraints in mysql

In the CREATE TABLE statement, specify the foreign key through the FOREIGN KEY keyword.

(Recommended learning: mysql tutorial)

The specific syntax format is as follows:

[CONSTRAINT <外键名>] FOREIGN KEY 字段名 [,字段名2,…]
REFERENCES <主表名> 主键列1 [,主键列2,…]

MySQL foreign key constraint (FOREIGN KEY) is a table Special field, often used with primary key constraints.

For two tables with an associated relationship, the table where the primary key in the associated field is located is the main table (parent table), and the table where the foreign key is located is the secondary table (child table).

When defining foreign keys, you need to comply with the following rules:

  • The main table must already exist in the database, or be the table currently being created. If it is the latter case, the master table and the slave table are the same table. Such a table is called a self-referential table, and this structure is called self-referential integrity. A primary key must be defined for the main table.

  • The primary key cannot contain null values, but null values ​​are allowed in foreign keys. That is, as long as every non-null value of the foreign key appears in the specified primary key, the contents of the foreign key are correct.

  • Specify the column name or a combination of column names after the table name of the main table. This column or combination of columns must be the primary key or candidate key of the main table.

  • The number of columns in the foreign key must be the same as the number of columns in the primary key of the main table.

  • The data type of the column in the foreign key must be the same as the data type of the corresponding column in the primary key of the main table.

Example:

In order to show the foreign key relationship between tables, this example creates a department table tb_dept1 in the test_db database. The table structure is as shown in the following table .

How to write foreign key constraints in mysql

The SQL statement and running results to create tb_dept1 are as follows.

mysql> CREATE TABLE tb_dept1
    -> (
    -> id INT(11) PRIMARY KEY,
    -> name VARCHAR(22) NOT NULL,
    -> location VARCHAR(50)
    -> );
Query OK, 0 rows affected (0.37 sec)

Create the data table tb_emp6, and create a foreign key constraint on the table tb_emp6, so that its key deptId is associated with the primary key id of the table tb_dept1 as a foreign key. The SQL statement and running results are as follows.

mysql> CREATE TABLE tb_emp6
    -> (
    -> id INT(11) PRIMARY KEY,
    -> name VARCHAR(25),
    -> deptId INT(11),
    -> salary FLOAT,
    -> CONSTRAINT fk_emp_dept1
    -> FOREIGN KEY(deptId) REFERENCES tb_dept1(id)
    -> );
Query OK, 0 rows affected (0.37 sec)

mysql> DESC tb_emp6;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(25) | YES  |     | NULL    |       |
| deptId | int(11)     | YES  | MUL | NULL    |       |
| salary | float       | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (1.33 sec)

After the above statement is successfully executed, a foreign key constraint named fk_emp_dept1 is added to table tb_emp6. The foreign key name is deptId, which depends on the primary key id of table tb_dept1.

Note: The foreign key of the secondary table must be related to the primary key of the primary table, and the data types of the primary key and the foreign key must be consistent. For example, both are of type INT, or both are of type CHAR. If such requirements are not met, the "ERROR 1005 (HY000): Can't create table" error will occur when creating the slave table.

The above is the detailed content of How to write foreign key constraints in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn