Browse Source

end of class

Jared Dunbar 2 years ago
parent
commit
0fb1fbc143
Signed by: Jared Dunbar <jrddunbr@gmail.com> GPG Key ID: CF202CC859BAC692
3 changed files with 462 additions and 0 deletions
  1. 295
    0
      HAFH.sql
  2. 134
    0
      ZAGI.sql
  3. 33
    0
      inclass.sql

+ 295
- 0
HAFH.sql View File

@@ -0,0 +1,295 @@
1
+-- create tables
2
+
3
+CREATE TABLE manager (
4
+  managerid CHAR(4) NOT NULL,
5
+  nfname VARCHAR(15) NOT NULL,
6
+  mlname VARCHAR(15) NOT NULL,
7
+  mbdate DATE NOT NULL,
8
+  msalary NUMERIC (9,2) NOT NULL,
9
+  mbonus NUMERIC (9,2),
10
+  mresbuildingid CHAR(3),
11
+  PRIMARY KEY (managerid)
12
+);
13
+
14
+CREATE TABLE managerphone (
15
+  managerid CHAR(4) NOT NULL,
16
+  mphone CHAR(11) NOT NULL,
17
+  PRIMARY KEY (managerid, mphone)
18
+  FOREIGN KEY (managerid) REFERENCES manager (managerid)
19
+);
20
+
21
+CREATE TABLE building (
22
+  buildingid CHAR(3) NOT NULL,
23
+  bnooffloors INT NOT NULL,
24
+  bmanagerid CHAR(4) NOT NULL,
25
+  PRIMARY KEY (buildingid),
26
+  FOREIGN KEY (bmanagerid) REFERENCES manager (managerid)
27
+);
28
+
29
+CREATE TABLE inspector (
30
+  insid CHAR(3) NOT NULL,
31
+  insname VARCHAR(15) NOT NULL,
32
+  PRIMARY KEY (insid)
33
+);
34
+
35
+CREATE TABLE inspecting (
36
+  insid CHAR(3) NOT NULL,
37
+  buildingid CHAR(3) NOT NULL,
38
+  datelast DATE NOT NULL,
39
+  datenext DATE NOT NULL,
40
+  PRIMARY KEY (insid, buildingid),
41
+  FOREIGN KEY (insid) REFERENCES inspector (insid),
42
+  FOREIGN KEY (buildingid) REFERENCES building (buildingid)
43
+);
44
+
45
+CREATE TABLE corpclient (
46
+  ccid CHAR(4) NOT NULL,
47
+  ccname VARCHAR(25) NOT NULL,
48
+  ccindustry VARCHAR(25) NOT NULL,
49
+  cclocation VARCHAR(25) NOT NULL,
50
+  ccidreferredby CHAR(4),
51
+  PRIMARY KEY (ccid),
52
+  UNIQUE (ccname),
53
+  FOREIGN KEY (ccidreferredby) REFERENCES corpclient (ccid)
54
+);
55
+
56
+CREATE TABLE apartment (
57
+  buildingid CHAR(3) NOT NULL,
58
+  aptno CHAR(5) NOT NULL,
59
+  anppfbedrooms INT NOT NULL,
60
+  ccid CHAR(4),
61
+  PRIMARY KEY (buildingid, aptno),
62
+  FOREIGN KEY (buildingid) REFERENCES building (buildingid),
63
+  FOREIGN KEY (ccid) REFERENCES corpclient (ccid)
64
+);
65
+
66
+CREATE TABLE staffmember (
67
+  smemberid CHAR(4) NOT NULL,
68
+  smembername VARCHAR(15) NOT NULL,
69
+  PRIMARY KEY (smemberid)
70
+);
71
+
72
+CREATE TABLE cleaning (
73
+  buildingid CHAR(3) NOT NULL,
74
+  aptno CHAR(5) NOT NULL,
75
+  smemberid CHAR(4) NOT NULL,
76
+  CONSTRAINT cleaningpk
77
+  PRIMARY KEY (buildingid, aptno, smemberid),
78
+  CONSTRAINT cleaningfk
79
+  FOREIGN KEY (buildingid, aptno) REFERENCES apartment (buildingid, aptno)
80
+);
81
+
82
+-- insert statements
83
+
84
+INSERT INTO manager VALUES (
85
+  'M12', 'Boris', 'Grant', '1980-06-20', 60000, null, null
86
+);
87
+INSERT INTO manager VALUES (
88
+  'M23', 'Austin', 'Lee', '1975-10-30', 50000, 5000, null
89
+);
90
+INSERT INTO manager VALUES (
91
+  'M34', 'George', 'Sherman', '1976-01-11', 52000, 2000, null
92
+);
93
+
94
+INSERT INTO managerphone VALUES ('M12', '555-2222');
95
+INSERT INTO managerphone VALUES ('M12', '555-3232');
96
+INSERT INTO managerphone VALUES (`M23`, '555-9988');
97
+INSERT INTO managerphone VALUES ('M34', '555-9999');
98
+
99
+INSERT INTO building VALUES ('B1', '5', 'M12');
100
+INSERT INTO building VALUES ('B2', '6', 'M23');
101
+INSERT INTO building VALUES ('B3', '4', 'M23');
102
+INSERT INTO building VALUES ('B4', '4', 'M34');
103
+
104
+INSERT INTO inspector VALUES ('I11', 'Jane');
105
+INSERT INTO inspector VALUES ('I22', 'Niko');
106
+INSERT INTO inspector VALUES ('I33', 'Mick');
107
+
108
+INSERT INTO inspecting VALUES (
109
+  'I11','B1','2012-05-15','2013-05-14'
110
+);
111
+INSERT INTO inspecting VALUES (
112
+  'I11','B2','2013-02-17','2013-05-17'
113
+);
114
+INSERT INTO inspecting VALUES (
115
+  'I22','B2','2013-02-17','2013-05-17'
116
+);
117
+INSERT INTO inspecting VALUES (
118
+  'I22','B3','2013-01-11','2014-01-11'
119
+);
120
+INSERT INTO inspecting VALUES (
121
+  'I33','B3','2013-01-12','2014-01-12'
122
+);
123
+INSERT INTO inspecting VALUES (
124
+  'I33','B4','2013-01-11','2014-01-11'
125
+);
126
+
127
+INSERT INTO corpclient VALUES (
128
+  'C111', 'BlingNotes', 'Music', 'Chicago', null
129
+);
130
+INSERT INTO corpclient VALUES (
131
+  'C222', 'SkyJet', 'Airline', 'Oak Park', 'C111'
132
+);
133
+INSERT INTO corpclient VALUES (
134
+  'C777', 'WindyCT', 'Music', 'Chicago', 'C222'
135
+);
136
+INSERT INTO corpclient VALUES (
137
+  'C888', 'SouthAlps', 'Sports', 'Rosemont', 'C777'
138
+);
139
+
140
+INSERT INTO apartment VALUES ('B1', '21', 1, 'C111');
141
+INSERT INTO apartment VALUES ('B1', '41', 1, null);
142
+INSERT INTO apartment VALUES ('B2', '11', 2, 'C222');
143
+INSERT INTO apartment VALUES ('B2', '31', 2, null);
144
+INSERT INTO apartment VALUES ('B3', '11', 2, 'C777');
145
+INSERT INTO apartment VALUES ('B4', '11', 2, 'C777');
146
+
147
+INSERT INTO staffmember VALUES ('5432', 'Brian');
148
+INSERT INTO staffmember VALUES ('9876', 'Boris');
149
+INSERT INTO staffmember VALUES ('7652', 'Caroline');
150
+
151
+INSERT INTO cleaning VALUES ('B1', '21', '5432');
152
+INSERT INTO cleaning VALUES ('B1', '41', '9876');
153
+INSERT INTO cleaning VALUES ('B2', '31', '5432');
154
+INSERT INTO cleaning VALUES ('B2', '11', '9876');
155
+INSERT INTO cleaning VALUES ('B3', '11', '5432');
156
+INSERT INTO cleaning VALUES ('B4', '11', '7652');
157
+
158
+-- Alter Statement 5
159
+
160
+ALTER TABLE manager
161
+ADD CONSTRAINT fkresidesin
162
+FOREIGN KEY (mresbuildingid) REFERENCES building (buildingid);
163
+
164
+-- Update Statement 3
165
+
166
+UPDATE manager
167
+SET mresbuildingid = 'B1'
168
+WHERE managerid = 'M12';
169
+
170
+-- Update Statement 4
171
+
172
+UPDATE manager
173
+SET mresbuildingid = 'B2'
174
+WHERE managerid = 'M23';
175
+
176
+-- Update Statement 5
177
+
178
+UPDATE manager
179
+SET mresbuildingid = 'B3'
180
+WHERE managerid = 'M34';
181
+
182
+-- Alter Statement 6
183
+
184
+ALTER TABLE manager
185
+MODIFY mresbuildingid CHAR(3) NOT NULL;
186
+
187
+-- drop first 7 tables
188
+
189
+DROP TABLE   cleaning;
190
+DROP TABLE   staffmember;
191
+DROP TABLE   apartment;
192
+DROP TABLE   corpclient;
193
+DROP TABLE   inspecting;
194
+DROP TABLE   inspector;
195
+DROP TABLE   managerphone;
196
+
197
+-- Alter Statement 7
198
+
199
+ALTER TABLE manager
200
+DROP FOREIGN KEY fkresidesin;
201
+
202
+-- drop last 2 tables
203
+
204
+DROP TABLE building;
205
+DROP TABLE manager;
206
+
207
+DROP TABLE manager;
208
+DROP TABLE building;
209
+
210
+-- Query 39
211
+
212
+SELECT c.ccname AS client, r.ccname AS recommender
213
+FROM corpclient c, corpclient r
214
+WHERE r.ccid = c.ccidreferredby;
215
+
216
+-- Query 40
217
+
218
+SELECT a.buildingid, a.aptno, c.ccname
219
+FROM apartment a, corpclient c
220
+ON a.ccid = c.ccid;
221
+
222
+-- Query 41
223
+
224
+SELECT a.buildingid, a.aptno, c.ccname
225
+FROM apartment a, LEFT OUTER JOIN corpclient c
226
+ON a.ccid = c.ccid;
227
+
228
+-- Query 42
229
+
230
+SELECT a.buildingid, a.aptno, c.ccname
231
+FROM apartment a, RIGHT OUTER JOIN corpclient c
232
+ON a.ccid = c.ccid;
233
+
234
+-- Query 43
235
+
236
+SELECT a.buildingid, a.aptno, c.ccname
237
+FROM apartment a, LEFT OUTER JOIN corpclient c
238
+ON a.ccid = c.ccid
239
+UNION
240
+
241
+
242
+
243
+--
244
+SELECT a.buildingid, a.aptno, c.ccname
245
+FROM apartment a, RIGHT OUTER JOIN corpclient c
246
+ON a.ccid = c.ccid;
247
+
248
+-- Query 44
249
+
250
+SELECT m.managerid, m.fname, m.mlname, s.smemberid
251
+FROM manager m, staffmember s
252
+WHERE m.mfname = s.smembername;
253
+
254
+-- Query 45
255
+
256
+SELECT *
257
+FROM manager
258
+WHERE mbonus IS NULL;
259
+
260
+-- Query 46
261
+
262
+SELECT *
263
+FROM building
264
+WHERE EXISTS (
265
+  SELECT *
266
+  FROM manager
267
+  WHERE buildingid = mresbuildingid
268
+);
269
+
270
+-- Query 47
271
+
272
+SELECT *
273
+FROM building
274
+WHERE NOT EXISTS (
275
+  SELECT *
276
+  FROM manager
277
+  WHERE buildingid = mresbuildingid
278
+);
279
+
280
+-- Create Table Statement 1
281
+
282
+CREATE TABLE cleaningdenormalized (
283
+  buildingid CHAR(3) NOT NULL,
284
+  aptno CHAR(5) NOT NULL,
285
+  smemberid CHAR(4) NOT NULL,
286
+  smembername VARCHAR(15) NOT NULL,
287
+  PRIMARY KEY (buildingid, aptno, smemberid)
288
+);
289
+
290
+-- Insert Statement 2
291
+
292
+INSERT INTO cleaningdenormalized
293
+SELECT c.buildingid, c.aptno, s.smemberid, s.smembername
294
+FROM cleaning c, staffmember s
295
+WHERE c.smemberid = s.smemberid;

