我的代码SQL Oracle不断出错

时间:2016-04-05 03:23:03

标签: sql oracle ddl

所以我已经在这个代码上工作了几天,我无法弄清楚为什么我会收到错误。这些是我得到的错误:

SQL Error: ORA-00904: "PATIENTID_FK": invalid identifier
00904. 00000 -  "%s: invalid identifier"

SQL Error: ORA-02264: name already used by an existing constraint
02264. 00000 -  "name already used by an existing constraint"

我已经包含了我编写的代码。 " ..."象征着我正在创建的表中不重要的信息,例如" DoctorFirstName"。有人可以帮我弄清楚我搞砸了哪里?谢谢!

DROP TABLE HealthRecord;
DROP TABLE Patient;    
DROP TABLE Insurance;
DROP TABLE Doctor;
DROP TABLE Hospital;
DROP TABLE Prescription;

CREATE TABLE Insurance (
InsuranceID number,
...
CONSTRAINT InsuranceID_pk
PRIMARY KEY (InsuranceID));

CREATE TABLE Prescription (
PrescriptionID number,
...
CONSTRAINT PrescriptionID_pk
PRIMARY KEY(PrescriptionID));

CREATE TABLE Hospital (
HospitalID number,
...
CONSTRAINT HospitalID_pk
PRIMARY KEY(HospitalID));

CREATE TABLE Doctor (
DoctorID number,
...
HospitalID number,
CONSTRAINT DoctorID_pk
PRIMARY KEY(DoctorID),
CONSTRAINT HospitalID_fk
FOREIGN KEY (HospitalID)
REFERENCES Hospital (HospitalID));

CREATE TABLE Patient (
PatientID number,
...
InsuranceID number,
CONSTRAINT PatientID_pk
PRIMARY KEY (PatientID),
CONSTRAINT InsuranceID_fk
FOREIGN KEY (InsuranceID)
REFERENCES Insurance(InsuranceID));

CREATE TABLE HealthRecord(
RecordID number,
...
DoctorID number,
PrescriptionID number,
PatientID number,
CONSTRAINT RecordID_pk
PRIMARY KEY(RecordID),
CONSTRAINT DoctorID_fk
FOREIGN KEY (DoctorID)
REFERENCES Doctor (DoctorID),
CONSTRAINT PrescriptionID_fk
FOREIGN KEY (PrescriptionID)
REFERENCES Prescription (PrescriptionID),
CONSTRAINT PatientID_fk
FOREIGN KEY (PatientID_fk)
REFERENCES Patient(PatientID));

1 个答案:

答案 0 :(得分:1)

删除表格并不能删除约束。所以你的约束仍然存在。您应该删除约束和索引。检查此链接:

https://www.1keydata.com/sql/alter-table-drop-constraint.html

引用下面的链接,在一个语句中删除表和约束(CASCADE CONSTRAINTS)

http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9003.htm