MySQL's foreign keys and referential integrity: Part 1_PHP tutorial

WBOY
Release: 2016-07-13 17:02:40
Original
1136 people have browsed it

Reprinted from: http://sofee.cn/blog/2006/08/07/26/
1. What is referential integrity?
——————–
Referential integrity (integrity Constraints) is an important concept in database design. Referential integrity is involved when a table in the database is associated with one or more tables. For example, the following example:
category_id name
1 SQL Server
2 Oracle
3 PostgreSQL
5 SQLite
Article table- articles
article_id category_id title
1 1 aa
2 2 bb
3 It can be seen that the relationship between the above two tables is through category _id, of which the categories table has 4 records and the articles table has 3 records.
However, maybe for some reason we deleted the record of category_id=4 in the categories table, but there is still a record of category_id=4 in the articles table. Obviously, this record of category_id=4 should not exist in articles. In the table, this can easily cause data confusion.
On the contrary, Foreign Key relationships discuss the relationship between parent tables (categories) and child tables (articles). The concept of foreign keys (Foreign Key) is introduced to ensure referential integrity (Referential integrity). It will make the database very simple. For example, if we want to delete the category_id=4 record in the categories table and delete all the category_id=4 records in the articles table at the same time, if no foreign key is introduced, we must execute 2 SQL statements; if there is a foreign key, , the requirement can be easily achieved with a single SQL statement.
2. Conditions for using foreign keys
——————
MySQL only introduced foreign keys after version v3.23.34, so forget about versions before that :), except In addition, the following conditions must be met:


http://www.bkjia.com/PHPjc/631030.html

truehttp: //www.bkjia.com/PHPjc/631030.htmlTechArticleReprinted from: http://sofee.cn/blog/2006/08/07/26/ 1. What is Referential integrity? Referential integrity (integrity constraints) is an important concept in database design. When a table in the database matches a...
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template