MySQL——多表操作(一)外键(4)删除外键约束

        在实际开发中,根据业务逻辑的需求,需要解除两个表之间的关联关系时,就需要删除外键约束。删除外键约束的语法格式如下:

alter table 表名 drop foreign key 外键名;

        接下来,将表 student 中的外键约束删除,具体语句如下:

mysql> alter table student drop foreign key FK_ID;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

        语句执行成功后,查看表 student 现在的详细结构,查询结果如下:

mysql> show create table student;
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                             |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
  `sid` int NOT NULL,
  `sname` varchar(36) DEFAULT NULL,
  `gid` int NOT NULL,
  PRIMARY KEY (`sid`),
  KEY `FK_ID` (`gid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

        从查询结果可以看出,表 student 中的外键约束已经被成功删除。

你可能感兴趣的:(MySQL,数据库,mysql)