Foreign key and referential integrity in MySQL development

Source: Internet
Author: User
Tags insert key mysql query table name

Referential integrity (referential integrity) is an important concept in database design. In a different list of systems, referential integrity is involved when all references to the database are legitimate or not legitimate. When referential integrity exists, any association with nonexistent records becomes invalid, thereby preventing a user from having a variety of errors, thus providing a more accurate and useful database.

Referential integrity is often widely used through the use of foreign keys (foreign key). For a long time, popular tools open source RDBMS MySQL does not support foreign keys, because this support will reduce the speed and performance of the RDBMS. However, because many users are interested in the benefits of referential integrity, recent versions of MySQL support foreign keys through the new InnoDB list engine. As a result, it is easy to keep referential integrity in a list of database components.

In order to establish a foreign key relationship between two MySQL tables, the following three scenarios must be met:

Two tables must be InnoDB table types.

Fields that use foreign key relationships must be indexed (index).

Fields that use foreign key relationships must be similar to data types.

Examples are the best way to understand the above points. As shown in table A, create two tables, one of which lists the animal species and the corresponding code (table name: species), and the other table lists the animals in the Zoo (table name: Zoo). Now, we want to associate these two tables through species, so we just need to accept and save the entry of the zoo table that contains the legitimate animals in the species table into the database.

Table A

mysql> CREATE TABLE species (id TINYINT not NULL auto_increment, name VARCHAR (m) not NULL, PRIMARY KEY (ID)) Engine=inn ODB;

Query OK, 0 rows affected (0.11 sec)

mysql> INSERT into species VALUES (1, ' Orangutan '), (2, ' Elephant '), (3, ' Hippopotamus '), (4, ' Yak ');

Query OK, 4 rows affected (0.06 sec)

Records:4 duplicates:0 warnings:0

Mysql> CREATE TABLE Zoo (ID INT (4) Not NULL, name VARCHAR is not NULL, Fk_species TINYINT (4) is not NULL, INDEX (fk_speci ES), FOREIGN key (fk_species) REFERENCES species (ID), PRIMARY key (ID)) Engine=innodb;

Note: The FOREIGN KEY statement will be ignored for InnoDB tables.

There is now a foreign key relationship between Fieldszoo.species and Species.id. Only the corresponding Zoo.specie matches a value of Species.idfield, the entry in the animal table can be accessed. The following output demonstrates the use of an illegal species code when you want to enter a Harry Hippopotamus record:

Mysql> INSERT into Zoo VALUES (1, ' Harry ', 5);

ERROR 1216 (23000): Cannot add or update a child row:a FOREIGN KEY constraint fails

Here, MySQL checks the species table to see if the species code exists and rejects the record if it finds it does not exist. When you enter the correct code, you can compare it with the above.

Mysql> INSERT into Zoo VALUES (1, ' Harry ', 3);

Query OK, 1 row affected (0.06 sec)

1 2 Next page > full text reading tips: Try "←→" button, turn the page more convenient Oh!

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.