REFERENCES is the keyword to create foreign key constraints in MySQL, ensuring that the values in the child table exist in the corresponding records in the parent table. Its functions include: ensuring data consistency. Enforce a one-to-many or many-to-many relationship. Simplify queries and joins.
REFERENCES in MySQL
What are REFERENCES?
REFERENCES is the keyword used to create foreign key constraints in MySQL. Foreign key constraints ensure that values in the child table (referring table) have corresponding records in the parent table (referenced table).
Syntax of REFERENCES
<code class="sql">ALTER TABLE 子表 ADD CONSTRAINT 外键名 FOREIGN KEY (子表字段) REFERENCES 父表 (父表字段);</code>
Example
Suppose we have two tables: Orders
and product
, where the foreign key product_id
of the order
table refers to the id
primary key of the product
table.
<code class="sql">ALTER TABLE 订单 ADD CONSTRAINT FK_product FOREIGN KEY (product_id) REFERENCES 产品 (id);</code>
The role of REFERENCES
Notes
The above is the detailed content of What does references mean in mysql. For more information, please follow other related articles on the PHP Chinese website!