2017-05-30 125 views
1

我想我osclass安装迁移到另一台服务器。我已经复制了所有文件并创建了一个新的数据库。当试图从备份导入我的数据库时,我得到“#1215 - 无法添加外键约束”。#1215 - 不能添加外键约束

这表明该位是一个问题:

-- 
    -- Table structure for table `oc_t_user` 
    -- 

    CREATE TABLE IF NOT EXISTS `oc_t_user` (
     `pk_i_id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
     `dt_reg_date` datetime NOT NULL, 
     `dt_mod_date` datetime DEFAULT NULL, 
     `s_name` varchar(100) NOT NULL, 
     `s_username` varchar(100) NOT NULL, 
     `s_password` char(60) NOT NULL, 
     `s_secret` varchar(40) DEFAULT NULL, 
     `s_email` varchar(100) NOT NULL, 
     `s_website` varchar(100) DEFAULT NULL, 
     `s_phone_land` varchar(45) DEFAULT NULL, 
     `s_phone_mobile` varchar(45) DEFAULT NULL, 
     `b_enabled` tinyint(1) NOT NULL DEFAULT '1', 
     `b_active` tinyint(1) NOT NULL DEFAULT '0', 
     `s_pass_code` varchar(100) DEFAULT NULL, 
     `s_pass_date` datetime DEFAULT NULL, 
     `s_pass_ip` varchar(15) DEFAULT NULL, 
     `fk_c_country_code` char(2) DEFAULT NULL, 
     `s_country` varchar(40) DEFAULT NULL, 
     `s_address` varchar(100) DEFAULT NULL, 
     `s_zip` varchar(15) DEFAULT NULL, 
     `fk_i_region_id` int(10) unsigned DEFAULT NULL, 
     `s_region` varchar(100) DEFAULT NULL, 
     `fk_i_city_id` int(10) unsigned DEFAULT NULL, 
     `s_city` varchar(100) DEFAULT NULL, 
     `fk_i_city_area_id` int(10) unsigned DEFAULT NULL, 
     `s_city_area` varchar(200) DEFAULT NULL, 
     `d_coord_lat` decimal(10,6) DEFAULT NULL, 
     `d_coord_long` decimal(10,6) DEFAULT NULL, 
     `b_company` tinyint(1) NOT NULL DEFAULT '0', 
     `i_items` int(10) unsigned DEFAULT '0', 
     `i_comments` int(10) unsigned DEFAULT '0', 
     `dt_access_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
     `s_access_ip` varchar(15) NOT NULL DEFAULT '', 
     PRIMARY KEY (`pk_i_id`), 
     UNIQUE KEY `s_email` (`s_email`), 
     KEY `idx_s_name` (`s_name`(6)), 
     KEY `idx_s_username` (`s_username`), 
     KEY `fk_c_country_code` (`fk_c_country_code`), 
     KEY `fk_i_region_id` (`fk_i_region_id`), 
     KEY `fk_i_city_id` (`fk_i_city_id`), 
     KEY `fk_i_city_area_id` (`fk_i_city_area_id`), 
     CONSTRAINT `oc_t_user_ibfk_1` FOREIGN KEY (`fk_c_country_code`)   REFERENCES `oc_t_country` (`pk_c_code`), 
     CONSTRAINT `oc_t_user_ibfk_2` FOREIGN KEY (`fk_i_region_id`) REFERENCES `oc_t_region` (`pk_i_id`), 
     CONSTRAINT `oc_t_user_ibfk_3` FOREIGN KEY (`fk_i_city_id`) REFERENCES `oc_t_city` (`pk_i_id`), 
     CONSTRAINT `oc_t_user_ibfk_4` FOREIGN KEY (`fk_i_city_area_id`) REFERENCES `oc_t_city_area` (`pk_i_id`) 
    ) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8; 

请帮助。

+0

做底部存在外键的4个表吗?如果是的话,父表中是否存在oc_t_user中的所有oc_t_user_ibfk _#?我猜你可能有一些数据质量问题。其中4个字段中的一个在父表中没有记录,或者在尝试创建此表时未创建父表。是相同数据类型的引用键吗? https://stackoverflow.com/questions/16969060/mysql-error-1215-cannot-add-foreign-key-constraint。你不能导入这个表,直到它所依赖的表被创建! – xQbert

回答

0

您必须首先创建要引用的表,例如,这里是引用一个表,关键必须匹配也是类型一个foreing项,请按照本例中添加表的其余部分,我会显示创建表的最小示例,您必须添加正确的数据当然是:

