--------------------------------------------------------------------------
-- LAV Query processing example
-- Students marks database
-- Three extensional schemas - S1, S2 and S3
-- One global/public schema - SP
-- 
-- Data constraints
-- - each student studies for one degree
-- - tutors introduced from 1994 onwards
-- - cwmark only once per course for each student
-- - student may repeatedly sit exam for a course over any number of 
--    semesters
-- - each student studies only one degree
-- - each degree identified by dcode, has title dname, and has an 
--   associated qualification
--
-- Author: Sandeep Mittal (sandeep@dcs.bbk.ac.uk)
-- Last Modified : 24 August 2006
--------------------------------------------------------------------------

-- 
-- Public Schema - SP
-- degree (dcode, dname) pk dcode
-- student (sname, dcode) pk sname
-- course (ccode, title) pk ccode
-- sit (sname, ccode, sem, mark, cwmark?) pk (sname, ccode, sem)
-- 

--------------------------------------------------------------------------
-- datasource1 - S1
--------------------------------------------------------------------------

create table studies
( sname  varchar(50) not null
, ccode  integer not null check (ccode >= 500)
, sem    char(6) not null
, mark   integer not null
, title  varchar(50) not null
, dname  varchar(50) not null
, constraint studies_pk primary key (sname, ccode, sem)
);


--------------------------------------------------------------------------
-- datasource2 - S2
--------------------------------------------------------------------------

create table teach
( sname varchar(50) not null 
, ccode integer not null check (ccode < 500)
, sem   char(6) not null
, mark  integer not null
, tname varchar(50)
, constraint teach_pk primary key (sname, ccode, sem)
);


--------------------------------------------------------------------------
-- datasource3 - S3
--------------------------------------------------------------------------

create table degree
( dcode integer not null
, dname varchar(50) not null
, qual  varchar(50) not null
, constraint degree_pk primary key (dcode)
);


create table ug
( sname varchar(50) not null
, dcode integer not null
, constraint ug_pk primary key (sname)
, constraint ug_deg_fk foreign key (dcode) references degree (dcode)
);


create table reg
( sname varchar(50) not null
, ccode integer not null
, cwmark integer null
, tutor  varchar(50) not null
, constraint reg_pk primary key (sname, ccode)
, constraint reg_ug_fk foreign key (sname) references ug (sname)
);



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

-- Schema S1
insert into studies (sname,ccode,sem,mark,title,dname) values ('student1',500,'1993-1',90,'course500','BSc Computer Science');
insert into studies (sname,ccode,sem,mark,title,dname) values ('student1',600,'1994-1',80,'course600','BSc Computer Science');
insert into studies (sname,ccode,sem,mark,title,dname) values ('student1',700,'1995-2',70,'course700','BSc Computer Science');
insert into studies (sname,ccode,sem,mark,title,dname) values ('student1',800,'1996-1',90,'course800','BSc Computer Science');
insert into studies (sname,ccode,sem,mark,title,dname) values ('student1',900,'1997-1',90,'course900','BSc Computer Science');
insert into studies (sname,ccode,sem,mark,title,dname) values ('student2',500,'1994-2',90,'course500','BSc Mathematics');
insert into studies (sname,ccode,sem,mark,title,dname) values ('student2',600,'1995-1',80,'course600','BSc Mathematics');
insert into studies (sname,ccode,sem,mark,title,dname) values ('student2',700,'1996-2',70,'course700','BSc Mathematics');
insert into studies (sname,ccode,sem,mark,title,dname) values ('student2',800,'1997-1',90,'course800','BSc Mathematics');
insert into studies (sname,ccode,sem,mark,title,dname) values ('student2',900,'1998-2',90,'course900','BSc Mathematics');


-- Schema S2
insert into teach (sname,ccode,sem,mark,tname) values ('student1',100,'1993-2',90,'tutor1');
insert into teach (sname,ccode,sem,mark,tname) values ('student1',200,'1994-2',70,'tutor2');
insert into teach (sname,ccode,sem,mark,tname) values ('student1',300,'1995-1',80,'tutor3');
insert into teach (sname,ccode,sem,mark,tname) values ('student1',400,'1996-2',90,'tutor4');
insert into teach (sname,ccode,sem,mark,tname) values ('student2',100,'1994-2',90,'tutor1');
insert into teach (sname,ccode,sem,mark,tname) values ('student2',200,'1995-2',70,'tutor2');
insert into teach (sname,ccode,sem,mark,tname) values ('student2',300,'1996-1',80,'tutor3');
insert into teach (sname,ccode,sem,mark,tname) values ('student2',400,'1997-2',90,'tutor4');
insert into teach (sname,ccode,sem,mark,tname) values ('student2',450,'1997-2',90,'tutor4');

-- Schema S3
insert into degree (dcode, dname, qual) values (100,'BSc Computer Science', 'Bachelors');
insert into degree (dcode, dname, qual) values (200,'BSc Mathematics', 'Masters');

insert into ug (sname, dcode) values ('student1', 100);
insert into ug (sname, dcode) values ('student2', 200);

insert into reg (sname,ccode,cwmark,tutor) values ('student1',200,75,'tutor2');
insert into reg (sname,ccode,cwmark,tutor) values ('student1',250,65,'tutor2');
insert into reg (sname,ccode,cwmark,tutor) values ('student1',300,85,'tutor3');
insert into reg (sname,ccode,cwmark,tutor) values ('student1',600,70,'tutor6');
insert into reg (sname,ccode,cwmark,tutor) values ('student2',500,80,'tutor5');
insert into reg (sname,ccode,cwmark,tutor) values ('student1',210,null,'tutor2');
insert into reg (sname,ccode,cwmark,tutor) values ('student1',220,null,'tutor2');
