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. 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 loginExample
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 loginIt 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.cnPrevious article:How to query all columns (fields) in a table with mysql? Next article:How many ways are there to write mysql comments?Statement of this WebsiteThe 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.cnLatest Articles by Author
2023-04-26 17:59:18 2023-04-26 17:47:48 2023-04-26 17:41:42 2023-04-26 17:37:05 2023-04-26 17:31:25 2023-04-26 17:27:32 2023-04-25 19:57:58 2023-04-25 19:53:11 2023-04-25 19:49:11 2023-04-25 19:41:54Latest IssuesHow to group and count in MySQL? I'm trying to write a query that extracts the total number of undeleted messages sent to f...From 2024-04-06 18:30:1701353MySQL gets data from multiple tables I have a eg_design table which contains the following columns: and eg_domains table which ...From 2024-04-06 18:42:4402479Related TopicsMore>
- About us Disclaimer Sitemap
- php.cn:Public welfare online PHP training,Help PHP learners grow quickly!