2013-09-21 116 views
36

我已閱讀數據庫系統概念,第6版,Silberschatz。我將在MySQL上實現第2章中有關OS X的大學數據庫系統。但是我在創建表course時遇到了麻煩。表department看起來像創建course導致以下錯誤表MySQL:錯誤1215(HY000):無法添加外鍵約束

mysql> select * from department 
    -> ; 
+------------+----------+-----------+ 
| dept_name | building | budget | 
+------------+----------+-----------+ 
| Biology | Watson | 90000.00 | 
| Comp. Sci. | Taylor | 100000.00 | 
| Elec. Eng. | Taylor | 85000.00 | 
| Finance | Painter | 120000.00 | 
| History | Painter | 50000.00 | 
| Music  | Packard | 80000.00 | 
| Physics | Watson | 70000.00 | 
+------------+----------+-----------+ 

mysql> show columns from department 
    -> ; 
+-----------+---------------+------+-----+---------+-------+ 
| Field  | Type   | Null | Key | Default | Extra | 
+-----------+---------------+------+-----+---------+-------+ 
| dept_name | varchar(20) | NO | PRI |   |  | 
| building | varchar(15) | YES |  | NULL |  | 
| budget | decimal(12,2) | YES |  | NULL |  | 
+-----------+---------------+------+-----+---------+-------+ 

mysql> create table course 
    -> (course_id varchar(7), 
    -> title varchar (50), 
    -> dept_name varchar(20), 
    -> credits numeric(2,0), 
    -> primary key(course_id), 
    -> foreign key (dept_name) references department); 
ERROR 1215 (HY000): Cannot add foreign key constraint 

爲外鍵約束谷歌搜索後,我剛剛得知這個詞「外鍵約束」表示,從下表course外鍵列數據必須在主鍵列存在於表department。但是在插入數據時我應該遇到這個錯誤。

如果沒有,爲什麼作者讓我執行該SQL語句?

如果我真的執行了錯誤的SQL語句,插入一些數據後我是否必須在課程表中指定dept_name作爲外鍵?

編輯:輸入set foreign_key_checks=0mysql>不能修復錯誤。

------------------------ 
LATEST FOREIGN KEY ERROR 
------------------------ 
2013-09-21 16:02:20 132cbe000 Error in foreign key constraint of table university/course: 
foreign key (dept_name) references department): 
Syntax error close to: 
) 
mysql> set foreign_key_checks=0 
    -> ; 
Query OK, 0 rows affected (0.00 sec) 
mysql> create table course 
    -> (course_id varchar(7), 
    -> title varchar(50), 
    -> dept_name varchar(20), 
    -> credits numeric(2,0), 
    -> primary key(course_id), 
    -> foreign key (dept_name) references department); 
ERROR 1215 (HY000): Cannot add foreign key constraint 
+0

http://stackoverflow.com/a/15535110/242520 –

+0

這兩個表中的所有'dept_name'都是varchar(15)。我在哪裏輸入'set foreign_key_checks = 0'? – inherithandle

+0

在'mysql>'中鍵入'set foreign_key_checks = 0'不能解決錯誤。 – inherithandle

回答

40

FOREIGN KEYCREATE TABLE的語法結構如下:

FOREIGN KEY (index_col_name) 
     REFERENCES table_name (index_col_name,...) 

所以你的MySQL DDL應該是:

create table course (
     course_id varchar(7), 
     title varchar(50), 
     dept_name varchar(20), 
     credits numeric(2 , 0), 
     primary key (course_id), 
     FOREIGN KEY (dept_name) 
      REFERENCES department (dept_name) 
    ); 

此外,在departmentdept_name應該VARCHAR(20)

更多信息可以在MySQL documentation

+0

我很樂意爲您解釋詳細說明。我從未見過statemnt'CONSTRAINT'。 – inherithandle

+0

'CONSTRAINT link_dept_course'給這個關係賦予一個符號。並且它是可選的。如果沒有給出該子句,或者在CONSTRAINT關鍵字後面沒有包含符號,則會自動創建該約束的名稱。 FYI:http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html – Strik3r

+0

約束部分是可選的,但PK列的列表不是。所以'參考部門'與'參考部門(dept_name)'確實修復了它。我不知道MySQL不支持「短手」符號(由SQL標準定義) –

6
foreign key (dept_name) references department 

此語法是無效的MySQL找到。它應該是:

foreign key (dept_name) references department(dept_name) 

MySQL requires dept_name to be used twice。一次定義外部列,一次定義主要列。

13.1.17.2. Using FOREIGN KEY Constraints

... [the] essential syntax for a foreign key constraint definition in a CREATE TABLE or ALTER TABLE statement looks like this:

