Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of canceling Foreign key constraint by mysql

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the method of canceling foreign key constraints by mysql, which has a certain reference value and can be used for reference by friends who need it. I hope you will learn a lot after reading this article. Next, let the editor take you to learn about it.

Mysql removes foreign key restrictions (constraints) through the "ALTER TABLE DROP FOREIGN KEY;" statement; once the foreign key is deleted, it disassociates the master table from the slave table.

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

Foreign keys are used to establish the relationship between the master table and the slave table, to establish a connection between the data of the two tables, and to restrict the consistency and integrity of the data in the two tables. For example, a fruit stall, only apples, peaches, plums, watermelons and other four kinds of fruit, then you come to the fruit stall to buy fruit, you can only choose apples, peaches, plums and watermelons, other fruits can not be bought.

The method of canceling the restriction (constraint) of Foreign key by mysql

When a foreign key constraint is not needed in a table, it needs to be removed from the table. Once the foreign key is deleted, the relationship between the master table and the slave table is disassociated.

The syntax format for deleting a foreign key constraint is as follows:

ALTER TABLE DROP FOREIGN KEY

Example

Delete the foreign key constraint fk_tb_dept1,SQL statement from the data table tb_emp2 and run the result as shown below.

Mysql > ALTER TABLE tb_emp2-> DROP FOREIGN KEY fk_tb_dept1 Query OK, 0 rows affected (0.19 sec) Records: 0 Duplicates: 0 Warnings: 0mysql > SHOW CREATE TABLE tb_emp2\ Graph * 1. Row * * Table: tb_emp2Create 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=gb23121 row in set (0.00 sec)

As you can see, FOREIGN KEY no longer exists in tb_emp2, and the original foreign key constraint named fk_emp_dept was deleted successfully.

Thank you for reading this article carefully. I hope it will be helpful for everyone to share the method and content of mysql to cancel the foreign key constraint. At the same time, I also hope that you will support us, pay attention to the industry information channel, and find out if you encounter problems. Detailed solutions are waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report