我有一個需要創建三個表的研究案例。有創造性的陳述如下。Mysql無法添加外鍵
create table COMMUNITY(
c_id varchar(10) primary key,
name varchar(30) not null,
longitude float,
latitude float,
post_code varchar(15) not null,
key(c_id))
create table UNIT(
c_id varchar(10) not null,
u_id int not null,
name varchar(20) not null,
key(c_id, u_id),
primary key(c_id, u_id),
constraint unique(c_id, u_id),
constraint FK_UNIT foreign key(c_id) references COMMUNITY(c_id)
on delete cascade on update cascade)
create table ROOM(
r_id int not null,
u_id int not null,
c_id varchar(10) not null,
name varchar(20),
primary key(c_id, u_id, r_id),
constraint FK_ROOM_UID foreign key(u_id) references UNIT(u_id)
on delete cascade on update cascade,
constraint FK_ROOM_CID foreign key(c_id) references UNIT(c_id)
on delete cascade on update cascade)
社區和單元表創建成功,但是當我嘗試創建房間,MySQL的給我Error Code: 1215. Cannot add foreign key constraint
我不知道發生了什麼事情在這裏和我怎樣才能創建它們? (我知道的InnoDB可以解決這個問題,但是否有其他方法可以讓我做到這一點?)
感謝
對不起,這是一個輸入錯誤,我糾正了這個錯誤。謝謝 – Korben
PS - 您不需要c_id,u_id中的唯一索引 - 根據定義,PK是唯一的 –