-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathschema.sql
40 lines (35 loc) · 1.31 KB
/
schema.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
-- Students table
CREATE TABLE IF NOT EXISTS STUDENT (
nip INTEGER PRIMARY KEY,
dni TEXT NOT NULL,
name TEXT NOT NULL
);
-- Subject table
CREATE TABLE IF NOT EXISTS SUBJECT (
code INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
-- Relationships between students and subjects
CREATE TABLE IF NOT EXISTS ENROLLMENT (
nip INTEGER NOT NULL,
code INTEGER NOT NULL,
FOREIGN KEY(nip) REFERENCES ALUMNOS(nip),
FOREIGN KEY(code) REFERENCES SUBJECT(code)
);
CREATE TABLE IF NOT EXISTS BACHELOR (
code INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS REGISTER (
nip INTEGER NOT NULL,
code INTEGER NOT NULL,
FOREIGN KEY(nip) REFERENCES ALUMNOS(nip),
FOREIGN KEY(code) REFERENCES BACHELOR(code)
);
CREATE VIEW IF NOT EXISTS ALL_INFO AS
SELECT STUDENT.nip AS nip, STUDENT.dni AS dni, STUDENT.name AS name, SUBJECT.name AS subject, BACHELOR.name AS bachelor
FROM STUDENT
LEFT JOIN ENROLLMENT ON STUDENT.nip = ENROLLMENT.nip
LEFT JOIN SUBJECT ON ENROLLMENT.code = SUBJECT.code
LEFT JOIN REGISTER ON STUDENT.nip = REGISTER.nip
LEFT JOIN BACHELOR ON REGISTER.code = BACHELOR.code;