SQL error: “name already used by an existing constraint”

谁说胖子不能爱 提交于 2020-01-11 14:39:11

问题


I'm trying to create some tables and setup the foreign keys but I keep encountering problems with the foreign keys.

Earlier on I created the below table and it works fine

CREATE TABLE inpatient
(PatientNo varchar(6) NOT NULL,
WardNo number(2),
BedNo number(3) NOT NULL,
OnWaitingList date, 
WardRequired varchar(25), 
ExpectStayInDays number(4), 
DatePlaced date, 
DateLeave date, 
ActualLeave date, 

constraint PatientFK foreign key (PatientNo)     references Patient (patientNo),

constraint bedFK foreign key (BedNo)     references Bed (bedNo));

Notice the use of patientFK on 2nd last line.

then I went on to create another table

CREATE TABLE NOK
(PatientNo varchar(6)   NOT NULL,
NOKFullName   varchar(25), 
NOKRelationship   varchar(25), 
NOKTelephone   number(11), 
NOKStreetAddress   varchar(25), 
NOKSuburb   varchar(25), 
NOKState   char(2), 
NOKPostCode   number(4), 


constraint patientFK foreign key (PatientNo)
references Patient (patientNo));

and I get the following error message

Error report: SQL Error: ORA-02264: name already used by an existing constraint 02264. 00000 - "name already used by an existing constraint" *Cause: The specified constraint name has to be unique. *Action: Specify a unique constraint name for the constraint.

I have no idea how to resolve it or find similar examples that can suggest a solution.


回答1:


The name of the constraint (PatientFK in this case) as to be unique in the database. You've already used it once, so you're getting an error message wen you try and use it again. Just give the constraint you're setting up on your NOK table a different name:

Perhaps:

constraint patientFK_nok foreign key (PatientNo)


来源:https://stackoverflow.com/questions/20279973/sql-error-name-already-used-by-an-existing-constraint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!