-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_tables.sql
136 lines (116 loc) · 3.14 KB
/
create_tables.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
CREATE TABLE IF NOT EXISTS test (
id int NOT NULL,
course_professor_id int NOT NULL,
name text NOT NULL,
PRIMARY KEY(id)/*,
CONSTRAINT test_in_course UNIQUE (course_professor_id, name)*/
);
CREATE TABLE IF NOT EXISTS person (
id int NOT NULL,
name text NOT NULL,
graduation date,
gpa decimal(3,2),
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS major (
id int NOT NULL,
department_id int NOT NULL,
name text NOT NULL,
PRIMARY KEY(id)/*,
CONSTRAINT department_name UNIQUE (department_id, name)*/
);
CREATE TABLE IF NOT EXISTS department (
id int NOT NULL,
name text NOT NULL,
PRIMARY KEY(id)/*,
CONSTRAINT department_name UNIQUE (name)*/
);
CREATE TABLE IF NOT EXISTS course (
id int NOT NULL,
department_id int NOT NULL,
course_number int NOT NULL,
name text NOT NULL,
PRIMARY KEY(id)/*,
CONSTRAINT department_course_number UNIQUE (department_id, course_number)*/
);
CREATE TABLE IF NOT EXISTS professor (
id int NOT NULL,
name text NOT NULL,
PRIMARY KEY (id)/*,
CONSTRAINT professor_name UNIQUE (name)*/
);
CREATE TABLE IF NOT EXISTS person_major (
id int NOT NULL,
person_id int NOT NULL,
major_id int NOT NULL,
PRIMARY KEY (id)/*,
CONSTRAINT person_major_unique UNIQUE (person_id, major_id)*/
);
CREATE TABLE IF NOT EXISTS course_professor (
id int NOT NULL,
course_id int NOT NULL,
professor_id int NOT NULL,
quarter date NOT NULL,
PRIMARY KEY (id)/*,
CONSTRAINT course_professor_quarter UNIQUE (course_id, professor_id, quarter)*/
);
CREATE TABLE IF NOT EXISTS course_professor_person (
id int NOT NULL,
course_professor_id int NOT NULL,
person_id int NOT NULL,
gpa decimal(3,2),
PRIMARY KEY (id)/*,
CONSTRAINT course_professor_person_unique UNIQUE (course_professor_id, person_id)*/
);
CREATE TABLE IF NOT EXISTS test_person (
id int NOT NULL,
test_id int NOT NULL,
person_id int NOT NULL,
grade decimal(4,2),
PRIMARY KEY (id)/*,
CONSTRAINT test_person_unique UNIQUE (test_id, person_id)*/
);
ALTER TABLE test
ADD CONSTRAINT test_course_professor
FOREIGN KEY (course_professor_id)
REFERENCES course_professor (id);
ALTER TABLE major
ADD CONSTRAINT major_department
FOREIGN KEY (department_id)
REFERENCES department (id);
ALTER TABLE course
ADD CONSTRAINT course_department
FOREIGN KEY (department_id)
REFERENCES department (id);
ALTER TABLE person_major
ADD CONSTRAINT person_major_person_id
FOREIGN KEY (person_id)
REFERENCES person (id);
ALTER TABLE person_major
ADD CONSTRAINT person_major_major_id
FOREIGN KEY (major_id)
REFERENCES major (id);
ALTER TABLE course_professor
ADD CONSTRAINT course_professor_course_id
FOREIGN KEY (course_id)
REFERENCES course (id);
ALTER TABLE course_professor
ADD CONSTRAINT course_professor_professor_id
FOREIGN KEY (professor_id)
REFERENCES professor (id);
ALTER TABLE course_professor_person
ADD CONSTRAINT course_professor_course_professor_id
FOREIGN KEY (course_professor_id)
REFERENCES course_professor (id);
ALTER TABLE course_professor_person
ADD CONSTRAINT course_professor_person_id
FOREIGN KEY (person_id)
REFERENCES person (id);
ALTER TABLE test_person
ADD CONSTRAINT test_person_test_id
FOREIGN KEY (test_id)
REFERENCES test (id);
ALTER TABLE test_person
ADD CONSTRAINT test_person_person_id
FOREIGN KEY (person_id)
REFERENCES person (id);