Get the App
SLTechnology News&Howtos  ›  Database  › 

How to delete foreign key columns in MySQL

Shulou Source: shulou.com Published: 2022-05-31 18:58:00 09月10日 Update

This article is to share with you about how to delete foreign key columns in MySQL, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Sql code

Show create table userfans

Java code

| | userfans | CREATE TABLE `userfans` (

`id`int (11) NOT NULL AUTO_INCREMENT

`status` int (11) DEFAULT NULL

`update_ time`datetime DEFAULT NULL

`user`int (11) DEFAULT NULL

`fllower` int (11) DEFAULT NULL

`follower` int (11) DEFAULT NULL

PRIMARY KEY (`id`)

KEY

`FKF025E60B81E8743E

`(`fllower`)

KEY `FKF025E60BAFF2360E` (`user`)

KEY `FKF025E60BC1B86A61` (`follower`)

CONSTRAINT

`FKF025E60B81E8743E

`FOREIGN

KEY

(`fllower`) REFERENCES `user` (`id`)

CONSTRAINT `FKF025E60BAFF2360E` FOREIGN KEY (`user`) REFERENCES `user` (`id`)

CONSTRAINT `FKF025E60BC1B86A61` FOREIGN KEY (`follower`) REFERENCES `user` (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 |

At first I thought: should I delete KEY first, then delete constraint, and finally delete columns? Because the foreign key columns of MySQL are different from those of MySQL, they all have KEY. As a result, I tried and failed for a long time, and finally found that deleting the foreign key column was surprisingly simple:

Sql code

Alter table userfans drop foreign key FKF025E60B81E8743E

Display:

Sql code

Query OK, 2 rows affected (0.32 sec)

Records: 2 Duplicates: 0 Warnings: 0

The above is how to delete the foreign key column in MySQL. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Code more knowledge articles surprisingly practical that is work meetings articles read knowledge points results industry details information information channels follow parts channels learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Apple macOS MariaDB vpn