2010-03-30 68 views
1

我想創建一個數據庫,其中表藥物和表項之間有n×m的關係,表目標和表項之間有n×m的關係。與mysql中的同一個表有兩個n×m關係

我得到的錯誤:Cannot delete or update a parent row: a foreign key constraint fails 我必須在我的代碼中更改什麼?

DROP TABLE IF EXISTS `textmine`.`article`; 
CREATE TABLE `textmine`.`article` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Pubmed ID', 
    `abstract` blob NOT NULL, 
    `authors` blob NOT NULL, 
    `journal` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

DROP TABLE IF EXISTS `textmine`.`drugs`; 
CREATE TABLE `textmine`.`drugs` (
    `id` int(10) unsigned NOT NULL COMMENT 'This ID is taken from the biosemantics dictionary', 
    `primaryName` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

DROP TABLE IF EXISTS `textmine`.`targets`; 
CREATE TABLE `textmine`.`targets` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `primaryName` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 


DROP TABLE IF EXISTS `textmine`.`containstarget`; 
CREATE TABLE `textmine`.`containstarget` (
    `targetid` int(10) unsigned NOT NULL, 
    `articleid` int(10) unsigned NOT NULL, 
    KEY `target` (`targetid`), 
    KEY `article` (`articleid`), 
    CONSTRAINT `article` FOREIGN KEY (`articleid`) REFERENCES `article` (`id`), 
    CONSTRAINT `target` FOREIGN KEY (`targetid`) REFERENCES `targets` (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

DROP TABLE IF EXISTS `textmine`.`contiansdrug`; 
CREATE TABLE `textmine`.`contiansdrug` (
    `drugid` int(10) unsigned NOT NULL, 
    `articleid` int(10) unsigned NOT NULL, 
    KEY `drug` (`drugid`), 
    KEY `article` (`articleid`), 
    CONSTRAINT `article` FOREIGN KEY (`articleid`) REFERENCES `article` (`id`), 
    CONSTRAINT `drug` FOREIGN KEY (`drugid`) REFERENCES `drugs` (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

回答

-2

我解決了這個問題,沒有在MySql中聲明外鍵,而只是將它們聲明爲整數。

+0

-1這絕對不是給定問題的解決方案。外鍵(參照完整性關係)是RDBMS的全部內容,並且絕對是一種不會強迫您更改設計的解決方案。 – Unreason 2010-04-03 10:43:20

1

您正在嘗試創建無序表。

例如,您正在嘗試創建contiansdrug表,其中涉及藥物表之前的表藥物。

請記住,任何SQL,甚至是DDL,都會嘗試使數據庫保持一致狀態。

我會建議把命令按正確的順序。或者你有選擇暫時關閉的檢查並運行內幕交易創造紙條,看到說明here

相關章節

SET AUTOCOMMIT = 0; 
SET FOREIGN_KEY_CHECKS=0; 

.. your script.. 

SET FOREIGN_KEY_CHECKS = 1; 
COMMIT; 
SET AUTOCOMMIT = 1; 

編輯:

OK,儘量不要有相同的名字爲了約束。閱讀fine手動啓發:

If the CONSTRAINT symbol clause is given, the symbol value must be unique in the database. If the clause is not given, InnoDB creates the name automatically.

EDIT2:

拼寫出來,你有重複的約束符號article,重命名它和所有將被罰款。

標準做法是如果您將約束命名爲使用描述相關內容的名稱,例如containsdrug_acticelid_article_id(firsttablename_column_secondtablename_column)將是唯一且描述性的。

+0

嗯,這不是主要的錯誤。訂單來自錯誤地複製。我糾正了問題中的順序。 – Christian 2010-03-30 10:34:25

相關問題