解决在Mysql上删除外键时报错Error Code: 1091. Can‘t DROP ‘XXX‘; check that column/key exists
Posted 五小悟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决在Mysql上删除外键时报错Error Code: 1091. Can‘t DROP ‘XXX‘; check that column/key exists相关的知识,希望对你有一定的参考价值。
1.问题起因
由于作业题目要求删除学生课程表的外键约束,于是我执行以下语句删除其外键约束
alter table sc drop foreign key cno
然后Navicat提示这个错误。
2.错误分析
这句话翻译过来就是让我检查一下外键是否存在,我就很纳闷,是我创建外键的时候名字记错了吗?然后我就查了一下创建学生课程表的细节
查询语句
show create table sc
CREATE TABLE `sc` (
`Sno` char(9) NOT NULL,
`Cno` char(4) NOT NULL,
`Grade` smallint DEFAULT NULL,
PRIMARY KEY (`Sno`,`Cno`),
KEY `Cno` (`Cno`),
CONSTRAINT `sc_ibfk_2` FOREIGN KEY (`Cno`) REFERENCES `course` (`Cno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
观察此表,仔细揣摩发现了不得了的玩意儿"CONSTRAINT `sc_ibfk_2` ",我在创建此表的时候并未写此关键字。莫不是Navicat在我创建此表的时候自动创建一个限制名,为了验证猜想,我创建了测试表
3.验证猜想
--1.创建测试学生表--
create table stu(
sid char(10),
sname varchar(20),
primary key(sid)
);
--2.创建测试课程表--
create table cu(
cid char(4),
cname varchar(10),
primary key(cid)
);
--3.创建关系表--
create table demo(
sid char(10),
cid char(4),
primary key(sid,cid),
foreign key(sid) references stu(sid),
foreign key(cid) references cu(cid)
);
为了验证猜想,在Navicat查一下创建demo的细节
CREATE TABLE `demo` (
`sid` char(10) NOT NULL,
`cid` char(4) NOT NULL,
PRIMARY KEY (`sid`,`cid`),
KEY `cid` (`cid`),
CONSTRAINT `demo_ibfk_1` FOREIGN KEY (`sid`) REFERENCES `stu` (`sid`),
CONSTRAINT `demo_ibfk_2` FOREIGN KEY (`cid`) REFERENCES `cu` (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
果不然,这个老六偷偷摸摸的创建一个限制名。现在我们删除这个限制名来删除demo表的外键试试
4.见证奇迹
执行语句
alter table demo drop foreign key demo_ibfk_1
然后我们可以看到Navicat的示,删除外键成功啦!
这也就是说我们以后在Navicat上删除外键,就需要找到这个限制名然后直接将其删除即可。
5.进一步猜想
我在想这是不是这个创建限制名的操作是不是Navicat独有的,然后不甘心的在Dos命令下创建一个新的关系表,然后一查创建的细节
我滴妈呀,这结果跟Navicat一样,所有就证明了,这是mysql编译器搞的鬼。也就是说,以后只要我们想要执行mysql的外键约束删除操作,只需要查看此表的创建细节,找到这个限制名,然后将其删除即可。
6.结语
在学习数据库系统概论这个课程上,书上的一些操作案例,mysql语句有时候在Navicat上并不会执行成功。这就造成了一些坑,但是无所谓,只要有bug,就有人出手,这门课的理论学习任重而道远。哈哈哈哈,以上就是此次的坑,如果有什么疑问或更好的想法,可以留言评论区哦。
rhel6.5搭建mantis时报错“INTERNAL APPLICATION ERROR”
参考之前成功搭建mantis时整理的文档,再次在rhel6.5上搭建mantis时遇到个之前没遇到过的问题,在搜寻过网上一些文档后依旧没能解决,因此在这整理出来,希望有路过的大佬能顺便帮忙指点一二。
图中信息理论上来讲是没有问题的,但是点击之后开始报错,如下图:
操作系统:Redhat 6.5
php版本:7.0
mysql版本:5.6.22 (编译安装)
mysql数据库root密码为root
mysql -uroot -proot
mantis版本:mantisbt-2.17.1.tar.gz
mantisbt-2.16.0-dev-master-1e14941.tar.gz ---> 这个版本也试过,也是一样的报错,但是上一次成功搭建用的就是这个版本
mantis库:
create database mantis default character set utf8 collate utf8_general_ci;
grant all privileges on mantis. to ‘mantis‘@‘%‘ identified by ‘mantis‘;
grant all privileges on mantis. to ‘mantis‘@‘localhost‘ identified by ‘mantis‘;
grant all privileges on mantis. to ‘root‘@‘%‘ identified by ‘root‘;
grant all privileges on mantis. to ‘root‘@‘localhost‘ identified by ‘root‘;
flush privileges;
http日志中报错:
/var/log/httpd/error_log
26 [Tue Oct 23 09:30:41 2018] [error] [client 20.20.20.1] File does not exist: /var/www/html/mantisbt/css/admin, referer: http://20.20.20.42/mantisbt/admin/install.php
27 [Tue Oct 23 09:31:27 2018] [error] [client 20.20.20.1] Call to undefined function mysqli_connect_error()
/var/www/html/mantisbt/core/database_api.php: 588: ADODB_mysqli - - > - ErrorMsg()
/var/www/html/mantisbt/admin/install.php: 431: - - - - db_error_msg()
, referer: http://20.20.20.42/mantisbt/admin/install.php
28 [Tue Oct 23 09:31:27 2018] [error] [client 20.20.20.1] File does not exist: /var/www/html/mantisbt/css/admin, referer: http://20.20.20.42/mantisbt/admin/install.php
29 [Tue Oct 23 09:38:54 2018] [error] [client 20.20.20.1] Call to undefined function mysqli_connect_error()
/var/www/html/mantisbt/core/database_api.php: 588: ADODB_mysqli - - > - ErrorMsg()
/var/www/html/mantisbt/admin/install.php: 431: - - - - db_error_msg()
, referer: http://20.20.20.42/mantisbt/admin/install.php
30 [Tue Oct 23 09:38:55 2018] [error] [client 20.20.20.1] File does not exist: /var/www/html/mantisbt/css/admin, referer: http://20.20.20.42/mantisbt/admin/install.php
31 [Tue Oct 23 09:38:58 2018] [error] [client 20.20.20.1] File does not exist: /var/www/html/mantisbt/css/admin, referer: http://20.20.20.42/mantisbt/admin/install.php
32 [Tue Oct 23 09:39:00 2018] [error] [client 20.20.20.1] File does not exist: /var/www/html/mantisbt/css/admin, referer: http://20.20.20.42/mantisbt/admin/install.php
33 [Tue Oct 23 09:39:01 2018] [error] [client 20.20.20.1] File does not exist: /var/www/html/mantisbt/css/admin, referer: http://20.20.20.42/mantisbt/admin/install.php
34 [Tue Oct 23 09:39:25 2018] [error] [client 20.20.20.1] Call to undefined function mysqli_connect_error()
/var/www/html/mantisbt/core/database_api.php: 588: ADODB_mysqli - - > - ErrorMsg()
/var/www/html/mantisbt/admin/install.php: 431: - - - - db_error_msg()
, referer: http://20.20.20.42/mantisbt/admin/install.php
35 [Tue Oct 23 09:39:25 2018] [error] [client 20.20.20.1] File does not exist: /var/www/html/mantisbt/css/admin, referer: http://20.20.20.42/mantisbt/admin/install.php
36 [Tue Oct 23 09:58:15 2018] [error] [client 20.20.20.1] File does not exist: /var/www/html/mantisbt/css/admin, referer: http://20.20.20.42/mantisbt/admin/install.php
37 [Tue Oct 23 09:59:11 2018] [error] [client 20.20.20.1] Call to undefined function mysqli_connect_error()
/var/www/html/mantisbt/core/database_api.php: 588: ADODB_mysqli - - > - ErrorMsg()
/var/www/html/mantisbt/admin/install.php: 431: - - - - db_error_msg()
, referer: http://20.20.20.42/mantisbt/admin/install.php
38 [Tue Oct 23 09:59:11 2018] [error] [client 20.20.20.1] File does not exist: /var/www/html/mantisbt/css/admin, referer: http://20.20.20.42/mantisbt/admin/install.php
39 [Tue Oct 23 10:11:28 2018] [error] [client 20.20.20.1] Call to undefined function mysqli_connect_error()
/var/www/html/mantisbt/core/database_api.php: 588: ADODB_mysqli - - > - ErrorMsg()
/var/www/html/mantisbt/admin/install.php: 431: - - - - db_error_msg()
, referer: http://20.20.20.42/mantisbt/admin/install.php
40 [Tue Oct 23 10:11:29 2018] [error] [client 20.20.20.1] File does not exist: /var/www/html/mantisbt/css/admin, referer: http://20.20.20.42/mantisbt/admin/install.php
/var/www/html/mantisbt/admin/install.php
431 ‘Does administrative user have access to the database? ( ‘ . string_attribute( db_error_msg() ) . ‘ )‘
以上是关于解决在Mysql上删除外键时报错Error Code: 1091. Can‘t DROP ‘XXX‘; check that column/key exists的主要内容,如果未能解决你的问题,请参考以下文章
mysql中删除时报错Cannot truncate a table referenced in a foreign key constraint (`em_hgh`.`monitoritem`, C
centos7启动iptables时报Job for iptables.service failed because the control process exited with error cod
mysql 使用 GROUP BY 时报错 ERROR 1055 (42000)
rhel6.5搭建mantis时报错“INTERNAL APPLICATION ERROR”
Mysql导入zabbix的sql语句时报错:ERROR 1045 (28000)
Error Fix – Replication subscriber does not exist on the server anymore(删除Replication时报错的解决办法)