In-depth parsing MySQL Association Table Creation

Source: Internet
Author: User

Creating an association table in MySQL makes our database more accurate and practical. When we mention creating an association table in MySQL, we cannot help but mention the concept of integrity.

Referentialintegrity is an important concept in database design. In different lists of systems, the integrity of the reference is involved when all references of the database are legal or illegal. When the integrity of the reference exists, any association with the non-existent reCord becomes invalid, which prevents various errors and provides a more accurate and practical database.

Integrity of reference is usually widely used through the use of foreign keys. For a long time, the popular open-source RDBMSMySQL tool does not support foreign keys because such support will reduce the speed and performance of RDBMS. However, many users are interested in the advantages of the integrity of the reference. Recently, different MySQL versions support foreign keys through the new InnoDB list engine. Therefore, it is very easy to maintain the integrity of the reference in the list of databases.

To establish a foreign key relationship between two MySQL tables, MySQL must meet the following three conditions:

* The two tables must be InnoDB tables.
* The fields using the foreign key relationship must be indexed ).
* The fields using the foreign key relationship must be of the same data type.

The example is the best way to understand the above points. Create a parts table, use the cpu field to store all the cpu accessory models, and then create a new pc table, the cpumodel field is used to store the cpu model in the PC. Obviously, all records in the cpumodel field should exist in the parts table.

 
 
  1. mysql> create table parts(   
  2. -> cpu char(20) not null,   
  3. -> index(cpu)   
  4. -> )engine=innodb;   
  5. Query OK, 0 rows affected (0.01 sec)   
  6.  
  7. mysql> create table pc(   
  8. -> cpumodel char(20) not null,   
  9. -> index(cpumodel),   
  10. -> foreign key(cpumodel) references parts(cpu)   
  11. -> )engine=innodb;   
  12. Query OK, 0 rows affected (0.01 sec)   
  13.  

Note: For non-InnoDB tables, the foreign key statement is ignored. Add data 1, 2, 3 to the parts table, and then test the pc table. If 1 meets the conditions, the data can be smoothly inserted. If the character 5 does not meet the conditions, A foreign Key Binding error occurs. This is what we want.

 
 
  1. mysql> insert into parts values('1'),('2'),('3');   
  2. Query OK, 3 rows affected (0.00 sec)   
  3. Records: 3 Duplicates: 0 Warnings: 0   
  4.  
  5. mysql> insert into pc values('1');   
  6. Query OK, 1 row affected (0.01 sec)   
  7.  
  8. mysql> insert into pc values('5');   
  9. ERROR 1452 (23000): Cannot add or update a child row: a foreign   
  10. key constraint fails (`Orange/pc`, CONSTRAINT `pc_ibfk_1` FOREIGN KEY   
  11. (`cpumodel`) REFERENCES `parts` (`cpu`))   
  12.  

As explained in the preceding example, foreign keys play an important role in identifying data entry errors, so that a more robust and integrated database can be created. On the other hand, it is worth mentioning that executing foreign key verification is a process of internal data processing, and specifying complex internal relationships between different tables can lead to database performance degradation. Therefore, it is important to find a balance between the integrity of the reference and performance considerations. The use of foreign keys ensures the optimal combination between performance and stability.

Alternative usage of MySQL table alias

Solution to three types of mysql tabulation garbled data

How to modify the table structure in mysql

Three Common MySQL table creation statements

MySQL connection Query

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.