数据库课程第6章练习(03版)
- 格式:ppt
- 大小:252.50 KB
- 文档页数:32
第六章6.1.1Attributes must be separated by commas. Thus here B is an alias of A.6.1.2a)SELECT address AS Studio_AddressFROM StudioWHERE NAME = 'MGM';b)SELECT birthdate AS Star_BirthdateFROM MovieStarWHERE name = 'Sandra Bullock';c)SELECT starNameFROM StarsInWHERE movieYear = 1980OR movieTitle LIKE '%Love%';However, above query will also return words that have the substring Love e.g. Lover. Below query will only return movies that have title containing the word Love.SELECT starNameFROM StarsInWHERE movieYear = 1980OR movieTitle LIKE 'Love %'OR movieTitle LIKE '% Love %'OR movieTitle LIKE '% Love'OR movieTitle = 'Love';d)SELECT name AS Exec_NameFROM MovieExecWHERE netWorth >= 10000000;e)SELECT name AS Star_NameFROM movieStarWHERE gender = 'M'OR address LIKE '% Malibu %';6.1.3a)SELECT model,speed,hdFROM PCWHERE price < 1000 ;MODEL SPEED HD----- ---------- ------1002 2.10 2501003 1.42 801004 2.80 2501005 3.20 2501007 2.20 2001008 2.20 2501009 2.00 2501010 2.80 3001011 1.86 1601012 2.80 1601013 3.06 8011 record(s) selected.b)SELECT model ,speed AS gigahertz,hd AS gigabytesFROM PCWHERE price < 1000 ;MODEL GIGAHERTZ GIGABYTES ----- ---------- ---------1002 2.10 2501003 1.42 801004 2.80 2501005 3.20 2501007 2.20 2001008 2.20 2501009 2.00 2501010 2.80 3001011 1.86 1601012 2.80 1601013 3.06 8011 record(s) selected.c)SELECT makerFROM ProductWHERE TYPE = 'printer' ; MAKER-----DDEEEHH7 record(s) selected.d)SELECT model,ram ,screenFROM LaptopWHERE price > 1500 ;MODEL RAM SCREEN----- ------ -------2001 2048 20.12005 1024 17.02006 2048 15.42010 2048 15.44 record(s) selected.e)SELECT *FROM PrinterWHERE color ;MODEL CASE TYPE PRICE----- ----- -------- ------3001 TRUE ink-jet 993003 TRUE laser 9993004 TRUE ink-jet 1203006 TRUE ink-jet 1003007 TRUE laser 2005 record(s) selected.Note: Implementation of Boolean type is optional in SQL standard (feature ID T031). PostgreSQL has implementation similar to above example. Other DBMS provide equivalent support. E.g. In DB2 the column type can be declare as SMALLINT with CONSTRAINT that the value can be 0 or 1. The result can be returned as Boolean type CHAR using CASE.CREATE TABLE Printer(model CHAR(4) UNIQUE NOT NULL,color SMALLINT ,type VARCHAR(8) ,price SMALLINT ,CONSTRAINT Printer_ISCOLOR CHECK(color IN(0,1)));SELECT model,CASE colorWHEN 1THEN 'TRUE'WHEN 0THEN 'FALSE'ELSE 'ERROR'END CASE ,type,priceFROM PrinterWHERE color = 1;f)SELECT model,hdFROM PCWHERE speed = 3.2AND price < 2000;MODEL HD----- ------1005 2501006 3202 record(s) selected.6.1.4a)SELECT class,countryFROM ClassesWHERE numGuns >= 10 ;CLASS COUNTRY ------------------ ------------Tennessee USA1 record(s) selected.b)SELECT name AS shipName FROM ShipsWHERE launched < 1918 ;SHIPNAME------------------HarunaHieiKirishimaKongoRamilliesRenownRepulseResolutionRevengeRoyal OakRoyal Sovereign11 record(s) selected.c)SELECT ship AS shipName, battleFROM Outcomes WHERE result = 'sunk' ; SHIPNAME BATTLE ------------------ ------------------Arizona Pearl Harbor Bismark Denmark Strait Fuso Surigao Strait Hood Denmark Strait Kirishima Guadalcanal Scharnhorst North Cape Yamashiro Surigao Strait 7 record(s) selected.d)SELECT name AS shipName FROM ShipsWHERE name = class ;SHIPNAME------------------IowaKongoNorth CarolinaRenownRevengeYamato6 record(s) selected.e)SELECT name AS shipNameFROM ShipsWHERE name LIKE 'R%';SHIPNAME------------------RamilliesRenownRepulseResolutionRevengeRoyal OakRoyal Sovereign7 record(s) selected.Note: As mentioned in exercise 2.4.3, there are some dangling pointers and to retrieve all ships a UNION of Ships and Outcomes is required.Below query returns 8 rows including ship named Rodney.SELECT name AS shipNameFROM ShipsWHERE name LIKE 'R%'UNIONSELECT ship AS shipNameFROM OutcomesWHERE ship LIKE 'R%';f) Only using a filter like '% % %' will incorrectly match name such as ' a b 'since % can match any sequence of 0 or more characters.SELECT name AS shipNameFROM ShipsWHERE name LIKE '_% _% _%' ;SHIPNAME------------------0 record(s) selected.Note: As in (e), UNION with results from Outcomes.SELECT name AS shipNameFROM ShipsWHERE name LIKE '_% _% _%'UNIONSELECT ship AS shipNameFROM OutcomesWHERE ship LIKE '_% _% _%' ;SHIPNAME------------------Duke of YorkKing George VPrince of Wales3 record(s) selected.6.1.5a)The resulting expression is false when neither of (a=10) or (b=20) is TRUE.a = 10b = 20 a = 10 OR b = 20NULL TRUE TRUETRUE NULL TRUEFALSE TRUE TRUETRUE FALSE TRUETRUE TRUE TRUEb)The resulting expression is only TRUE when both (a=10) and (b=20) are TRUE.a = 10b = 20 a = 10 AND b = 20TRUE TRUE TRUEc)The expression is always TRUE unless a is NULL.a < 10 a >= 10 a = 10 ANDb = 20TRUE FALSE TRUEFALSE TRUE TRUEd)The expression is TRUE when a=b except when the values are NULL.a b a = bNOT NULL NOT NULL TRUE when a=b; else FALSEe)Like in (d), the expression is TRUE when a<=b except when the values are NULL.a b a <= bNOT NULL NOT NULL TRUE when a<=b; else FALSE6.1.6SELECT *FROM MoviesWHERE LENGTH IS NOT NULL;6.2.1a)SELECT AS starNameFROM MovieStar M,StarsIn SWHERE = S.starNameAND S.movieTitle = 'Titanic'AND M.gender = 'M';b)SELECT S.starNameFROM Movies M ,StarsIn S,Studios TWHERE ='MGM'AND M.year = 1995AND M.title = S.movieTitleAND M.studioName = ;c)SELECT AS presidentNameFROM MovieExec X,Studio TWHERE X.cert# = T.presC#AND = 'MGM';d)SELECT M1.titleFROM Movies M1,Movies M2WHERE M1.length > M2.lengthAND M2.title ='Gone With the Wind' ;e)SELECT AS execNameFROM MovieExec X1,MovieExec X2WHERE Worth > WorthAND = 'Merv Griffin' ;6.2.2a)SELECT R.maker AS manufacturer, L.speed AS gigahertzFROM Product R,Laptop LWHERE L.hd >= 30AND R.model = L.model ; MANUFACTURER GIGAHERTZ ------------ ----------A 2.00A 2.16A 2.00B 1.83E 2.00E 1.73E 1.80F 1.60F 1.60G 2.0010 record(s) selected.b)SELECT R.model,P.priceFROM Product R,PC PWHERE R.maker = 'B'AND R.model = P.model UNIONSELECT R.model,L.priceFROM Product R,Laptop LWHERE R.maker = 'B'AND R.model = L.model UNIONSELECT R.model,T.priceFROM Product R,Printer TWHERE R.maker = 'B'AND R.model = T.model ;----- ------1004 6491005 6301006 10492007 14294 record(s) selected.c)SELECT R.makerFROM Product R,Laptop LWHERE R.model = L.model EXCEPTSELECT R.makerFROM Product R,PC PWHERE R.model = P.model ; MAKER-----FG2 record(s) selected.d)SELECT DISTINCT P1.hd FROM PC P1,PC P2WHERE P1.hd =P2.hdAND P1.model > P2.model ; Alternate Answer:SELECT DISTINCT P.hdFROM PC PGROUP BY P.hdHAVING COUNT(P.model) >= 2 ; e)SELECT P1.model,P2.modelFROM PC P1,PC P2WHERE P1.speed = P2.speedAND P1.ram = P2.ramAND P1.model < P2.model ; MODEL MODEL----- -----1004 10121 record(s) selected.f)FROM(SELECT maker,R.modelFROM PC P,Product RWHERE SPEED >= 3.0AND P.model=R.modelUNIONSELECT maker,R.modelFROM Laptop L,Product RWHERE speed >= 3.0AND L.model=R.model) MGROUP BY M.makerHAVING COUNT(M.model) >= 2 ; MAKER-----B1 record(s) selected.6.2.3a)SELECT FROM Ships S,Classes CWHERE S.class = C.classAND C.displacement > 35000; NAME------------------IowaMissouriMusashiNew JerseyNorth CarolinaWashingtonWisconsinYamato8 record(s) selected.b)SELECT ,C.displacement,C.numGunsFROM Ships S ,Outcomes O,Classes CWHERE = O.shipAND S.class = C.classAND O.battle = 'Guadalcanal' ;NAME DISPLACEMENT NUMGUNS------------------ ------------ -------Kirishima 32000 8Washington 37000 92 record(s) selected.Note:South Dakota was also engaged in battle of Guadalcanal but not chosen since it is not in Ships table(Hence, no information regarding it's Class is available).c)SELECT name shipNameFROM ShipsUNIONSELECT ship shipNameFROM Outcomes ;SHIPNAME------------------ArizonaBismarkCaliforniaDuke of YorkFusoHarunaHieiHoodIowaKing George VKirishimaKongoMissouriMusashiNew JerseyNorth CarolinaPrince of WalesRamilliesRenownRepulseResolutionRevengeRodneyRoyal OakRoyal SovereignScharnhorstSouth DakotaTenneseeTennesseeWashingtonWest VirginiaWisconsinYamashiroYamato34 record(s) selected.d)SELECT C1.countryFROM Classes C1,Classes C2WHERE C1.country = C2.country AND C1.type = 'bb'AND C2.type = 'bc' ; COUNTRY------------Gt. BritainJapan2 record(s) selected.e)SELECT O1.shipFROM Outcomes O1,Battles B1WHERE O1.battle = AND O1.result = 'damaged'AND EXISTS(SELECT B2.dateFROM Outcomes O2,Battles B2WHERE O2.battle= AND O1.ship = O2.shipAND B1.date < B2.date) ;SHIP------------------0 record(s) selected.f)SELECT O.battleFROM Outcomes O,Ships S ,Classes CWHERE O.ship = AND S.class = C.class GROUP BY C.country,O.battleHAVING COUNT(O.ship) > 3;SELECT O.battleFROM Ships S ,Classes C,Outcomes OWHERE C.Class = S.classAND O.ship = GROUP BY C.country,O.battleHAVING COUNT(O.ship) >= 3;6.2.4Since tuple variables are not guaranteed to be unique, every relation Ri should be renamed using an alias. Every tuple variable should be qualified with the alias. Tuple variables for repeating relations will also be distinctly identified this way.Thus the query will be likeSELECT A1.COLL1,A1.COLL2,A2.COLL1,…FROM R1 A1,R2 A2,…,Rn AnWHERE A1.COLL1=A2.COLC2,…6.2.5Again, create a tuple variable for every Ri, i=1,2,...,nThat is, the FROM clause isFROM R1 A1, R2 A2,...,Rn An.Now, build the WHERE clause from C by replacing every reference to some attribute COL1 of Ri by Ai.COL1. In addition apply Natural Join i.e. add condition to check equality of common attribute names between Ri and Ri+1 for all i from 0 to n-1. Also, build the SELECT clause from list of attributes L by replacing every attribute COLj of Ri by Ai.COLj.6.3.1a)SELECT DISTINCT makerFROM ProductWHERE model IN(SELECT modelFROM PCWHERE speed >= 3.0);SELECT DISTINCT R.makerFROM Product RWHERE EXISTS(SELECT P.modelFROM PC PWHERE P.speed >= 3.0AND P.model =R.model);b)SELECT P1.modelFROM Printer P1WHERE P1.price >= ALL(SELECT P2.priceFROM Printer P2) ;SELECT P1.modelFROM Printer P1WHERE P1.price IN(SELECT MAX(P2.price)FROM Printer P2) ;c)SELECT L.modelFROM Laptop LWHERE L.speed < ANY(SELECT P.speedFROM PC P) ;SELECT L.modelFROM Laptop LWHERE EXISTS(SELECT P.speedFROM PC PWHERE P.speed >= L.speed ) ;d)SELECT modelFROM(SELECT model,priceFROM PCUNIONSELECT model,priceFROM LaptopUNIONSELECT model,priceFROM Printer) M1WHERE M1.price >= ALL (SELECT priceFROM PCUNIONSELECT priceFROM LaptopUNIONSELECT priceFROM Printer) ;(d) – contd --SELECT modelFROM(SELECT model,priceFROM PCUNIONSELECT model,priceFROM LaptopUNIONSELECT model,priceFROM Printer) M1WHERE M1.price IN(SELECT MAX(price)FROM(SELECT priceFROM PCUNIONSELECT priceFROM LaptopUNIONSELECT priceFROM Printer) M2) ;e)SELECT R.makerFROM Product R,Printer TWHERE R.model =T.model AND T.price <= ALL(SELECT MIN(price)FROM Printer);SELECT R.makerFROM Product R,Printer T1WHERE R.model =T1.model AND T1.price IN(SELECT MIN(T2.price) FROM Printer T2);f)SELECT R1.makerFROM Product R1,PC P1WHERE R1.model=P1.model AND P1.ram IN(SELECT MIN(ram)FROM PC)AND P1.speed >= ALL(SELECT P1.speedFROM Product R1,PC P1WHERE R1.model=P1.model AND P1.ram IN(SELECT MIN(ram)FROM PC));SELECT R1.makerFROM Product R1,PC P1WHERE R1.model=P1.modelAND P1.ram =(SELECT MIN(ram)FROM PC)AND P1.speed IN(SELECT MAX(P1.speed)FROM Product R1,PC P1WHERE R1.model=P1.model AND P1.ram IN(SELECT MIN(ram)FROM PC));6.3.2a)SELECT C.countryFROM Classes CWHERE numGuns IN(SELECT MAX(numGuns)FROM Classes);SELECT C.countryFROM Classes CWHERE numGuns >= ALL(SELECT numGunsFROM Classes);b)SELECT DISTINCT C.class FROM Classes C,Ships SWHERE C.class = S.classAND EXISTS(SELECT shipFROM Outcomes OWHERE O.result='sunk'AND O.ship = ) ;SELECT DISTINCT C.class FROM Classes C,Ships SWHERE C.class = S.classAND IN(SELECT shipFROM Outcomes OWHERE O.result='sunk' ) ;c)SELECT FROM Ships SWHERE S.class IN(SELECT classFROM Classes CWHERE bore=16) ;SELECT FROM Ships SWHERE EXISTS(SELECT classFROM Classes CWHERE bore =16AND C.class = S.class );d)SELECT O.battleFROM Outcomes O WHERE O.ship IN(SELECT nameFROM Ships SWHERE S.Class ='Kongo' );SELECT O.battleFROM Outcomes O WHERE EXISTS(SELECT nameFROM Ships SWHERE S.Class ='Kongo' AND = O.ship );e)SELECT FROM Ships S,Classes CWHERE S.Class = C.ClassAND numGuns >= ALL(SELECT numGunsFROM Ships S2,Classes C2WHERE S2.Class = C2.Class AND C2.bore = C.bore) ;SELECT FROM Ships S,Classes CWHERE S.Class = C.ClassAND numGuns IN(SELECT MAX(numGuns)FROM Ships S2,Classes C2WHERE S2.Class = C2.Class AND C2.bore = C.bore) ;Better answer;SELECT FROM Ships S,Classes CWHERE S.Class = C.ClassAND numGuns >= ALL(SELECT numGunsFROM Classes C2WHERE C2.bore = C.bore) ;SELECT FROM Ships S,Classes CWHERE S.Class = C.ClassAND numGuns IN(SELECT MAX(numGuns)FROM Classes C2WHERE C2.bore = C.bore) ;6.3.3SELECT titleFROM MoviesGROUP BY titleHAVING COUNT(title) > 1 ;6.3.4SELECT FROM Ships S,Classes CWHERE S.Class = C.Class ;Assumption: In R1 join R2, the rows of R2 are unique on the joining columns. SELECT COLL12,COLL13,COLL14FROM R1WHERE COLL12 IN(SELECT COL22FROM R2)AND COLL13 IN(SELECT COL33FROM R3)AND COLL14 IN(SELECT COL44FROM R4) ...6.3.5(a)SELECT ,S.addressFROM MovieStar S,MovieExec EWHERE S.gender ='F'AND Worth > 10000000AND = AND S.address = E.address ;Note: As mentioned previously in the book, the names of stars are unique. However no such restriction exists for executives. Thus, both name and address are required as join columns.Alternate solution:SELECT name,addressFROM MovieStarWHERE gender = 'F'AND (name, address) IN(SELECT name,addressFROM MovieExecWHERE netWorth > 10000000) ;(b)SELECT name,addressFROM MovieStarWHERE (name,address) NOT IN(SELECT name addressFROM MovieExec) ;6.3.6By replacing the column in subquery with a constant and using IN subquery for the constant, statement equivalent to EXISTS can be found.i.e. replace "WHERE EXISTS (SELECT C1 FROM R1..)" by "WHERE 1 IN (SELECT 1 FROM R1...)"Example:SELECT DISTINCT R.makerFROM Product RWHERE EXISTS(SELECT P.modelFROM PC PWHERE P.speed >= 3.0AND P.model =R.model) ;Above statement can be transformed to below statement.SELECT DISTINCT R.makerFROM Product RWHERE 1 IN(SELECT 1FROM PC PWHERE P.speed >= 3.0AND P.model =R.model) ;6.3.7(a)n*m tuples are returned where there are n studios and m executives. Each studio will appear m times; once for every exec.(b)There are no common attributes between StarsIn and MovieStar; hence no tuples are returned.(c)There will be at least one tuple corresponding to each star in MovieStar. The unemployed stars will appear once with null values for StarsIn. All employed stars will appear as many times as the number of movies they are working in. In other words, for each tuple in StarsIn(starName), the correspoding tuple from MovieStar(name)) is joined and returned. For tuples in MovieStar that do not have a corresponding entry in StarsIn, the MovieStar tuple is returned with null values for StarsIn columns.6.3.8Since model numbers are unique, a full natural outer join of PC, Laptop and Printer will return one row for each model. We want all information about PCs, Laptops and Printers even if the model does not appear in Product but vice versa is not true. Thus a left natural outer join between Product and result above is required. The type attribute from Product must be renamed since Printer has a type attribute as well and the two attributes are different.(SELECT maker,model,type AS productTypeFROM Product) RIGHT NATURAL OUTER JOIN ((PC FULL NATURAL OUTER JOIN Laptop) FULL NATURAL OUTER JOIN Printer); Alternately, the Product relation can be joined individually with each of PC,Laptop and Printer and the three results can be Unioned together. For attributes that do not exist in one relation, a constant such as 'NA' or 0.0 can be used. Below is an example of this approach using PC and Laptop.SELECT R.MAKER ,R.MODEL ,R.TYPE ,P.SPEED ,P.RAM ,P.HD ,0.0 AS SCREEN,P.PRICEFROM PRODUCT R,PC PWHERE R.MODEL = P.MODELUNIONSELECT R.MAKER ,R.MODEL ,R.TYPE ,L.SPEED ,L.RAM ,L.HD ,L.SCREEN,L.PRICEFROM PRODUCT R,LAPTOP LWHERE R.MODEL = L.MODEL;6.3.9SELECT *FROM Classes RIGHT NATURALOUTER JOIN Ships ;6.3.10SELECT *FROM Classes RIGHT NATURALOUTER JOIN ShipsUNION(SELECT C2.class ,C2.type ,C2.country ,C2.numguns ,C2.bore ,C2.displacement,C2.class NAME ,FROM Classes C2,Ships S2WHERE C2.Class NOT IN(SELECT ClassFROM Ships)) ;6.3.11(a)SELECT *FROM R,S ;(b)Let Attr consist ofAttrR = attributes unique to RAttrS = attributes unique to SAttrU = attributes common to R and SThus in Attr, attributes common to R and S are not repeated. SELECT AttrFROM R,SWHERE R.AttrU1 = S.AttrU1AND R.AttrU2 = S.AttrU2 ...AND R.AttrUi = S.AttrUi ;(c)SELECT *FROM R,SWHERE C ;6.4.1(a)DISTINCT keyword is not required here since each model only occurs once in PC relation. SELECT modelFROM PCWHERE speed >= 3.0 ;(b)SELECT DISTINCT R.makerFROM Product R,Laptop LWHERE R.model = L.modelAND L.hd > 100 ;(c)SELECT R.model,P.priceFROM Product R,PC PWHERE R.model = P.modelAND R.maker = 'B'UNIONSELECT R.model,L.priceFROM Product R,Laptop LWHERE R.model = L.modelAND R.maker = 'B'UNIONSELECT R.model,T.priceFROM Product R,Printer TWHERE R.model = T.modelAND R.maker = 'B' ;(d)SELECT modelFROM PrinterWHERE color=TRUEAND type ='laser' ;(e)SELECT DISTINCT R.makerFROM Product R,Laptop LWHERE R.model = L.modelAND R.maker NOT IN(SELECT R1.makerFROM Product R1,PC PWHERE R1.model = P.model) ;better:SELECT DISTINCT R.makerFROM Product RWHERE R.type = 'laptop'AND R.maker NOT IN(SELECT R.makerFROM Product RWHERE R.type = 'pc') ;(f)With GROUP BY hd, DISTINCT keyword is not required. SELECT hdFROM PCGROUP BY hdHAVING COUNT(hd) > 1 ;(g)SELECT P1.model,P2.modelFROM PC P1,PC P2WHERE P1.speed = P2.speedAND P1.ram = P2.ramAND P1.model < P2.model ;(h)SELECT R.makerFROM Product RWHERE R.model IN(SELECT P.modelFROM PC PWHERE P.speed >= 2.8)OR R.model IN(SELECT L.modelFROM Laptop LWHERE L.speed >= 2.8)GROUP BY R.makerHAVING COUNT(R.model) > 1 ;(i)After finding the maximum speed, an IN subquery can provide the manufacturer name. SELECT MAX(M.speed)FROM(SELECT speedFROM PCUNIONSELECT speedFROM Laptop) M ;SELECT R.makerFROM Product R,PC PWHERE R.model = P.modelAND P.speed IN(SELECT MAX(M.speed)FROM(SELECT speedFROM PCUNIONSELECT speedFROM Laptop) M)UNIONSELECT R2.makerFROM Product R2,Laptop LWHERE R2.model = L.modelAND L.speed IN(SELECT MAX(N.speed)FROM(SELECT speedFROM PCUNIONSELECT speedFROM Laptop) N) ;Alternately,SELECT COALESCE(MAX(P2.speed),MAX(L2.speed),0) SPEEDFROM PC P2FULL OUTER JOIN Laptop L2ON P2.speed = L2.speed ;SELECT R.makerFROM Product R,PC PWHERE R.model = P.modelAND P.speed IN(SELECT COALESCE(MAX(P2.speed),MAX(L2.speed),0) SPEED FROM PC P2FULL OUTER JOIN Laptop L2ON P2.speed = L2.speed)UNIONSELECT R2.makerFROM Product R2,Laptop LWHERE R2.model = L.modelAND L.speed IN(SELECT COALESCE(MAX(P2.speed),MAX(L2.speed),0) SPEED FROM PC P2FULL OUTER JOIN Laptop L2ON P2.speed = L2.speed)(j)SELECT R.makerFROM Product R,PC PWHERE R.model = P.modelGROUP BY R.makerHAVING COUNT(DISTINCT speed) >= 3 ;(k)SELECT R.makerFROM Product R,PC PWHERE R.model = P.modelGROUP BY R.makerHAVING COUNT(R.model) = 3 ;better;SELECT R.makerFROM Product RWHERE R.type='pc'GROUP BY R.makerHAVING COUNT(R.model) = 3 ;6.4.2(a)We can assume that class is unique in Classes and DISTINCT keyword is not required.SELECT class,countryFROM ClassesWHERE bore >= 16 ;(b)Ship names are not unique (In absence of hull codes, year of launch can help distinguish ships).SELECT DISTINCT name AS Ship_NameFROM ShipsWHERE launched < 1921 ;(c)SELECT DISTINCT ship AS Ship_NameFROM OutcomesWHERE battle = 'Denmark Strait'AND result = 'sunk' ;(d)SELECT DISTINCT AS Ship_NameFROM Ships S,Classes CWHERE S.class = C.classAND C.displacement > 35000 ;(e)SELECT DISTINCT O.ship AS Ship_Name,C.displacement ,C.numGunsFROM Classes C ,Outcomes O,Ships SWHERE C.class = S.classAND = O.shipAND O.battle = 'Guadalcanal' ;SHIP_NAME DISPLACEMENT NUMGUNS------------------ ------------ -------Kirishima 32000 8Washington 37000 92 record(s) selected.Note: South Dakota was also in Guadalcanal but its class information is not available. Below query will return name of all ships that were in Guadalcanal even if no other information is available (shown as NULL). The above query is modified from INNER joins to LEFT OUTER joins.SELECT DISTINCT O.ship AS Ship_Name,C.displacement ,C.numGunsFROM Outcomes OLEFT JOIN Ships SON = O.shipLEFT JOIN Classes CON C.class = S.classWHERE O.battle = 'Guadalcanal' ;SHIP_NAME DISPLACEMENT NUMGUNS------------------ ------------ -------Kirishima 32000 8South Dakota - -Washington 37000 93 record(s) selected.(f)The Set opearator UNION guarantees unique results.SELECT ship AS Ship_NameFROM OutcomesUNIONSELECT name AS Ship_NameFROM Ships ;(g)SELECT C.classFROM Classes C,Ships SWHERE C.class = S.classGROUP BY C.classHAVING COUNT() = 1 ;better:SELECT S.classFROM Ships SGROUP BY S.classHAVING COUNT() = 1 ;(h)The Set opearator INTERSECT guarantees unique results.SELECT C.countryFROM Classes CWHERE C.type='bb'INTERSECTSELECT C2.countryFROM Classes C2WHERE C2.type='bc' ;However, above query does not account for classes without any ships belonging to them. SELECT C.countryFROM Classes C,Ships SWHERE C.class = S.classAND C.type ='bb'INTERSECTSELECT C2.countryFROM Classes C2,Ships S2WHERE C2.class = S2.classAND C2.type ='bc' ;(i)SELECT O2.ship AS Ship_Name FROM Outcomes O2,Battles B2WHERE O2.battle = AND B2.date > ANY(SELECT B.dateFROM Outcomes O,Battles BWHERE O.battle = AND O.result ='damaged'AND O.ship = O2.ship);6.4.3a)SELECT DISTINCT R.maker FROM Product R,PC PWHERE R.model = P.modelAND P.speed >= 3.0;b)Models are unique.SELECT P1.modelFROM Printer P1LEFT OUTER JOIN Printer P2 ON (P1.price < P2.price) WHERE P2.model IS NULL ;c)SELECT DISTINCT L.model FROM Laptop L,PC PWHERE L.speed < P.speed ;d)Due to set operator UNION, unique results are returned.It is difficult to completely avoid a subquery here. One option is to use Views. CREATE VIEW AllProduct ASSELECT model,priceFROM PCUNIONSELECT model,priceFROM LaptopUNIONSELECT model,priceFROM Printer ;SELECT A1.modelFROM AllProduct A1LEFT OUTER JOIN AllProduct A2ON (A1.price < A2.price)WHERE A2.model IS NULL ;But if we replace the View, the query contains a FROM subquery. SELECT A1.modelFROM(SELECT model,priceFROM PCUNIONSELECT model,priceFROM LaptopUNIONSELECT model,priceFROM Printer) A1LEFT OUTER JOIN(SELECT model,priceFROM PCUNIONSELECT model,priceFROM LaptopUNIONSELECT model,priceFROM Printer) A2ON (A1.price < A2.price) WHERE A2.model IS NULL ;e)SELECT DISTINCT R.makerFROM Product R,Printer TWHERE R.model =T.modelAND T.price <= ALL(SELECT MIN(price)FROM Printer);f)SELECT DISTINCT R1.maker FROM Product R1,PC P1WHERE R1.model=P1.modelAND P1.ram IN(SELECT MIN(ram)FROM PC)AND P1.speed >= ALL(SELECT P1.speedFROM Product R1,PC P1WHERE R1.model=P1.modelAND P1.ram IN(SELECT MIN(ram)FROM PC));6.4.4a)SELECT DISTINCT C1.country FROM Classes C1LEFT OUTER JOIN Classes C2 ON (C1.numGuns < C2.numGuns) WHERE C2.country IS NULL ;。
第六章习题一、选择题:形框代替形框表示实体的属性。
1.在数据库设计中,用E-R图来描述信息结构但不涉及信息在计算机中的表示,它是数据库设计的____阶段。
A.需求分析B.概念设计C.逻辑设计D.物理设计答案:B2.E-R图是数据库设计的工具之一,它适用于建立数据库的____。
A.概念模型B.逻辑模型C.结构模型D.物理模型答案:A3.在关系数据库设计中,设计关系模式是____的任务。
A.需求分析阶段B.概念设计阶段C.逻辑设计阶段D.物理设计阶段答案:C4.数据库物理设计完成后,进入数据库实施阶段,下列各项中不属于实施阶段的工作是____。
A.建立库结构B.扩充功能C.加载数据D.系统调试答案:B5.数据库概念设计的E-R方法中,用属性描述实体的特征,属性在E-R图中,用____表示。
A.矩形B.四边形C.菱形D.椭圆形答案:D6.在数据库的概念设计中,最常用的数据模型是____。
A形象模型B.物理模型C.逻辑模型D.实体联系模型答案:D7.在数据库设计中,在概念设计阶段可用E-R方法,其设计出的图称为____。
A.实物示意图B.实用概念图C.实体表示图D.实体联系图答案:D8.从E-R模型关系向关系模型转换时,一个M:N联系转换为关系模式时,该关系模式的关键字是____。
A.M端实体的关键字B.N端实体的关键字C.M端实体关键字与N端实体关键字组合D.重新选取其他属性答案:C9.当局部E-R图合并成全局E-R图时可能出现冲突,不属于合并冲突的是____。
A.属性冲突B.语法冲突C.结构冲突D.命名冲突答案:B10.E-R图中的主要元素是①、②和属性。
A.记录型B.结点C.实体型D.表E.文件F.联系G.有向边答案:①C ②F11.数据库逻辑设计的主要任务是____。
A.建立E-R图和说明书B.创建数据库说明C.建立数据流图D.把数据送入数据库答案:B12.E-R图中的联系可以与____实体有关。
第1章数据库概论1.1 基本内容分析1.1.1 本章的重要概念(1)DB、DBMS和DBS的定义(2)数据管理技术的发展阶段人工管理阶段、文件系统阶段、数据库系统阶段和高级数据库技术阶段等各阶段的特点。
(3)数据描述概念设计、逻辑设计和物理设计等各阶段中数据描述的术语,概念设计中实体间二元联系的描述(1:1,1:N,M:N)。
(4)数据模型数据模型的定义,两类数据模型,逻辑模型的形式定义,ER模型,层次模型、网状模型、关系模型和面向对象模型的数据结构以及联系的实现方式。
(5)DB的体系结构三级结构,两级映像,两级数据独立性,体系结构各个层次中记录的联系。
(6)DBMSDBMS的工作模式、主要功能和模块组成。
(7)DBSDBS的组成,DBA,DBS的全局结构,DBS结构的分类。
1.1.2本章的重点篇幅(1)教材P23的图1.24(四种逻辑数据模型的比较)。
(2)教材P25的图1.27(DB的体系结构)。
(3)教材P28的图1.29(DBMS的工作模式)。
(4)教材P33的图1.31(DBS的全局结构)。
1.2 教材中习题1的解答1.1 名词解释·逻辑数据:指程序员或用户用以操作的数据形式。
·物理数据:指存储设备上存储的数据。
·联系的元数:与一个联系有关的实体集个数,称为联系的元数。
·1:1联系:如果实体集E1中每个实体至多和实体集E2中的一个实体有联系,反之亦然,那么E1和E2的联系称为“1:1联系”。
·1:N联系:如果实体集E1中每个实体可以与实体集E2中任意个(零个或多个)实体有联系,而E2中每个实体至多和E1中一个实体有联系,那么E1和E2的联系是“1:N联系”。
·M:N联系:如果实体集E1中每个实体可以与实体集E2中任意个(零个或多个)实体有联系,反之亦然,那么E1和E2的联系称为“M:N联系”。
·数据模型:能表示实体类型及实体间联系的模型称为“数据模型”。
第六章习题一、选择题:1.关系规范化中的删除操作异常是指①,插入操作异常是指②。
A.不该删除的数据被删除B.不该插入的数据被插入C.应该删除的数据未被删除D.应该插入的数据未被插入答案:①A ②D2.设计性能较优的关系模式称为规范化,规范化主要的理论依据是____。
A.关系规范化理论B.关系运算理论C.关系代数理论D.数理逻辑答案:A3.规范化理论是关系数据库进行逻辑设计的理论依据。
根据这个理论,关系数据库中的关系必须满足:其每一属性都是____。
A.互不相关的B.不可分解的C.长度可变的D.互相关联的答案:B4.关系数据库规范化是为解决关系数据库中____问题而引人的。
A.插入、删除和数据冗余B.提高查询速度C.减少数据操作的复杂性D.保证数据的安全性和完整性答案:A5.规范化过程主要为克服数据库逻辑结构中的插入异常,删除异常以及____的缺陷。
A.数据的不一致性B.结构不合理C.冗余度大D.数据丢失答案:C6.当关系模式R(A,B)已属于3NF,下列说法中____是正确的。
A.它一定消除了插入和删除异常B.仍存在一定的插入和删除异常C.一定属于BCNF D.A和C都是答案:B7.关系模型中的关系模式至少是____。
A.1NF B.2NF C.3NF D.BCNF答案:A8.在关系DB中,任何工元关系模式的最高范式必定是____。
A.1NF B.2NF C.3NF D.BCNF答案:D9.在关系模式R中,若其函数依赖集中所有候选关键宇都是决定因素,则R最高范式是____。
A.2NF B.3NF C.4 NF D.BCNF答案:C10.当B属性函数依赖于A属性时,属性A与B的联系是____。
A.1对多B.多对1 C.多对多D.以上都不是答案:B11.在关系模式中,如果属性A和B存在1对1的联系,则说____。
A.A→B B.B→A C.A↔B D.以上都不是答案:C12.候选码中的属性称为____。
练习题66.1 名词解释1)事务:事务是构成单一逻辑工作单元的操作集合。
要么完整地执行,要么完全不执行。
2)数据库的一致性:一个事务独立执行的结果,应保持数据库的一致性,即数据不会因事务的执行而遭受破坏。
3)数据库的可恢复性:系统能把数据库从被破坏、不确定的状态,恢复到最近一个正确的状态,DBMS的这种能力称为数据库的可恢复性(Recovery)4)并发操作:在多用户共享系统中,许多事务可能同时对同一数据进行操作,这种操作称为并发操作。
5)封锁:封锁是系统保证对数据项的访问以互斥方式进行的一种手段。
6)X锁:事务T对某数据加了X锁后,其他事务要等T解除X锁后,才能对这个数据进行封锁。
7)S锁:事务T对某数据加了S锁后,仍允许其他事务再对该数据加S锁,但在对该数据的所有S锁都解除之前决不允许任何事务对该数据加X锁。
8)调度:事务的执行次序称为“调度”。
9)串行调度:多个事务依次执行,称为事务的串行调度。
10)并发调度:利用分时的方法,同时处理多个事务,则称为事务的并发调度。
11)可串行化调度:如果一个并发调度的执行结果与某一串行调度的执行结果等价,那么这个并发调度称为“可串行化的调度”。
12)不可串行化调度:如果一个并发调度的执行结果不与某一串行调度的执行结果等价,那么这个并发调度称为“不可串行化调度”。
6.2事务的COMMIT语句和ROLLBACK语句各做什么事情?答:COMMIT(提交):语句表示事务执行成功地结束,此时告诉系统,DB要进入一个新的正确状态,该事务对DB的所有更新都已交付实施(写入磁盘)。
ROLLBACK(“回退”或“回滚”):语句表示事务执行不成功地结束,此时告诉系统,已发生错误,DB可能处在不正确的状态,该事务对DB的所有更新必须被撤消,DB应恢复该事务到初始状态。
6.3试叙述事务的四个性质,并解释每一个性质由DBMS的哪个子系统实现?每一个性质对DBS有什么益处?答:①事务的原子性:是指一个事务对DB的所有操作,是一个不可分割的工作单元。
例题――第六章例6-1 分别显示Sales数据库中的员工表employee、商品表goods、销售表sell_order 表和部门表department中的所有记录。
SELECT * FROM employeeSELECT * FROM goodsSELECT * FROM sell_orderSELECT * FROM department例6-2 显示employee表中全部员工的姓名和年龄,去掉重名。
SELECT DISTINCT employee_name AS 姓名,YEAR(GETDATE())-YEAR(birth_date) AS 年龄 FROM employee例6-3 对employee表,分别查询公司的员工总数和公司员工的平均收入。
SELECT COUNT(*) AS 总数 FROM employeeSELECT AVG(wages) AS 平均收入 FROM employee例6-4 对employee表,列出月工资在2000以上的员工记录。
SELECT * FROM employee WHERE wages>2000例6-5 对employee表,求出男员工的平均工资。
SELECT AVG(wages) as 平均工资 FROM employee WHERE sex='男‘例6-6 对employee表,列出市场部和销售部的员工名单。
SELECT d.department_name, e.employee_nameFROM employee e INNER JOIN department d ON e.department_id = d.department_id WHERE d.department_name IN ('市场部', '销售部')语句中的WHERE子句还有等价的形式:WHERE (d.department_name = '市场部') OR (d.department_name = '销售部')例6-7 对employee表,列出月工资在2000到3000之间的员工名单。
1.选择题(1)(C)允许用户定义一组操作,这些操作通过对指定的表进行删除、插入和更新命令来执行或触发。
A.存储过程B.视图C.触发器D.索引(2)SQL Server为每个触发器创建了两个临时表,它们是( B )。
A.Updated和Deleted B.Inserted和DeletedC.Inserted和Updated D.Seleted和Inserted(3)SQL Server中存储过程由一组预先定义并被(C)的Transact-SQL语句组成。
A.编写B.解释C.编译D.保存(4)下列可以查看表的行数以及表使用的存储空间信息的系统存储过程是( A )。
A.sq_spaceused B.sq_dependsC.sq_help D.sq_rename2.填空题(1)存储过程(stored procedure)是SQL Server服务器中(一组预编译的)Transact-SQL 语句的集合。
(2)SQL Server中的存储过程具有如下(的系统存储过程、用户自定义存储过程、临时存储过程、远程存储过程、扩展存储过程5 )种类型。
(3)创建存储过程实际是对存储过程进行定义的过程,主要包含存储过程名称及其(参数)和存储过程的主体两部分。
(4)触发器主要用于强制复杂的(业务规则)或要求。
(5)当某个表被删除后,该表上的(所有触发器)将自动被删除。
3.问答题(1)使用存储过程的主要优点有哪些?答:存储过程的主要优点有:- 实现模块化编程,一个存储过程可以被多个用户共享和重用。
- 可以加快程序的运行速度。
- 可以减少网络的流量。
- 可以提高数据库的安全性(2)存储过程分哪两类?各有何特点?答:存储过程最常见的主要分为系统存储过程和用户自定义存储过程两类,此外还有临时存储过程、远程存储过程和扩展存储过程:- 系统存储过程由系统自动创建,主要存储在master数据库中,一般以sp_为前缀,系统存储过程完成的功能主要是从系统表中获取信息,通过系统存储过程,SQL Server中的许多管理性或信息性的活动都可以被顺利地完成。
第6章关系数据理论一.选择题1.对关系模式进行规范化的主要目的是BA.提高数据操作效率B.维护数据的一致性C.加强数据的安全性D.为用户提供更快捷的数据操作2.关系模式中的插入异常是指DA.插入的数据违反了实体完整性约束B.插入的数据违反了用户定义的完整性约束C.插入了不该插入的数据D.应该被插入的数据不能被插入3.如果有函数依赖X→Y,并且对X的任意真子集X’,都有X’Y,则称C A.X完全函数依赖于Y B.X部分函数依赖于YC.Y完全函数依赖于X D.Y部分函数依赖于X4.如果有函数依赖X→Y,并且对X的某个真子集X’,有X’→Y成立,则称B A.Y完全函数依赖于X B.Y部分函数依赖于XC.X完全函数依赖于Y D.X部分函数依赖于Y5.若X→Y和Y→Z在关系模式R上成立,则X→Z在R上也成立。
该推理规则称为C A.自反规则B.增广规则C.传递规则D.伪传递规则6.若关系模式R中属性A仅出现在函数依赖的左部,则A为AA.L类属性B.R类属性C.N类属性D.LR类属性7.若关系模式R中属性A是N类属性,则A DA.一定不包含在R任何候选码中B.可能包含也可能不包含在R的候选码中C.一定包含在R的某个候选码中D.一定包含在R的任何候选码中8.设F是某关系模式的极小函数依赖集。
下列关于F的说法,错误的是B A.F中每个函数依赖的右部都必须是单个属性B.F中每个函数依赖的左部都必须是单个属性C.F中不能有冗余的函数依赖D.F中每个函数依赖的左部不能有冗余属性9.有关系模式:学生(学号,姓名,所在系,系主任),设一个系只有一个系主任,则该关系模式至少属于BA.第一范式B.第二范式C.第三范式D.BC范式10.设有关系模式R(X, Y, Z),其F={Y→Z, Y→X, X→YZ},则该关系模式至少属于DA.第一范式B.第二范式C.第三范式D.BC范式11.下列关于关系模式与范式的说法,错误的是DA.任何一个只包含两个属性的关系模式一定属于3NFB.任何一个只包含两个属性的关系模式一定属于BCNFC.任何一个只包含两个属性的关系模式一定属于2NFD.任何一个只包含三个属性的关系模式一定属于3NF12.有关系模式:借书(书号,书名,库存量,读者号,借书日期,还书日期),设一个读者可以多次借阅同一本书,但对一种书(用书号唯一标识)不能同时借多本。
第6章关系数据理论1. 理解下列术语的定义:函数依赖、部分函数依赖、完全函数依赖、传递依赖、候选码、码、主码、外码、全码、1NF 、2NF 、3NF 、BCNF 、4NF 。
解:函数依赖:设R (U)是属性集U 上的关系模式。
X ,Y 是U 的子集,若对于R(U)的任意一个可能的关系r ,r 中不可能存在两个元组在X 上的属性相等,而在Y 上的属性值不等,则称X 函数确定Y 或Y 函数依赖于X ,记作X→Y 。
部分函数依赖:如果X→Y ,但Y 不完全函数依赖于X ,则称Y 对X 部分函数依赖(Partial Functional Dependency ),记作:X Y 。
P→完全函数依赖:如果X→Y ,但对于X 中的任意一个真子集,都有Y 不依赖于,X 'X ',则称Y 对X 完全函数依赖(Full Functional Dependency ),记作:X Y 。
即X '↛Y F→传递依赖:如果X→Y ,Y→Z ,且Y X ,,则称Z 对X 传递函数依赖⊈Y↛X (Transitive Functional Dependency )。
记作:X Z 。
T→候选码:若关系中的某一属性组的值能惟一地标识一个元组,则称该属性组为候选码(Candidate key)。
主码:若一个关系有多个候选码,则选定其中一个为主码(Primarykey) 。
外部码:设F 是基本关系R 的一个或一组属性,但不是关系R 的码,如果F 与基本关系S 的主码K s 相对应,则称F 是基本关系R 的外部码(Foreign key),简称外码。
全码:最复杂的情况,整个属性组是码,称为全码(All-key )。
1NF:若一个关系模式R 中每一个属性值都是一个不可再分的最小数据单元,则称关系模式满足第一范式,记作R 1NF 。
∈2NF :若R 1NF ,且每一个非主属性完全函数依赖于R 的码,则R 2NF 。
∈∈3NF :关系模式R<U ,F>中若不存在这样的候选码X ,属性组Y 及非主属性Z(Z Y)⊈使得X Y ,Y Z 成立,则称R<U ,F>3NF 。