+ 134
- 0
ZAGI.sql View File

@@ -386,3 +386,137 @@ SELECT t.tid, t.tdate, p.productname, sv.noofitems AS quantity, (sv.noofitems *
386 386
 FROM product p, salestransaction t, soldvia sv
387 387
 WHERE sv.productid = p.productid AND sv.tid = t.tid
388 388
 ORDER BY t.tid;
389
+
390
+-- Alter Statement 1
391
+
392
+ALTER TABLE vendor ADD (
393
+  vendorphonenumber CHAR(11)
394
+);
395
+
396
+-- Alter Statement 2
397
+
398
+ALTER TABLE vendor DROP (
399
+  vendorphonenumber
400
+);
401
+
402
+-- Insert Statement 1
403
+
404
+INSERT INTO product VALUES (
405
+  '7X7', 'Airy Sock', '1000', 'MK', 'CP'
406
+);
407
+
408
+-- Update Statement 1
409
+
410
+UPDATE product
411
+SET productprice = 10
412
+WHERE productid = '7X7';
413
+
414
+-- Alter Statement 3
415
+
416
+ALTER TABLE product ADD (
417
+  discount CHAR(11)
418
+);
419
+
420
+-- Update Statement 2
421
+
422
+UPDATE product
423
+SET discount = 0.2;
424
+
425
+-- Update Statement 3
426
+
427
+UPDATE product
428
+SET discount = 0.3
429
+WHERE vendorid = 'MK';
430
+
431
+-- Alter Statement 4
432
+
433
+ALTER TABLE product DROP (
434
+  discount
435
+);
436
+
437
+-- Delete Statement 1
438
+
439
+DELETE FROM product
440
+WHERE productid = '7X7';
441
+
442
+-- Create View Statement 1
443
+-- first line may be broken
444
+
445
+CREATE VIEW products_more_than_3_sold AS
446
+SELECT productid, productname, productprice
447
+FROM product
448
+WHERE productid IN (
449
+  SELECT productid
450
+  FROM soldvia
451
+  GROUP BY productid
452
+  HAVING SUM(noofitems) > 3
453
+);
454
+
455
+-- Query 29a
456
+
457
+SELECT *
458
+FROM products_more_than_3_sold;
459
+
460
+-- Create View Statement 2
461
+-- first line may be broken
462
+
463
+CREATE VIEW products_in_multiple_trnsc AS
464
+SELECT productid, productname, productprice
465
+FROM product
466
+WHERE productid in (
467
+  SELECT productid
468
+  FROM soldvia
469
+  GROUP BY productid
470
+  HAVING COUNT(tid) > 1
471
+);
472
+
473
+-- Query 30a
474
+
475
+SELECT *
476
+FROM products_in_multiple_trnsc;
477
+
478
+-- Drop View Statement 1
479
+
480
+DROP VIEW products_more_than_3_sold;
481
+
482
+-- Drop View Statement 2
483
+
484
+DROP VIEW products_in_multiple_trnsc;
485
+
486
+-- Query 36
487
+
488
+SELECT *
489
+FROM products_more_than_3_sold
490
+UNION
491
+SELECT *
492
+FROM products_in_multiple_trnsc
493
+
494
+-- Query 37
495
+
496
+SELECT *
497
+FROM products_more_than_3_sold
498
+INNER JOIN products_in_multiple_trnsc
499
+USING (
500
+  productid, productname, productprice
501
+);
502
+
503
+-- Query 38
504
+
505
+SELECT DISTINCT *
506
+FROM products_more_than_3_sold
507
+WHERE (
508
+  productid, productname, productprice
509
+) NOT IN (
510
+  SELECT *
511
+  FROM products_in_multiple_trnsc
512
+);
513
+
514
+-- Alternative Query 38
515
+
516
+SELECT DISTINCT *
517
+FROM products_more_than_3_sold
518
+LEFT JOIN products_in_multiple_trnsc
519
+USING (
520
+  productid, productname, productprice
521
+)
522
+WHERE products_in_multiple_trnsc.productid IS NULL;

+ 33
- 0
inclass.sql View File

@@ -0,0 +1,33 @@
1
+-- Show the name and population in millions for the countries of the continent 'South America'.
2
+-- Divide the population by 1000000 to get population in millions.
3
+
4
+SELECT name, population/1000000
5
+AS popMillions, continent
6
+FROM world;
7
+
8
+-- Show the countries which have a name that includes the word 'United'
9
+
10
+SELECT name
11
+FROM countries
12
+WHERE name %like% 'United';
13
+
14
+-- Show the countries that are big by area or big by population. Show name, population, and area.
15
+
16
+SELECT name, population, area
17
+FROM world
18
+WHERE area > 3000000
19
+OR population > 250000000;
20
+
21
+-- For South America, show population
22
+-- not correct
23
+
24
+SELECT name, gdp
25
+AS popMillions, gdp/1000000000
26
+FROM world
27
+WHERE continent = 'South America';
28
+
29
+-- Show the name and capital where the name and the captial have the same number of characters
30
+
31
+SELECT name, capital
32
+FROM world
33
+WHERE length(name) = length(capital)

Loading…
Cancel
Save