Home > Database > Mysql Tutorial > body text

How to cancel foreign key restrictions (constraints) in MySQL?

青灯夜游
Release: 2020-10-06 08:13:14
Original
15804 people have browsed it

Mysql method to cancel foreign key restrictions (constraints): Cancel through the "ALTER TABLE

DROP FOREIGN KEY ;" statement; once the foreign key is deleted, it The association between the master table and the slave table will be released.

How to cancel foreign key restrictions (constraints) in MySQL?

MySQL foreign key constraint (FOREIGN KEY) is a special field of the table, often used together with primary key constraints. For two tables with an associated relationship, the table where the primary key in the associated field is located is the primary table (parent table), and the table where the foreign key is located is the secondary table (child table).

Foreign keys are used to establish the association between the master table and the slave table, establish a connection for the data in the two tables, and constrain the consistency and integrity of the data in the two tables. For example, a fruit stall only has four kinds of fruits: apples, peaches, plums, and watermelons. Then, when you come to the fruit stall to buy fruits, you can only choose apples, peaches, plums, and watermelons. Other fruits are not available for purchase.

Mysql method to cancel foreign key restrictions (constraints)

When foreign key constraints are not required in a table, you need to remove them from the table Delete it. Once the foreign key is deleted, the association between the master table and the slave table will be released.

The syntax format for deleting foreign key constraints is as follows:

ALTER TABLE <表名> DROP FOREIGN KEY <外键约束名>;
Copy after login

Example

Delete foreign key constraints in data table tb_emp2 The key constraint fk_tb_dept1, the SQL statement and the running results are as follows.

mysql> ALTER TABLE tb_emp2
    -> DROP FOREIGN KEY fk_tb_dept1;
Query OK, 0 rows affected (0.19 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> SHOW CREATE TABLE tb_emp2\G
*************************** 1. row ***************************
       Table: tb_emp2
Create Table: CREATE TABLE `tb_emp2` (
  `id` int(11) NOT NULL,
  `name` varchar(30) DEFAULT NULL,
  `deptId` int(11) DEFAULT NULL,
  `salary` float DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_tb_dept1` (`deptId`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312
1 row in set (0.00 sec)
Copy after login

It can be seen that FOREIGN KEY no longer exists in tb_emp2, and the original foreign key constraint named fk_emp_dept was deleted successfully.

Recommended tutorial: mysql video tutorial

The above is the detailed content of How to cancel foreign key restrictions (constraints) in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!