CREATE TABLE IF NOT EXISTS `oc_t_country` (
    `pk_c_code` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY (`pk_c_code`) 
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8; 

CREATE TABLE IF NOT EXISTS `oc_t_region` (
    `pk_i_id_region` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY (`pk_i_id_region`) 
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8; 

CREATE TABLE IF NOT EXISTS `oc_t_city` (
    `pk_i_id_city` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY (`pk_i_id_city`) 
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8; 

CREATE TABLE IF NOT EXISTS `oc_t_city_area` (
    `pk_i_id_area` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY (`pk_i_id_area`) 
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8; 

CREATE TABLE IF NOT EXISTS `oc_t_user` (
    `pk_i_id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `dt_reg_date` datetime NOT NULL, 
    `dt_mod_date` datetime DEFAULT NULL, 
    `s_name` varchar(100) NOT NULL, 
    `s_username` varchar(100) NOT NULL, 
    `s_password` char(60) NOT NULL, 
    `s_secret` varchar(40) DEFAULT NULL, 
    `s_email` varchar(100) NOT NULL, 
    `s_website` varchar(100) DEFAULT NULL, 
    `s_phone_land` varchar(45) DEFAULT NULL, 
    `s_phone_mobile` varchar(45) DEFAULT NULL, 
    `b_enabled` tinyint(1) NOT NULL DEFAULT '1', 
    `b_active` tinyint(1) NOT NULL DEFAULT '0', 
    `s_pass_code` varchar(100) DEFAULT NULL, 
    `s_pass_date` datetime DEFAULT NULL, 
    `s_pass_ip` varchar(15) DEFAULT NULL, 
    `fk_c_country_code` int(10) unsigned DEFAULT NULL, 
    `s_country` varchar(40) DEFAULT NULL, 
    `s_address` varchar(100) DEFAULT NULL, 
    `s_zip` varchar(15) DEFAULT NULL, 
    `fk_i_region_id` int(10) unsigned DEFAULT NULL, 
    `s_region` varchar(100) DEFAULT NULL, 
    `fk_i_city_id` int(10) unsigned DEFAULT NULL, 
    `s_city` varchar(100) DEFAULT NULL, 
    `fk_i_city_area_id` int(10) unsigned DEFAULT NULL, 
    `s_city_area` varchar(200) DEFAULT NULL, 
    `d_coord_lat` decimal(10,6) DEFAULT NULL, 
    `d_coord_long` decimal(10,6) DEFAULT NULL, 
    `b_company` tinyint(1) NOT NULL DEFAULT '0', 
    `i_items` int(10) unsigned DEFAULT '0', 
    `i_comments` int(10) unsigned DEFAULT '0', 
    `dt_access_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `s_access_ip` varchar(15) NOT NULL DEFAULT '', 
    PRIMARY KEY (`pk_i_id`), 
    UNIQUE KEY `s_email` (`s_email`), 
    KEY `idx_s_name` (`s_name`(6)), 
    KEY `idx_s_username` (`s_username`), 
    KEY `fk_c_country_code` (`fk_c_country_code`), 
    KEY `fk_i_region_id` (`fk_i_region_id`), 
    KEY `fk_i_city_id` (`fk_i_city_id`), 
    KEY `fk_i_city_area_id` (`fk_i_city_area_id`), 
    CONSTRAINT `oc_t_user_ibfk_1` FOREIGN KEY (`fk_c_country_code`) REFERENCES `oc_t_country` (`pk_c_code`), 
    CONSTRAINT `oc_t_user_ibfk_2` FOREIGN KEY (`fk_i_region_id`) REFERENCES `oc_t_region` (`pk_i_id_region`), 
    CONSTRAINT `oc_t_user_ibfk_3` FOREIGN KEY (`fk_i_city_id`) REFERENCES `oc_t_city` (`pk_i_id_city`), 
    CONSTRAINT `oc_t_user_ibfk_4` FOREIGN KEY (`fk_i_city_area_id`) REFERENCES `oc_t_city_area` (`pk_i_id_area`) 
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8; 
+0

我得到了一些错误,当我运行该查询: 错误 似乎是在您的SQL查询的错误。下面的MySQL服务器错误输出,如果有的话,也可能帮助你诊断问题。 错误:未知的标点符号字符串@ 743 STR:// MySQL表示:文件 #1064 - 你在你的SQL语法错误;检查对应于你的MySQL服务器版本使用附近的正确语法手册在行“//必须具有相同类型的其它表 's_country' VARCHAR(40)默认的” 18个 – AlexRns

+0

@AlexRns难得,我想和工作得很好,你是否删除了评论? –

+0

删除//和我的意见,只写SQL –