2013-03-20 50 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