cs122a-2021-fall: swooshDDL.sql

File swooshDDL.sql, 5.6 KB (added by Nada Lahjouji, 4 years ago)
Line 
1CREATE SCHEMA IF NOT EXISTS swoosh;
2
3--DROP TABLES in specific order to avoid foreign key issues
4--Relationship
5DROP TABLE IF EXISTS swoosh.ThumbsUp;
6DROP TABLE IF EXISTS swoosh.Teaches;
7DROP TABLE IF EXISTS swoosh.EnrolledIn;
8DROP TABLE IF EXISTS swoosh.WatchedSegment;
9DROP TABLE IF EXISTS swoosh.Watched;
10DROP TABLE IF EXISTS swoosh.Attended;
11--Entities
12DROP TABLE IF EXISTS swoosh.PostTopics;
13DROP TABLE IF EXISTS swoosh.Post ;
14DROP TABLE IF EXISTS swoosh.Recording ;
15DROP TABLE IF EXISTS swoosh.Meeting ;
16DROP TABLE IF EXISTS swoosh.Recurrence ;
17DROP TYPE IF EXISTS swoosh.week_days ;
18DROP TABLE IF EXISTS swoosh.Course ;
19DROP TABLE IF EXISTS swoosh.InstructorEducation ;
20DROP TABLE IF EXISTS swoosh.Instructor ;
21DROP TABLE IF EXISTS swoosh.Student ;
22DROP TABLE IF EXISTS swoosh.Users ;
23
24--------------
25-- Entities
26--------------
27CREATE TABLE swoosh.Users (
28 user_id text,
29 email text NOT NULL,
30 first_name text NOT NULL,
31 last_name text NOT NULL,
32 PRIMARY KEY(user_id)
33);
34
35CREATE TABLE swoosh.Student (
36 user_id text,
37 occupation text,
38 PRIMARY KEY(user_id),
39 --isA User
40 FOREIGN KEY(user_id) REFERENCES swoosh.Users(user_id) ON DELETE CASCADE
41);
42
43CREATE TABLE swoosh.Instructor (
44 user_id text,
45 title text,
46 PRIMARY KEY(user_id),
47 --isA User
48 FOREIGN KEY(user_id) REFERENCES swoosh.Users(user_id) ON DELETE CASCADE
49);
50
51--Multivalued attribute "Education" of Instructor
52CREATE TABLE swoosh.InstructorEducation (
53 instructor_id text,
54 education_id text,
55 degree text,
56 major text,
57 school text,
58 graduation_year integer,
59 PRIMARY KEY(instructor_id, education_id),
60 FOREIGN KEY(instructor_id) REFERENCES swoosh.Instructor(user_id) ON DELETE CASCADE
61);
62
63CREATE TABLE swoosh.Course (
64 course_id text,
65 course_name text NOT NULL,
66 description text,
67 PRIMARY KEY(course_id)
68);
69CREATE TYPE swoosh.week_days AS ENUM('Mon', 'Tue','Wed', 'Thu','Fri','Sat','Sun');
70
71CREATE TABLE swoosh.Recurrence (
72recurr_id text,
73repeat_on week_days NOT NULL,
74end_date DATE NOT NULL,
75PRIMARY KEY(recurr_id)
76);
77
78CREATE TABLE swoosh.Meeting (
79 meeting_id text,
80 meeting_name text NOT NULL,
81 passcode text,
82 start_at timestamp NOT NULL,
83 duration integer NOT NULL,
84 mute_participants boolean NOT NULL,
85 course_id text NOT NULL, -- Course--<Associated>--Meeting, total participation
86 instructor_id text NOT NULL, --Meeting--<HostedBy>--Instructor, total participation
87 recurr_id text, --Meeting--<RecursOn>--Recurrence
88 PRIMARY KEY(meeting_id),
89 FOREIGN KEY(course_id) REFERENCES swoosh.Course(course_id) ON DELETE CASCADE,
90 FOREIGN KEY(instructor_id) REFERENCES swoosh.Instructor(user_id) ON DELETE CASCADE,
91 FOREIGN KEY(recurr_id) REFERENCES swoosh.Recurrence(recurr_id) ON DELETE CASCADE
92);
93
94CREATE TABLE swoosh.Recording (
95 recording_id text,
96 start_time time,
97 end_time time,
98 meeting_id text NOT NULL, --Recording--<Recorded>--Meeting, total participation
99 PRIMARY KEY(recording_id),
100 FOREIGN KEY(meeting_id) REFERENCES swoosh.Meeting(meeting_id) ON DELETE CASCADE
101);
102
103
104
105CREATE TABLE swoosh.Post (
106 post_id text,
107 post_type text NOT NULL,
108 body text,
109 created_at timestamp NOT NULL,
110 user_id text NOT NULL, -- Post--<PostedBy>--User, total participation
111 meeting_id text NOT NULL, -- Post--<PostAbout>--Meeting, total participation
112 replied_to_post_id text, -- Post--<RepliedTo>--Post
113 PRIMARY KEY(post_id),
114 FOREIGN KEY(user_id) REFERENCES swoosh.Users(user_id) ON DELETE CASCADE,
115 FOREIGN KEY(meeting_id) REFERENCES swoosh.Meeting(meeting_id) ON DELETE CASCADE,
116 FOREIGN KEY(replied_to_post_id) REFERENCES swoosh.Post(post_id) ON DELETE CASCADE
117);
118
119--Multi-valued attribute "topics" for Post
120CREATE TABLE swoosh.PostTopics (
121 post_id text,
122 topic text,
123 PRIMARY KEY(post_id, topic),
124 FOREIGN KEY(post_id) REFERENCES swoosh.Post(post_id) ON DELETE CASCADE
125);
126
127
128----------------------
129-- Relationships
130----------------------
131
132-- Student <-- Attended --> Meeting
133CREATE TABLE swoosh.Attended(
134 user_id text,
135 meeting_id text,
136 PRIMARY KEY(user_id, meeting_id),
137 FOREIGN KEY(meeting_id) REFERENCES swoosh.Meeting(meeting_id) ON DELETE CASCADE,
138 FOREIGN KEY(user_id) REFERENCES swoosh.Student(user_id) ON DELETE CASCADE
139);
140
141
142-- Student <-- Watched --> Recording
143CREATE TABLE swoosh.Watched(
144 recording_id text,
145 user_id text,
146 PRIMARY KEY(recording_id, user_id),
147 FOREIGN KEY(user_id) REFERENCES swoosh.Student(user_id) ON DELETE CASCADE,
148 FOREIGN KEY(recording_id) REFERENCES swoosh.Recording(recording_id) ON DELETE CASCADE
149);
150
151-- Watched Segment Multi-valued attribute
152CREATE TABLE swoosh.WatchedSegment(
153 recording_id text,
154 user_id text,
155 segment_id text,
156 watched_from time NOT NULL,
157 watched_to time NOT NULL,
158 PRIMARY KEY(recording_id, user_id,segment_id),
159 FOREIGN KEY(user_id,recording_id) REFERENCES swoosh.Watched(user_id,recording_id) ON DELETE CASCADE
160);
161
162-- Student <-- EnrolledIn --> Course
163CREATE TABLE swoosh.EnrolledIn(
164 user_id text,
165 course_id text,
166 enroll_date date NOT NULL,
167 PRIMARY KEY(user_id, course_id),
168 FOREIGN KEY(user_id) REFERENCES swoosh.Student(user_id) ON DELETE CASCADE,
169 FOREIGN KEY(course_id) REFERENCES swoosh.Course(course_id) ON DELETE CASCADE
170);
171
172-- Instructor <-- Teaches --> Course
173CREATE TABLE swoosh.Teaches(
174 user_id text,
175 course_id text,
176 PRIMARY KEY(user_id, course_id),
177 FOREIGN KEY(user_id) REFERENCES swoosh.Instructor(user_id) ON DELETE CASCADE,
178 FOREIGN KEY(course_id) REFERENCES swoosh.Course(course_id) ON DELETE CASCADE
179);
180
181-- User <-- ThumbsUp --> Post
182CREATE TABLE swoosh.ThumbsUp(
183 user_id text,
184 post_id text,
185 PRIMARY KEY(user_id, post_id),
186 FOREIGN KEY(user_id) REFERENCES swoosh.Users(user_id) ON DELETE CASCADE,
187 FOREIGN KEY(post_id) REFERENCES swoosh.Post(post_id) ON DELETE CASCADE
188);