DROP FOREIGN KEY Home >Database >Mysql Tutorial >How to cancel foreign key restrictions (constraints) in MySQL? Mysql method to cancel foreign key restrictions (constraints): Cancel through the "ALTER TABLE ccc43248daffbac9770dee47fdaff697 DROP FOREIGN KEY b68d62cd438e572b3020e4fa65e8d0c3;" statement; once the foreign key is deleted, it The association between the master table and the slave table will be released. 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: 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. 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!How to cancel foreign key restrictions (constraints) in MySQL?
ALTER TABLE <表名> DROP FOREIGN KEY <外键约束名>;
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)
Related articles
See more