--------------------------------------------------------------------------
-- AutoMed Update and Query processing example
-- Hospital/Patient Database
-- 
-- Two extensional schemas - HS1 and HS2
-- One public schema - PUBH1
-- HS1 maintained by a hospital
-- HS2 maintained by Dr. Davies
-- 
-- Author: Sandeep Mittal (sandeep@dcs.bbk.ac.uk)
-- Last Modified : 17 November 2006
--------------------------------------------------------------------------

--------------------------------------------------------------------------
-- Public Schema - PUBH1
--------------------------------------------------------------------------
-- The peer schemas HS1 and HS2 are integrated to provide a public schema 
-- called PUBH1. Any patient record in HS1 or HS2 might be sent to another 
-- peer conforming to PUBH1. 
--------------------------------------------------------------------------
-- allPatients(ni, name, sex, gp) pk ni
-- allTreatments(ni, date, description) pk (ni, date)

--------------------------------------------------------------------------
-- Peer Schema HS1
--------------------------------------------------------------------------
-- Peer schema HS1 is the schema for a hospital patient database. Each 
-- patient is assigned a unique hospital identifier (hid) and a record is 
-- kept of their national insurance number, name, sex, age, and the name 
-- of their gp (doctor). Patients receive treatment, identified by a 
-- treatment id (tid, and a record is kept of the patient (via their hid), 
-- date, description and the consultant whole authorised the treatment. 
--
-- hpatient(hid, ni, name, sex, age, gp) pk hid
-- treatment(tid, hid, date, desc, consultant) pk tid fk hid->hpatient.hid
--------------------------------------------------------------------------

create table hpatient
( hid   integer not null
, ni    varchar(10) not null
, name  varchar(50) not null
, sex   char(1)     not null
, age   integer     
, gp    varchar(50) 
, constraint hpatient_pk primary key (hid)
);

--insert into hpatient(hid, ni, name, sex, age, gp) values 
--                    (hid, ni, name, sex, age, gp);
insert into hpatient(hid, ni, name, sex, age, gp) values 
       (10000,'ZS341234P','Joe Bloggs','M',56,'Dr. Davies');
insert into hpatient(hid, ni, name, sex, age, gp) values 
       (10050,'ZS341235P','John Doe','M',45,'Dr. Jones');
insert into hpatient(hid, ni, name, sex, age, gp) values 
       (10100,'ZS341236P','Jane Eyre','F',37,'Dr. Smith');

create table treatment
( tid         integer     not null
, hid         integer     not null
, date        varchar(10) not null   -- DD-MM-YYYY
, description varchar(50)
, consultant  varchar(50) 
, constraint  treatment_pk primary key (tid)
, constraint  treatment_hpatient_fk foreign key (hid) references hpatient(hid)
);

--insert into treatment (tid, hid, date, description, consultant) values 
--                      (tid, hid, date, description, consultant)
insert into treatment (tid, hid, date, description, consultant) values 
        (100, 10000, '01-05-1999', 'A treatment for Mr Bloggs', 'Dr. Davies');
insert into treatment (tid, hid, date, description, consultant) values 
        (101, 10000, '01-08-1999', 'Another treatment for Mr Bloggs', 'Dr. Boyle');
insert into treatment (tid, hid, date, description, consultant) values 
        (103, 10050, '01-05-1997', 'A treatment for Mr Doe', 'Dr. Jones');


--------------------------------------------------------------------------
-- Peer Schema HS2
--------------------------------------------------------------------------
-- Peer schema HS2 is the schema for the database maintained by General 
-- Practitioner Dr. Davies. He identifies his patients by their ni number 
-- and records their first name (fName), last name (lName), sex and 
-- address. His database also records in the event table all treatments 
-- and consultations for each of his patients as plain text description 
-- within the field desc. 
--
-- patient(ni, fName, lName, sex, address) pk ni
-- event(ni, date, desc) pk (ni, date)
--------------------------------------------------------------------------

create table patient
( ni       varchar(10)  not null
, fName    varchar(24)  
, lName    varchar(25)  not null
, sex      char(1)      not null
, address  varchar(100) 
, constraint patient_pk primary key (ni)
);

--insert into patient (ni, fName, lName, sex, address) values 
--                    (ni, fName, lName, sex, address);

insert into patient (ni, fName, lName, sex, address) values 
           ('PS201231A', 'Lisa', 'Lee', 'F', 'Address for Lisa');
insert into patient (ni, fName, lName, sex, address) values 
           ('PS201232A', 'Bill', 'Cooper', 'M', 'Address for Bill Cooper');

create table event
( ni           varchar(10)  not null
, date         varchar(10) not null   -- DD-MM-YYYY
, description  varchar(50)
, constraint event_pk primary key (ni, date)
);

--insert into event(ni, date, description) values 
--                 (ni, date, description);

insert into event(ni, date, description) values 
            ('PS201231A', '01-10-2001', 'A treatment for Lisa at Dr. Davies');
insert into event(ni, date, description) values 
            ('PS201231A', '01-07-2002', 'Another treatment for Lisa at Dr. Davies');
insert into event(ni, date, description) values 
            ('PS201232A', '01-07-2002', 'A treatment for Bill at Dr. Davies');


--------------------------------------------------------------------------
