I am facing problems when querying my SQL code.
If i create the table "treats" i got an Error, and simular by creating table "operated".
I know that I can write, that my Disease in the table "patients" is unique, this will fix my problem but it is not correct. Every patient can have the same disease. Person 1 can get a headache and person 2 too, but if I say that disease is unique, I can't add the same values on disease, so I can't say that person 1 had a headache and person 2 too.
How can i fix this code, so that i can insert into table treats , same disease's ?
For example: insert into treats ( persNr, patNr, disease) values (123, 12, "cough"); insert into treats (persnr, patnr, disease) values(234, 21, "cough");
My code:
CREATE TABLE Doctors
(PersNr INTEGER PRIMARY KEY UNIQUE,
Name VARCHAR(30) NOT NULL,
Rang CHAR(2) CHECK (Rang in ('C2', 'C3', 'C4')));
CREATE TABLE Insurance
(InsuranceNr INTEGER PRIMARY KEY UNIQUE,
Name VARCHAR(30) NOT NULL,
TelefonNr VARCHAR(30) NOT NULL,
Headquarter VARCHAR(30) NOT NULL);
CREATE TABLE Patients
(PatNr INTEGER PRIMARY KEY UNIQUE,
Name VARCHAR(30) NOT NULL,
InsuranceNr INTEGER REFERENCES Insurance(InsuranceNr) on DELETE CASCADE,
Disease VARCHAR(30) NOT NULL,
Damage VARCHAR(2) CHECK (Insurance in('J','j','N','n')),
DescriptionDamage VARCHAR(30)Not Null);
CREATE TABLE assistant
(PerlNr INTEGER PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Boss INTEGER,
FOREIGN KEY (Boss) REFERENCES doctors(PersNr));
CREATE TABLE hospital
(hospitalNr INTEGER PRIMARY KEY,
Name VARCHAR(30) NOT NULL UNIQUE,
Street VARCHAR(30) NOT NULL);
CREATE TABLE operations
(OperationId INTEGER PRIMARY KEY,
DoctorNr INTEGER REFERENCES doctors(PersNr) ON DELETE CASCADE,
PatientNr INTEGER REFERENCES patients(PatNr) ON DELETE CASCADE,
room INTEGER UINQUE,
hospitalnr INTEGER REFERENCES hospital(hospitalnr) ON DELETE CASCADE);
CREATE TABLE treats
(PersNr INTEGER REFERENCES doctors(PersNr) ON DELETE CASCADE,
PatNr INTEGER REFERENCES patients(PatNr) ON DELETE CASCADE,
Disease VARCHAR(30) NOT NULL References patients(Disease ) ON DELETE CASCADE,
PRIMARY KEY (PersNr, PatNr, Disease ));
CREATE TABLE operated
(PersNr INTEGER REFERENCES doctors(PersNr) ON DELETE CASCADE,
PatNr INTEGER REFERENCES patients(PatNr) ON DELETE CASCADE,
OperationID INTEGER REFERENCES operations(OperationID) ON DELETE CASCADE,
damage VARCHAR(30) NOT NULL REFERENCES patients(descriptiondamage) ON DELETE CASCADE,
PRIMARY KEY (PersNr, PatNr, OperationID, damage));
My error:
ERROR: there is no unique constraint matching given keys for referenced table "patienten"