Steps for implementing the MySQL foreign key and reference integrity

Source: Internet
Author: User
We all know that MySQL's reference integrity is generally applied through the specific use of the MySQL foreign key (foreignkey. In general, the popular open-source RDBMSMySQL (the best combination with PHP) 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 references.

We all know that MySQL's reference integrity is generally applied through the specific use of the MySQL foreign key (foreign key. In general, the popular tool open-source RDBMS MySQL (the best combination with PHP) 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 references.

We all know that MySQL's reference integrity is generally applied through the specific use of the MySQL foreign key (foreign key. In general, the popular tool open-source RDBMS MySQL (the best combination with PHP) 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 versions of MySQL (the best combination with PHP) 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 MySQL foreign key relationship between two MySQL (the best combination with PHP) tables, the following three conditions must be met:

The two tables must be InnoDB tables.

The fields using the foreign key relationship must be indexed ).

The fields used in the MySQL foreign key relationship must be of the same data type.

The example is the best way to understand the above points. As shown in Table A, create two tables, one of which lists the animal types and the corresponding code (Table Name: species), and the other lists the animals in the zoo (Table Name: zoo ). Now, we want to associate these two tables through species, so we only need to accept and save the zoo table's entry containing valid animals in the species table to the database.

Table

MySQL (best combination with PHP)> create table species (id tinyint not null AUTO_INCREMENT, name VARCHAR (50) not null, Prima (the most complete VM management system) ry key (id) ENGINE = INNODB;

 
 
  1. Query OK, 0 rows affected (0.11 sec)
  2. MySQL (best combination with PHP)> insert into species VALUES (1, 'orangutan '), (2, 'elephant'), (3, 'hippotamus '), (4, 'yak ');
  3. Query OK, 4 rows affected (0.06 sec)
  4. Records: 4 Duplicates: 0 Warnings: 0

MySQL (best combination with PHP)> create table zoo (id INT (4) not null, name VARCHAR (50) not null, FK_species TINYINT (4) not null, INDEX (FK_species), foreign key (FK_species) REFERENCES species (id), Prima (the most complete VM management system) ry key (id) ENGINE = INNODB;

Note: For non-InnoDB tables, the foreign key statement is ignored.

Currently, there is a MySQL foreign key relationship between fieldszoo. species and species. id. Only one corresponding zoo. specie matches the species. idfield value. The input eloquence in the animal table can be accessed. The following output demonstrates the use of invalid species code when you want to input a Harry hippotamus record:

 
 
  1. MySQL (best combination with PHP)> insert into zoo VALUES (1, 'Harry ', 5 );
  2. ERROR 1216 (23000): Cannot add or update a child row: a foreign key constraint fails

Here, MySQL (the best combination with PHP) checks the species table to check whether the species code exists. if it finds that it does not exist, the record is rejected. When you enter the correct code, you can compare it with the above.

 
 
  1. MySQL (best combination with PHP)> insert into zoo VALUES (1, 'Harry ', 3 );
  2. Query OK, 1 row affected (0.06 sec)

Here, MySQL (the best combination with PHP) checks the species table to check whether the species code exists. If the code is found to exist, records can be saved in the zoo table.

To delete a foreign key relationship of MySQL, use show create table to find the internal tag of InnoDB, as shown in table B:

Table B

 
 
  1. +-------+---------------------------------------------------+
  2. | Table | Create Table |
  3. +-------+---------------------------------------------------+
  4. | zoo | CREATE TABLE `zoo` (
  5. `id` int(4) NOT NULL default '0',
  6. `name` varchar(50) NOT NULL default '',
  7. `FK_species` tinyint(4) NOT NULL default '0',
  8. KEY `FK_species` (`FK_species`),
  9. CONSTRAINT `zoo_ibfk_1` FOREIGN KEY (`FK_species`)
  10. REFERENCES `species` (`id`)
  11. ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
  12. +-------+----------------------------------------------------+

Use the alter table command with the drop foreign key statement, as shown in the following code:

 
 
  1. MySQL (best combination with PHP)> alter table zoo drop foreign key zoo_ibfk_1;
  2. Query OK, 1 row affected (0.11 sec)
  3. Records: 1 Duplicates: 0 Warnings: 0

To ADD a foreign key to a ready-made TABLE, use the alter table statement of add foreign key to specify an appropriate field as a foreign key:

 
 
  1. MySQL (best combination with PHP)> alter table zoo add foreign key (FK_species) REFERENCES species (id );
  2. Query OK, 1 rows affected (0.11 sec)
  3. Records: 1 Duplicates: 0 Warnings: 0

As explained in the above example, the MySQL foreign key plays an important role in identifying data entry errors, thus establishing a more robust and integrated database. 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.

I expect that the introduction of MySQL Foreign keys in this issue will be helpful to you. You will feel the benefits of foreign keys in the next MySQL (best combination with PHP) database design. Happy programming!

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.