2017-05-30 123 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 –