] FOREIGN KEY field name [, field name 2,...] REFERENCES
primary key column 1 [ , primary key column 2,…]” statement setting."> How to set foreign keys when creating a table in MySQL?-Mysql Tutorial-php.cn

How to set foreign keys when creating a table in MySQL?

青灯夜游
Release: 2020-10-07 12:15:15
Original
22949 people have browsed it

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.

How to set foreign keys when creating a table in MySQL?

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,…]
Copy after login

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)
Copy after login

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!