How to set foreign keys when creating a table in MySQL?
Mysql method to set foreign keys when creating a table: In the "CREATE TABLE" statement, pass "[CONSTRAINT
] FOREIGN KEY field name [, field name 2,...] REFERENCES < "Main table name> Primary key column 1 [, primary key column 2,...]" statement setting.
MySQL foreign key constraint (FOREIGN KEY) is a special field of the table, often used together 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 primary table (parent table), and the table where the foreign key is located is the secondary table (child table).
Set foreign key constraints when creating the table
In the CREATE TABLE statement, specify the foreign key through the FOREIGN KEY keyword, specifically The syntax format is as follows:
[CONSTRAINT <外键名>] FOREIGN KEY 字段名 [,字段名2,…] REFERENCES <主表名> 主键列1 [,主键列2,…]
Example
In order to show the foreign key relationship between tables, this example creates a department table tb_dept1 in the test_db database, table structure As shown in the table below.
Field name | Data type | Remarks |
---|---|---|
id | INT(11) | Department number |
name | VARCHAR(22) | Department name |
location | VARCHAR(22) | Department location |
The SQL statement and running results to create tb_dept1 are as follows Show.
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.
Recommended tutorial:mysql video tutorial
The above is the detailed content of How to set foreign keys when creating a table in MySQL?. For more information, please follow other related articles on the PHP Chinese website!
-
2023-04-26 17:59:18
-
2023-04-26 17:47:48
-
2023-04-26 17:41:42
-
2023-04-26 17:37:05
-
2023-04-26 17:31:25
-
2023-04-26 17:27:32
-
2023-04-25 19:57:58
-
2023-04-25 19:53:11
-
2023-04-25 19:49:11
-
2023-04-25 19:41:54
- About us Disclaimer Sitemap
- php.cn:Public welfare online PHP training,Help PHP learners grow quickly!