How to implement MySQL foreign key association operation
Jun 03, 2023 am 09:40 AMMySQL’s foreign key constraints
Note that only MySQL’s InnoDB table engine supports foreign key associations, and MyISAM does not. SET FOREIGN_KEY_CHECKS = 0/1 can be used to manually turn on or off MySQL's foreign key constraints.
The biggest benefit of MySQL's foreign key constraints is that it can help us complete data consistency verification. Using the default RESTRICT foreign key type, reference validity checks will be performed when creating, modifying, or deleting records.
Assume that our database contains two tables: posts(id, author_id, content) and authors(id, name). When performing the following operations, the database will trigger the check of foreign keys:
When inserting data into the posts table, check whether the author_id exists in the authors table;
When modifying the data in the posts table, check whether the author_id exists in the authors table;
Delete When entering data in the authors table, check whether there is a foreign key referencing the current record in posts;
As a system specifically designed to manage data, the database can better ensure integrity compared with application services, and the above These operations are all extra work caused by introducing foreign keys, but this is also a necessary price for the database to ensure data integrity. We can conduct a simple quantitative analysis to understand the specific impact of introducing foreign keys on performance, rather than just a theoretical qualitative analysis.
Define foreign keys (References, reference) when creating a table
In the CREATE TABLE statement, specify the foreign key through the FOREIGN KEY keyword. The specific syntax format is as follows:
1 |
|
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
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.
After the above statement is successfully executed, a foreign key constraint named fk_emp_dept1 is added to the representation tb_emp6. The foreign key name is deptId, which depends on the primary key id of the table tb_dept1.
View the constraint information of the main table
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Modify the foreign key constraints of the original table
Foreign key constraints can also be added when modifying the table, but adding foreign key constraints is The premise is: the data in the foreign key column in the secondary table must be consistent with the data in the primary key column in the primary table or there is no data.
The syntax format for adding foreign key constraints when modifying the data table is as follows:
1 |
|
Example: Modify the data table tb_emp2, set the field deptId as a foreign key, and compare it with the primary key id of the data table tb_dept1 association.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
Delete foreign key constraints
When a foreign key constraint is not needed in a table, it needs to be deleted from the table. Once the foreign key is deleted, the association between the master table and the slave table will be released.
The syntax format for deleting foreign key constraints is as follows:
1 |
|
Example: Delete the foreign key constraint fk_tb_dept1 in the data table tb_emp2.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
The above is the detailed content of How to implement MySQL foreign key association operation. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP?

What are the application scenarios of Java enumeration types in databases?

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP?

Performance optimization strategies for PHP array paging
