即使foreign_key_checks = 0,什么可能导致外键无法被删除?

时间:2012-01-05 05:04:43

标签: mysql foreign-keys sql-drop

我试图重建我的数据库,但是我无法从表中删除任何外键,即使我也调用SET foreign_key_checks = 0;从MySQL文档中,似乎&# 39;我应该做的一切。我还需要做什么?

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 |
+-------+-----------------------------------------------+

1 个答案:

答案 0 :(得分:0)

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