所以,这是我的意见,我正在设计一个医院数据库,我正在创建一个表格,用于保存“患者”表中患者与“房间”表中房间之间的关系(聪明,我知道。)
这里的语句用于声明两个表:
create table patient_room
(
pr_id int NOT NULL PRIMARY KEY auto_increment,
patient_id int NOT NULL,
room_id int NOT NULL,
)ENGINE = InnoDB;
create table patients
(
patient_id int primary key not null auto_increment,
fname varchar(30),
lname varchar(30),
suffix enum('I','II','III','IV','JR','SR'),
sex enum('M','F','U','T'),
eye_color enum('BK','BR','BL','GY','GR','HZ','MN','DX','UN'),
hair_color enum('BK','BR','BN','RD','WH','SN','BD','UN'),
date_of_birth date not null,
height int unsigned not null,
weight int unsigned not null,
admitted datetime not null
) Engine = InnoDB;
这是我的修改声明
alter table patient_room add foreign key (patient_id) references patient(patient_id) on delete cascade on update cascade;
我得到了回报:
ERROR 1005 (HY000): Can't create table 'Mthomas.#sql-3dac_5f1' (errno: 150)
我能够使用
更改表以创建外键alter table patient_room add foreign key (room_id) references room(room_id) on delete cascade on update cascade;
没有错误。我确实已将patient_id作为外键放在另一个Patient_meds表上,我认为这可能是问题......如果是这样的话?我该如何缓解它?
答案 0 :(得分:2)
您的表名为patients
,而不是patient
。你需要改变
alter table patient_room add foreign key (patient_id) references patient(patient_id) on delete cascade on update cascade;
到
alter table patient_room add foreign key (patient_id) references patients(patient_id) on delete cascade on update cascade;
^