110 likes | 184 Views
Database midterm2 answer. 張兢真 beautidays at ms11.voip.edu.tw 王鐘逸 chungyi at ms11.voip.edu.tw 陳柏州 mac at ms11.voip.edu.tw. 1-1. select a.name,b.homework from (select distinct(name) from hw) as a, (select distinct(homework) from hw) as b where b.homework not in
E N D
Database midterm2 answer 張兢真 beautidays at ms11.voip.edu.tw 王鐘逸 chungyi at ms11.voip.edu.tw 陳柏州 mac at ms11.voip.edu.tw
1-1 • select a.name,b.homework from • (select distinct(name) from hw) as a, • (select distinct(homework) from hw) as b • where b.homework not in • (select distinct(homework) from hw as c where c.name=a.name) • order by a.name,b.homework
1-2 • CREATE TEMPORARY TABLE T SELECT * FROM HW;UPDATE HW SET Score = 60 WHERE Score < 60 AND Name IN (SELECT Name from T GROUP BY Name HAVING COUNT(*)>=3);
2-1 http://ms11.voip.edu.tw/~mac/db/1.php http://ms11.voip.edu.tw/~mac/db/1.phps • <?php • define('HOST', 'localhost'); • define('USER', 'mac'); • define('PASS', ' yourpassword '); • define('DB', 'mac'); • mysql_connect(HOST, USER, PASS); • mysql_select_db(DB); • $result = mysql_query("select homework from hw2 group by homework having count(*) >=5"); • $num=mysql_num_rows($result); • $i=0; • while($i<$num) • { • $r=mysql_fetch_array($result); • echo $r[0]."<br>"; • $result_each = mysql_query("select count(*), avg(score) from hw2 where homework = '$r[0]'"); • $num_each = mysql_num_rows($result_each); • $i_each=0; • while ($i_each<$num_each) • { • $r_each=mysql_fetch_array($result_each); • echo $r_each[0]." ".$r_each[1]."<br>"; • $i_each++; • } • $i++; • } • ?>
2-2 為了demo,這裡使用table hw2 http://ms11.voip.edu.tw/~mac/db/2.php http://ms11.voip.edu.tw/~mac/db/2.phps • <?php • define('HOST', 'localhost'); • define('USER', 'mac'); • define('PASS', ‘yourpassword'); • define('DB', 'mac'); • mysql_connect(HOST, USER, PASS); • mysql_select_db(DB); • $result = mysql_query("select name from hw2 group by name having count(homework) <(select count(distinct homework) from hw2)"); • $num=mysql_num_rows($result); • $i=0; • while($i<$num) • { • $r=mysql_fetch_array($result); • echo $r[0]."<br>"; • $i++; • } • ?>
3 (1) 6 (2) T1 -> T2 -> T3 T3 -> T2 -> T1 T2 -> T3 -> T1 T2 -> T1 -> T3 T3 -> T1 -> T2 T1 -> T3 -> T2 (3)13!
T1 T2 T3 4.2 T1 T2 T3 X T1 T2 R(X) R(X) X W(X) X X T3 W(X) R(X) 4.1 T1 T2 T3 X R(X) R(X) X W(X) X X R(X) cycle W(X) cycle
4.3 T1 T2 T3 X R(X) R(X) X W(X) X X R(X) W(X) 4.4 T1 T2 T3 X T1 T1 T2 T2 R(X) R(X) X R(X) X X T3 T3 W(X) W(X) Serializable : T2-T3-T1 cycle
5.S1 T1 T2 T3 R(X) Z R(Z) R(Z) R(X) X Y R(Y) W(X) R(Y) Serializable : T3-T1-T2 R(Y) T1 T2 W(Z) W(Y) T3
5.S2 T1 T2 T3 R(X) Z R(Z) T1 T2 R(X) Y R(Z) X Y R(Y) T3 cycle R(Y) W(X) W(Z) W(Y) W(Y)
6 (1) no (2) Figure 18.4 (P.634) T1’T2’ read_lock(Y); read_lock(X); read_item(Y); read_item(X); write_lock(X); write_lock(Y); unlock(Y); unlock(X); read_item(X); read_item(Y); X := X+Y; Y := X+Y; write_item(X); write_item(Y); unlock(X); unlock(Y);