Oracle Project(使用外键和模型的真实方法)
您好,我做了这个,但无法打开模型。我遇到了这个错误。我知道对于模型,我们需要使用外键和主键,但是执行第一代码时会遇到错误。是否有任何简单的打印模型的方法。另外,我没有得到我认为我们链接2的参考部分 放弃密钥。
ALTER TABLE Recipient
ADD FOREIGN KEY (firstName)
REFERENCES Donor(firstName);
CREATE TABLE Recipient( recipientID INT NOT NULL,
firstName VARCHAR( 50 ) NOT NULL, lastname VARCHAR( 50 ) not null,
address VARCHAR( 60 ) not null, email VARCHAR( 100 ) not null,
phone VARCHAR( 20 ) not null, birthday INT not null, bloodType varchar (3)NOT NULL,
PRIMARY KEY (recipientID));
CREATE TABLE Donor( donorID INT NOT NULL,firstName VARCHAR( 50 ) NOT NULL,
lastname VARCHAR(50) not null, address VARCHAR(60) not null, email VARCHAR( 100 ) not null,
phone VARCHAR(20) not null, birthday INT not null, bloodtype VARCHAR(3)NOT NULL, PRIMARY KEY (donorID));
CREATE TABLE Blood_BANK( donorID INT NOT NULL , Blood_BANK VARCHAR(50) not null, Adress VARCHAR(50) not null,PRIMARY KEY(donorID));
Create TABLE Hospital(donorID INT NOT NULL,
recipientID INT NOT NULL ,hospital VARCHAR(50),room INT NOT NULL,doctor VARCHAR(50) not null, primary key(hospital) );
CREATE TABLE doctor( doctor VARCHAR(50) not null, hospital VARCHAR(50) not null, primary key (doctor));
CREATE SEQUENCE seq20
INCREMENT BY 1 START WITH 1;
INSERT INTO Donor (donorID,firstName, lastname, address, email,phone, birthday , bloodType)
VALUES (seq20.NEXTVAL,'Neriman','Nerimanov' , 'Marneuli' ,'[email protected]' , 597156, 2004,'O+' );
CREATE SEQUENCE seq21
INCREMENT BY 1 START WITH 1 ;
INSERT INTO Recipient(recipientID, firstName,lastname,address,email,phone,birthday,bloodType)
VALUES ( seq21.NEXTVAL, 'Nizami', 'Lom', 'Batumi', '[email protected]', 597931,2000,'O+');
drop sequence seq22;
CREATE SEQUENCE seq22
INCREMENT BY 1 START with 1;
INSERT INTO Blood_BANK (donorID, Blood_BANK , Adress)
VALUES (seq22.NEXTVAL, 'Marneuli Hospital', 'Marneuli');
INSERT INTO doctor( doctor, hospital)
VALUES( 'Nana Oposhvili ', ' havit medical');
CREATE SEQUENCE seq23
INCREMENT BY 1 START with 1;
CREATE SEQUENCE seq24
INCREMENT BY 1 START WITH 1;
INSERT INTO Hospital(donorID,recipientID, hospital,room, doctor)
VALUES( seq23.NEXTVAL, seq24.NEXTVAL,'Havit medical',511,'Nana Oposhvili');
错误:
模型:
Hello so i make this but could not open model. i am getting this error. I know for model we need to use foreign and main keys but i am getting errors when i execute 1st code. Is there any easy way to print models. plus i did not get this reference part i thought like that we are linking 2
foregn key .
ALTER TABLE Recipient
ADD FOREIGN KEY (firstName)
REFERENCES Donor(firstName);
CREATE TABLE Recipient( recipientID INT NOT NULL,
firstName VARCHAR( 50 ) NOT NULL, lastname VARCHAR( 50 ) not null,
address VARCHAR( 60 ) not null, email VARCHAR( 100 ) not null,
phone VARCHAR( 20 ) not null, birthday INT not null, bloodType varchar (3)NOT NULL,
PRIMARY KEY (recipientID));
CREATE TABLE Donor( donorID INT NOT NULL,firstName VARCHAR( 50 ) NOT NULL,
lastname VARCHAR(50) not null, address VARCHAR(60) not null, email VARCHAR( 100 ) not null,
phone VARCHAR(20) not null, birthday INT not null, bloodtype VARCHAR(3)NOT NULL, PRIMARY KEY (donorID));
CREATE TABLE Blood_BANK( donorID INT NOT NULL , Blood_BANK VARCHAR(50) not null, Adress VARCHAR(50) not null,PRIMARY KEY(donorID));
Create TABLE Hospital(donorID INT NOT NULL,
recipientID INT NOT NULL ,hospital VARCHAR(50),room INT NOT NULL,doctor VARCHAR(50) not null, primary key(hospital) );
CREATE TABLE doctor( doctor VARCHAR(50) not null, hospital VARCHAR(50) not null, primary key (doctor));
CREATE SEQUENCE seq20
INCREMENT BY 1 START WITH 1;
INSERT INTO Donor (donorID,firstName, lastname, address, email,phone, birthday , bloodType)
VALUES (seq20.NEXTVAL,'Neriman','Nerimanov' , 'Marneuli' ,'[email protected]' , 597156, 2004,'O+' );
CREATE SEQUENCE seq21
INCREMENT BY 1 START WITH 1 ;
INSERT INTO Recipient(recipientID, firstName,lastname,address,email,phone,birthday,bloodType)
VALUES ( seq21.NEXTVAL, 'Nizami', 'Lom', 'Batumi', '[email protected]', 597931,2000,'O+');
drop sequence seq22;
CREATE SEQUENCE seq22
INCREMENT BY 1 START with 1;
INSERT INTO Blood_BANK (donorID, Blood_BANK , Adress)
VALUES (seq22.NEXTVAL, 'Marneuli Hospital', 'Marneuli');
INSERT INTO doctor( doctor, hospital)
VALUES( 'Nana Oposhvili ', ' havit medical');
CREATE SEQUENCE seq23
INCREMENT BY 1 START with 1;
CREATE SEQUENCE seq24
INCREMENT BY 1 START WITH 1;
INSERT INTO Hospital(donorID,recipientID, hospital,room, doctor)
VALUES( seq23.NEXTVAL, seq24.NEXTVAL,'Havit medical',511,'Nana Oposhvili');
Error:
Model:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简化了,这就是您所做的:
错误说
供体
表中没有匹配的主键或唯一键,然后可以从coceptient
表中引用该键。换句话说:任何表格 - 应该引用
供体
表的表格 - 必须引用其主键列,即donorid
。您不能引用firstName
,它只是只是列(对此没什么特别的) - 您当然不会使其成为主要键,并且将其作为唯一钥匙甚至更糟(肯定还有世界上一个Omar(或在供体
表中)。如果不是现在,它将是。 怎么办?
该 。 表明哪个
第三个
角色
。
人
的 机会)。
Simplified, this is what you did:
Error says that there's no matching primary or unique key in
DONOR
table which could then be referenced from theRECIPIENT
table.In other words: any table - which is supposed to reference the
DONOR
table - must reference its primary key column, and that isdonorID
. You can't referencefirstname
, it is just a column (nothing special about it) - you certainly won't make it a primary key, and having it as a unique key is even worse (there surely is more than just one Omar in the world (or in thedonor
table). If not now, it will be. First names are poor choice for unique keys.Therefore, what to do?
recipient
table should havedonorid
column which would then referencedonor
table.On the other hand: your data model seems to be wrong. There should be only one table that holds personal info (ID, first and last name, address, etc.). There should be another table which says which "roles" are possible (currently, a donor or a recipient). Finally, the third table which says which person acts which role.
Something like this (simplified, again):
Inserts:
Tables' contents:
Query you might write:
As you can see, Scott is both a donor and a recipient (why not? There's such a chance).