[CONSTRAINT [symbol]] FOREIGN KEY 
    [index_name] (index_col_name, ...) 
    REFERENCES tbl_name (index_col_name, ...) 
    [ON DELETE reference_option] 
    [ON UPDATE reference_option] 

reference_option: 
    RESTRICT | CASCADE | SET NULL | NO ACTION 
19

也許你的dept_name列有不同的字符集。

你可以嘗試改變的一個或兩個人:

ALTER TABLE department MODIFY dept_name VARCHAR(20) CHARACTER SET utf8; 
ALTER TABLE course MODIFY dept_name VARCHAR(20) CHARACTER SET utf8; 
+0

這是一個棘手的問題。我有一個表使用明確的UTF8,而不是其他。在遷移到不使用UTF8的服務器作爲默認設置時,工作模式停止工作。這救了我! –

+0

感謝這個答案,在我的情況下,主鍵是utf8_bin,而「wannabe-foreign」是utf8_general_ci。這也救了我! –

0

值得一提的,如果你正在使用中的參考部分目標表或列根本不存在也可能發生這種錯誤。

40

當你得到這個模糊的錯誤信息,您可以通過運行

SHOW ENGINE INNODB STATUS; 

找出更具體的錯誤最常見的原因是創建一個外鍵時,雙方所引用的字段和外鍵字段需要匹配:

  • 引擎應該是相同的如InnoDB
  • 數據類型應該是相同的,並且具有相同的長度。
    例如VARCHAR(20)或INT(10)UNSIGNED
  • 整理應該是相同的。 例如utf8
  • 唯一 - 外鍵應該指的是在參考表中唯一(通常爲私人)的字段。

此錯誤的另一個原因是:
您已經定義雖然某些列被定義爲NOT NULL一個SET NULL條件。

+4

我剛剛被絆倒了,因爲一個字段是'UNSIGNED'而另一個字段不是,所以只是編輯了你的答案。希望沒關係。 –

+0

@SimonEast thx指向我在正確的方向有相同的問題:) – DevJ3rry

+0

這應該是被接受的答案,因爲它會最有幫助的人誰發現這個網頁尋找解決他們的問題 – blueimpb

-1
CONSTRAINT vendor_tbfk_1 FOREIGN KEY (V_CODE) REFERENCES vendor (V_CODE) ON UPDATE CASCADE 

這是怎麼回事......查看引用列部分。 (V_code)

3

如果外鍵不是自己表中的主鍵,也可能會出現此錯誤。

我做了一個ALTER TABLE,並意外刪除了列的主鍵狀態,並得到了這個錯誤。

0

只需添加「無符號」爲外國約束

`FK` int(11) unsigned DEFAULT NULL, 
1

ERROR 1215 (HY000): Cannot add foreign key constraint

還值得一提的是,你得到這個錯誤,當類型,它是另一種能夠沒有按一個外鍵的列沒有明確地匹配正確表中的列。

例如:

alter table schoolPersons 
     add index FKEF5AB5E532C8FBFA (student_id), 
     add constraint FKEF5AB5E532C8FBFA 
     foreign key (student_id) 
     references student (id); 
ERROR 1215 (HY000): Cannot add foreign key constraint 

這是因爲student_id字段被定義爲:

mysql> desc schoolPersons; 
+--------------------+------------+------+-----+---------+----------------+ 
| Field    | Type  | Null | Key | Default | Extra   | 
+--------------------+------------+------+-----+---------+----------------+ 
| student_id   | bigint(20) | YES |  | NULL |    | 

而在studentid字段被定義爲:

mysql> desc persons; 
+--------------+----------------------+------+-----+-------------------+-----------------+ 
| Field  | Type     | Null | Key | Default   | Extra   | 
+--------------+----------------------+------+-----+-------------------+-----------------+ 
| id   | int(10) unsigned  | NO | PRI | NULL    | auto_increment | 

bigint(20)(屬)通過休眠從Java long得到)與int(10) unsigned(Java int)不兼容。

0

下面的代碼爲我工作

set @@foreign_key_checks=0; 
ALTER TABLE `table1` ADD CONSTRAINT `table1_fk1` FOREIGN KEY (`coloumn`) REFERENCES `table2` (`id`) ON DELETE CASCADE; 
0

I don't meet the problem as you. But I get the same ERROR Message. So I mark it down here for others' convience.

檢查兩個表的字符集,如果列類型是charvarchar。我使用charset=gbk,但我創建了一個新表,其默認值爲charset=utf8。所以charset是不一樣的。

ERROR 1215 (HY000): Cannot add foreign key constraint 

要解決它是使用相同的字符集。例如utf8

相關問題