-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartups.sql
More file actions
317 lines (281 loc) · 10.4 KB
/
Copy pathstartups.sql
File metadata and controls
317 lines (281 loc) · 10.4 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
----------------------------------------------------------------------
-- DROP statements: We begin with these to ensure that everytime we
-- start with a clean database state.
--
-- NOTE: If you create additional triggers, remember to drop therm
-- there (and BEFORE dropping the tables they are based on).
DROP TRIGGER TG_StealthStartup ON StealthStartup;
DROP TRIGGER TG_PrivateStartup ON PrivateStartup;
--DROP TRIGGER TG_StealthFund ON Fund;
--DROP TRIGGER TG_PrivateStartupSector ON PrivateStartup;
DROP VIEW StealthStartup;
DROP VIEW PrivateStartup;
DROP TABLE TargetOf;
DROP TABLE Fund;
DROP TABLE PrivateCompany;
DROP TABLE StealthCompany;
DROP TABLE Startup;
DROP TABLE Sector;
DROP TABLE Industry;
DROP TABLE VCFund;
----------------------------------------------------------------------
-- Tables:
-- You will need to modify this section to add constraints (such as
-- primary, unique, and foreign keys).
CREATE TABLE VCFund(
vcid INTEGER NOT NULL UNIQUE,
name VARCHAR(50) NOT NULL,
number INTEGER NOT NULL,
size INTEGER NOT NULL,
closing_date DATE NOT NULL,
PRIMARY KEY(vcid, name)
);
CREATE TABLE Industry(
name VARCHAR(50) NOT NULL PRIMARY KEY,
market_size INTEGER NOT NULL
);
CREATE TABLE Sector(
industry_name VARCHAR(50) NOT NULL,
sector_name VARCHAR(50) NOT NULL,
project_growth INTEGER NOT NULL,
PRIMARY KEY(industry_name, sector_name),
FOREIGN KEY (industry_name) REFERENCES Industry(name)
);
CREATE TABLE Startup(
sid INTEGER NOT NULL PRIMARY KEY,
industry_name VARCHAR(50) NOT NULL,
startup_name VARCHAR(50) NOT NULL,
address VARCHAR(200) NOT NULL
);
CREATE TABLE StealthCompany(
sid INTEGER NOT NULL PRIMARY KEY,
buzz_factor INTEGER NOT NULL
);
CREATE TABLE PrivateCompany(
sid INTEGER NOT NULL PRIMARY KEY,
CEO VARCHAR(50) NOT NULL,
website VARCHAR(50) NOT NULL,
sector_name VARCHAR(50) NOT NULL
);
CREATE TABLE Fund(
vcid INTEGER NOT NULL,
sid INTEGER NOT NULL,
PRIMARY KEY (vcid, sid)
);
CREATE TABLE TargetOf(
target_sid INTEGER NOT NULL,
sid INTEGER NOT NULL,
PRIMARY KEY (target_sid, sid)
);
-- StealthStartup view and associated trigger/function:
--
-- You do not need to edit this section, but do read it to get an idea
-- about what to do for PrivateStartup view.
--
-- StealthStartup view "wraps" Startup and StealthCompany, so that
-- users can access complete information about stealth startups
-- through this view. The trigger below allows users to modify this
-- view. To make constraints easier to enforce, you may assume that
-- users CANNOT modify Startup and StealthCompany directly (which can
-- be ensured by GRANT statements---a topic that we don't cover in
-- class but you can read more about by yourself).
CREATE VIEW
StealthStartup(sid, industry_name, startup_name, address,
buzz_factor) AS
SELECT Startup.sid, industry_name, startup_name, address, buzz_factor
FROM Startup, StealthCompany
WHERE Startup.sid = StealthCompany.sid;
CREATE OR REPLACE FUNCTION TF_StealthStartup() RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'INSERT') THEN
INSERT INTO Startup
VALUES(NEW.sid, NEW.industry_name, NEW.startup_name, NEW.address);
INSERT INTO StealthCompany
VALUES(NEW.sid, NEW.buzz_factor);
ELSEIF (TG_OP = 'UPDATE') THEN
IF (NEW.sid <> OLD.sid) THEN
RAISE EXCEPTION 'Cannot update Startup(sid)';
ELSE
UPDATE Startup
SET industry_name = NEW.industry_name,
startup_name = NEW.startup_name,
address = NEW.address
WHERE sid = NEW.sid;
UPDATE StealthCompany
SET buzz_factor = NEW.buzz_factor
WHERE sid = NEW.sid;
END IF;
ELSEIF (TG_OP = 'DELETE') THEN
DELETE FROM StealthCompany WHERE sid = OLD.sid;
DELETE FROM Startup WHERE sid = OLD.sid;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER TG_StealthStartup
INSTEAD OF INSERT OR UPDATE OR DELETE
ON StealthStartup
FOR EACH ROW
EXECUTE PROCEDURE TF_StealthStartup();
-- PrivateStartup view and associated trigger/function:
--
-- You need to complete this section.
--
-- PrivateStartup view "wraps" Startup and PrivateCompany, so that
-- users can access complete information about private startups
-- through this view. The trigger below allows users to modify this
-- view. To make constraints easier to enforce, you may assume that
-- users CANNOT modify Startup and PrivateCompany directly (which can
-- be ensured by GRANT statements---a topic that we don't cover in
-- class but you can read more about by yourself).
--
CREATE VIEW
PrivateStartup(sid, industry_name, startup_name, address,
CEO, website, sector_name) AS
SELECT Startup.sid, industry_name, startup_name, address, CEO, website, sector_name
FROM Startup, PrivateCompany
WHERE Startup.sid = PrivateCompany.sid;
CREATE OR REPLACE FUNCTION TF_PrivateStartup() RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'INSERT') THEN
INSERT INTO Startup
VALUES(NEW.sid, NEW.industry_name, NEW.startup_name, NEW.address);
INSERT INTO PrivateCompany
VALUES(NEW.sid, NEW.CEO, NEW.website, NEW.sector_name);
ELSEIF (TG_OP = 'UPDATE') THEN
IF (NEW.sid <> OLD.sid) THEN
RAISE EXCEPTION 'Cannot update Startup(sid)';
ELSE
UPDATE Startup
SET industry_name = NEW.industry_name,
startup_name = NEW.startup_name,
address = NEW.address
WHERE sid = NEW.sid;
UPDATE PrivateCompany
SET CEO = NEW.CEO,
website = NEW.website,
sector_name = NEW.sector_name
WHERE sid = NEW.sid;
END IF;
ELSEIF (TG_OP = 'DELETE') THEN
DELETE FROM PrivateCompany WHERE sid = OLD.sid;
DELETE FROM Startup WHERE sid = OLD.sid;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER TG_PrivateStartup
INSTEAD OF INSERT OR UPDATE OR DELETE
ON PrivateStartup
FOR EACH ROW
EXECUTE PROCEDURE TF_PrivateStartup();
-- Other triggers/functions, if any, should go here:
-- Maintain 1 financier for each stealth startup
CREATE OR REPLACE FUNCTION TF_StealthFunder() RETURNS TRIGGER AS $$
BEGIN
IF (NEW.sid IN (SELECT sid FROM StealthStartup)
AND 1 < (SELECT COUNT(*) FROM Fund WHERE NEW.sid = Fund.sid)) THEN
RAISE EXCEPTION 'Stealth Startups may only have one financier';
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER TG_StealthFunder
AFTER INSERT OR UPDATE ON FUND
FOR EACH ROW
EXECUTE PROCEDURE TF_StealthFunder();
-- private startups must have valid sectors
CREATE OR REPLACE FUNCTION TF_PrivateStartupSector() RETURNS TRIGGER AS $$
BEGIN
IF (NEW.sector_name NOT IN (SELECT sector_name FROM Sector))
THEN
RAISE EXCEPTION 'Private startups must be in valid sectors';
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER TG_PrivateStartupSector
AFTER INSERT OR UPDATE ON PrivateStartup
FOR EACH ROW
EXECUTE PROCEDURE TF_PrivateStartupSector();
-- VCFunds can only fund one private startup per industry sectory
CREATE OR REPLACE FUNCTION TF_FundStartupSector() RETURNS TRIGGER AS $$
BEGIN
IF ( EXISTS (SELECT sid, industry_name, sector_name, COUNT(*) FROM PrivateStartup GROUP BY sid, industry_name, sector_name HAVING COUNT(*)>1 ))
THEN
RAISE EXCEPTION 'VC Funds can not fund more than one private company per sector';
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER TG_FundStartupSector
AFTER INSERT OR UPDATE ON Fund
FOR EACH ROW
EXECUTE PROCEDURE TF_FundStartupSector();
-- Only stealth companies can be targets of private companies
CREATE OR REPLACE FUNCTION TF_PrivateStealthTarget() RETURNS TRIGGER AS $$
BEGIN
IF (NEW.target_sid IN (SELECT sid FROM StealthStartup) AND NEW.sid IN (SELECT sid FROM PrivateStartup) )
THEN
ELSE
RAISE EXCEPTION 'Private companies can only target Stealth companies';
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER TG_PrivateStealthTarget
AFTER INSERT OR UPDATE ON TargetOf
FOR EACH ROW
EXECUTE PROCEDURE TF_PrivateStealthTarget();
----------------------------------------------------------------------
-- Data modification statements:
-- The following statements should be accepted:
INSERT INTO VCFund VALUES(101, 'Kleiner Perkins', 7, 76923, '2013-12-31');
INSERT INTO VCFund VALUES(102, 'Sequoia Capital', 3, 52631, '2014-08-31');
INSERT INTO Industry VALUES('IT', 10000);
INSERT INTO Industry VALUES('Education', 500);
INSERT INTO Industry VALUES('Entertainment', 9000);
INSERT INTO Sector VALUES('Education', 'Higher Education', 0);
INSERT INTO StealthStartup VALUES
(1, 'IT', '316 Consulting', 'Box 90129, Durham, NC', 100);
INSERT INTO PrivateStartup VALUES
(2, 'Education', 'Blue Devils', 'Durham, NC',
'Brodhead', 'www.duke.edu', 'Higher Education');
INSERT INTO PrivateStartup VALUES
(3, 'Education', 'Tar Heels', 'Chapel Hill, NC',
'Folt', 'www.unc.edu', 'Higher Education');
INSERT INTO Fund VALUES(101, 1);
INSERT INTO Fund VALUES(101, 2);
INSERT INTO Fund VALUES(102, 3);
INSERT INTO TargetOf VALUES(1, 2);
INSERT INTO TargetOf VALUES(1, 3);
-- The following statement should fail because (Entertainment, Higher
-- Education) is not a valid industry sector.
INSERT INTO PrivateStartup VALUES
(4, 'Entertainment', 'Wolf Pack', 'Raleigh, NC',
'Folt', 'www.unc.edu', 'Higher Education');
-- The following two statements should fail because a stealth company
-- cannot be financed by more than one VC fund.
INSERT INTO Fund VALUES(102, 1);
UPDATE Fund SET sid = 1 WHERE vcid = 102 AND sid = 3;
-- The following statement should fail because a VC fund cannot fund
-- two private startups in the same sector.
INSERT INTO Fund VALUES(102, 2);
-- The following statements should fail because only stealth companies
-- can be targets of private companies.
INSERT INTO TargetOf VALUES(2, 3);
-- Write modification statements below (one per constraint) that
-- illustrate how the following constraints are enforced by your
-- schema:
-- 1. No two VC funds can be identical in both name and number:
INSERT INTO VCFund VALUES(101, 'Kleiner Perkins', 7, 76923, '2013-12-31');
-- 2. Every industry has a unique name.
INSERT INTO Industry VALUES('IT', 10000);
-- 3. No two startups in the same industry can have a same name. You
-- should write a modification statement on StealthStartup or
-- PrivateStartup (recall that we don't allow direct modifications to
-- Startup, StealthCompany, and PrivateCompany).
INSERT INTO StealthStartup VALUES
(1, 'IT', '316 Consulting', 'Box 90129, Durham, NC', 100);
-- 4. Sector names are unique within an industry.
INSERT INTO Sector VALUES('Education', 'Higher Education', 0);