3
CREATE TABLE IF NOT EXISTS students (
2
This file should be executed to create the tables. It contains the constraints
3
for foreign keys and composite keys.
5
This file is going to be used to generate the JPA stuffs.
7
Changes to this file can come at any time.
10
CREATE TABLE IF NOT EXISTS student (
4
11
id SERIAL PRIMARY KEY,
5
user_name VARCHAR (32) NOT NULL UNIQUE ,
12
user_name VARCHAR (32) NOT NULL UNIQUE,
6
13
pwd VARCHAR (256) NOT NULL,
12
19
class VARCHAR (32) /* This could be a foreign key :-) */
15
23
address_country VARCHAR (32) NOT NULL,
16
24
address_city VARCHAR (32) NOT NULL,
17
25
address_street VARCHAR (32) NOT NULL,
32
CREATE TABLE IF NOT EXISTS staff_type (
33
id SERIAL PRIMARY KEY,
34
name VARCHAR (32) UNIQUE
38
/* Add staff types to database ************************************************/
39
INSERT INTO staff_type (id, name) VALUES (0, 'NONE'); /* We need to have the
41
so we don't get caught
44
INSERT INTO staff_type (name) VALUES ('Admin');
45
INSERT INTO staff_type (name) VALUES ('Teacher');
46
/******************************************************************************/
24
48
CREATE TABLE IF NOT EXISTS staff (
25
49
id SERIAL PRIMARY KEY,
26
user_name VARCHAR(32) NOT NULL UNIQUE,
27
pwd VARCHAR(256) NOT NULL,
50
user_name VARCHAR (32) NOT NULL UNIQUE,
51
pwd VARCHAR (256) NOT NULL,
35
59
employee_nr VARCHAR (32) NOT NULL UNIQUE,
37
FOREIGN KEY (staff_type) REFERENCES staff_type (id)
61
CONSTRAINT staff_type_kf FOREIGN KEY (staff_type)
62
REFERENCES staff_type (id)
40
CREATE TABLE IF NOT EXISTS staff_type (
67
CREATE TABLE IF NOT EXISTS course (
41
68
id SERIAL PRIMARY KEY,
70
name VARCHAR (30) UNIQUE,
74
CREATE TABLE IF NOT EXISTS lecture (
75
course BIGINT NOT NULL,
76
start_time TIME NOT NULL,
77
end_time TIME NOT NULL,
79
CONSTRAINT course_fk FOREIGN KEY (course)
80
REFERENCES course (id),
82
CONSTRAINT lecture_pk PRIMARY KEY (course, start_time, end_time)
85
CREATE TABLE IF NOT EXISTS teaches (
86
course BIGINT NOT NULL,
87
staff BIGINT NOT NULL,
89
CONSTRAINT course_fk FOREIGN KEY (course)
90
REFERENCES course (id),
92
CONSTRAINT staff_fk FOREIGN KEY (staff)
93
REFERENCES course (id),
95
CONSTRAINT teaches_pk PRIMARY KEY (course, staff)
98
CREATE TABLE IF NOT EXISTS student_attendance (
99
lecture_course BIGINT NOT NULL,
100
lecture_start_time TIME NOT NULL,
101
lecture_end_time TIME NOT NULL,
104
student BIGINT NOT NULL,
106
CONSTRAINT lecture_fk FOREIGN KEY (lecture_course,
109
REFERENCES lecture (course,
113
CONSTRAINT student_fk FOREIGN KEY (student)
114
REFERENCES student (id)