<!-- saved from url=(0054)http://infolab.stanford.edu/~ullman/dscbsols/sol8.html -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Database Systems: The Complete Book: Solutions for Chapter 8</title>
</head>
<body bgcolor="E0F7F0">
<center><table>
<tbody><tr><td><img src="./Database Systems_ The Complete Book_ Solutions for Chapter 8_files/logo.dbg.gif">
</td><td><table><tbody><tr><td align="MIDDLE"><font size="6">Database Systems: The Complete
Book</font>
</td></tr><tr><td align="MIDDLE"><font size="6">Solutions for Chapter 8</font></td></tr></tbody></table>
</td></tr></tbody></table></center>
Revised 11/4/01.
<p>
<a name="top"></a>
</p><p>
<a href="http://infolab.stanford.edu/~ullman/dscbsols/sol8.html#sol81">Solutions for Section 8.1</a><br>
<a href="http://infolab.stanford.edu/~ullman/dscbsols/sol8.html#sol82">Solutions for Section 8.2</a><br>
<a href="http://infolab.stanford.edu/~ullman/dscbsols/sol8.html#sol84">Solutions for Section 8.4</a><br>
<a href="http://infolab.stanford.edu/~ullman/dscbsols/sol8.html#sol85">Solutions for Section 8.5</a><br>
<a href="http://infolab.stanford.edu/~ullman/dscbsols/sol8.html#sol86">Solutions for Section 8.6</a><br>
<a href="http://infolab.stanford.edu/~ullman/dscbsols/sol8.html#sol87">Solutions for Section 8.7</a><br>
<a name="sol81"></a>
</p><h2>Solutions for Section 8.1</h2>
<h3>Exercise 8.1.1(a)</h3>
In the following, we use macro <tt>NO_MORE_TUPLES</tt> as defined in
the section.
<pre>void closestMatchPC() {
EXEC SQL BEGIN DECLARE SECTION;
char manf[30], SQLSTATE[6];
int targetPrice, /* holds price given by user */
speedOfClosest, modelOfClosest, priceOfClosest,
/* for closest price found so far */
tempSpeed, tempModel, tempPrice;
/* for tuple just read from PC */
EXEC SQL END DECLARE SECTION;
EXEC SQL DECLARE pcCursor CURSOR FOR
SELECT model, price, speed FROM PC;
EXEC SQL OPEN pcCursor;
/* ask user for target price and read the answer into variable
targetPrice */
/* Initially, the first PC is the closest to the target price.
If PC is empty, we cannot answer the question, and so abort. */
EXEC SQL FETCH FROM pcCursor INTO :modelOfClosest, :priceOfClosest,
:speedOfClosest;
if(NO_MORE_TUPLES) /* print message and exit */ ;
while(1) {
EXEC SQL FETCH pcCursor INTO :tempModel, :tempPrice,
:tempSpeed;
if(NO_MORE_TUPLES) break;
if( /* tempPrice closer to targetPrice than is priceOfClosest */) {
modelOfClosest = tempModel;
priceOfClosest = tempPrice;
speedOfClosest = tempSpeed;
}
}
/* Now, modelOfClosest is the model whose price is closest to
target. We must get its manufacturer with a single-row select */
EXEC SQL SELECT maker
INTO :manf
FROM Product
WHERE model = :modelOfClosest;
printf("manf = %s, model = %d, speed = %d\n", :manf, :modelOfClosest,
:speedOfClosest);
EXEC SQL CLOSE CURSOR pcCursor;
}
</pre>
<h3>Exercise 8.1.1(f)</h3>
To make sure that we don't change the price of ``new'' PC's, we have
only to make the cursor insensitive.<p>
Again, the macro <tt>NO_MORE_TUPLES</tt> is used to test for the end
of the relation.</p><p>
</p><pre>void lowerPrices() {
EXEC SQL BEGIN DECLARE SECTION;
char SQLSTATE[6];
EXEC SQL END DECLARE SECTION;
EXEC SQL DECLARE pcCursor INSENSITIVE CURSOR FOR PC;
EXEC SQL OPEN pcCursor;
while(1) {
EXEC SQL FETCH FROM pcCursor;
if(NO_MORE_TUPLES) break;
EXEC SQL UPDATE PC
SET price = price - 100
WHERE CURRENT OF execCursor;
}
EXEC SQL CLOSE pcCursor;
}
</pre>
<h3>Exercise 8.1.3</h3>
<pre>void twoMoreExpensive() {
EXEC SQL BEGIN DECLARE SECTION;
char SQLSTATE[6], cd[5], cd1[5];
int model, model1, speed, speed1, ram, ram1, price, price1;
float hd, hd1; /* we use the variables cd1, model1, etc, to
read the tuple 2 positions later, to see if the speed has
not changed (in which case there are at least two PC's
with the same speed and at least as high a price) */
EXEC SQL END DECLARE SECTION;
EXEC SQL DECLARE pcCursor SCROLL CURSOR FOR
SELECT * FROM PC ORDER BY speed, price;
EXEC SQL OPEN pcCursor;
while(1) {
EXEC SQL FETCH NEXT FROM pcCursor INTO :model, :speed, :ram,
:hd, :cd, :price;
if(NO_MORE_TUPLES) break;
EXEC SQL FETCH RELATIVE +2 FROM pcCursor INTO :model1, :speed1, :ram1,
:hd1, :cd1, :price1;
if(NO_MORE_TUPLES) break;
if(speed1 == speed) /* print the tuple :model, :speed, etc. */ ;
EXEC SQL FETCH RELATIVE -2 FROM pcCursor INTO :model1, :speed1, :ram1,
:hd1, :cd1, :price1;
/* line above just to reset the cursor to where it was */
}
EXEC SQL CLOSE pcCursor;
</pre>
<p>
<a href="http://infolab.stanford.edu/~ullman/dscbsols/sol8.html#top">Return to Top</a>
</p><p>
<a name="sol82"></a>
</p><h2>Solutions for Section 8.2</h2>
<h3>Exercise 8.2.1(a)</h3>
<pre>CREATE FUNCTION PresNetWorth(IN studioName CHAR[15])
DECLARE presNetWorth INT;
BEGIN
SELECT netWorth
INTO presNetWorth
FROM Studio, MovieExec
WHERE Studio.name = studioName AND presC# = cert#;
RETURN(presNetWorth);
END;
</pre>
<h3>Exercise 8.2.1(b)</h3>
<pre>CREATE FUNCTION status(IN person, IN addr)
DECLARE isStar INT;
DECLARE isExec INT;
BEGIN
SELECT COUNT(*)
INTO isStar
FROM MovieStar
WHERE MovieStar.name = person AND MovieStar.address = addr;
SELECT COUNT(*)
INTO isExec
FROM MovieExec
WHERE MovieExec.name = person AND MovieExec.address = addr;
IF isStar + isExec = 0 THEN RETURN(4)
ELSE RETURN(isStar + 2*isExec)
END IF;
END;
</pre>
<h3>Exercise 8.2.1(c)</h3>
<pre>CREATE PROCEDURE twoLongest(
IN studio CHAR(15),
OUT longest VARCHAR(255),
OUT second VARCHAR(255)
)
DECLARE longestLg INT;
DECLARE secondLg INT;
DECLARE t VARCHAR(255);
DECLARE l INT;
DECLARE Not_Found CONDITION FOR SQLSTATE = '02000';
DECLARE MovieCursor CURSOR FOR
SELECT title, length FROM Movie WHERE studioName = studio;
BEGIN
SET longest = NULL;
SET second = NULL;
SET longestLg = -1;
SET secondLg = -1;
OPEN MovieCursor;
mainLoop: LOOP
FETCH MovieCursor INTO t, l;
IF Not_Found THEN LEAVE mainLoop END IF;
IF l > longestLg THEN
SET secondLg = longestLg;
SET second = longest;
SET longestLg = l;
SET longest = t;
ELSIF l > secongLg THEN
SET secondLg = l;
SET second = t;
END IF;
END LOOP;
CLOSE MovieCursor;
END;
</pre>
<p>
In explanation, as we run through movies, we need to remember not only
the titles of the two longest movies seen so far, but their lengths.
That way, when we see a new title and length, fetched into the pair of
local variables <i>(t, l)</i>, we can compare the length <i>l</i> with
the two longest so far.
The body of the loop first asks if <i>l</i> is longer than the longest;
if so, the old longest becomes second, and the current movie becomes
longest.
If the current movie is not longest, then we next ask if it is longer
than the second longest, and we replace the latter, if so.
</p><h3>Exercise 8.2.2(a)</h3>
One can actually write a tricky SQL query that will retrieve the model
with the closest price, use this query in a single-row select, and
return the selected model number.
However, we can also scan the PC's and keep track of how close we have
co
没有合适的资源?快使用搜索试试~ 我知道了~
数据库系统基础教程习题答案(A First Courses in Database Systems Answers)
共9个文件
html:9个
36 下载量 93 浏览量
2016-10-08
11:58:04
上传
评论 1
收藏 31KB ZIP 举报
温馨提示
官方英文原版A First Courses in Database Systems Answers数据库系统基础教程习题答案 chapter2-10
资源推荐
资源详情
资源评论
收起资源包目录
A First Courses in Database Systems Answers数据库系统基础教程习题答案.zip (9个子文件)
A First Courses in Database Systems Answers数据库系统基础教程习题答案
A First Courses in Database Systems Chapter 6.html 12KB
A First Courses in Database Systems Chapter 3.html 14KB
A First Courses in Database Systems Chapter 9.html 7KB
A First Courses in Database Systems Chapter 5.html 12KB
A First Courses in Database Systems Chapter 7.html 6KB
A First Courses in Database Systems Chapter 8.html 15KB
A First Courses in Database Systems Chapter 2.html 5KB
A First Courses in Database Systems Chapter 4.html 12KB
A First Courses in Database Systems Chapter 10.html 8KB
共 9 条
- 1
资源评论
cquptzzq
- 粉丝: 2
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 课程设计-python爬虫-爬取日报,爬取日报文章后存储到本地,附带源代码+课程设计报告
- 软件和信息技术服务行业投资与前景预测.pptx
- 课程设计-基于SpringBoot + Mybatis+python爬虫NBA球员数据爬取可视化+源代码+文档+sql+效果图
- 软件品质管理系列二项目策划规范.doc
- 基于TensorFlow+PyQt+GUI的酒店评论情感分析,支持分析本地数据文件和网络爬取数据分析+源代码+文档说明+安装教程
- 软件定义无线电中的模拟电路测试技术.pptx
- 软件开发协议(作为技术开发合同附件).doc
- 软件开发和咨询行业技术趋势分析.pptx
- 软件测试题详解及答案.doc
- 软件漏洞生命周期管理策略.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功