-
Notifications
You must be signed in to change notification settings - Fork 0
/
04 Many_to_one.sql
75 lines (62 loc) · 2.1 KB
/
04 Many_to_one.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
-- Entering Many-to-One Data - Automobiles
-- Create tables make and model
CREATE TABLE make (
id SERIAL,
name VARCHAR(128) UNIQUE,
PRIMARY KEY(id)
);
CREATE TABLE model (
id SERIAL,
name VARCHAR(128),
make_id INTEGER REFERENCES make(id) ON DELETE CASCADE,
PRIMARY KEY(id)
);
-- Insert data into the tables make and model
INSERT INTO (make, model) VALUES('Hyundai','Excel');
INSERT INTO (make, model) VALUES ('Hyundai', 'Genesis');
INSERT INTO (make, model) VALUES ('Volkswagen', 'Tiguan 4motion');
INSERT INTO (make, model) VALUES('Hyundai','Equus');
INSERT INTO (make, model) VALUES('Hyundai','Excel');
INSERT INTO (make, model) VALUES ('Hyundai', 'Genesis');
INSERT INTO (make, model) VALUES ('Volkswagen', 'Tiguan 4motion');
-- Building a many-to-many roster
-- Create the tables student, course and the junction table roster
CREATE TABLE student (
id SERIAL,
name VARCHAR(128) UNIQUE,
PRIMARY KEY(id)
);
DROP TABLE course CASCADE;
CREATE TABLE course (
id SERIAL,
title VARCHAR(128) UNIQUE,
PRIMARY KEY(id)
);
DROP TABLE roster CASCADE;
CREATE TABLE roster (
id SERIAL,
student_id INTEGER REFERENCES student(id) ON DELETE CASCADE,
course_id INTEGER REFERENCES course(id) ON DELETE CASCADE,
role INTEGER,
UNIQUE(student_id, course_id),
PRIMARY KEY (id)
);
-- list all tables in the current schema
\dt
List of relations
Schema | Name | Type | Owner
--------+-------------+-------+-----------------
public | ages | table | pg4e_73285570e1
public | album | table | pg4e_73285570e1
public | automagic | table | pg4e_73285570e1
public | course | table | pg4e_73285570e1
public | make | table | pg4e_73285570e1
public | model | table | pg4e_73285570e1
public | pg4e_debug | table | pg4e_73285570e1
public | pg4e_meta | table | pg4e_73285570e1
public | pg4e_result | table | pg4e_73285570e1
public | roster | table | pg4e_73285570e1
public | student | table | pg4e_73285570e1
public | track | table | pg4e_73285570e1
public | track_raw | table | pg4e_73285570e1
(13 rows)