2017-03-01 44 views
1

我想添加引用Member.id的外鍵Enrols.mid,但經過幾次嘗試(添加索引,創建鍵,googleing)後,它仍然無法工作。 這兩列具有完全相同的數據類型,爲什麼添加外鍵不起作用?儘管數據類型相同,但「外鍵約束錯誤地形成」

MariaDB [alumni]> alter table Enrols add foreign key (`mid`) references Member.id; 
ERROR 1005 (HY000): Can't create table `alumni`.`#sql-31e_21d` (errno: 150 "Foreign key constraint is incorrectly formed") 
MariaDB [alumni]> desc Member; 
+-----------+-------------+------+-----+---------+-------+ 
| Field  | Type  | Null | Key | Default | Extra | 
+-----------+-------------+------+-----+---------+-------+ 
| id  | char(8)  | NO | PRI |   |  | 
| cName  | varchar(8) | NO |  | NULL |  | 
| eName  | varchar(40) | NO |  | NULL |  | 
| gender | char(1)  | NO |  | NULL |  | 
| org  | varchar(40) | YES |  | NULL |  | 
| pos  | varchar(20) | YES |  | NULL |  | 
| hobby  | int(11)  | NO |  | NULL |  | 
| issueDate | date  | NO |  | NULL |  | 
| note  | text  | YES |  | NULL |  | 
+-----------+-------------+------+-----+---------+-------+ 
9 rows in set (0.01 sec) 

MariaDB [alumni]> desc Enrols; 
+-------+---------+------+-----+---------+-------+ 
| Field | Type | Null | Key | Default | Extra | 
+-------+---------+------+-----+---------+-------+ 
| id | int(11) | YES |  | NULL |  | 
| aid | int(11) | NO | PRI | NULL |  | 
| mid | char(8) | NO | PRI |   |  | 
| paid | date | YES |  | NULL |  | 
+-------+---------+------+-----+---------+-------+ 
4 rows in set (0.01 sec) 

MariaDB [alumni]> 
+1

豈不是'引用會員(ID);'? –

回答

0

像@Damien_The_Unbeliever在他們的評論說,這應該是references Member(id)

.語法意味着:在架構Member參考表id(但你要引用idMember