2015-04-17 27 views
0

我有一個奇怪的問題,我無法在外鍵約束失敗時刪除表。情景如下。在「drop table」期間外鍵約束失敗

我從我的DB試圖dropdepartments,該結構是如下:

show create table `departments` 
CREATE TABLE `departments` (
    `dept_id` int(11) NOT NULL AUTO_INCREMENT, 
    `dept_name` varchar(50) NOT NULL, 
    PRIMARY KEY (`dept_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 

現在,已經部門標識數據庫中的唯一其他表是employee表:

show create table employee 
CREATE TABLE `employee` (
    `emp_id` varchar(20) NOT NULL, 
    `role` varchar(10) DEFAULT NULL, 
    `password` varchar(500) DEFAULT NULL, 
    `division_id` int(20) DEFAULT NULL, 
    `email_bb` varchar(100) DEFAULT NULL, 
    `is_active` tinyint(1) NOT NULL, 
    `date_joining` date DEFAULT NULL, 
    `date_confirmation` date DEFAULT NULL, 
    `date_appraisal` date DEFAULT NULL, 
    `date_leaving` date DEFAULT NULL, 
    `first_name` varchar(100) DEFAULT NULL, 
    `middle_name` varchar(100) DEFAULT NULL, 
    `last_name` varchar(100) DEFAULT NULL, 
    `sex` varchar(1) DEFAULT NULL, 
    `dob` date DEFAULT NULL, 
    `email_other` varchar(100) DEFAULT NULL, 
    `contact` varchar(100) DEFAULT NULL, 
    `present_addr` varchar(1000) DEFAULT NULL, 
    `perma_addr` varchar(1000) DEFAULT NULL, 
    PRIMARY KEY (`emp_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 

正如你所看到的,這些表都沒有通過外鍵相關聯。

#1217 - Cannot delete or update a parent row: a foreign key constraint fails 

有沒有更好的方法(和希望,更簡單的)辦法看到外鍵定義:所以試圖dropdepartment表時,爲什麼會出現這樣的錯誤?什麼可能出錯?

回答

1

顯示創建表不顯示來電FK約束

因此,有你有另一個表與FK約束到該表的可能性(例如FK在子表,而不是父母指定)。我通常轉儲數據庫的模式,顯示所有FK約束。

+0

你說得對!確實有另一個引用'dept_id'的表。我感到很慚愧。 :'( – dotslash

+0

發生在我們身上,很高興你找到了它。 – ac3d912