2014-04-01 100 views
6

我得到了這兩個succesfull查詢:MySQL的 - 錯誤代碼1215,不能添加外鍵約束

create table Donors (
    donor_id int not null auto_increment primary key, 
    gender varchar(1) not null, 
    date_of_birth date not null, 
    first_name varchar(20) not null, 
    middle_name varchar(20), 
    last_name varchar(30) not null, 
    home_phone tinyint(10), 
    work_phone tinyint(10), 
    cell_mobile_phone tinyint(10), 
    medical_condition text, 
    other_details text); 

create table Donors_Medical_Condition (
    donor_id int not null, 
    condition_code int not null, 
    seriousness text, 
    primary key(donor_id, condition_code), 
    foreign key(donor_id) references Donors(donor_id) ); 

但是當我嘗試這一個:

create table Medical_Conditions (
    condition_code int not null, 
    condition_name varchar(50) not null, 
    condition_description text, 
    other_details text, 
    primary key(condition_code), 
    foreign key(condition_code) references Donors_Medical_Condition(condition_code)); 

我得到「錯誤代碼:1215,無法添加外鍵約束」

我不知道我做錯了什麼。

+0

您正在嘗試引用複合主鍵的一部分,在最後一個表的外鍵。 –

+0

爲什麼要將條件代碼外鍵放在醫療條件表上?它不應該在Donors_Medical_Condition表上嗎? – Jim

+0

可能重複的[錯誤代碼:1215.無法添加外鍵約束(外鍵)](https://stackoverflow.com/questions/17691282/error-code-1215-cannot-add-foreign-key-constraint-foreign -keys) – Alexander

回答

9

在MySql中,外鍵引用需要引用索引(包括主鍵),索引的第一部分與外鍵字段匹配。如果你在condition_code上創建了一個索引或者改變了主鍵st condition_code是第一個,你應該可以創建索引。

0

我認爲你已經讓你的桌子有點倒退了。我假設Donors_Medical_Condtion鏈接捐助者和醫療條件,所以你需要一個外部捐贈者和條件的關鍵表。

修訂

好吧,你也以錯誤的順序創建表。這裏是整個腳本:

create table Donors (
donor_id int not null auto_increment primary key, 
gender varchar(1) not null, 
date_of_birth date not null, 
first_name varchar(20) not null, 
middle_name varchar(20), 
last_name varchar(30) not null, 
home_phone tinyint(10), 
work_phone tinyint(10), 
cell_mobile_phone tinyint(10), 
medical_condition text, 
other_details text); 

create table Medical_Conditions (
condition_code int not null, 
condition_name varchar(50) not null, 
condition_description text, 
other_details text, 
primary key(condition_code)); 

create table Donors_Medical_Condition (
donor_id int not null, 
condition_code int not null, 
seriousness text, 
primary key(donor_id, condition_code), 
foreign key(donor_id) references Donors(donor_id), 
foreign key(condition_code) references Medical_Conditions(condition_code)); 
4

要定義foreign key,引用的父字段必須在其上定義的索引。

按照文件上foreign key約束:

參考tbl_name(一個index_col_name,...)

定義上condition_codeINDEX在父表Donors_Medical_Condition,它應該是工作。

create table Donors_Medical_Condition (
    donor_id int not null, 
    condition_code int not null, 
    seriousness text, 

    KEY (condition_code), -- <---- this is newly added index key 

    primary key(donor_id, condition_code), 
    foreign key(donor_id) references Donors(donor_id) ); 

但似乎你定義了你的表順序和引用錯誤。 您應該在Donors_Medical_Condition表中定義foreign key,但不在Donors_Medical_Conditions表中。後者似乎是父母

相應地修改您的腳本。

他們應該寫成:

-- create parent table first (general practice) 
create table Medical_Conditions (
    condition_code int not null, 
    condition_name varchar(50) not null, 
    condition_description text, 
    other_details text, 
    primary key(condition_code) 
); 

-- child table of Medical_Conditions 
create table Donors_Medical_Condition (
    donor_id int not null, 
    condition_code int not null, 
    seriousness text, 
    primary key(donor_id, condition_code), 
    foreign key(donor_id) references Donors(donor_id), 
    foreign key(condition_code) 
     references Donors_Medical_Condition(condition_code) 
); 

參考

[約束[符號] FOREIGN KEY
[INDEX_NAME] (一世ndex_col_name,...)
參考tbl_name(一個index_col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]

reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION

0

我得到了同樣的問題,並根據給定的答案,我驗證了所有的數據類型和引用,但每次我重新創建我的表我得到這個錯誤。花了幾個小時後,我開始知道下面的命令,它給了我內部的錯誤 -

SHOW ENGINE INNODB STATUS;

LATEST FOREIGN KEY ERROR 
------------------------ 
2015-05-16 00:55:24 12af3b000 Error in foreign key constraint of table letmecall/lmc_service_result_ext: 
there is no index in referenced table which would contain 
the columns as the first columns, or the data types in the 
referenced table do not match the ones in table. Constraint: 
, 
    CONSTRAINT "fk_SERVICE_RESULT_EXT_LMC_SERVICE_RESULT1" FOREIGN KEY ("FK_SERVICE_RESULT") REFERENCES "LMC_SERVICE_RESULT" ("SERVICE_RESULT") ON DELETE NO ACTION ON UPDATE NO ACTION 

我刪除了所有關於使用MySQL工作臺,但我仍看到同樣的錯誤。花了幾分鐘後,我執行以下語句來查看可用的所有約束在DB-

SELECT * FROM information_schema.table_constraints其中 constraint_schema =「XXXXX」

我想知道,我已刪除所有使用mysql工作臺的關係,但仍然有那個約束。原因是因爲這個約束已經在db中創建。

因爲它是我的測試數據庫所以我刪除了數據庫,當我重新創建所有的表與這張表一起工作。所以解決方案是在創建新表之前必須從數據庫中刪除該約束。

0

檢查兩個字段的大小是否相同,如果引用的字段是無符號的,那麼引用字段也應該是無符號的。

1

一個解決方法對於那些誰需要一個快速的操作方法:
FYI:我的問題沒有被列的數據類型/大小,整理或InnoDB存儲引擎的不一致而產生的。

如何:
下載一個MySQL工作臺並使用它的GUI添加外鍵。而已!

爲什麼:
錯誤是否與索引有關。我從MySQL工作臺自動生成的DML腳本中學到了這一點。這也幫助我排除了所有這些不一致的可能性。它適用於外鍵定義主題的其中一個條件。那就是:「MySQL需要外鍵和引用鍵的索引,以便外鍵檢查可以很快並且不需要表掃描。」以下是官方聲明:http://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html
我沒有想到將索引添加到外鍵列(在子表中),只關注引用的TO列(在父表中)。
這裏是自動生成的腳本(PHONE.PERSON_ID沒有指數最初):

ALTER TABLE `netctoss`.`phone` 
ADD INDEX `personfk_idx` (`PERSON_ID` ASC); 
ALTER TABLE `netctoss`.`phone` 
ADD CONSTRAINT `personfk` 
    FOREIGN KEY (`PERSON_ID`) 
    REFERENCES `netctoss`.`person` (`ID`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION; 
+0

對不起,關於作曲。新的堆棧溢出。 > _ < – Heather

相關問題