【问题标题】:What could cause a foreign key to not be able to be dropped even when foreign_key_checks = 0?什么会导致即使foreign_key_checks = 0也无法删除外键?
【发布时间】:2012-01-05 05:04:43
【问题描述】:

我正在尝试重建我的数据库,但我无法从表中删除任何外键,即使我也从 MySQL 文档中调用了 SET foreign_key_checks = 0;,看来这就是我需要做的所有事情.我还需要做什么?

SET foreign_key_checks=0;
alter table galleries drop foreign key fk_page_gallery ;
alter table photos drop foreign key fk_photo_gallery ;

create table galleries (
   id          int(11) auto_increment not null  ,
   page_id     int(11)                          ,
   cover_id    int                    null      ,
   title       varchar(1024)                    ,
   slug        varchar(1024)          not null  ,
   description text                   null      ,
   sort_order  int(11)                          ,
   published   tinyint(1)              default 0,
   created     varchar(20)                      ,
   modified    datetime                         ,
   constraint pk_galleries primary key (id)
)   ENGINE=InnoDB DEFAULT CHARSET=latin1;

create table pages (
   id                int(11) auto_increment not null  ,
   menu_id           int(11)                not null  ,
   title             varchar(1024)          not null  ,
       slug              varchar(1024)          not null  ,
   body              text                   not null  ,
   short_description varchar(1024)                    ,
   published         tinyint(1)              default 0,
   created           datetime                         ,
   modified          datetime                         ,
   constraint pk_pages primary key (id)
)   ENGINE=InnoDB DEFAULT CHARSET=latin1;

SET foreign_key_checks=1;

供参考:

mysql> SHOW CREATE TABLE galleries;
+-----------+-------------------------------------------+
| Table     | Create Table                              |
+-----------+-------------------------------------------+
| galleries | CREATE TABLE `galleries` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `page_id` int(11) DEFAULT NULL,
  `cover_id` int(11) DEFAULT NULL,
  `title` varchar(1024) DEFAULT NULL,
  `slug` varchar(1024) NOT NULL,
  `description` text,
  `sort_order` int(11) DEFAULT NULL,
  `published` tinyint(1) DEFAULT '0',
  `created` varchar(20) DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_page_gallery` (`page_id`),
  KEY `fk_gallery_cover` (`cover_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)

mysql> SHOW CREATE TABLE pages;
+-------+-----------------------------------------------+
| Table | Create Table                                  |
+-------+-----------------------------------------------+
| pages | CREATE TABLE `pages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `menu_id` int(11) NOT NULL,
  `title` varchar(1024) NOT NULL,
  `slug` varchar(1024) NOT NULL,
  `body` text NOT NULL,
  `short_description` varchar(1024) DEFAULT NULL,
  `published` tinyint(1) DEFAULT '0',
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_pages_menu` (`menu_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------+

【问题讨论】:

  • 放弃 FK 约束几乎是你能做的最糟糕的事情......我已经看到它发生了几次,而且每次都在浪费时间困扰工程团队,滑倒最后期限,并且在某些情况下接近物理资源的临界状态。
  • 不过,这是在开发过程中,所以这不是问题。

标签: mysql foreign-keys sql-drop


【解决方案1】:

create table 语句中的外键与 drop 语句中的外键不同。更重要的是,您的表定义似乎根本没有外键。如果您尝试删除不存在的外键,MySQL 可能会抛出一个奇怪的错误。

【讨论】:

  • 重新阅读您的评论后,现在我看到我的外键已创建,而是有简单的键!这将解释所有的错误。现在弄清楚为什么Parse::Dia::SQL 决定这样做。谢谢!
猜你喜欢
  • 2016-04-14
  • 1970-01-01
  • 2021-05-02
  • 2020-11-08
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多