mysql ERROR 1215 (HY000): Cannot add foreign key constraint

來源:互聯網
上載者:User

標籤:ima   ann   creat   ror   實驗   不同   incr   一個   for   

ERROR 1215 (HY000): Cannot add foreign key constraint

最近在建表時遇到了這個錯誤,然後找了下找到瞭解決辦法,記錄下:

本來是要建兩張表:

create table department(id int,name varchar(20) );create table employee(id int primary key auto_increment,name varchar(20),sex enum(‘male‘,‘female‘) not null default ‘male‘,age int,dep_id int,foreign key(dep_id) references department(id));

 出現下面的提示:

然後開始上網查,有的說是用的引擎不同的原因,查看了下我的,兩引擎一模一樣,

又有的說一個是int ,因為加了auto_increment後,實際變成了int undesigned,既然變成了

int undesigned那我也設定成undesigned,再插入,錯誤依舊,

 

最後 我想到了上面的department表中的id,只是整形,可能不唯一,

然後加上了 primary key

create table department(id int primary key,name varchar(20) );

 再次實驗,發現能正常建立表

這裡的具體原理是什麼還沒搞清楚,上面的也只是猜測,特此記錄

如果找到原因再更新!

 

 

更新:

也就是說我上面 的猜測是正確的!

看看對unique的測試:

 

mysql ERROR 1215 (HY000): Cannot add foreign key constraint

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.