我们正在尝试创建一个简单的数据库,但我们总是面临同样的错误,而且我记录了其他外键约束问题,但它们根本没有帮助。
正如您所知,当我们将EER图转换为关系图时,关系会采用它们所连接的实体的主键,例如: 让我们说我们的entites有一个主键,如TC_NO,randevu_no,hasta_no和我们的关系帽绑定他们将获取这些实体的主键。 等;
CREATE TABLE REQUEST(
TC_NO int not null,
randevu_no int not null,
hasta_no int not null,
PRIMARY KEY(randevu_no,TC_NO,hasta_no),
FOREIGN KEY(hasta_no) REFERENCES PATIENT(hasta_no) ON DELETE CASCADE,
FOREIGN KEY(randevu_no) REFERENCES APPOINTMENT(randevu_no) ON DELETE CASCADE,
FOREIGN KEY(TC_NO) REFERENCES PATIENT(TC_NO) ON DELETE CASCADE
);
但是,我们总是得到同样的错误。没有拼写错误,我们检查了几个小时,但无法解决它,我们不知道为什么这不起作用。
对于需要数据库所有代码的人:
CREATE TABLE PERSON(
isim varchar(12) not null,
soyisim varchar(12) not null,
cinsiyet char(1) not null,
dogum_tarihi date not null,
adres varchar(150) not null,
TC_NO int not null,
PRIMARY KEY(TC_NO),
UNIQUE KEY(TC_NO));
CREATE TABLE EMPLOYEE(
departman varchar(20) not null,
sicil_no int not null,
TC_NO int not null,
PRIMARY KEY(TC_NO,sicil_no),
UNIQUE KEY(sicil_no),
FOREIGN KEY(TC_NO) REFERENCES PERSON(TC_NO) ON DELETE CASCADE
);
CREATE TABLE PATIENT(
hastalik_gecmisi varchar(400) not null,
kan_grubu char(4) not null,
hasta_no int not null,
TC_NO int not null,
PRIMARY KEY(TC_NO,hasta_no),
FOREIGN KEY(TC_NO) REFERENCES PERSON(TC_NO) ON DELETE CASCADE
);
CREATE TABLE DOCTOR(
diploma_no int not null,
brans varchar(40) not null,
sicil_no int not null,
TC_NO int not null,
PRIMARY KEY(TC_NO,sicil_no,diploma_no),
FOREIGN KEY(TC_NO) REFERENCES EMPLOYEE(TC_NO) ON DELETE CASCADE,
FOREIGN KEY(sicil_no) REFERENCES EMPLOYEE(sicil_no) ON DELETE CASCADE
);
CREATE TABLE APPOINTMENT(
hasta_no int not null,
departman varchar(20) not null,
randevu_no int not null,
doktor_no int not null,
tarih date not null,
saat int not null,
PRIMARY KEY(randevu_no)
);
CREATE TABLE SECRETARY(
TC_NO int not null,
sicil_no int not null,
PRIMARY KEY(sicil_no,TC_NO),
FOREIGN KEY(sicil_no) REFERENCES EMPLOYEE(sicil_no) ON DELETE CASCADE,
FOREIGN KEY(TC_NO) REFERENCES EMPLOYEE(TC_NO) ON DELETE CASCADE
);
CREATE TABLE REQUEST(
TC_NO int not null,
randevu_no int not null,
hasta_no int not null,
PRIMARY KEY(randevu_no,TC_NO,hasta_no),
FOREIGN KEY(hasta_no) REFERENCES PATIENT(hasta_no) ON DELETE CASCADE,
FOREIGN KEY(randevu_no) REFERENCES APPOINTMENT(randevu_no) ON DELETE CASCADE,
FOREIGN KEY(TC_NO) REFERENCES PATIENT(TC_NO) ON DELETE CASCADE
);
CREATE TABLE NOTIFY(
randevu_no int not null,
sicil_no int not null,
PRIMARY KEY(randevu_no,sicil_no),
FOREIGN KEY(randevu_no) REFERENCES APPOINTMENT(randevu_no) ON DELETE CASCADE,
FOREIGN KEY(sicil_no) REFERENCES SECRETARY(sicil_no) ON DELETE CASCADE
);
CREATE TABLE CONFIRMATION(
hasta_no int not null,
sicil_no_s int not null,
diploma_no int not null,
sicil_no_d int not null,
PRIMARY KEY(sicil_no_s,hasta_no,diploma_no,sicil_no_d),
FOREIGN KEY(sicil_no_s) REFERENCES SECRETARY(sicil_no_s) ON DELETE CASCADE,
FOREIGN KEY(hasta_no) REFERENCES PATIENT(hasta_no) ON DELETE CASCADE,
FOREIGN KEY(diploma_no) REFERENCES DOCTOR(diploma_no) ON DELETE CASCADE,
FOREIGN KEY(sicil_no_d) REFERENCES DOCTOR(sicil_no_d) ON DELETE CASCADE
);
答案 0 :(得分:0)
Patient表是否有复合键作为主键?你能展示其他表的创建语句吗?
答案 1 :(得分:0)
好的,我刚刚找到了解决方案。 http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html 这个链接让我思考,尝试失败的方式,我找到了解决方案。
当您从表中创建外键并且该键也是该表中的外键时,您应该执行外键,也可以使用唯一键,以便它们可以拥有索引并连接到较低的表。