sql - Dual use of composite foreign keys -


i'm modelling hierarchy of 3 entities therapy planning software. therapy understood number of medications given patient on number of days. want able cancel therapy on given day, in write-only-once fashion (for quality control certification purposes).

here's concrete question: ok reuse part of composite foreign key foreign key? in case composite key points medication table day table, points therapy. therapy id included in composite foreign key in medication table, use foreign key, making querying more easy?

the table definitions should (modulo hickups in raw sql skills, employ kind of orm):

create table therapy (     "id" integer not null,     "start" date not null,     primary key (id) );  create table day (     "therapy_id" integer not null,     "day" integer not null,     "revision" integer not null,     "comment" text;     "cancelled" boolean not null;     primary key (therapy_id, day, revision), foreign key(therapy_id) references therapy (id) );  create table medication (     "id" integer not null,     "therapy_id" integer not null,     "day" integer not null,     "revision" integer not null,     "substance" varchar(100) not null,     "dosage" varchar(50) not null,     primary key (id), foreign key(therapy_id, day, revision) references day (therapy_id, day, revision), -- wondering if ok     foreign key(therapy_id) references therapy (id) ); 

please note relation day medication one-to-many, e.g. on day 3 want plaster patient deadly mix of ibuprofen, paracetamol, aspirin, ground coffee beans , shot of jaegermeister.

here's started with.

create table medication (     "id" integer not null,               -- don't need this.     "therapy_id" integer not null,     "day" integer not null,     "revision" integer not null,     "substance" varchar(100),     "dosage" varchar(50),     primary key (id),                    -- drop this.     foreign key(therapy_id, day, revision)          references day (therapy_id, day, revision),  -- ok, should pk.     foreign key(therapy_id) references therapy (id) -- don't need this. ); 

the column "id" serves no useful purpose, , has instead convinced you don't need unique constraint on real key: {therapy_id, day, revision}. replace useless primary key constraint on "id" primary key constraint on {therapy_id, day, revision}. drop last foreign key constraint. know "therapy_id" reference existing row, because foreign key constraint in table "day" requires it.

that leaves this.

create table medication (     "therapy_id" integer not null,     "day" integer not null,     "revision" integer not null,     "substance" varchar(100),     "dosage" varchar(50),     primary key (therapy_id, day, revision),     foreign key(therapy_id, day, revision)          references day (therapy_id, day, revision) ); 

it troubles me "substance" , "dosage" nullable. think little.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -