-2

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"


11
  • 1
    Don't duplicate the entries. Dec 24, 2016 at 18:43
  • what u mean ? How i duplicate the entries in this example o.O?
    – user7322832
    Dec 24, 2016 at 18:45
  • can you pleas stop duplicating your questions here. I gonna flag this and hope someone closes it
    – Ilja KO
    Dec 24, 2016 at 18:46
  • The problem with teh current table is that, it needs a composite primary key. Dec 24, 2016 at 18:46
  • i tried : Patiens( PatNr INTEGER 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,);
    – user7322832
    Dec 24, 2016 at 18:49

1 Answer 1

1

Your table creation script is utterly messed up and full of errors. It should be like below rather. Here is a demo fiddle proving that it works: http://sqlfiddle.com/#!9/08e1e0. Comapre it with your posted script and the mistakes or errors would become obvious. Many a places you have tried referencing a column which is not a primary key and that's the biggest mistake since you can't refer a non-key column per normalization rules. I have removed all those FOREIGN KEY construction. Along with that there were many spelling mistake like UINQUE instead of UNIQUE. Also I see you are defining CHECK constraint. I have removed them as well since MySQL doesn't support it and this even if you have it; MySQL engine simply ignores it and doesn't enforce it at all.

CREATE TABLE  Doctors
       (PersNr              INTEGER PRIMARY KEY,
        Name                VARCHAR(30) NOT NULL,
        Rang                CHAR(2));

CREATE TABLE Insurance
       (InsuranceNr      INTEGER PRIMARY KEY,
        Name                VARCHAR(30) NOT NULL,
        TelefonNr       VARCHAR(30) NOT NULL,
        Headquarter    VARCHAR(30) NOT NULL);

CREATE TABLE Patients
        (PatNr              INTEGER PRIMARY KEY,
        Name                VARCHAR(30) NOT NULL,
        InsuranceNr        INTEGER ,
     Disease      VARCHAR(30) NOT NULL, 
     Damage        VARCHAR(2), 
DescriptionDamage VARCHAR(30) Not Null,
FOREIGN KEY (InsuranceNr) REFERENCES Insurance(InsuranceNr) on DELETE CASCADE);


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 ,
        PatientNr       INTEGER ,
        room            INTEGER UNIQUE,
        hospitalnr       INTEGER ,
        FOREIGN KEY  (hospitalnr) REFERENCES hospital(hospitalnr) ON DELETE CASCADE,
        FOREIGN KEY  (DoctorNr) REFERENCES doctors(PersNr) ON DELETE CASCADE,
        FOREIGN KEY  (PatientNr) REFERENCES patients(PatNr) ON DELETE CASCADE
       );

  CREATE TABLE treats
       (PersNr         INTEGER ,
        PatNr          INTEGER ,
      Disease          VARCHAR(30) NOT NULL ,
        PRIMARY KEY   (PersNr, PatNr, Disease ),
       FOREIGN KEY  (PersNr) REFERENCES doctors(PersNr) ON DELETE CASCADE,
       FOREIGN KEY  (PatNr) REFERENCES patients(PatNr) ON DELETE CASCADE
     );     

  CREATE TABLE operated
     (PersNr        INTEGER ,
      PatNr     INTEGER ,
      OperationID   INTEGER ,
      damage  VARCHAR(30) NOT NULL ,
      PRIMARY KEY   (PersNr, PatNr, OperationID, damage),
     FOREIGN KEY  (PersNr) REFERENCES doctors(PersNr) ON DELETE CASCADE,
     FOREIGN KEY  (PatNr) REFERENCES patients(PatNr) ON DELETE CASCADE,
     FOREIGN KEY  (OperationID) REFERENCES operations(OperationID) ON DELETE CASCADE
     );   
8
  • 2
    Thank you for the right answer and for fighting through the ambigious "question"
    – Ilja KO
    Dec 24, 2016 at 19:28
  • @Rahul thanks thanks thanks thanks..... tanks so much and i am sorry for all my mistakes but thanks thanks thanks..... :))))))) u helped me realy
    – user7322832
    Dec 24, 2016 at 19:30
  • @IljaKO, LOL ... fighting through the ambigious "question"... yeah kind of
    – Rahul
    Dec 24, 2016 at 19:37
  • @MustafaOtuzalti, Welcome but would have I been your place, I would referred the MySQL document rather. Do always refer MySQL document when in doubt.
    – Rahul
    Dec 24, 2016 at 19:38
  • @Rahul i see that your correction is maybe not completely. All Vallues ´which i inserted to table patients will not taken by table treats. so if i want insert a value into my table treats, they should take the information (disease) from the patient table. So i want that in table treats: (disease) references patients(disease) on delete cascade. u know how i can do that??
    – user7322832
    Dec 24, 2016 at 21:30

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.