0% found this document useful (0 votes)
8 views3 pages

create schema

Uploaded by

xhcnkh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

create schema

Uploaded by

xhcnkh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

drop table if exists orgperson, relative, Class, Student,

Teacher, Department, TeachClass, Course, Grade;

drop type if exists person_t, grade_t;

create type person_t as(


name varchar(30),
bdate date,
gender char(1),
email varchar(30),
addr varchar(30),
phone varchar(13)
);

create table OrgPerson(


id numeric primary key,
pinfo person_t
);

create table Relative(


orgp_id numeric, -- org person id
pinfo person_t,
name varchar(30) generated always as ((pinfo).name) stored,
primary key (orgp_id, name),
foreign key (orgp_id) references OrgPerson(id)
on delete set null
on update cascade -- also update fk in pk automatically
deferrable initially deferred
);

create table Class(


id numeric primary key
);

create table Student(


id numeric primary key,

orgp_id numeric not null,


foreign key (orgp_id) references OrgPerson(id)
on delete set null
on update cascade
deferrable initially deferred,

class_id numeric not null,


foreign key (class_id) references Class(id)
on delete set null
on update cascade
deferrable initially deferred

);

create table Department(


id numeric primary key,
headteach_id numeric
);

create table Teacher(


id numeric primary key,

orgp_id numeric not null,


foreign key (orgp_id) references OrgPerson(id)
on delete set null
on update cascade
deferrable initially deferred,

dept_id numeric,
foreign key (dept_id) references Department(id)
on delete set null
on update cascade
deferrable initially deferred
);

create table Course(


id numeric primary key,
name varchar(10),

dept_id numeric,
foreign key (dept_id) references Department(id)
on delete set null
on update cascade
deferrable initially deferred
);

create table TeachClass(


class_id numeric, teacher_id numeric not null, course_id numeric,
primary key (class_id, course_id),
foreign key (class_id) references Class(id)
on delete set null
on update cascade
deferrable initially deferred,

foreign key (teacher_id) references Teacher(id)


on delete set null
on update cascade
deferrable initially deferred,

foreign key (course_id) references Course(id)


on delete set null
on update cascade
deferrable initially deferred

);

alter table Department


add constraint fk_head_dept
foreign key (headteach_id) references Teacher(id)
on delete set null
on update cascade
deferrable initially deferred;

create type grade_t as(


grade_1stord float,
grade_2ndord float,
grade_3rdord float
);
create table Grade(
student_id numeric, course_id numeric,
primary key (student_id, course_id),
grade grade_t,
foreign key (student_id) references Student(id)
on delete set null
on update cascade
deferrable initially deferred,

foreign key (course_id) references Course(id)


on delete set null
on update cascade
deferrable initially deferred

);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy