Document

Report
0911数据库作业答疑
2012年5月
第一次作业
• 1.2,1.3 (Version 7) 1.?(Version 8)
使用数据库系统的优点和缺点是什么?
优点:
数据共享
减少冗余
避免不一致
提供事务支持
保持完整性
增强安全性
平衡相互冲突的请求
加强标准化
缺点:
性全安的衷折
性整完的衷折
件硬的外额要需
大常非销开行执
要重关至作操的功成
杂复很能可统系
• 1.7 (Version 7) 1.6 (Version 8)
a) SELECT WINE, PRODUCER FROM CELLAR
WHERE BIN# = 72;
检索结果:
WINE
PRODUCER
Zinfandel
Rafanelli
解释:
Rafanelli是Zinfandel 商产生的酒。
b) SELECT WINE, PRODUCER FROM CELLAR
WHERE YEAR > 1996;
WINE
PRODUCER
Chardonnay
Buena Vista
Chardonnay
Geyser Peak
Joh. Riesling
Jekel
Fumé Blanc
Ch. St. Jean
Gewurztraminer
Ch. St. Jean
解释:
Buena Vista是Chardonnay年且,商产生的
在是份1996行一第中表(后之年(。
c) SELECT BIN#, WINE, YEAR FROM CELLAR
WHERE READY < 1999;
BIN#
WINE
YEAR
6
Chardonnay
1996
22
Fumé Blanc
1996
52
Pinot Noir
1995
解释:
些一 1996的产生年Chardonnay且并酒
ready在间时1999前之年
d) SELECT BIN#, WINE, YEAR FROM CELLAR
WHERE PRODUCER = ‘Robt. Mondavi’ AND
BOTTLES > 6;
WINE
BIN#
YEAR
Can, Sauvignon
48
1993
解释:
些一 Cab. Sauvignon是们它,酒1993生年
是商产生,的产Robt. Mondavi,于大数瓶6。
注意:不要再解释中加入查询结果或条件
没有的信息。
• 2.4 列出DBMS的主要功能
答:
a) 如比, 义定据数create database,create
b)
c)
d)
e)
f)
g)
h)
table,create view,create index.
纵操据数 ,如比select insert update delete
行执和化优,对部内如比sql。化优会行执的句语
据数有还,全安码密如比,性整完和性全安据数
性照参键外,键主,性整完的值录记中表
同问访户用多,电断然突如比,发并和复恢据数
。理处的时录记一
器控监
,库据数(象对种各中库据数对如比,典字据数
。据数述描的)等表
能性
• 2.5 区分数据的物理独立性和逻辑独立性。
答::性立独辑逻和性立独理物
a) 对能序程用应的户用和户用:性立独理物
方储存,式形织组(构结理物库据数于
,变改构结理物,变不持保而变改的)式
。变不以可序程
b) 于对序程用应的户用和户用:性立独辑逻
变不持保而变改的构结辑逻库据数
对图视改修若,图视于应对序程个有如比
程用应。可即图视应相改修,构结表的应
。化变用不序
• 2.6 如何理解元数据?
答:是,典字即据数元 “据数的据数”括包,
,息信节细的象对种各的用有身自统系对了
数对于关了括包它,量变系关统系组一是
关本基(息信节细的目条种各的用有库据
用。)等等,户用和引索、图视、量变系
典字问访样一据数的己自问访像够能户.
解理何如“据数的据数”据数录记如比,
名的库据数个每,库据数些哪有中统系库
哪有表个每,表些哪有中库据数个每,字
看查以可,段字些mssqlserver的门专个有中
。值些这录记库据数
第二次作业
• 3.2 描述供应商和零件数据库的字典变量
TABLE 和 COLUMN的内容。
TABLE、COLUMN含包不或含包系关身自的
,可均 TABLE不以所之据数的分部影阴中表
字的统系定特个一为因是,值确精出给能
看别分。息信的定特统系多许含包须必典
。况情的含包和含包不系关身自
• TABLE:
TABNAME
COLCOUNT
ROWCOUNT
S
4
P
5
SP
3
TABLE
>3
COLUMN
>2
......
......
...
...
6
...
...
12
...
...
>5 (table和column个两 ...
入加被少至表)
...
>17
(table和列三的 ...
column的2加被少至列 ...
入)
......
...
...
……….
5
注意:ROWCOUNT不要把列名给算上。
• COLUMN:
TABNAME
COLNAME
S
S#
S
SNAME
S
STATUS
S
CITY
P
P#
P
PNAME
P
COLOR
P
WEIGHT
P
CITY
SP
S#
SP
P#
SP
QTY
TABLE
TABNAME
TABLE
COLCOUNT
TABLE
ROWCOUNT
COLUMN
TABNAME
COLUMN
COLNAME
......
......
.....
.
.....
.
.....
.
.....
.
.....
.
.....
.
.....
.
.....
.
.....
.
.....
.
.....
.
.....
.
.....
.
.....
.
.....
.
.....
.
.....
.
.....
.
.....
• 3.3 在3.6节提到,字典是自描述的——也就
是说,包括字典关系变量自身的条目。扩
展图3.6,以包括关系变量TABLE和COLUMN
的条目。
TABLE:
......
DEPT
COLCOUNT ROWCOUN
T
3
3
EMP
4
4
......
TABLE
(>3)
COLUMN
(>2)
(>4)包少至( ......
含table和
column)行两
(>12)
......
TABNAME
......
...... ...... ...... ......
• COLUMN:
TABNAME
COLNAME
DEPT
DEPT#
DEPT
DNAME
DEPT
BUDGET
EMP
EMP#
EMP
ENAME
EMP
DEPT#
EMP
SALARY
TABLE
TABNAME
TABLE
COLCOUNT
TABLE
ROWCOUNT
COLUMN
TABNAME
COLUMN
COLNAME
......
......
....
..
....
..
....
..
....
..
....
..
....
..
....
..
....
..
....
..
....
..
....
..
....
..
....
..
....
..
第三次作业
• 补1:Create following tables and some index, based on your
understanding.
Movie(title, year, length, inColor, studioName, producerC#)
CREATE TABLE Movie
( TITLE CHAR(20),
YEAR NUMERIC(4),
LENGTH NUMERIC(4),
INCOLOR CHAR(10),
STUDIONAME CHAR(15),
PRODUCERC# CHAR(15),
PRIMARY KEY(TITLE),
FOREIGN KEY(STUDIO NAME) REFERENCES STUDIO(NAME));
FOREIGN KEY(PRODUCERC#) REFERENCES
MOVIEEXEC(NAME));
CREATE INDEX I1 ON MOVIE (TITLE);
StarsIn(movieTitle, movieYear, strName)
CREATE TABLE STARSIN
(MOVIETITLE CHAR(20),
MOVIEYEAR CHAR(20),
STRNAME CHAR(20),
PRIMARY KEY(MOVIETITLE,STRNAME),
FOREIGN KEY(MOVIETITLE) REFERENCE MOVIE(TITLE));
FOREIGN KEY(STRNAME) REFERENCES MOVIESTAR(NAME));
CREATE INDEX I2 ON STARSIN(MOVIETITLE,STRNAME);
MovieStar(name, address, gender, birthdate)
CREATE TABLE MOVIESTAR
(NAME CHAR(20),
ADDRESS CHAR(30),
GENDER NUMERIC(10),
BIRTHDATE CHAR(15),
PRIMARY KEY(NAME));
CREATE INDEX I3 ON MOVIESTAR(NAME);
MovieExec(name, address, cert#, netWorth)
CREATE TABLE MOVIEEXEC
(NAME CHAR(20),
ADDRESS CHAR(30),
CERT CHAR(10),
NETWORTH CHAR(20),
PRIMARY KEY(NAME));
CREATE INDEX I4 ON MOVIEEXEC(NAME);
Studio(name, address, presC#)
CREATE TABLE STUDIO
(NAME CHAR(15),
ADDRESS CHAR(30),
PRES CHAR(15),
PRIMARY KEY(NAME));
CREATE INDEX I5 ON STUDIO(NAME);
Classes(class, type, country, numGuns, bore, displacement)
CREATE TABLE CLASSES
(CLASS CHAR(10),
TYPE NUMERIC(6),
COUNTRY CHAR(15),
NUMGUNS NUMERIC(10),
BORE CHAR(10),
DISPLACEMENT CHAR(15),
PRIMARY KEY(CLASS));
CREATE INDEX I1 ON CLASSES (CLASS);
Ships(name, class, launched)
CREATE TABLE SHIPS
(NAME CHAR(10),
CLASS CHAR(10),
LAUNCHED NUMERIC(5),
PRIMARY KEY(NAME),
FOREIGN KEY(CLASS) REFERENCE CLASSES);
CREATE INDEX I2 ON SHIPS(NAME);
Battles(name, date)
CREATE TALBLE BATTLES
(NAME CHAR(15),
DATE CHAR(15),
PRIMARY KEY(NAME));
CREATE INDEX I3 ON BATTLES(NAME);
Outcomes(ship, battle, result)
CREATE TABLE OUTCOMES
(SHIP CHAR(20),
BATTLE CHAR(15),
RESULT NUMERIC(4),
PRIMARY KEY(BATTLE),
FOREIGN KEY(SHIP) REFERENCE SHIPS,
FOREIGN KEY(BATTLE) REFERENCE BATTLES);
CREATE INDEX I4 ON OUTCOMES(BATTLE);
• 补2: Write following queries in SQL, based on
the Exercise 1.
(1). Find Sandra Bullock’s birthdate
SELECT birthdate FROM MovieStar WHERE name =
‘Sandra Bullock’;
(2). Find all executives worth at least $10,000,000
SELECT name FROM MovieExec WHERE netWorth
>= 10,000,000;
(3). Find the countries whose ships had the largest
number of guns.)总和(
SELECT country FROM Classes GROUP BY country
HAVING SUM(numGuns)>=ANY(SELECT
SUM(numGuns) FROM Classes GROUP BY country)
(4). Find the average number of guns of battleship
classes.
SELECT AVG(numGuns) FROM Classes WHERE class
= ‘battleship’
(5). Find all the stars who either are male or live in
Malibu.
SELECT name FROM MovieStar WHERE gender
= ’male’ OR address = ’Malibu’;
(4).Which stars appeared in movies produced by
MGM in 1995?
SELECT strName FROM StarsIn WHERE movieTitle
IN (SELECT title FROM Movie WHERE STUDIONAME
=’MGM’ AND year = ‘1995’);
第四次作业
• 选择自己生活、学习中的需求,创建有2-3
个关系的数据库。
略。
• 在创建的数据库上给出两个查询访问要求,
并用SQL语句实现)要求有join/group等(
略。
第五次作业
• 8.1 写出“供应商-工程”数据库中的一些完整
性约束)SQL(
b. 合法的供应商编号为大于两个字母的字符串,
其中第一个字母为”S”,其余部分为从0到9999范
围内的十进制整数。
CREATE DOMAIN S# CHAR
CONSTRAINT VALID_S# CHECK
( SUBSTRING ( VALUE FROM 1 FOR 1 ) = ’S’
AND CAST ( SUBSTRING ( VALUE FROM 2 ) AS INTEGER )
>= 0
AND CAST ( SUBSTRING ( VALUE FROM 2 ) AS INTEGER )
<= 9999 )
d.任何两个工程不能位于同一城市
CREATE ASSERTION D CHECK
( NOT EXISTS ( SELECT * FROM J AS J1, J AS J2
WHERE J1.J# <> J2.J# AND J1.CITY = J2.CITY ) )
g.供应商状态值最高的供应商和状态值最低的供应商不
能再同一城市中
CREATE ASSERTION G CHECK
( NOT EXISTS ( SELECT * FROM S AS S1, S AS S2
WHERE S1.STATUS = ( SELECT MAX ( S.STATUS ) FROM S )
AND S2.STATUS = ( SELECT MIN ( S.STATUS ) FROM S )
AND S1.CITY = S2.CITY AND S1.STATUS <> S2.STATUS ) )
j.至少有一种红色的零件
CREATE ASSERTION J CHECK
( EXISTS ( SELECT * FROM P WHERE P.COLOR = ‘Red’ )
OR NOT EXISTS ( SELECT * FROM P ) )
g.至少有一种红色零件的单重小于50磅
CREATE ASSERTION M CHECK
( EXISTS ( SELECT * FROM P WHERE P.COLOR = ‘Red’
AND P.WEIGHT < 50.0 )
OR NOT EXISTS ( SELECT * FROM P WHERE P.COLOR =
‘Red’ ) )
o.在London的供应商提供的零件总数量要多于在Paris的
供应商提供的零件总数量
CREATE ASSERTION O CHECK
( SELECT SUM ( SPJ.QTY ) FROM S, SPJ
WHERE S.S# = SPJ.S# AND S.CITY = ‘London’ GROUP BY
S#
>
SELECT SUM (SPJ.QTY ) FROM S, SPJ
WHERE S.S# = SPJ.S# AND S.CITY = ‘Paris’ GROUP BY S#
)
o.位于Athens的供应商只能搬到London或Paris,London
的供应商只能搬到Paris。
而,型类束约态动的上量变系关为题此SQL用,束约态动持支不SQL描法无
用以可,述Tutorial D。误错有案答上书,是但,句语
CONSTRAINT Q
IS_EMPTY ( ( S JOIN ( S’ WHERE CITY = ’Athens’ ) )
WHERE CITY <> ’Athens’ AND CITY <> ’London’ AND CITY <> ’Paris’ )
AND
IS_EMPTY ( ( S JOIN ( S’ WHERE CITY = ’London’ ) )
WHERE CITY <> ’London’ AND CITY <> ’Paris’ )
是里这(名量变系关,前之作操新更在:定约的下如入引S’(指为解理被已
:解理样这以可就束约的中题本,样这。量变系关的应对向
(a) 前新更将CITY=’Athens’ 行执量变系关的后新更与量变系关的JOIN;作操
(b) 在不到找Athens在不也住London在不,住Paris;组元的住
(c) 。空为须必集果结
。理同况情种一另
动态约束是指数据库从一种状态转变为另一种状态时新、旧值之间所应满足
的约束条件,它是反映数据库状态变迁的约束。
• 8.9 用图4-5中“供应商-零件-工程”例子中的数据,
说出下列操作会引起什么结果。
(b) 更新零件P5,置P#值为P4;
会被拒绝,违反了主码唯一性。
(d) 删除供应商S3,此时的参照行为是CASCADE;
接受,S中供应商S3对应的元祖被删去,SPJ中所有
S#=‘S3’的元组也被删去。
(h) 更新供货量S5-P5-J5,置J#为J7;
拒绝,违反了主码唯一性。
(j) 插入供货量记录S5-P6-J7;
接受。
第六次作业
• 9.2 定义一个视图,这一视图包括了“供应
商和他所提供的零件不在同一地点”的供
应商号和零件号
CREATE VIEW V92
AS SELECT S.S#, P.P#
FROM S, P, SP
WHERE S.CITY <> P.CITY AND S.S# = SP.S# AND P.P# =
SP.P#
• 9.5 在供应商-零件-工程数据库上定义视图,
使它包含所有由S1提供零件而且使用零件
P1的工程)只要求工程编号和城市属性(
CREATE VIEW V95
AS SELECT J.J#, J.CITY
FROM J, SPJ
WHERE J.J# = SPJ.J# AND SPJ.S# = ‘S1’ AND SPJ.P# =
‘P1’
• 9.6 对于下面的视图定义
VAR HEAVYWEIGHT VIEW
((P RENAME WEIGHT AS WT, COLOR AS COL)
WHERE WT > WEIGHT(14.0)) {P#, WT, COL};
请给出对下列语句实施替换过程后的转化形
式:
a. RA := HEAVYWEIGHT WHERE COL =
COLOR(‘GREEN’);
SELECT P.P#, P.WEIGHT AS WT, P.COLOR AS COL
FROM P WHERE P.WT > 14.0 AND P.COL = ‘Green’
b. RB := (EXTEND HEAVYWEIGHT ADD
WT+WEIGHT(5.3) AS WTP) {P#, WTP};
SELECT P.P#, P.WEIGHT +5.3 AS WTP
FROM P WHERE P.WEIGHT > 14.0
c. UPDATE HEAVYWEIGHT WHERE WT = WEIGHT
(18.0) COL := ‘White’;
UPDATE P
SET COLOR = ‘White’
WHERE ( WEIGHT > 14.0) AND WEIGHT = 18.0
d. DELETE HEAVYWEIGHT WHERE WT <
WEIGHT(10.0);
DELETE FROM P
WHERE ( WEIGHT > 14.0) AND WEIGHT < 10.0
不会有任何元组被删除
e. INSERT INTO HEAVYWEIGHT
RELATION {TUPLE{P# P#(‘P99’),
WT WEIGHT(12.0), COL COLOR(‘Purple’)}};
INSERT INTO P ( P#, WEIGHT, COLOR )
VALUES ( ‘P99’, 12.0, ‘Purple’ )
CHECK WEIGHT > 14.0
失败,因为插入的元组不满足视图谓词
第七次作业
关系代数:黑色 关系演算:绿色
RANGEVAR SX RANGES OVER S;
RANGEVAR PX RANGES OVER P;
RANGEVAR JX RANGES OVER J;
RANGEVAR SPJX RANGES OVER SPJ;
一定要先定义范围变量
• 6.13 求所有有关工程的信息
∏J#,JNAME,CITY(J) 或者 J
JX
• 6.15 求为工程J1提供零件的供应商的号码。
∏S#(бJ#=J1(SPJ))
SPJX.S# WHERE SPJX.J# = J#( ‘J1’ )
• 6.20 求所有的供应商号/零件号/工程号三元组。其
中所指的供应商、零件和工程三者中的任意两个都
不在同一个城市。
∏S#,P#,J#(бS.city≠P.city∧P.city≠J.city∧J.city≠S.city(
S×P×J))
(SX.S#, PX.P#, JX.J#) WHERE SX.CITY ≠ PX.CITY
AND PX.CITY ≠ JX.CITY
AND JX.CITY ≠ SX.CITY
• 6.34 求使用了S1供应的零件的总量。
∏J#(∏P#(б S#=S1(SPJ)) SPJ)
SPJX.J# WHERE EXISTS SPJY ( SPJX.P# = SPJY.P# AND
SPJY.S# = S#(‘S1’))
注意:其他供应商供应的也应算入总量
• 7.3 设p(x)和q(x)是任意的合式公式,x、y分
别为自由变量,下面哪一种说法是正确
的?
a) EXISTS x (p(x)) AND EXISTS y(q(y)) ≡ EXISTS x
EXISTS y (p(x) AND q(y))
正确
b) EXISTS x (IF p(x) THEN q(x) END IF) ≡ IF
FORALL x (p(x)) THEN EXISTS x (q(x)) END IF
∃  →  ≡ ∃ ¬ ∪  ≡ ∃¬ ∪ ∃
≡ ¬ ¬∃¬ ∪ ∃ ≡ ¬∀ ∪ ∃
≡ ∀ → (∃)
第八次作业
• 1.求最小函数依赖集
R(A,B,C,D,E,F,G)
F={AB→C, C→A, BC→D, ACD→B, D→EG, BE→C, CG→BD, CE→AG}
解:将右边写出单属性并去除重复FD)分解律(
F={AB→C, C→A, BC→D, ACD→B, D→E,D→G, BE→C, CG→B,CG→D,
CE→A,CE→G}
消去左部冗余属性CE→A,C→A;ACD→CD,D→G,CD→CG,
CG→B,则ACD->B变为CD→B;
F={AB→C, C→A, BC→D, CD→B, D→E,D→G, BE→C, CG→B,
CG→D,CE→G}
消去冗余函数依赖
①CD→B冗余,因为可以由D→G, CG→B推出
F={AB→C, C→A, BC→D, D→E,D→G, BE→C, CG→B,CG→D,CE→G}
CG→D冗余,因为可以由CG→B,BC→D推出
F={AB→C, C→A, BC→D, D→E,D→G, BE→C, CG→B,CE→G}
②CG→B冗余,因为可以由CG→D, CD→B推出
F={AB→C, C→A, BC→D, CD→B, D→E,D→G, BE→C, CG→D,CE→G}
• 2 R(A,B,C,D), F={A→C, D→C, BD→A},问模式分
解 p={R1,R2,R3} 相对于F是否是无损分解?是否
保持函数依赖?
其中: R1(A,B),R2(A,C,D),R3(B,C,D)
解:根据Chase过程)略(,终止时表格中未出
现全a的行,所以不是无损分解
∏R1(F)={按自反律推出的平凡FD}
∏R2(F)={ A→C, D→C,以及按自反律推出的平凡
FD}
∏R3(F)={ D→C,以及按自反律推出的平凡FD}
∏R1(F)∪∏R2(F)∪∏R3(F)={ A→C, D→C }+ ≠F+
不保持函数依赖
• 3 设关系模式R(S#,C#,Score,Tname,Addr),其属
性分别表示学号、课程号、成绩、任课教师姓
名、教师地址,规定:每个学生每门课只有一
个成绩,每门课只有一个教师任教,每个教师
只有一个地址,教师没有同名同姓。
(1)写出R的基本函数依赖和候选码,并说明理
由
由题意得出函数依赖集:
S#, C#->Score
C#->Tname
Tname->Addr
可以推导出S#,C# -> S#, C#, Tname, Addr,可知
)S#,C#(是超码,而S#和C#不是超码,所以
)S#,C#(是候选码
(2)把R分解为2NF模式,并说明理由
将R分解为
R1(S#, C#, Score)
R2(C#, Tname, Addr)
验证R1和R2是2NF
2NF:每一个非主属性不局部依赖于主码
(3)把R分解为3NF模式,并说明理由
R1(S#, C#, Score)
R2(C#, Tname)
R3(Tname, Addr)
验证R1,R2和R3是3NF
3NF:每一个非主属性都不传递依赖于于主码
• 4 R(ABCDEF), F={E→D,C→B,CE→F,B→A} 问:
(1)R最高属于第几范式?
E
D
C
B
A
F
候选码为CE。为1NF。
(2)无损联接并保持函数依赖地分解R为3NF
1. 先将R保持函数依赖地分解到3NF,R1(E,D), R2(C, B),
R3(C, E, F), R4(B, A)
2. 得到无损连接并保持函数依赖的3NF分解
q = {R1, R2, R3, R4}
p = q U {R(C, E)}
因为{C,E}为{C,E,F}子集,故去掉。所以1结果即为结
果。
(3)无损联接地分解R为BCNF
1. p = {R}
2. B->A不满足BCNF定义,分解R:
p={R1(A,B), R2(B,C,D,E,F)}
3. R2中C->B不满足BCNF定义,分解R2:
p={R1(A,B), R3(B,C), R4(C,D,E,F)}
4. R4中E->D不满足BCNF定义,分解R4:
p={R1(A,B),R3(B,C),R5(D,E),R6(C,E,F)}
5. p中各关系模式均满足BCNF定义,终止。
则p={R1(A,B),R3(B,C),R5(D,E),R6(C,E,F)}
第九次作业
• 1. ACID property
性子原)atomicity(要,做都么要,的子原是务事
。做不都么
性致一)consistency(。性致一的库据数了证保务事
状致一个一另为变转态状致一个一从库据数将务事
。性致一证保须无内务事在但,态
性离隔)isolation(使即,说是就也。离隔互相务事
到直作操新更的务事一任,行执发并务事个多常通
。的见可不是都务事他其对交提功成其
性久持)durability(统系使即,交提功成务事旦一
。效有久永将也新更的库据数对其,溃崩
• 2. Transaction state transfer condition.
Active: the initial state; the transaction stays in this
state while it is executing
Partially committed: after the final statement has been
executed.
Failed: after the discovery that normal execution can no
longer proceed.
Aborted: after the transaction has been rolled back and
the database restored to its state prior to the start of
the transaction
Commit: after successful completion
• 3. Give two transaction examples.
。票订站车火;账转行银(应详细说明(
第十次作业
• 14.4 ?么什是别分义含的复恢,下况情列下
a. Commit库据数入写的制强容内的区冲缓将时
”作操做重“须无时复恢障故统系:经已commit
要需不以所,库据数入写的理物经已定一务事的
redo
b. Commit据数入写的理物不容内的区冲缓前之
库
记志日的”消撤“于用而进,作操”消撤“须无
要必再不也录:
有没commit,库据数入写的理物有没定一务事的
要需不以所undo
• 14.6 利用供应商和零件数据库,写一个SQL应用程序,按
零件编号顺序查询并显示所有的零件,每十个记录重新开
始一个新的事物,并且要求将第十个记录删除。假设从零
件表到供应表的DELETE外码规则为CASCADE)也就是说,
该练习可忽略对供应表的操作(。注:可是用SQL游标机
制。
and write the possible log records for it. Change the exercises
to delete the every third part according the data on the book.
日志:
<Ti, start>
<Ti, P10, old_value, NULL>
<Ti, commit>
:注<Ti, D, old_value, NULL>
或<Ti, delete, D, value>
EXEC SQL DELARE CP CURSOR FOR
SELECT P.P#, P.PNAME, P.COLOR, P.WEIGHT, P.CITY
FROM P
WHERE P.P# > previous_P# (previous_P#需要因为游标关闭后不再和原来
的项对应(
ORDER BY P# ;
previous_P# := ’ ’ ;
eof := false ;
no := 0 ;
DO WHILE ( eof = false ) ;
EXEC SQL OPEN CP ;
DO count := 1 TO 10 ;
EXEC SQL FETCH CP INTO : P#, PNAME, COLOR, WEIGHT, CITY ;
IF SQLSTATE = ‘02000’ THEN
DO ;
EXEC SQL CLOSE CP ;
EXEC SQL COMMIT ;
eof := true ;
END DO ;
ELSE
DO ;
print P#, PNAME, COLOR, WEIGHT, CITY ;
no := no + 1 ;
IF no = 3 THEN
DO ;
no := 0 ;
EXEC SQL DELETE FROM P WHERE
P.P# = :P# ;
END DO ;
END IF ;
END DO ;
END IF ;
END DO ;
EXEC SQL CLOSE CP ;
EXEC SQL COMMIT ;
previous_P# := :P# ;
END DO ;
• 3. Two transaction T and U have log record as follows:
<T,Start>,<T,A,10>,<U,Start>,<U,B,20>,<T,C,30>,<U,D,40>,<U,Commit>,<T,E,50>,<T,Com
mit>
Describe the action of the recovery manager, if there is a crash and the last log record
to appear on disk is:
a) <U,Start>
b) <U,Commit>
c) <T,E,50>
d) <T,Commit>
础基的复恢:从Log修改记录中没有数据项之前的值可判断这是Deffered Database
Modification. commit 才写入
——有没<Commit,T>可时复恢在此因,据数盘磁写改有没定必作操,的录记
会理不以
——有<Commit,T>要时复恢在此因,盘磁回写未还能可果结,的录记Redo
(a)作操无
(b)redo U;
中库据数将B为置值20,D为置值40
入写中志日在<T, abort>;;
(c)redo U
中库据数将B为置值20,D为置值40
入写中志日在<T, abort>;
(d)redo U , redo T
中库据数将A为置值10,B为置值20,C为置值30,D为置值40,E为置值50 。
•
4. Consider cache buffering of disk pages in memory under
the assumption that, to begin with, the cache buffer disk
pages are empty, and that the maximum number of pages
that can be held in cache is four. Assume in the history that
follows that each of the data items A, B, C, D, E, and F
accessed in the history lies on a distinct page.
H=R1(A,1)R2(B,2)W1(A,3)R3(C,4)W2(B,5)C2W3(C,6)R4(D,7)R5(E,
8)W5(E,9)R6(B,5)R6(A,3)R3(F,10)W3(F,11)W4(D,12)
(a)Name the first operation where an existing page in buffer
must be dropped in order that another page can be read in.
(b)Pages in buffer are called dirty if they have been updated in
buffer but not yet written back out to their place on disk. What
are the dirty pages in buffer at the time of the operation named
in (a).
(1) 如果事务提交后,其占用页面置为“空闲”
(a) R6(B,5);(b) A,C,E (C2提交事务2后B占页面重新被置空闲(
(2) 如果事务提交后,其占用页面不被置为“空闲”
(a) R5(E,8);(b) A,C
第十一次作业
• 15.3 给定事务T1、T2、T3、执行下列操作:
T1:将A加1
T2: 将A加倍
T3: 在屏幕上输出A,并将A置为1,其中A为
数据库中的某个数据项。
(a) 假设T1、T2、T3可以并发执行。若A初值
为0,那么存在多少种正确的可能结果?列
举之。
T1-T2-T3 A = 1;T1-T3-T2 A = 2;T2-T1-T3 A = 1;
T2-T3-T1 A = 2;T3-T1-T2 A = 4;T3-T2-T1 A = 3;
(b) 各个事务的内部结果如下表所示。若事务
执行不施加任何锁,则有多少种可能的调度?
T1
T2
T3
R1: RETRIEVE A
INTO t1;
t1 := t1 + 1;
U1: UPDTAE A
FROM t1;
R2: RETRIEVE A
INTO t2;
t2:= t2 * 2;
U2: UPDTAE A
FROM t2;
R3: RETRIEVE A
INTO t3;
display t3;
U3: UPDTAE A
FROM 1;
由事务的内部结构可知每个事务由Read和
Write两个操作,而事务内部的操作顺序不能
改变,故有6! / (2!*2!*2!) = 90种可能的调度。
(c) 在A的初值给定为0时,能够产生“正确”结
果但不可串行化的调度吗?
是。只要找到与6个可能的串行调度中的结果相
同即可。如调度R1-R2-R3-U3-U2-U1.当A初值不为
0时,得不到正确结果。
(d) 如果这三个事务都遵循2PL)两段锁(协议,
那么存在事实上可串行化但又不能形成的调度吗?
是。例如,调度R1-R3-U1-U3-R2-U2是可串行的
)等价于串行调度T1-T3-T2,其中虽然display t3
的值不一样,但两个调度数据库改变的状态是一
样的(但在T1、T2、T3都遵循2PL协议时就不能
出现。
• 15.4
tn时刻存在死锁,因为当前等待图)如下(有环。
•
2. Write procedures to process Lock-s(A), Lock-x(A), Unlock(A) operations in
share/exclusive locking system.
基于下面关系矩阵:
If a lock cannot be granted, the requesting transaction is made to wait till all
incompatible locks held by other transactions have been released. The lock is
then granted.
S
X
S
True
False
X
False
False
Lock_S(A){
if( xlock(A) == true ) then wait
else slock(A) = true;
}
Lock_X(A){
if( xlock(A) == true || slock(A) == true ) then wait
else xlock(A) = true;
}
Unlock(A){
Xlock(A) = false;
Slock(A) = false;
} (宏观来说Unlock还需知道目前S锁的个数,不过这里内部就不处理了(
• 3 State which of the following schedules are
serializable.
a. R1(x)r2(y)r1(z)r3(z)r2(x)r1(y)
可以,因为全是read操作;
b. R1(x)w2(y)r1(z)r3(z)w2(x)r1(y)
R1(x)在W2(x)之前,而R1(y)在W2(y)之后,
所以非冲突可串,其优先图:
T1
T2
T3
c. R1(x)w2(y)r1(z)r3(z)w1(x)r2(y)
可串,不存在冲突。
• 4 For each of the schedules of transactions T1, T2, and T3 below:
1) r1(A);r2(B);r3(C);w1(B);w2(C);w3(D);
2) r1(A);r2(B);r3(C);w1(B);w2(C);w3(A);
3) r1(A);r2(B);r3(C);r1(B);r2(C);r3(D);w1(C);w2(D);w3(E);
Do each of the following:
a) Insert shared and exclusive locks, and insert unlock actions. Place a
shared lock immediately in front of each read action that is not followed
by a write action of the same element by the same transaction. Place an
exclusive lock in front of every other read or write action. Place the
necessary unlock at the end of every transaction.
b) Tell what happens when each schedule is run by a scheduler that
supports shared and exclusive lock.
Key:
1a)
S1(A);r1(A);S2(B);r2(B);S3(C);r3(C);X1(B);w1(B);U1(A);U1(B);X2(C);w2(C);
U2(B);U2(C);X3(D);w3(D);U3(C);U3(D);
1b) 相当于串行调度:T3-T2-T1
2a)
S1(A);r1(A);S2(B);r2(B);S3(C);r3(C);X1(B);w1(B);U1(
A);U1(B);X2(C);w2(C);U2(B);U2(C);X3(A);w3(A);U3(C
);U3(A);
2b) 死锁
3a)
S1(A);r1(A);S2(B);r2(B);S3(C);r3(C);S1(B);r1(B);S2(C);
r2(C);S3(D);r3(D);X1(C);w1(C);U1(A);U1(B);U1(C);X2
(D);w2(D);U2(B);U2(C);U2(D);X3(E);w3(E);U3(C);U3(
D);U3(E);
3b) 相当于串行调度:T3-T2-T1
注意:X锁和S锁一般要保持到事务结束)commit
和rollback(
第十二次作业
• 16.1 见课后答案,注意格式正确.
常见错误:SQL与tutorial D语句混用!)c(不必使用视图
SQL语句格式
GRANT <privilege commlist>
ON < relvar name >
TO <user ID commlist>
[WITH GRANT OPTION] ;
合法的<privilege>有
USAGE/SELECT/INSERT/UPDATE/DELETE/REFERENCES
书中假定的Tutorial D语句形式授权定义格式:
AUTHORITY <authority name>
GRANT <privilege commalist>
ON <relvar name>
TO <user ID commalist> ;

similar documents