-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path6.final_exam.sql
122 lines (103 loc) · 3.11 KB
/
6.final_exam.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
/* Questions 10 */
-- R(A) = { (1) }
-- S(B) = { (2), (2), (3) }
create trigger First
after insert on R
for each row
update S set B = 3 where B = New.A
;
create trigger Second
after update on S
for each row
update R set A = A+1
;
insert into R values (2)
/* -> */
update S set B = 3 where B = 2
/* S(B) = { (3), (3), (3) } */
/* -> */
/* there are two tuples updated */
update R set A = A+1
/* R(A) = { (2), (3) } */
update R set A = A+1
/* R(A) = { (3), (4) } */
/* Questions 11 */
-- T(A) = { (1), (1), (2), (3) }
/* to make the two triggers no difference, */
/* the row-level trigger should be executed only once */
create trigger RowLevel
after update on T
for each row
insert into T values (0)
create trigger StatementLevel
after update on T
insert into T values (0)
/* Question 12 - 14 */
-- Bid(item,price)
/* S1, S2, S3, and S4 within the transactions each execute atomically.
meaning: update is atomic. */
/* T1 - Always Serializable */
/* S1: update Bid set price = price + 5 */
/* S2: insert into Bid values (desktop,30) */
/* T2 */
/* S3: select sum(price) as s from Bid */
/* S4: select max(price) as m from Bid */
/* T2 - Serializable */
-- T1, T2: s = 70, m = 30
-- T2, T1: s = 30, m = 20
/* T2 - Read Committed */
-- T1, T2: s = 70, m = 30
-- T2, T1: s = 30, m = 20
-- S3, T1, S4: s = 30, m = 30
/* T2 - Read Un-Committed */
-- T1, T2: s = 70, m = 30
-- T2, T1: s = 30, m = 20
-- S3, T1, S4: s = 30, m = 30
-- S3(10, 20), S1(15, 25), S4(25), S2: s = 30, m = 25
-- S1(15, 25), S3(40), S4(25), S2: s = 40, m = 25
-- S1(15, 25), S3(40), S2(15, 25, 30), S4(30): s = 40, m = 30
/* Question 15 - updatable view */
/* cannot, aggregation is used */
create view V as select person, avg(score) from Rating group by person
/* cannot, involved in two relations */
create view V as select Movie.title, director, person, score from Movie, Rating where Movie.title = Rating.title
/* cannot, select distinct is used instead of select */
create view V as select distinct(director) from Movie
create view V as select title, director from Movie where title in (select title from Rating where score > 3)
/* Question 18 */
/* G(n1,n2,label) */
/* a directed edge from node n1 to node n2 with the given label */
/* n1 -> n2 */
/* the length of (i.e., the number of edges in) the longest path */
/* in the graph that contains red edges only. */
with recursive
Path(n1, n2, length) as
(
/* The first round path available */
select (n1, n2, 1) from G where label = 'red'
union
select P.n1, G.n2, P.length + 1
from Path P, G
where P.n2 = G.n1
and G.label = 'red'
)
/* Question 19 */
create view FlightRollup
select origin, destination, airline, sum(price) as p from Flight
group by origin, destination, airline with rollup;
-- ROLLUP
-- origin, destination, airline
-- value, value, value
-- value, value, null
-- value, null, null *
-- null, null, null
-- CUBE
-- origin, destination, airline
-- value, value, value *
-- value, value, null
-- value, null, value
-- null, value, value
-- value, null, null *
-- null, value, null
-- null, null, value
-- null, null, null