-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMovieDB_TableMerge.sql
executable file
·194 lines (153 loc) · 4.44 KB
/
MovieDB_TableMerge.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
CREATE DATABASE CSI2132PJ;
CREATE SCHEMA MOVEDB;
SET SEARCH_PATH='movedb';
DROP TABLE USERS;
/*Users*****************************************************/
CREATE SEQUENCE your_seq;
CREATE TABLE IF NOT EXISTS "category" (
"id" integer PRIMARY KEY default nextval('your_seq'),
"name" varchar(30) DEFAULT NULL
);
CREATE TABLE USERS(
user_id SERIAL PRIMARY KEY,
password CHAR(20) NOT NULL,
last_name CHAR(20) NOT NULL,
first_name CHAR(20) NOT NULL,
Email CHAR(40) NOT NULL UNIQUE,
City CHAR(40),
Province CHAR(40),
Country CHAR(40)
);
SELECT * FROM USERS;
INSERT INTO USERS("password","last_name","first_name","email","city","province","country")
VALUES('214','Liu','Ray','[email protected]','nanjing','jiangsu','China');
INSERT INTO USERS("password","last_name","first_name","email","city","province","country")
VALUES('321','Liu','fei','[email protected]','nanjing','jiangsu','China');
/*Profile************************************/
CREATE TABLE PROFILE(
user_id INT NOT NULL,
CONSTRAINT profile_fkey FOREIGN KEY (user_id)
REFERENCES USERS (user_id)
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT profile_pkey PRIMARY KEY (user_id),
age_range char(20) CHECK(age_range IN('Youth','Adult','Senior')),
year_born char(4),
gender char(20) CHECK(gender IN('Male','Female','Unknown')),
occupation char(40),
device_used char(40));
SELECT * FROM PROFILE;
INSERT INTO profile
VALUES(1,'Adult','1994','Male','Software Engineer','Mac book pro');
INSERT INTO profile
VALUES(2,'Adult','1995','Male','Software Engineer','Mac book pro');
CREATE FUNCTION check_password()
RETURNS trigger AS
$BODY$
BEGIN
IF
NEW.password LIKE '%123%' THEN
RAISE EXCEPTION 'Sorry the password you entered is too simple.';
END IF;
RETURN NEW;
END
$BODY$ LANGUAGE plpgsql;
CREATE TRIGGER check_password
BEFORE INSERT OR UPDATE
ON USERS
FOR EACH ROW
EXECUTE PROCEDURE check_password();
/*Topics******************************/
CREATE TABLE TOPICS(
topic_id serial PRIMARY key,
description char(40) NOT NULL
);
/*MOVIES*******************************/
CREATE TABLE MOVIE
(
movie_id serial PRIMARY key,
name char(50) NOT NULL UNIQUE,
date_released DATE NOT NULL,
Language VARCHAR(20),
Subtitles CHAR(1) NOT NULL,
Country VARCHAR(20)
Check (Subtitles = 'True' or Subtitles = 'False')
);
/*WATCHED*****************************/
CREATE TABLE WATCHES(
user_id INT NOT NULL,
movie_id INT NOT NULL,
rating FLOAT check (rating>0 and rating<10),
date DATE NOT NULL,
Primary Key (user_id,movie_id)
);
/*Kyle's Table***********************************************/
/*Director*/
CREATE TABLE DIRECTOR
(director_id serial NOT NULL ,
lastname character varying(20),
firstname character varying(20),
country character varying(20),
CONSTRAINT Directors_pkey PRIMARY KEY (director_id)
);
/*Directs*/
CREATE TABLE DIRECTS
(director_id integer NOT NULL,
movie_id integer NOT NULL,
CONSTRAINT Direct_pkey PRIMARY KEY (director_id,movie_id),
CONSTRAINT Directs_Director_fkey FOREIGN KEY (director_id)
REFERENCES Director (director_id)
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT Directs_movies_fkey FOREIGN KEY (movie_id)
REFERENCES MOVIE (movie_id)
ON UPDATE CASCADE ON DELETE CASCADE
);
/*Studio*/
CREATE TABLE STUDIO
(studio_id serial NOT NULL,
name character varying(20),
country character varying(20),
CONSTRAINT Studio_pkey PRIMARY KEY (studio_id)
);
/*Sponsors*/
CREATE TABLE Sponsors
(studio_id integer NOT NULL,
movie_id integer NOT NULL,
CONSTRAINT Directs_pkey PRIMARY KEY (studio_id,movie_id),
CONSTRAINT Sponsors_StudioID_fkey FOREIGN KEY (studio_id)
REFERENCES STUDIO (studio_id)
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT Sponsors_movies_fkey FOREIGN KEY (movie_id)
REFERENCES MOVIE (movie_id)
ON UPDATE CASCADE ON DELETE CASCADE
);
/*Alice's tables*/
/*Movie Topics*/
Create Table MovieTopics (
topic_id INT NOT NULL,
movie_id INT NOT NULL,
Primary Key (topic_id, movie_id)
);
/*Actor*/
Create Table Actor (
actor_id INT NOT NULL,
last_name VARCHAR(20),
first_name VARCHAR(20),
date_of_birth DATE,
Primary key (actor_id)
);
/*Role*/
Create Table Role(
role_id serial NOT NULL,
name VARCHAR(20),
movie_id INT NOT NULL,
Primary Key(role_id)
Foreign Key (movie_id) References movie
);
/*ActorPlays*/
Create Table ActorPlays(
movie_id INT NOT NULL,
actor_id INT NOT NULL,
role_id INT NOT NULL,
Primary Key (movie_id, actor_id),
Foreign Key (role_id) References Role
);