2013-02-10 112 views
0

我想引用一個外鍵到它的父鍵在MySQL中,我得到一個尷尬的錯誤。MySQL:引用外鍵

我試過以下。

ALTER TABLE `website` 
    ADD CONSTRAINT `website_cms_fk1` FOREIGN KEY (`cms_id`) REFERENCES `cms_technology` (`ID`); 

ALTER TABLE website ADD FOREIGN KEY (cms_id) REFERENCES cms_technology (ID) 

我碰到下面的錯誤。

*#1005 - 無法創建表(錯誤:150)*

以下是我的表

CREATE TABLE IF NOT EXISTS `cms_technology` (
    `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
    `cms_name` varchar(250) NOT NULL DEFAULT '', 
    `cms_description` varchar(250) NOT NULL DEFAULT '', 
    PRIMARY KEY (`ID`) 
); 

INSERT INTO `cms_technology` (`ID`, `cms_name`, `cms_description`) VALUES 
(1, 'Wordpress', 'WordPress › Blog Tool, Publishing Platform, and CMS'); 

CREATE TABLE IF NOT EXISTS `website` (
    `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
    `website_url` varchar(60) NOT NULL DEFAULT '', 
    `website_ip` varchar(20) NOT NULL DEFAULT '', 
    `website_title` varchar(250) NOT NULL DEFAULT '', 
    `website_status` varchar(10) NOT NULL DEFAULT '', 
    `website_scanned` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `website_response` int(3) NOT NULL DEFAULT '0', 
    `cms_id` int(5) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`ID`) 
); 

INSERT INTO `website` (`ID`, `website_url`, `website_ip`, `website_title`, `website_status`, `website_scanned`, `website_response`, `website_cms`) VALUES 
(1, 'http://www.wpbeginner.com/', '', '', '', '0000-00-00 00:00:00', 0, 0); 

我做什麼我錯了 '腳本#SQL-5203_110b8ba。'?

+0

嘗試:: 'ALTER TABLE網站MODIFY外鍵(cms_id)參考cms_technology( ID)' – 2013-02-10 07:39:07

回答

2

您需要更改表website的列cms_ID的數據類型,以便參考表cms_technology。 to列的屬性必須相同。

CREATE TABLE IF NOT EXISTS `website` (
    `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
    `website_url` varchar(60) NOT NULL DEFAULT '', 
    `website_ip` varchar(20) NOT NULL DEFAULT '', 
    `website_title` varchar(250) NOT NULL DEFAULT '', 
    `website_status` varchar(10) NOT NULL DEFAULT '', 
    `website_scanned` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `website_response` int(3) NOT NULL DEFAULT '0', 
    `cms_id` bigint(20) unsigned NOT NULL,      -- <<== HERE 
    PRIMARY KEY (`ID`) 
); 
+0

我認爲這將解決問題,我chan ged的數據類型屬性匹配,並仍然得到相同的錯誤。 – 2013-02-10 07:48:59

+0

什麼是完整的錯誤信息? – 2013-02-10 07:51:57

+0

同樣的一個,但現在已經修復了,我在主鍵後添加了KEY'cms_id'('cms_id'),並且當你定義一個外鍵時約束工作了 – 2013-02-10 07:53:12

0

我錯過了KEY cms_idcms_id

CREATE TABLE IF NOT EXISTS `website` (
     `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
     `website_url` varchar(60) NOT NULL DEFAULT '', 
     `website_ip` varchar(20) NOT NULL DEFAULT '', 
     `website_title` varchar(250) NOT NULL DEFAULT '', 
     `website_status` varchar(10) NOT NULL DEFAULT '', 
     `website_scanned` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
     `website_response` int(3) NOT NULL DEFAULT '0', 
     `cms_id` bigint(20) unsigned NOT NULL DEFAULT '0', 
     PRIMARY KEY (`ID`), 
     KEY `cms_id` (`cms_id`) 
    ); 

ALTER TABLE `website` 
    ADD CONSTRAINT `website_cms_fk1` FOREIGN KEY (`cms_id`) REFERENCES `cms_technology` (`ID`); 
+0

,你不需要明確地定義關於cms的鍵。 [**見此**](http://www.sqlfiddle.com/#!2/31e8f/3) – 2013-02-10 08:37:15