数据库系统基础教程第一版数据库习题答案ch9.docx
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《数据库系统基础教程第一版数据库习题答案ch9.docx》由会员分享,可在线阅读,更多相关《数据库系统基础教程第一版数据库习题答案ch9.docx(60页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、a)In the following, we use macro NOT_FOUND as defined in the section.void closestMatchPC() EXEC SQL BEGIN DECLARE SECTION;char manf, SQLSTATE6;int targetPrice, / holds price given by user /float tempSpeed, speedOfClosest;char tempModel4, modelOfClosest4;int tempPrice, priceOfClosest;/* for tuple jus
2、t read from PC & closest price found so far */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
3、price.If PC is empty, we cannot answer the question, and so abort. /EXEC SQL FETCH FROM pcCursor INTO :modelOfClosestz :priceOfClosest, :speedOfClosest;if(NOT_FOUND) /* print message and exit */ ;while(1) EXEC SQL FETCH pcCursor INTO :tempModel, :tempPricez:tempSpeed;if (NOT FOUND) break;strcpy(maxF
4、irepowerClass, cclass);:%snH, maxFirepowerClass);printf ( nClass of maximum firpowrEXEC SQL CLOSE CURSOR cursorl;b)void getCountry() EXEC SQLEXEC SQLBEGIN DECLARE SECTION;charibattle20,iresult10,ocountry20;charstmtl200,stmt2200;EXEC SQLENDDECLARESECTION;strcpy(stmtl,SELECT COUNTRY FROMClasses CWHERE
5、 C.class IN (SELECT S.class FROM Ships SWHERE S.name IN (SELECT ship FROM OutcomesWHERE battle = ?) );Strcpy (stm2,SELECT country FROM ClassesWHERE class = ( SELECT MAX(COUNT(class)FROM Ships s, Outcomes oWHERE o.name = s.ship ANDEXECEXECSQLSQLPREPAREPREPAREquerylquery2FROM stmtl;FROM stmt2;EXECSQLD
6、ECLAREcursorlCURSOR FOR queryl;EXECSQLDECLAREcursor2CURSOR FOR query2;/* ask user for battle */* get countries of the ships involved in the battle */ EXEC SQL OPEN cursorl USING :ibattle;while(!NOT_FOUND) EXEC SQL FETCH cursorl INTO :ocountry;if (FOUND) printf (contry:%sn, ocoutry); )EXEC SQL CLOSE
7、CURSOR cursorl;/* get the country with the most ships sunk */ strcpy (iresult, sunk);EXEC SQL OPEN cursor2 USING :iresult;/* loop for the case theres the same max# of ships sunk */While(!NOT_FOUND) EXEC SQL FETCH cursor2 INTO :ocountry;If (FOUND)Printf (country with the most ships sunk: %s, ocountry
8、);)/* get the country with the most ships damaged */ strcpy (iresult, damaged);EXEC SQL OPEN cursor2 USING :iresult;* loop for the case theres the same max# of ships damaged */While(!NOT FOUND) EXEC SQL FETCH cursor2 INTO :ocountry;If (FOUND) Printf (country with the most ships damaged: %s, ocountry
9、);)c)void addShips() EXEC SQL BEGIN DECLARE SECTION;char iclass20z itype3z icontry20, iship20; int inumGuns, iborez idisplacement, ilaunched;char stmtl100, stmt2100;EXEC SQL END DECLARE SECTION;strcpy (stmtl, INSERT INTO Classes VALUES (?, ?, ?, ?, ?, ?);strcpy(stmt2, vINSERT INTO Ships VALUES (?, ?
10、, ?);/* ask user for a class and other info for Classes table */EXEC SQL EXECUTE IMMEDATE :stmtl USING :iclass, :itype, :icontry, :inumGuns, :ibore, :idisplacement;/ ask user for a ship and launched */WHILE(there_is_input)EXEC SQL EXECUTE IMMEDATE :stmt2 USING :iship, :iclass, ilaunched;/* ask user
11、for a ship and launched */)d)void findError () EXEC SQLBEGIN DECLARE SECTION;char bname20, bdate8, newbdate8;char sname20, 【year4, newlyear4; char stmtl100, stmt2100;EXEC SQL END DECLARE SECTION;strcpy (stmtl, UPDATE Battls SET date = ? WHERE name = ?);strcpy (stmt2, UPDATE Ships SET launched = ? WH
12、ERE name = ?)EXEC SQL DECLARE Cl CURSOR FORSelect b.name, b.date, s.name, s.launchedFROM Battles b, Outcomes o, Ships sWHERE b.name = o.battle ANDo.ship = s.name ANDYEAR(b.date) s.launched;EXEC SQL OPEN Cl;while(!NOT_FOUND) EXEC SQL FETCH Cl INTO :bname, :bdate, :sname, :lyear;/ prompt user and ask
13、if a change is needed */if(change battle)/ get a new battle date to newbdate /EXEC SQL EXECUTE IMMEDATE :stmtlUSING :bname, :newbdate; )if (change_ship) (/* get a new launched year to newlyear /EXEC SQL EXECUTE IMMEDATE :stmt2USING :sname, :newlyear; )a)CREATE FUNCTION PresNetWorth(studioName CHAR15
14、) RETURNS INTEGER DECLARE presNetWorth 工NT;BEGINSELECT netWorthINTO presNetWorthFROM Studio, MovieExecWHERE Studio.name = studioName AND presC# = cert#;RETURN(presNetWorth);END;b)CREATE FUNCTION status(person CHAR(30)z add工 CHAR(255) RETURNS INTEGERDECLARE isStar INT;DECLARE isExec INT;BEGINSELECT C
15、OUNT(*)INTO isStarFROM MovieStarWHERE MovieStar.name = person AND MovieStar.address = addr;SELECT COUNT(*)INTO isExecFROM MovieExecWHERE MovieExec.name = person AND MovieExec.address = addr;IF isStar + isExec = 0 THEN RETURN(4)ELSE RETURN(isStar + 2*isExec)END IF;END;c)CREATE PROCEDURE twoLongest( I
16、N studio CHAR(15),OUT longest VARCHAR(255),OUT second VARCHAR(255)DECLARE t VARCHAR(255);DECLARE i INT;DECLARE Not_Found CONDITION FOR SQLSTATE = 1 02000 1 ;DECLARE MovieCursor CURSOR FORSELECT title FROM Movies WHERE studioName = studio ORDER BY length DESC;BEGINSET longest = NULL;SET second = NULL
17、;OPEN MovieCursor;SET i = 0;mainLoop: WHILE (i 120 ANDtitle IN (SELECT movieTitle FROM StarslnWHERE starName = star); BEGINSET earliestYear = 0;OPEN MovieCursor;FETCH MovieCursor INTO earliestYear;CLOSE MovieCursor; END;CREATE PROCEDURE uniqueStar(IN addr CHAR(255),OUT star CHAR(30) )BEGINSET star =
18、 NULL;IF 1 = (SELECT COUNT(*) FROM MovieStar WHERE address = addr) THENSELECT name INTO star FROM MovieStar WHERE address = addr; END;CREATE PROCEDURE removeStar(IN star CHAR(30) )BEGINDELETE FROM Movies WHERE title INstar);(SELECT movieTitle FROM Starsln WHERE starName = DELETE FROM Starsln WHERE s
19、tarName = star;DELETE FROM MovieStar WHERE name = star;END;a)CREATE FUNCTION closestMatchPC(targetPrice 工NT) RETURNS CHARDECLARE closestModel CHAR(4);DECLARE diffSq 工NT;DECLARE currSq INT;DECLARE m CHAR(4);DECLARE p INT;DECLARE Not_Found CONDITION FOR SQLSTATE 1 020001;DECLARE PCCursor CURSOR FORSEL
20、ECT model, price FROM PC;BEGINSET closestModel = NULL;SET diffSq = -1;OPEN PCCursor;mainLoop: LOOPFETCH PCCursor INTO m, p;IF Not_Found THEN LEAVE mainLoop END IF;SET currSq = (p - targetPrice)*(p - targetPrice);IF diffSq = -1 OR diffSq currSqTHEN BEGINSET closestModel = m;SET diffSq = currSq;END IF
21、;END LOOP;CLOSE PDCursor;RETURN(closestModel);END;b)CREATE FUNCTION getPrice(imaker CHAR(l), imodel CHAR(4)RETURNS INTEGERIF ptype = pcTHENSELECT priceINTO pprice FROM PCWHERE model = imodel;ELSE IF ptypelaptop THENSELECT priceINTO pprice FROM LaptopWHERE model = imodel;ELSE IF ptypeprinter7 THENSEL
22、ECT priceINTO pprice FROM PrinterDECLARE ptype VARCHAR(IO);DECLARE pprice INT;DECLARE Not_Found CONDITION FOR SQLSTATE ,02000,BEGINSELECT type INTO ptype FROM Productimodel;imodel;WHERE maker = imaker AND modelWHERE model = imodel;ELSEpprice = NULL;END IF;RETURN (pprice);END;c)CREATE PROCEDURE addPC
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据库 系统 基础教程 第一版 习题 答案 ch9
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内