Connection between MySQL Foreign keys and reference integrity

Source: Internet
Author: User
Tags mysql update

The MySQL foreign key can ensure the integrity of the database's reference, making the database simpler. Integrity of reference is one of the most important concepts in database design.

Integrity of reference (integrity constraints) is an important concept in database design. When a table in the database is associated with one or more tables, the integrity of reference is involved. For example:
Document category table-categories
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 4 cc

The preceding two tables use category_id. The categories table has four records and the articles table has three records.

However, for some reason, we may have deleted the records whose category_id is 4 in the categories table, but the articles table still has a record whose category_id is 4. Obviously, this record with category_id = 4 should not exist in the articles table, which can easily cause data disorder.

On the contrary, the MySQL Foreign Key relationship (Foreign Key relationships) discusses the relationship between the parent table (categories) and the sub-table (articles) by introducing the MySQL Foreign Key (Foreign Key) this concept ensures Referential integrity, which makes the database quite simple. For example, to delete the category_id = 4 record in the categories table and delete all records in the category_id = 4 in the articles table, if the MySQL foreign key is not introduced, we have to execute two SQL statements. If there is a foreign key, we can easily use one SQL statement to meet the requirements.
 

MySql stored procedure with Parameters

Statements used to obtain MySQL table information

How to use mysql UPDATE statements

Mysql inserts an instance of the Clob Field

Default length of common MySql Fields

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.