2013-03-20 76 views
2

我有以下數據庫模式,有一堆表和外鍵,當我嘗試導入SQL轉儲時,我不斷收到以下錯誤。從SQL DUMP導入數據庫模式

Can't create table errno 150 

我明白,這是試圖創建與尚未創建表的依賴關係表,但我不明白如何導入模式,而不屠殺了所有的外鍵,然後重新創建它們根據Stack和Google提供的答案。

必須有一個更簡單的方法,大公司做什麼有數百個表?

我有下面的SQL語句和任何建議,將不勝感激。謝謝

# 
# Encoding: Unicode (UTF-8) 
# 


DROP TABLE IF EXISTS `contact_interest`; 
DROP TABLE IF EXISTS `contact_seeking`; 
DROP TABLE IF EXISTS `interests`; 
DROP TABLE IF EXISTS `job_current`; 
DROP TABLE IF EXISTS `job_desired`; 
DROP TABLE IF EXISTS `job_listings`; 
DROP TABLE IF EXISTS `my_contacts`; 
DROP TABLE IF EXISTS `profession`; 
DROP TABLE IF EXISTS `seeking`; 
DROP TABLE IF EXISTS `status`; 
DROP TABLE IF EXISTS `zip_code`; 


CREATE TABLE `contact_interest` (
    `contact_id` int(10) unsigned NOT NULL, 
    `interest_id` int(10) unsigned NOT NULL, 
    KEY `mycontacts_contactinterest_fk` (`contact_id`), 
    KEY `interests_contactinterest_fk` (`interest_id`), 
    CONSTRAINT `mycontacts_contactinterest_fk` FOREIGN KEY (`contact_id`) REFERENCES `my_contacts` (`contact_id`), 
    CONSTRAINT `interests_contactinterest_fk` FOREIGN KEY (`interest_id`) REFERENCES `interests` (`interest_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 


CREATE TABLE `contact_seeking` (
    `contact_id` int(10) unsigned NOT NULL, 
    `seeking_id` int(10) unsigned NOT NULL, 
    KEY `contactid_contactseeking_fk` (`contact_id`), 
    KEY `seeking_contactseeking_fk` (`seeking_id`), 
    CONSTRAINT `contactid_contactseeking_fk` FOREIGN KEY (`contact_id`) REFERENCES `my_contacts` (`contact_id`), 
    CONSTRAINT `seeking_contactseeking_fk` FOREIGN KEY (`seeking_id`) REFERENCES `seeking` (`seeking_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 


CREATE TABLE `interests` (
    `interest_id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `interest` varchar(50) DEFAULT NULL, 
    PRIMARY KEY (`interest_id`) 
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=latin1; 


CREATE TABLE `job_current` (
    `contact_id` int(10) unsigned NOT NULL, 
    `title` varchar(20) DEFAULT NULL, 
    `salary` decimal(8,2) DEFAULT NULL, 
    `start_date` date DEFAULT NULL, 
    KEY `mycontacts_jobcurrent_fk` (`contact_id`), 
    CONSTRAINT `mycontacts_jobcurrent_fk` FOREIGN KEY (`contact_id`) REFERENCES `my_contacts` (`contact_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 


CREATE TABLE `job_desired` (
    `contact_id` int(10) unsigned NOT NULL, 
    `title` varchar(20) DEFAULT NULL, 
    `salary_low` decimal(8,2) DEFAULT NULL, 
    `salary_high` decimal(8,2) DEFAULT NULL, 
    `available` date DEFAULT NULL, 
    `years_exp` int(11) DEFAULT NULL, 
    KEY `mycontacts_jobdesired_fk` (`contact_id`), 
    CONSTRAINT `mycontacts_jobdesired_fk` FOREIGN KEY (`contact_id`) REFERENCES `my_contacts` (`contact_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 


CREATE TABLE `job_listings` (
    `job_id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `title` varchar(25) DEFAULT NULL, 
    `salary` decimal(8,2) DEFAULT NULL, 
    `zip_code` char(5) DEFAULT NULL, 
    `description` varchar(50) DEFAULT NULL, 
    PRIMARY KEY (`job_id`) 
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1; 


CREATE TABLE `my_contacts` (
    `contact_id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `last_name` varchar(30) DEFAULT NULL, 
    `first_name` varchar(20) DEFAULT NULL, 
    `phone` char(10) DEFAULT NULL, 
    `email` varchar(50) DEFAULT NULL, 
    `gender` char(1) DEFAULT NULL, 
    `birthday` date DEFAULT NULL, 
    `prof_id` int(11) unsigned NOT NULL, 
    `status_id` int(10) unsigned NOT NULL, 
    `zip_code` char(5) DEFAULT NULL, 
    PRIMARY KEY (`contact_id`), 
    KEY `profession_mycontacts_fk` (`prof_id`), 
    KEY `zipcode_mycontacts_fk` (`zip_code`), 
    KEY `status_my_contacts_fk` (`status_id`), 
    CONSTRAINT `profession_mycontacts_fk` FOREIGN KEY (`prof_id`) REFERENCES `profession` (`prof_id`), 
    CONSTRAINT `status_my_contacts_fk` FOREIGN KEY (`status_id`) REFERENCES `status` (`status_id`), 
    CONSTRAINT `zipcode_mycontacts_fk` FOREIGN KEY (`zip_code`) REFERENCES `zip_code` (`zip_code`) 
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1; 


CREATE TABLE `profession` (
    `prof_id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `profession` varchar(30) DEFAULT NULL, 
    PRIMARY KEY (`prof_id`) 
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1; 


CREATE TABLE `seeking` (
    `seeking_id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `seeking` varchar(40) DEFAULT NULL, 
    PRIMARY KEY (`seeking_id`) 
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1; 


CREATE TABLE `status` (
    `status_id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `status` varchar(30) DEFAULT NULL, 
    PRIMARY KEY (`status_id`) 
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1; 


CREATE TABLE `zip_code` (
    `zip_code` char(5) NOT NULL DEFAULT '', 
    `city` varchar(20) DEFAULT NULL, 
    `state` char(2) DEFAULT NULL, 
    PRIMARY KEY (`zip_code`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

回答

2

我發現我只需要兩條線來解決我的問題,我在頂部添加了一個0,底部添加了一個,我很好。對不起,浪費你的時間......

SET FOREIGN_KEY_CHECKS = 0; 

SET FOREIGN_KEY_CHECKS = 1; 
+1

這不是浪費時間,因爲你學到了一些東西。實際上,如果你mysqldump一個數據庫,這些設置在mysqldump的開頭設置爲零,並在最後重置。所以,今天給你+1! – RolandoMySQLDBA 2013-03-20 18:39:09

0

最簡單的方法是通過命令行來做到這一點是這樣的:

mysql db_name < backup-file.sql 

這個執行你的SQL文件在一個事務中。如果你在一個事務中執行你的東西,那麼你將不會得到外鍵錯誤。

+0

我以前曾嘗試過,並且失敗了。我打開了轉儲文件,發現外鍵檢查位於create table命令之後出現的插入數據命令之前和之後。我只是將foreign_key_check = 0移到頂端,它工作。 – Drewdin 2013-03-20 18:51:20