数据库系统基础教程
Sybase数据库教程
数据定义语言(DDL)
包括CREATE、ALTER、DROP等语句,用 于定义和管理数据库对象。
数据控制语言(DCL)
包括GRANT、REVOKE等语句,用于控制 对数据库对象的访问权限。
查询优化策略分享
索引优化
合理使用索引可以大大提 高查询效率,包括聚集索 引和非聚集索引。
分区技术
将大表分成若干个小表, 可以提高查询和维护的效 率。
创建和删除数据库对象
创建数据库
使用`CREATE DATABASE`语句,指 定数据库名称、大小、增长参数等。
删除数据库
使用`DROP DATABASE`语句,注意 要谨慎操作,避免误删重要数据。
创建表
使用`CREATE TABLE`语句,定义表 结构、字段类型、约束等。
删除表
使用`DROP TABLE`语句,可以删除 整个表及其所有数据。
通过执行计划分析SQL语句的执行效率,找出可能的性能瓶颈,如 缺少索引、不必要的表扫描等。
数据库日志分析
定期检查数据库日志,了解数据库的运行状况,发现潜在的性能问 题。
优化数据库设计
规范化设计
通过数据库规范化设计,消除数据冗余,提高数据一 致性和完整性。
索引优化
根据查询需求合理创建索引,避免全表扫描,提高查 询效率。
能优化建议,提供索引、分区等优化方案。
第三方性能调优工具
03
根据需要选择适合的第三方性能调优工具进行更深入的性能分
析和优化。
THANKS
感谢观看
别进行优化,提高系统性能。
安全性
Sybase数据库支持多种操作系统 和硬件平台,提供了丰富的API 和开发工具,方便用户进行二次 开发。
数据库系统基础教程课后答案第四章
4.2.7 In below figure there exists a many-to-one relationship between Babies and Births and another many-to-one relationship between Births and Mothers. From transitivity of relationships, there is a many-to-one relationship between Babies and Mothers. Hence a baby has a unique mother while a birth can allow more than one baby.
4.1.7
4.1.8 a)
(b)
4.1.9
Assumptions A Professor only works in at most one department. A course has at most one TA. A course is only taught by one professor and offered by one department. Students and professors have been assigned unique email ids. A course is uniquely identified by the course no, section no, and semester (e.g. cs157-3 spring 09).
4.2.4 The entity sets should have single attribute. a) Stars: starName b) Movies: movieName c) Studios: studioName. However there exists a many-to-many relationship between Studios and Contracts. Hence, in addition, we need more information about studios involved. If a contract always involves two studios, two attributes such as producingStudio and starStudio can replace the Studios entity set. If a contact can be associated with at most five studios, it may be possible to replace the Studios entity set by five attributes viz. studio1, studio2, studio3, studio4, and studio5. Alternately, a composite attribute containing concatenation of all studio names in a contact can be considered. A separator character such as "$" can be used. SQL allows searching of such an attribute using query like '%keyword%'
数据库系统基础教程第六章答案
Solutions Chapter 6Attributes 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 %';a)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 IDT031). 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 OutcomesWHERE 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 shipNameFROM 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 TRUEThe 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 = ;SELECT AS presidentName FROM 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.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 ; MODEL PRICE----- ------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.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.speed AND P1.ram = P2.ramAND P1.model < P2.model ; MODEL MODEL----- -----1004 10121 record(s) selected.f)SELECT M.makerFROM(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 isavailable).SELECT name shipName FROM ShipsUNIONSELECT ship shipName FROM Outcomes ; SHIPNAME------------------ArizonaBismarkCaliforniaDuke of YorkFusoHarunaHieiHoodIowaKing George VKirishimaKongoMissouriMusashiNew JerseyNorth CarolinaPrince of Wales RamilliesRenownRepulseResolutionRevengeRodneyRoyal OakRoyal Sovereign ScharnhorstSouth DakotaTenneseeTennesseeWashingtonWest VirginiaWisconsinYamashiroYamato34 record(s) selected.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 distinctlyidentified this way.Thus the query will be likeSELECT A1.COLL1,A1.COLL2,A2.COLL1,…FROM R1 A1,R2 A2,…,Rn AnWH ERE 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);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 ) ;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.modelAND 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 );SELECT O.battleFROM Outcomes OWHERE O.ship IN(SELECT nameFROM Ships SWHERE S.Class ='Kongo' );SELECT O.battleFROM Outcomes OWHERE EXISTS(SELECT nameFROM Ships SWHERE S.Class ='Kongo' AND = O.ship );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 forthe 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 studiowill appear m times; once for every exec.(b)There are no common attributes between StarsIn and MovieStar; hence no tuplesare returned.(c)There will be at least one tuple corresponding to each star in MovieStar. Theunemployed stars will appear once with null values for StarsIn. All employedstars will appear as many times as the number of movies they are working in. Inother words, for each tuple in StarsIn(starName), the correspoding tuple fromMovieStar(name)) is joined and returned. For tuples in MovieStar that do nothave a corresponding entry in StarsIn, the MovieStar tuple is returned with nullvalues for StarsIn columns.6.3.8Since model numbers are unique, a full natural outer join of PC, Laptop andPrinter 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 versais not true. Thus a left natural outer join between Product and result above isrequired. The type attribute from Product must be renamed since Printer has atype 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 ofPC,Laptop and Printer and the three results can be Unioned together. Forattributes that do not exist in one relation, a constant such as 'NA' or 0.0 canbe 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' ;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 ;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) SPEED FROM 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)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 ;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' ;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 ;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.makerFROM 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.countryFROM Classes C1LEFT OUTER JOIN Classes C2 ON (C1.numGuns < C2.numGuns) WHERE C2.country IS NULL ;。
数据库系统基础教程课后答案第五章
Exercise 5.1.1 As a set:Average = 2.37 As a bag:Average = 2.48 Exercise 5.1.2 As a set:Average = 218 As a bag:Average = 215 Exercise 5.1.3a As a set:As a bag:Exercise 5.1.3bπbore(Ships Classes)Exercise 5.1.4aFor bags:On the left-hand side:Given bags R and S where a tuple t appears n and m times respectively, the union of bags R and S will have tuple t appear n + m times. The further union of bag T with the tuple t appearing o times will have tuple t appear n + m + o times in the final result.On the right-hand side:Given bags S and T where a tuple t appears m and o times respectively, the union of bags R and S will have tuple t appear m + o times. The further union of bag R with the tuple t appearing n times will have tuple t appear m + o + n times in the final result.For sets:This is a similar case when dealing with bags except the tuple t can only appear at most once in each set. The tuple t only appears in the result if all the sets have the tuple t. Otherwise, the tuple t will not appear in the result. Since we cannot have duplicates, the result only has at most one copy of the tuple t.Exercise 5.1.4bFor bags:On the left-hand side:Given bags R and S where a tuple t appears n and m times respectively, the intersectionof bags R and S will have tuple t appear min( n, m ) times. The further intersection of bag T with the tuple t appearing o times will produce tuple t min( o, min( n, m ) ) times in the final result.On the right-hand side:Given bags S and T where a tuple t appears m and o times respectively, the intersection of bags R and S will have tuple t appear min( m, o ) times. The further intersection of bag R with the tuple t appearing n times will produce tuple t min( n, min( m, o ) ) times in thefinal result.The intersection of bags R,S and T will yield a result where tuple t appears min( n,m,o ) times. For sets:This is a similar case when dealing with bags except the tuple t can only appear at most once in each set. The tuple t only appears in the result if all the sets have the tuple t. Otherwise, the tuple t will not appear in the result.Exercise 5.1.4cFor bags:On the left-hand side:Given that tuple r in R, which appears m times, can successfully join with tuple s in S,which appears n times, we expect the result to contain mn copies. Also given that tuple tin T, which appears o times, can successfully join with the joined tuples of r and s, weexpect the final result to have mno copies.On the right-hand side:Given that tuple s in S, which appears n times, can successfully join with tuple t in T,which appears o times, we expect the result to contain no copies. Also given that tuple rin R, which appears m times, can successfully join with the joined tuples of s and t, weexpect the final result to have nom copies.The order in which we perform the natural join does not matter for bags.For sets:This is a similar case when dealing with bags except the joined tuples can only appear at most once in each result. If there are tuples r,s,t in relations R,S,T that can successfully join, then the result will contain a tuple with the schema of their joined attributes.Exercise 5.1.4dFor bags:Suppose a tuple t occurs n and m times in bags R and S respectively. In the union of these two bags R ⋃ S, tuple t would appear n + m times. Likewise, in the union of these two bags S ⋃ R, tuple t would appear m + n times. Both sides of the relation yield the same result.For sets:A tuple t can only appear at most one time. Tuple t might appear each in sets R and S one or zero times. The combinations of number of occurrences for tuple t in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple t appears in both sets R and S will the union R ⋃ S have the tuple t. The same reasoning holds when we take the union S ⋃ R.Therefore the commutative law for union holds.Exercise 5.1.4eFor bags:Suppose a tuple t occurs n and m times in bags R and S respectively. In the intersection of these two bags R ∩ S, tuple t would appear min( n,m ) times. Likewise in the intersection of these two bags S ∩ R, tuple t would appear min( m,n ) times. Both sides of the relation yield the same result.For sets:A tuple t can only appear at most one time. Tuple t might appear each in sets R and S one or zero times. The combinations of number of occurrences for tuple t in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple t appears in at least one of the sets R and S will the intersection R ∩ S have the tuple t. The same reasoning holds when we take the intersection S ∩ R.Therefore the commutative law for intersection holds.Exercise 5.1.4fFor bags:Suppose a tuple t occurs n times in bag R and tuple u occurs m times in bag S. Suppose also that the two tuples t,u can successfully join. Then in the natural join of these two bags R S, the joined tuple would appear nm times. Likewise in the natural join of these two bags S R, the joined tuple would appear mn times. Both sides of the relation yield the same result.For sets:An arbitrary tuple t can only appear at most one time in any set. Tuples u,v might appear respectively in sets R and S one or zero times. The combinations of number of occurrences for tuples u,v in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple u exists in Rand tuple v exists in S will the natural join R S have the joined tuple. The same reasoning holds when we take the natural join S R.Therefore the commutative law for natural join holds.Exercise 5.1.4gFor bags:Suppose tuple t appears m times in R and n times in S. If we take the union of R and S first, we will get a relation where tuple t appears m + n times. Taking the projection of a list of attributes L will yield a resulting relation where the projected attributes from tuple t appear m + n times. If we take the projection of the attributes in list L first, then the projected attributes from tuple t would appear m times from R and n times from S. The union of these resulting relations would have the projected attributes of tuple t appear m + n times.For sets:An arbitrary tuple t can only appear at most one time in any set. Tuple t might appear in sets R and S one or zero times. The combinations of number of occurrences for tuple t in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple t exists in R or S (or both R and S) will the projected attributes of tuple t appear in the result.Therefore the law holds.Exercise 5.1.4hFor bags:Suppose tuple t appears u times in R, v times in S and w times in T. On the left hand side, the intersection of S and T would produce a result where tuple t would appear min(v , w) times. With the addition of the union of R, the overall result would have u + min(v , w) copies of tuple t. On the right hand side, we would get a result of min(u + v, u + w) copies of tuple t. The expressions on both the left and right sides are equivalent.For sets:An arbitrary tuple t can only appear at most one time in any set. Tuple t might appear in sets R,S and T one or zero times. The combinations of number of occurrences for tuple t in R, S and T respectively are (0,0,0), (0,0,1), (0,1,0), (0,1,1), (1,0,0), (1,0,1), (1,1,0) and (1,1,1). Only when tuple t appears in R or in both S and T will the result have tuple t.Therefore the distributive law of union over intersection holds.Exercise 5.1.4iSuppose that in relation R, u tuples satisfy condition C and v tuples satisfy condition D. Suppose also that w tuples satisfy both conditions C and D where w≤ min(v , w). Then the left hand side will return those w tuples. On the right hand side, σC(R) produces u tuples and σD(R) produces v tuples. However, we know the intersection will produce the same w tuples in the result.When considering bags and sets, the only difference is bags allow duplicate tuples while sets only allow one copy of the tuple. The example above applies to both cases.Therefore the law holds.Exercise 5.1.5aFor sets, an arbitrary tuple t appears on the left hand side if it appears in both R,S and not in T. The same is true for the right hand side.As an example for bags, suppose that tuple t appears one time each in both R,T and two times in S. The result of the left hand side would have zero copies of tuple t while the right hand side would have one copy of tuple t.Therefore the law holds for sets but not for bags.Exercise 5.1.5bFor sets, an arbitrary tuple t appears on the left hand side if it appears in R and either S or T. This is equivalent to saying tuple t only appears when it is in at least R and S or in R and T. The equivalence is exactly the right side’s expression.As an example for bags, suppose that tuple t appears one time in R and two times each in S and T. Then the left hand side would have one copy of tuple t in the result while the right hand side would have two copies of tuple t.Therefore the law holds for sets but not for bags.Exercise 5.1.5cFor sets, an arbitrary tuple t appears on the left hand side if it satisfies condition C, condition Dor both condition C and D. On the right hand side, σC(R) selects those tuples that satisfy condition C while σD(R) selects those tuples that satisfy condition D. However, the union operator will eliminate duplicate tuples, namely those tuples that satisfy both condition C and D. Thus we are ensured that both sides are equivalent.As an example for bags, we only need to look at the union operator. If there are indeed tuples that satisfy both conditions C and D, then the right hand side will contain duplicate copies of those tuples. The left hand side, however, will only have one copy for each tuple of the original set of tuples.Exercise 5.2.1bExercise 5.2.1cExercise 5.2.1dExercise 5.2.1fExercise 5.2.1gExercise 5.2.1hExercise 5.2.1iExercise 5.2.1jExercise 5.2.1kExercise 5.2.1lExercise 5.2.1mExercise 5.2.1nExercise 5.2.2aApplying the δ operator on a relation with no duplicates will yield the same relation. Thus δ is idempotent.Exercise 5.2.2bThe result of πL is a relation over the list of attributes L. Performing the projection again will return the same relation because the relation only contains the list of attributes L. Thus πL is idempotent.Exercise 5.2.2cThe result of σC is a relation where condition C is satisfied by every tuple. Performing the selection again will return the same relation because the relation only contains tuples that satisfy the condition C. Thus σC is idempotent.Exercise 5.2.2dThe result of γL is a relation whose schema consists of the grouping attributes and the aggregated attributes. If we perform the same grouping operation, there is no guarantee that the expression would make sense. The grouping attributes will still appear in the new result. However, the aggregated attributes may or may not appear correctly. If the aggregated attribute is given a different name than the original attribute, then performing γL would not make sense because it contains an aggregation for an attribute name that does not exist. In this case, the resultingrelation would, according to the definition, only contain the grouping attributes. Thus, γL is not idempotent.Exercise 5.2.2eThe result of τ is a sorted list of tuples based on some attributes L. If L is not the entire schema of relation R, then there are attributes that are not sorted on. If in relation R there are two tuples that agree in all attributes L and disagree in some of the remaining attributes not in L, then it is arbitrary as to which order these two tuples appear in the result. Thus, performing the operation τ multiple times can yield a different relation where these two tuples are swapped. Thus, τ is not idempotent.Exercise 5.2.3If we only consider sets, then it is possible. We can take πA(R) and do a product with itself. From this product, we take the tuples where the two columns are equal to each other.If we consider bags as well, then it is not possible. Take the case where we have the two tuples (1,0) and (1,0). We wish to produce a relation that contains tuples (1,1) and (1,1). If we use the classical operations of relational algebra, we can either get a result where there are no tuples or four copies of the tuple (1,1). It is not possible to get the desired relation because no operation can distinguish between the original tuples and the duplicated tuples. Thus it is not possible to get the relation with the two tuples (1,1) and (1,1).Exercise 5.3.1a)Answer(model) ← PC(model,speed,_,_,_) AND speed ≥ 3.00b)Answer(maker) ← Laptop(model,_,_,hd,_,_) AND Product(maker,model,_) AND hd ≥100c)Answer(model,price) ← PC(model,_,_,_,price) AND Product(maker,model,_) ANDmaker=’B’Answer(model,price) ← Laptop(mode l,_,_,_,_,price) AND Product(maker,model,_)AND maker=’B’Answer(model,price) ← Printer(model,_,_,price) AND Product(maker,model,_) ANDmaker=’B’d)Answer(model) ← Printer(model,color,type,_) AND color=’true’ AND type=’laser’e)PCMaker(maker) ← Product(maker,_,type) AND type=’pc’LaptopMaker(maker) ← Product(maker,_,type) AND type=’laptop’Answer(maker) ← LaptopMaker(maker) AND NOT PCMaker(maker)f)Answer(hd) ← PC(model1,_,_,hd,_) AND PC(model2,_,_,hd,_) AND model1 <>model2g)Answer(model1,model2) ← PC(model1,speed, ram,_,_) ANDPC(model2,_speed,ram,_,_) AND model1 < model2h)FastComputer(model) ← PC(model,speed,_,_,_) AND speed ≥ 2.80FastComputer(model) ← Laptop(model,speed,_,_,_,_) AND speed ≥ 2.80Answer(maker) ← Product(maker,model1,_) AND Product(maker,mod el2,_) ANDFastComputer(model1) AND FastComputer(model2) AND model1 <> model2i)Computers(model,speed) ← PC(model,speed,_,_,_)Computers(model,speed) ← Laptop(model,speed,_,_,_,_)SlowComputers(model) ← Computers(model,speed) AND Computers(model1,speed1)AND speed < speed1FastestComputers(model) ← Computers(model,_) AND NOT SlowComputers(model)Answer(maker) ← Fast estComputers(model) AND Product(maker,model,_)j)PCs(maker,speed) ← PC(model,speed,_,_,_) AND Product(maker,model,_) Answer(maker) ← PCs(maker,spe ed) AND PCs(maker,speed1) AND PCs(maker,speed2) AND speed <> speed1 AND speed <> speed2 AND speed1 <> speed2k)PCs(maker,model) ← Product(maker,model,type) AND type=’pc’Answer(maker) ← PCs(maker,model) AND PCs(maker,model1) ANDPCs(maker,model2) AND PCs(maker,model3) AND model <> model1 AND model <>model2 AND model1 <> model2 AND (model3 = model OR model3 = model1 ORmodel3 = model2)Exercise 5.3.2a)Answer(class,country) ← Classes(class,_,country,_,bore,_) AND bore ≥ 16b)Answer(name) ← Ships(name,_,launch ed) AND launched < 1921c)Answer(ship) ← Outcomes(ship,battle,result) AND battle=’Denmark Strait’ AND result= ‘sunk’d)Answer(name) ← Classes(class,_,_,_,_,displacement) AND Ships(name,class,launched)AND displacement > 35000 AND launched > 1921e)Answer(nam e,displacement,numGuns) ← Classes(class,_,_,numGuns,_,displacement)AND Ships(name,class,_) AND Outcomes (ship,battle,_) AND battle=’Guadalcanal’AND ship=namef)Answer(name) ← Ships(name,_,_)Answer(name) ← Outcomes(name,_,_) AND NOT Answer(name)g)MoreThan One(class) ← Ships(name,class,_) AND Ships(name1,class,_) AND name <>name1Answer(class) ← Classes(class,_,_,_,_,_) AND NOT MoreThanOne(class)h)Battleship(country) ← Classes(_,type,country,_,_,_) AND type=’bb’Battlecruiser(country) ← Classes(_,type,country,_,_,_) AND type=’bc’Answer(country) ← Battleship(country) AND Battlecruiser(country)i)Results(ship,result,date) ← Battles(name,date) AND Outcomes(ship,battle,result) ANDbattle=nameAnswer(ship) ← Results(ship,result,date) AND Results(ship,_,date1) ANDresult=’damaged’ AND date < date1Exercise 5.3.3A nswer(x,y) ← R(x,y) AND z = zExercise 5.4.1aAnswer(a,b,c) ← R(a,b,c)Answer(a,b,c) ← S(a,b,c)Exercise 5.4.1bAnswer(a,b,c) ← R(a,b,c) AND S(a,b,c)Exercise 5.4.1cAnswer(a,b,c) ← R(a,b,c) AND NOT S(a,b,c)Exercise 5.4.1dUnion(a,b,c) ← R(a,b,c)Union(a,b,c) ← S(a,b,c)Answer(a,b,c) ← Union(a,b,c) AND NOT T(a,b,c)Exercise 5.4.1eJ(a,b,c) ← R(a,b,c) AND NOT S(a,b,c)K(a,b,c) ← R(,a,b,c) AND NOT T(a,b,c)Answer(a,b,c) ← J(a,b,c) AND K(a,b,c)Exercise 5.4.1fAnswer(a,b) ← R(a,b,_)Exercise 5.4.1gJ(a,b) ← R(a,b,_)K(a,b) ← S(_,a,b)Answer(a,b) ← J(a,b) AND K(a,b)Exercise 5.4.2aAnswer(x,y,z) ← R(x,y,z) AND x = yExercise 5.4.2bAnswer(x,y,z) ← R(x,y,z) AND x < y AND y < z Exercise 5.4.2cAnswer(x,y,z) ← R(x,y,z) AND x < yAnswer(x,y,z) ← R(x,y,z) AND y < zExercise 5.4.2dChange: NOT(x < y OR x > y)To: x ≥ y AND x ≤ yThe above simplifies to x = yAnswer(x,y,z) ← R(x,y,z) AND x = yExercise 5.4.2eChange: NOT((x < y OR x > y) AND y < z)NOT(x < y OR x > y) OR y ≥ z(x ≥ y AND x ≤ y) OR y ≥ zTo: x = y OR y ≥ zAnswer(x,y,z) ← R(x,y,z) AND x = yAnswer(x,y,z) ← R(x,y,z) AND y ≥ zExercise 5.4.2fChange: NOT((x < y OR x < z) AND y < z)NOT(x < y OR x < z) OR y ≥ z To: (x ≥ y AND x ≥ z) OR y ≥ zAnswer(x,y,z) ← R(x,y,z) AND x ≥ y AND x ≥ zAnswer(x,y,z) ← R(x,y,z) AND y ≥zExercise 5.4.3aAnswer(a,b,c,d) ← R(a,b,c) AND S(b,c,d)Exercise 5.4.3bAnswer(b,c,d,e) ← S(b,c,d) AND T(d,e)Exercise 5.4.3cAnswer(a,b,c,d,e) ← R(a,b,c) AND S(b,c,d) AND T(d,e)Exercise 5.4.4a)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx = syb)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx < sy AND ry < szc)Answer(rx,ry,rz,sx,sy,s z) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx < syAnswer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND ry < szd)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx = sye)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx = syAnswe r(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND ry ≥ szf)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx ≥ sy AND rx ≥ szAnswer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND ry ≥ szExercise 5.4.5aR1 := πx,y(Q R)Exercise 5.4.5bR1 := ρR1(x,z)(Q)R2 := ρR2(z,y)(Q)R3 := πx,y(R1 (R1.z = R2.z) R2)Exercise 5.4.5cR1 := πx,y(Q R)R2 := σx < y(R1)。
初学者必读的SQL数据库基础教程
初学者必读的SQL数据库基础教程SQL数据库是一种常用的数据库管理系统,广泛应用于各种软件开发和数据管理领域。
对于初学者来说,掌握SQL数据库的基础知识是非常重要的。
本文将从数据定义语言、数据操作语言、数据查询语言和数据控制语言等方面,为初学者提供一份必读的SQL数据库基础教程。
第一章数据定义语言(DDL)数据定义语言(DDL)是SQL数据库中用来定义数据库结构的语言。
它包括创建、修改和删除数据库、表、列以及其他对象的操作。
在SQL中,创建数据库使用CREATE DATABASE语句,创建表使用CREATE TABLE语句,修改表结构使用ALTER TABLE语句,删除表使用DROP TABLE语句等。
初学者在学习时应该了解这些常用的DDL语句,并能够正确地使用它们。
第二章数据操作语言(DML)数据操作语言(DML)是SQL数据库中用来对数据库中的数据进行操作的语言。
它包括插入、更新和删除数据的操作。
在SQL中,插入数据使用INSERT INTO语句,更新数据使用UPDATE语句,删除数据使用DELETE FROM语句等。
初学者需要熟悉这些基本的DML语句,并能够通过它们来操作数据库中的数据。
第三章数据查询语言(DQL)数据查询语言(DQL)是SQL数据库中用来查询数据库中的数据的语言。
它包括SELECT语句和一些用于过滤、排序和聚合数据的函数。
初学者需要掌握SELECT语句的基本用法,了解如何使用WHERE子句进行条件过滤,如何使用ORDER BY子句进行排序,以及如何使用GROUP BY子句进行数据聚合。
第四章数据控制语言(DCL)数据控制语言(DCL)是SQL数据库中用来控制数据库访问权限和事务处理的语言。
它包括GRANT和REVOKE语句用于授权和撤销权限,以及BEGIN TRANSACTION、COMMIT和ROLLBACK语句用于管理事务。
初学者需要了解如何使用DCL语句来管理数据库的安全性和事务一致性。
数据库系统基础教程_[全文]
第一章数据库系统的世界The Worlds of Database Systems数据库系统的发展数据库管理系统的结构未来的数据库系统*§1.1 数据库系统的发展c一、术语1.数据库是长期储存在计算机内的、有组织的、可共享的数据的集合。
*2.数据库管理系统数据库系统基础教程A First Course in Database SystemsDBMS - DataBase Management System是处理数据库访问的软件。
提供数据库的用户接口。
DBMS的目的:提供一个可以方便地、有效地存取数据库信息的环境*3.数据库系统是指在计算机系统中引入数据库后的系统*数据库最终用户应用系统应用开发工具DBMS操作系统数据库管理员DBA数据库系统构成应用程序员*保存信息的两种不同方法:永久性的系统文件、数据库系统。
文件方式的问题:数据的冗余和不一致数据访问困难数据孤立完整性问题原子性问题并发访问异常安全性问题二、文件系统与数据库系统*数据库方法能较好地解决以上的问题数据的独立性有效地访问数据减少应用程序的开发时间数据的一致性和安全性统一的数据管理并发的数据访问三、为什么用数据库*几种模型:基于树的层次模型基于图的网状模型物理相关、无高级查询语言基于表的关系模型物理无关、支持高级查询语言,基于对象的面向对象模型OOOR四、数据库模型的发展定长记录*关系数据库系统属性元组*关查询语言SQL语言SELECT balanceFROM AccountsWHERE accountNO = 67890;关系数据库系统*DBMS的组成数据、元数据存储管理程序事务管理程序查询处理程序§1.2 数据库管理系统的结构数据元数据存储管理程序查询处理程序事务管理程序模式更新更新查询*数据、元数据关于数据结构的信息(关于数据的数据)索引(INDEX)DBMS的组成*存储管理程序文件管理程序缓冲区管理查程序DBMS的组成*查询处理程序查询优化磁盘访问,是查询的主要代价;索引是查询优化的利器DBMS的组成*事务管理程序事务:是用户定义的一个数据库操作序列事务的四个特性原子性A一致性C隔离性I持久性DDBMS的组成*客户-服务器程序体系结构浏览器-服务器体系结构DBMS的组成*客户-服务器程序体系结构浏览器-服务器体系结构§1.3 未来的数据库系统第二章数据库建模Database Modeling*数据库的设计步骤需求收集和分析设计概念结构设计逻辑结构设计物理结构物理实现*数据库的设计步骤需求收集和分析用户关心什么用户要什么结果设计概念结构设计逻辑结构设计物理结构物理实现*数据库的设计步骤需求收集和分析设计概念结构存什么关系(联系)如何ODL或E/R图,是各种数据模型的共同基础设计逻辑结构设计物理结构物理实现*数据库的设计步骤需求收集和分析设计概念结构设计逻辑结构用什么数据模型数据库的模式(database schema)用户子模式设计物理结构物理实现*数据库的设计步骤需求收集和分析设计概念结构设计逻辑结构设计物理结构数据怎么存根据DBMS产品、环境特点物理实现*数据库的设计步骤需求收集和分析设计概念结构设计逻辑结构设计物理结构物理实现运行DDL装入测试数据应用程序*数据库的设计步骤想法需求ODLE / R关系RDBMSOODBMS*§2.1 ODL对象定义语言Object Definition Language以面向对象的观点、方法,说明数据库的概念结构可方便地直接转换成OODBMS 的说明经过努力,可以转换成RDBMS 的说明*面向对象的设计对象标识—OID对象与对象的区别类具有相同特性的对象归为一类对象的归并必须有意义属于同一类的对象其特性必须相同*面向对象的设计对象的三个特性属性:特性联系:引用方法:函数接口说明interface < 名字> {< 特性表>}*属性对象某方面的特征,属性就是数据只由基本数据类型构成属性的类型,不能是类、也不能从类中构造Interface Movie { //Movie Class 的ODL说明attribute string title;attribute integer year;attribute integer length;attribute enum Film { color, blackAndWhite } filmType;};*Interface Star {attribute string name;attribute Struct Addr{ string street,string city } address;};记录结构类型*联系对象的引用对象的关联对象集合的引用(1:N)Relationship Set < Star > stars;单一对象集合的引用(1:1)Relationship Star starOf;*反向联系ODL要求显式表示存在的反向联系Interface Movie { //Movie Class 的ODL说明attribute string title;attribute integer year;attribute integer length;attribute enum Film { color, blackAndWhite } filmType;relationship Set < Star > starsinverse Star :: starredIn; //Star与Movie的联系};联系的多重性N:N在联系中,每个C都和D的集合有关,而在反向联系中,每个D都和C的集合有关N:1在联系中,每个C都和唯一的D有关,而在反向联系中,每个D都和C的集合有关1:1在联系中,每个C都和唯一的D有关,而在反向联系中,每个D都和唯一的C有关*Interface Moive{……relationship Set <Star> starsinverse Star :: staredIn;relationship Studio ownedByinverse Studio :: owns;};Interface Star{……relationship Set <Moive> staredIninverse Moive :: stars;};Interface Studio{……relationship Set <Moive> ownsinverse Moive :: ownedBy;};NNN1*ODL中的类型基本类型原子类型接口类型结构类型,可由以下类型组合而成集合无重复,次序无关包可重复,次序无关列表可重复,次序相关数组结构*§2.2 实体联系图(E/R)用图形的方法,描述实体及实体间的联系世界由一组称作实体的基本对象及这些对象间的联系组成元素实体(Entity)客观存在并可相互区别的事件或物体对应于ODL中的对象实体集(Entity Set)同类(具有相同类型、相同性质)实体的集合对应于ODL中的类用矩形表示*§2.2 实体联系图(E/R)元素属性(Attribute)实体所具有的某一特性用与实体集相连的椭圆表示联系(Relationship)实体集之间的关联可涉及多个实体集可表示双向的联系用与相应的实体集相连的菱形表示*MoviesStarsStars-inlenghtfilmTypetitleyearnameaddress*E/R联系的多重性N与1的表示MoviesStarsStars-inStudiosPresidentsRunsMoviesStudiosOwns*联系的多向性E/R图能方便地描述两个以上实体集间的联系StarsMoviesContractsStudios一个制片公司与一位特定的影星签约来演一部特定的电影*联系中的角色实体集在联系中的作用参与联系的实体集互异只标注联系名同一实体集在一个联系中多次出现标注联系名及角色名Sequel-ofMoviesOriginalSequelStarsMoviesContractsStudiosStudio of starProducing studio*联系中的属性联系中可以包含属性由联系而产生的属性可为由联系产生的属性建立实体集StarsMoviesContractsStudiossalary*将多向联系转换成二元联系新增连接实体集引入连接实体集至原实体集的多对一的联系*§2.3 设计原则真实性设计应当忠于规范存什么避免冗余任何事物只表达一次避免引入过多的元素选择合适的元素类型属性?类/实体集?联系集?*§2.4 子类特殊化与概括子类与超类属性的继承*ODL中的子类子类继承其超类的所有特性属性联系Interface Cartoon : Movie {relationship set < Star > voices;}*ODL中的多重继承类的层次一个类可以有多个超类Interface MurderMystery : Movie{attribute string weapon;}Interface Cartoon-MurderMystery : Cartoon,MurderMystery { }*E/R中的子类IsaE/R中的继承*§2.5 对约束的建模建模包含对现实世界的对象及联系的描述,也包含对它们的一些约束键码单值约束参照完整性约束域的约束一般约束*键码在类的范围内唯一标识一个对象(或者在实体集的范围内唯一标识一个实体)的属性或属性集一个类中的两个对象(或一个实体集中的两个实体)在构成键码的属性集上取值不能相同ODL中键码的表示interface Movie( key (title,year) ) {……}*超码一个或多个属性的集合,能在一个实体集中唯一地标识一个实体一个类(或实体集)中可能有多个超码候选码其任意真子集都不为超码的超码一个类(或实体集)中可能有多个候选码主码从候选码中选取的一个,一个类(实体集)中只有一个主码E / R图中只能表示主码:主码属性名加上下划线*单值约束要求某个角色的值是唯一的,如键码当一个属性为单值时可以要求该属性值存在(not null)可以允许该属性值任选(null)构成键码的属性,必须有值存在(not null)*参照完整性约束要求由某个对象引用的值在数据库中确实存在参照与被参照、引用与被引用参照完整性约束的操作(各产品不同)禁止删除被引用的对象级联删除/ 修改E/R图中参照完整性的表示MoviesStudiosOwns*§2.6 弱实体集弱实体集的属性不足以形成主码有主码的实体集称为强实体集弱实体集只有作为一对多联系的一部分(多)才有意义弱实体集与其拥有者之间的联系是标识性联系CrewsUnit-ofStudiosnumbernameaddr*§2.7 关于联系集联系集的成份参加联系的实体集的主码联系集的属性联系中属性的决策(二元联系)1:1 联系集的属性:放到任意一端1:N 联系集的属性:放到N 端N:M联系集的属性:只能留在联系集中*联系集的取舍(二元联系)1:1联系:将一端的主码作为另一端的属性1:N联系:将一端的主码作为N 端的属性N:M联系:必须保留联系集联系集的键码(二元联系)1:1联系:任意一端的主码1:N联系:N端的主码N:M联系:参加联系的所有实体集的主码*ODL、E/R建模关心:存什么数据、关系如何不关心:用什么数学模型、DBMS产品透过E/R图,便于与用户交流*作业思考所有带*的练习,并上网查阅解答练习2.1.7 / 2.2.8 / 2.3.2 / 2.5.3 / 2.5.4 /2.6.4(a) 第三章关系数据模型The Relational Data Model*ODL、E/R到关系模型的转换关系模型的设计理论*§3.1 关系模型的基本概念逻辑数据模型是用户从数据库所看到的数据模型与DBMS有关层次、网状、关系、面向对象关系数据模型数据结构两维的扁平表数据操作关系代数关系演算数据的完整性实体完整性参照完整性用户定义的完整性*现实世界的实体以及实体间的各种联系均用关系表示关系数据库系统是建立在关系模型上的数据库系统关系数据库是表的集合*模型和模式数据模型是描述数据的手段数据模式是用给定的数据模型对具体数据的描述属性元组域型值联系关系的联系是通过关联属性的值连接的*SnoSnameSsexSagesdept95001张三男25CS95002李四女24CS96101王五23MA96001赵六男23CS关系( 表)属性(列、字段)元组(行、记录)域(string,{男,女})Student ( sno, sname, ssex, sage, sdept )*关系实例关系→实体集、类关系的实例→元组的集合元组→实体、对象数据库实例→给定时刻数据库中数据的一个快照*§3.2 从ODL设计到关系设计ODL设计是概念设计的产物( Using OO )ODL描述→关系模式→实现*ODL属性→关系属性原子属性类→关系属性→属性非原子属性(复杂数据类型)必须转换成原子属性记录结构结构的每个item对应一个属性多值集合针对每个值建立一个元组会产生冗余→需规范化*ODL属性→关系属性(续)其他类型属性(包、数组、列表)针对每个元素建立一个元组增加一个记数属性,表示包的成员号定长数组扩展为多个属性*ODL联系→关系描述单值联系联系的类型为一个类增加一个(组)属性,存放相关类的键码属性(组)将类之间的联系→关系之间的联系*ODL联系→关系描述(续)多值联系联系的类型为某个类的集合类型1 : N、N : M增加一个键码属性为集合的每个成员建立一个元组其他原始属性重复多次(与集合成员的个数相等)导致大量的冗余,需要规范化*键码是必需的选择合适的属性(组)作为键码学号、工号、身份证号…...增加计数属性联系与反向联系在联系的双方均有联系的描述→冗余ODL:双向描述E/R:相关的键码值进行连接*§3.3 从E/R图到关系的设计E/R与ODL描述的差异联系作为独立的概念←→联系嵌套在类定义中结构化数据←→允许使用集合、聚集类型联系可以有属性←→联系无属性E/R →关系模式→实现*实体集到关系的转换非弱实体集实体集名→关系名属性→属性弱实体集为弱实体集建立关系属性:弱实体集的属性+ 辅助实体集的键码*E/R联系到关系的转换用关系表示联系联系名→关系名属性→属性+ 相关实体集的键码属性(集)多向联系的转换注意,属性的命名*§3.4 子类结构到关系的转换ODL中的子类一个对象完全属于一个类子类继承其超类的特性E/R中的子类分层结构通过与ISA联系有关的实体集进行扩展*用关系表示ODL子类每个子类都有自己的关系包含该子类的所有特性(含继承特性)在一个关系中含有所有属性Movie(title,year,length,filmType,studioName,starName)Cartoon(title,year,length,filmType,studioName,starName,voice) MurderMystery(title,year,length,filmType,studioName,starName,weapon)Cartoon- MurderMystery(title,year,length,filmType,studioName,starName,voice, weapon)*在关系模型中表示isa 联系子类的信息被分散到上层的几个关系中与ISA联系有关的实体集拥有相同的键码Movie(title,year,length,filmType)Cartoon(title,year)MurderMystery(title,year, weapon)Voice(title,year,name)*使用NULL值合并关系将关系描述成一个‘全集’属性:所有可能的属性描述:允许Null值层次越高,取Null值的属性越多Movie (title,year,length,filmType,studioName,starName,voice, weapon) 只是一种方法而已*作业思考所有带*的练习,并上网查询解答练习3.2.3 / 3.3.1 / 3.4.1 / 3.5.3 /*§3.5 函数依赖数据依赖函数依赖多值依赖数据依赖是针对数据模式,而不是特定的实例*函数依赖(FD)属性之间的联系假设给定X 属性的值,就知道Y的值,那么X 函数决定Y如果R的两个元组在属性A1,A2,…,An上一致,则它们在另一个属性B上也一致,那么A1,A2,…,An函数决定B,记作A1A2…An→Bif A1A2…An→B1 thenA1A2…An→B2 A1A2…An→B1 B2 ... Bm……A1A2…An→Bm*关系的键码如果一个或多个属性的集合{A1A2…An}满足如下条件,则该集合为关系R的键码:1.这些属性函数决定该关系的所有其他属性2. {A1A2…An}的任何真子集都不能函数决定R的所有其他属性*超键码包含键码的属性集称为超键码*寻找关系的键码(来自E/R)来自实体集的关系的键码就是该实体集的键码属性对于二元联系R:N:M,相关两个实体的键码都是R的键码属性N:1,多端实体集的键码是R的加码属性1:1,任意一端实体集的键码是R的键码对于多向联系R:如果多向联系R有一个箭头指向实体集E,则响应的关系中,除了E的键码以外,至少还存在一个键码。
vfp基础教程1
1.4 VFP 6.0 的一些规则 VFP6.0 的命名规则: 1. VFP6.0 的命名规则: · 只能使用字母、下划线和数字。 只能使用字母、下划线和数字。 · 使用字母或下划线作为名称的开头。 使用字母或下划线作为名称的开头。 · 名称可以是 1 至 128 个字符,但自由表的字段名和索引标识最多 个字符, 个字符。 只能有 10 个字符。 · 避免使用 Visual FoxPro 的保留字。 的保留字。 · 文件的命名遵循操作系统的约定。 文件的命名遵循操作系统的约定。 操作系统的约定
VFP6.0 6.0的用户界面 2. VFP6.0的用户界面
6.0的工作方式 3. VFP 6.0的工作方式 (1) 菜单操作方式 根据所需的操作从菜单中选择相应的命令( WORD类似)。每执行一 根据所需的操作从菜单中选择相应的命令(与WORD类似)。每执行一 次菜单命令,命令窗口中一般都会显示出与菜单对应的命令内容。 次菜单命令,命令窗口中一般都会显示出与菜单对应的命令内容。 利用工具菜单中的向导可以很方便地完成常规任务。 利用工具菜单中的向导可以很方便地完成常规任务。 (2) 命令交互方式 根据所要进行的各项操作, 根据所要进行的各项操作,采用人机对话方式在命令窗口中按格式要 求逐条输入所需命令,按回车后,机器逐条执行。 求逐条输入所需命令,按回车后,机器逐条执行。 (3) 程序执行方式 先在程序编辑窗口中编完程序,再从程序菜单中选择执行, 先在程序编辑窗口中编完程序,再从程序菜单中选择执行,或从命令 窗口中输入DO 命令,让机器执行。 窗口中输入DO 命令,让机器执行。
LOGO
第一章 数据库系统基础知识
浙江工业大学浙西分校 朱海华
1. 数据处理 数据: 数据: 是对事实、概念或指令的一种特殊表达形式, 是对事实、概念或指令的一种特殊表达形式,可以用人工的方式或自 动化的装置进行通信、翻译转换或者进行加工处理。 它包括两类: 动化的装置进行通信、翻译转换或者进行加工处理。 它包括两类: 一类是能参与数字运算的数值型数据; 一类是能参与数字运算的数值型数据;一类是不能参与数字运算的非 数值型数据,如文字、图画、声音、活动图象等。 数值型数据,如文字、图画、声音、活动图象等。 数据处理: 数据处理: 是对各种类型的数据进行收集、存储、分类、计算、加工、 是对各种类型的数据进行收集、存储、分类、计算、加工、检索与传 输的过程。 输的过程。 包括:收集原始数据、编码转换、数据输入、数据处理、数据输出。 包括:收集原始数据、编码转换、数据输入、数据处理、数据输出。
数据库基础教程(完整版)
数据库基础教程(完整版)第一部分:认识数据库数据库,顾名思义,就是一个用来存储、管理数据的仓库。
在这个信息爆炸的时代,数据已经成为了企业的核心资产,而数据库就是管理这些资产的重要工具。
无论是电商平台、社交媒体,还是企业内部的管理系统,都离不开数据库的支持。
一、数据库的分类1. 关系型数据库:以表的形式组织数据,每个表由行和列组成,行代表记录,列代表字段。
常见的有MySQL、Oracle、SQL Server等。
2. 非关系型数据库:与关系型数据库不同,非关系型数据库的数据结构更加灵活,常见的有MongoDB、Redis、Cassandra等。
3. NoSQL数据库:NoSQL是Not Only SQL的缩写,表示不仅仅是SQL,它包含了非关系型数据库以及一些新型的数据库技术,如NewSQL 等。
二、数据库的组成1. 数据库管理系统(DBMS):负责管理和维护数据库的软件系统,如MySQL、Oracle等。
2. 数据库:存储数据的仓库,由多个表组成。
3. 表:数据库中的基本单位,由行和列组成,行代表记录,列代表字段。
4. 记录:表中的一行数据,代表一个完整的信息。
5. 字段:表中的一列数据,代表记录中的一个属性。
三、数据库的作用1. 数据存储:将数据存储在数据库中,方便管理和查询。
2. 数据管理:通过数据库管理系统,可以对数据进行增删改查等操作。
3. 数据安全:数据库管理系统提供了数据备份、恢复、权限控制等功能,保障数据的安全。
4. 数据共享:多个用户可以同时访问数据库,实现数据共享。
5. 数据分析:通过数据库管理系统,可以对数据进行统计、分析等操作,为企业决策提供依据。
四、学习数据库的必要性1. 提高工作效率:掌握数据库技术,可以快速地处理大量数据,提高工作效率。
2. 适应市场需求:随着互联网的发展,数据库技术已经成为IT 行业的必备技能。
3. 拓展职业发展:学习数据库技术,可以为职业发展打下坚实的基础。
最新数据库基础教程课后习题答案(顾韵华)
习题11、简述数据库系统的特点。
答:数据库系统的特点有:1)数据结构化在数据库系统中,采用统一的数据模型,将整个组织的数据组织为一个整体;数据不再仅面向特定应用,而是面向全组织的;不仅数据内部是结构化的,而且整体是结构化的,能较好地反映现实世界中各实体间的联系。
这种整体结构化有利于实现数据共享,保证数据和应用程序之间的独立性。
2)数据共享性高、冗余度低、易于扩充数据库中的数据能够被多个用户、多个应用程序共享。
数据库中相同的数据不会多次重复出现,数据冗余度降低,并可避免由于数据冗余度大而带来的数据冲突问题。
同时,当应用需求发生改变或增加时,只需重新选择不同的子集,或增加数据即可满足。
3)数据独立性高数据独立性是由DBMS 的二级映像功能来保证的。
数据独立于应用程序,降低了应用程序的维护成本。
4)数据统一管理与控制数据库中的数据由数据库管理系统(DBMS )统一管理与控制,应用程序对数据的访问均经由DBMS 。
DBMS 提供四个方面的数据控制功能:并发访问控制、数据完整性、数据安全性保护、数据库恢复。
2、什么是数据库系统?答:在计算机系统上引入数据库技术就构成一个数据库系统(DataBase System ,DBS )。
数据库系统是指带有数据库并利用数据库技术进行数据管理的计算机系统。
DBS 有两个基本要素:一是DBS 首先是一个计算机系统;二是该系统的目标是存储数据并支持用户查询和更新所需要的数据。
3、简述数据库系统的组成。
答:数据库系统一般由数据库、数据库管理系统(及其开发工具)、数据库管理员(DataBase Administrator ,DBA )和用户组成。
4、试述数据库系统的三级模式结构。
这种结构的优点是什么?答:数据库系统的三级模式结构是指数据库系统是由外模式、模式和内模式三级构成,同时包含了二级映像,即外模式/模式映像、模式/内模式映像,如下图所示。
数据库应用1……外模式A 外模式B 模式应用2应用3应用4应用5……模式外模式/模式映像模式/内模式映像数据库系统的这种结构具有以下优点:(1)保证数据独立性。
数据库系统基础教程PPT完整版
THANKS FOR WATCHING
感谢您的观看
概念设计的输出
概念设计的输出是概念模型,它为后续的逻辑设计和物理 设计提供了基础。
逻辑设计
逻辑设计的定义
逻辑设计是根据概念设计的结果,将概念模型转换为逻辑模型的过 程。逻辑模型是对数据库结构的详细描述,包括表、视图、索引等。
逻辑设计的方法
逻辑设计通常采用关系型数据库管理系统(RDBMS)来实现,包 括表的设计、关系的定义、约束的添加等。
数据库系统的维护与优化
数据库备份与恢复
定期备份数据库,确保在数据丢失或损坏时能够 恢复。
数据库安全更新与补丁
及时更新数据库系统和应用软件,修补安全漏洞。
ABCD
性能监控与调优
监控数据库性能,通过调整参数和优化查询等方 式提高性能。
数据库系统硬件与软件的维护
定期检查硬件和软件的运行状况,确保数据库系 统的稳定运行。
格式。
模式
02
也称为逻辑模式,描述了数据在数据库中的逻辑结构和关系。
外模式
03
也称为用户模式,描述了数据在用户视角下的表现形式和结构。
03 数据库设计
数据库设计概述
数据库设计定义
数据库设计的基本步骤
数据库设计是指根据特定需求,构建 一个结构合理、性能良好、操作方便 的数据库的过程。
需求分析、概念设计、逻辑设计、物 理设计等。
01
概述
人工智能技术的快速发展对数据库系统产生了深远影响,推动了数据库
系统的智能化进程。
02
挑战
人工智能时代对数据库系统的要求更高,需要具备自适应、自学习、自
推理等能力。
03
技术发展
人工智能技术在数据库系统中的应用不断深入,如机器学习、深度学习、
数据库管理系统的入门教程
数据库管理系统的入门教程数据库管理系统是一种用于管理和存储数据的软件系统,它能够为企业和机构提供高效、安全、稳定和可扩展的数据处理服务。
在现代信息时代中,数据库管理系统已经成为企业信息化建设的重要组成部分,学习数据库管理系统已经成为计算机专业学习中不可或缺的一部分。
在企业中,数据库管理系统被广泛应用于客户关系管理(CRM)、供应链管理(SCM)、人力资源管理(HRM)、企业资源规划(ERP)等方面,每天都要处理海量的数据。
因此,学习数据库管理系统已经成为一项必不可少的基础知识。
本文将详细介绍如何入门学习数据库管理系统,包括概念理解、数据建模、数据库安装、SQL语言、数据备份与恢复等方面。
一、概念理解在学习数据库管理系统之前,必须理解一些基本的概念。
数据库是存储数据的一个集合。
数据库管理系统是用于管理和存储数据的软件系统。
数据模型是描述数据的结构和联系的方式。
数据模型有三种类型:1. 层次模型:数据由父子关系的树状结构来组织。
2. 网状模型:数据由复杂的网络结构来组织。
3. 关系模型:数据由表格(关系)来组织。
关系模型是最流行的数据模型,因为它简单易用、易于维护和扩展。
二、数据建模数据建模是设计数据库的过程。
它是确定数据库中的实体、属性和关系的过程。
实体是指具有独立特性的事物或对象。
属性是描述实体特征的特征或元素。
关系是实体之间的联系。
在数据建模中,需要定义实体、属性和关系。
对于关系模型,需要使用E-R 图来表示实体和关系之间的联系。
在建立数据模型时,必须按照规范化的过程。
规范化是将数据模型优化,以消除数据冗余、提高数据完整性和减少数据存储空间。
关系数据库中的每个表都必须符合第一范式、第二范式和第三范式。
三、数据库安装当完成数据建模后,就需要安装数据库管理系统。
常见的数据库管理系统有Oracle、MySQL、SQL Server、DB2等。
在安装数据库之前,需要确定硬件配置、操作系统版本和数据库版本。
(完整版)数据库系统基础教程第八章答案
Section 1Exercise 8.1.1a)CREATE VIEW RichExec ASSELECT * FROM MovieExec WHERE netWorth >= 10000000;b)CREATE VIEW StudioPres (name, address, cert#) ASSELECT , MovieExec.address, MovieExec.cert# FROM MovieExec, Studio WHERE MovieExec.cert# = Studio.presC#;c)CREATE VIEW ExecutiveStar (name, address, gender, birthdate, cert#, netWorth) AS SELECT , star.address, star.gender, star.birthdate, exec.cert#, WorthFROM MovieStar star, MovieExec exec WHERE = ANDstar.address = exec.address;Exercise 8.1.2a)SELECT name from ExecutiveStar WHERE gender = ‘f’;b)SELECT from RichExec, StudioPres where = ; c)SELECT from ExecutiveStar, StudioPresWHERE Worth >= 50000000 ANDStudioPres.cert# = RichExec.cert#;Section 2Exercise 8.2.1The views RichExec and StudioPres are updatable; however, the StudioPres view needs to be created with a subquery.CREATE VIEW StudioPres (name, address, cert#) ASSELECT , MovieExec.address, MovieExec.cert# FROM MovieExec WHERE MovieExec.cert# IN (SELECT presCt# from Studio);Exercise 8.2.2a) Yes, the view is updatable.b)CREATE TRIGGER DisneyComedyInsertINSTEAD OF INSERT ON DisneyComediesREFERENCING NEW ROW AS NewRowFOR EACH ROWINSERT INTO Movies(title, year, length, studioName, genre)VALUES(NewRow.title, NewRow.year, NewYear.length, ‘Disney’, ‘comedy’);c)CREATE TRIGGER DisneyComedyUpdateINSTEAD OF UPDATE ON DisneyComediesREFERENCING NEW ROW AS NewRowFOR EACH ROWUPDATE Movies SET length NewRow.lengthWHERE title = NewRow.title AND year = NEWROW.year ANDstudionName = ‘Disney’ AND genre = ‘comedy’;Exercise 8.2.3a) No, the view is not updatable since it is constructed from two different relations.b)CREATE TRIGGER NewPCInsertINSTEAD OF INSERT ON NewPCREFERENCING NEW ROW AS NewRowFOR EACH ROW(INSERT INTO Product VALUES(NewRow.maker, NewRow.model, ‘pc’))(INSERT INTO PC VALUES(NewRow.model, NewRow.speed, NewRow.ram, NewRow.hd, NewRow.price));c)CREATE TRIGGER NewPCUpdateINSTEAD OF UPDATE ON NewPCREFERENCING NEW ROW AS NewRowFOR EACH ROWUPDATE PC SET price = NewPC.price where model = NewPC.model;d)CREATE TRIGGER NewPCDeleteINSTEAD OF DELETE ON NeePCREFERENCING OLD ROW AS OldRowFOR EACH ROW(DELETE FROM Product WHERE model = OldRow.model)(DELETE FROM PC where model = OldRow.model);Section 3Exercise 8.3.1a)CREATE INDEX NameIndex on Studio(name);b)CREATE INDEX AddressIndex on MovieExec(address);c)CREATE INDEX GenreIndex on Movies(genre, length);Section 4Exercise 8.4.1Action No Index Star Index Movie Index Both Indexes Q110041004Q210010044I2446 Average 2 + 98p1 + 98p2 4 + 96 p2 4 + 96 p1 6 – 2 p1 – 2 p2 Exercise 8.4.2Q1 = SELECT * FROM Ships WHERE name = n;Q2 = SELECT * FROM Ships WHERE class = c;Q3 = SELECT * FROM Ships WHERE launched = y;I = InsertsIndexes Actions None Name Class Launched Name &ClassName &LaunchedClass &LaunchedThreeIndexesQ1502505022502 Q21121212 2 Q35050502650262626 I24446668Average 2 +48p1 -p2 +48p34 +46 p3- 2 p1- 3 p24 +46p1 -2p2 +46p34 + 46p1- 3p2 +22p36 - 4p1- 4p2 +44p36 - 4p1 -5p2 + 20p36 - 44p1 -4p2 + 20p38 - 6p1 -6p2 + 18p3The best choice of indexes (name and launched) has an average cost of 6 - 4p1 - 5p2 + 20p3 per operation.Section 5Exercise 8.5.1Updates to movies that involves title or yearUPDATE MovieProd SET title = ‘newTitle’ where title=’oldTitle’ AND year = oldYear; UPDATE MovieProd SET year = newYear where title=’oldYitle’ AND year = oldYear; Update to MovieExec involving cert#DELETE FROM MovieProdWHERE (title, year) IN (SELECT title, yearFROM Movies, MovieExecWHERE cert# = oldCert# AND cert# = producerC#);INSERT INTO MovieProdSELECT title, year, nameFROM Movies, MovieExecWHERE cert# = newCert# AND cert# = producerC#;Exercise 8.5.2Insertions, deletions, and updates to the base tables Product and PC would require a modification of the materialized view.Insertions into Product with type equal to ‘pc’:INSERT INTO NewPCSELECT maker, model, speed, ram, hd, price FROM Product, PC WHEREProduct.model = newModel and Product.model = PC.model;Insertions into PC:INSERT INTO NewPCSELECT maker, ‘newModel’, ‘newSpeed’, ‘newRam’, ‘newHd’, ‘newPrice’FROM Product WHERE model = ‘newModel’;Deletions from Product with type equal to ‘pc’:DELETE FROM NewPC WHERE maker = ‘deletedMaker’ ANDmodel=’deletedModel’;Deletions from PC:DELETE FROM NewPC WHERE model = ‘deletedModel’;Updates to PC:Update NewPC SET speed=PC.speed, ram=PC.ram, hd=PC.hd, price=PC.price FROM PC where model=pc.model;Update to the attribute ‘model’ needs to be treated as a delete and an insert. Updates to Product:Any changes to a Product tuple whose type is ‘pc’ need to be treated as a delete or an insert, or both.Exercise 8.5.3Modifications to the base tables that would require a modification to the materialized view: inserts and deletes from Ships, deletes from class, updates to a Class’ displacement. Deletions from Ship:UPDATE ShipStats SETdisplacement=((displacement * count) –(SELECT displacementFROM ClasssesWHERE class = ‘DeletedShipClass’)) / (count – 1),count = count – 1WHEREcountry = (SELECT country FROM Classes WHERE class=’DeletedShipClass’); Insertions into Ship:Update ShipStat SETdisplacement=((displacement*count) +(SELECT displacement FROM ClassesWHERE class=’InsertedShipClass’)) / (count + 1),count = count + 1WHEREcountry = (SELECT country FROM Classes WHERE classes=’InsertedShipClass); Deletes from Classes:NumRowsDeleted = SELECT count(*) FROM ships WHERE class = ‘DeletedClass’; UPDATE ShipStats SETdisplacement = (displacement * count) - (DeletedClassDisplacement *NumRowsDeleted)) / (count – NumRowsDeleted),count = count – NumRowsDeletedWHERE country = ‘DeletedClassCountry’;Update to a Class’ displacement:N = SELECT count(*) FROM Ships where class = ‘UpdatedClass’;UPDATE ShipsStat SETdisplacement = ((displacement * count) + ((oldDisplacement – newDisplacement) * N))/countWHEREcountry = ‘UpdatedClassCountry’;Exercise 8.5.4Queries that can be rewritten with the materialized view:Names of stars of movies produced by a certain producerSELECT starNameFROM StarsIn, Movies, MovieExecWHERE movieTitle = title AND movieYear = year AND producerC# = cert# AND name = ‘Max Bialystock’;Movies produced by a certain producerSELECT title, yearFROM Movies, MovieExecWhere producerC# = cert# AND name = ‘George Lucas’;Names of producers that a certain star has worked withSELECT nameFROM Movies, MovieExec, StarsInWhere producerC#=cert# AND title=movieTitle AND year=movieYear AND starName=’Carrie Fisher’;The number of movies produced by given producerSELECT count(*)FROM Movies, MovieExecWHERE producerC#=cert# AND name = ‘George Lucas‘;Names of producers who also starred in their own moviesSELECT nameFROM Movies, StarsIn, MovieExecWHERE producerC#=cert# AND movieTitle = title AND movieYear = year AND = starName;The number of stars that have starred in movies produced by a certain producer SELECT count(DISTINCT starName)FROM Movies, StarsIn, MovieExecWHERE producerC#=cert# AND movieTitle = title AND movieYear = year AND name ‘George Lucas’;The number of movies produced by each producerSELECT name, count(*)FROM Movies, MovieExecWHERE producerC#=cert# GROUP BY name。
Oracle数据库基础教程-参考答案
Oracle数据库基础教程参考答案第1章Oracle数据库概述1.简答题(1)数据是描述事物的符号,是数据库中存储的基本对象。
在计算机中,用记录的形式来描述数据。
数据与数据的解释即数据的语义是紧密结合的。
数据库是指按一定的数据模型组织、描述和存储的数据的集合。
数据库管理系统是位于操作系统与用户之间的一层数据管理软件。
数据库系统是指数据库、数据库管理系统与计算机系统的结合。
通常,在不引起混淆的情况下将数据库系统简称为数据库。
(2)数据库管理系统的主要功能包括:数据定义、数据操纵、数据库运行与控制、数据库建立与维护、数据字典定义以及数据通信等。
数据库管理系统的内部分多个层次,由应用层、语言定义及其翻译处理层、数据存取层、数据存储层、操作系统以及数据库组成。
(3)数据库系统由数据库、操作系统、数据库管理系统、开发工具、应用系统、数据库管理员以及数据库用户组成。
(4)概念模型是用简单、清晰、用户易于理解的概念来描述现实世界具体事物及事物之间的关系。
它是现实世界到信息世界的抽象,是数据库设计人员进行数据库设计的工具,与具体的数据库管理系统无关。
组织数据模型是从数据组织方式的角度来描述信息,它决定了数据在数据库中的组织结构。
(5)E-R图由3个要素组成:实体、联系与属性。
实体之间的联系有1:1、1:n、n:n三种类型。
(6)Oracle之所以得到广大用户的青睐,其主要原因在于:支持多用户、大事务量的事务处理、提供标准操作接口、实施安全性控制和完整性控制、支持分布式数据处理、具有可移值性、可兼容性和可连接性。
(7)目前市场上常见的关系数据库管理系统包括Oracle、DB2、Sybase和SQL Server等。
Oracle是当今最大的数据库厂商Oracle公司的数据库产品。
它是世界上第一个商品化的关系型数据库管理系统,也是第一个推出与数据库结合的第四代语言开发工具的数据库产品。
DB2是IBM公司于1983年推出的一个商业化关系数据库管理系统,它是基于System R 基础上实现的。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
25
§2.1 ODL
对象定义语言 Object Definition Language 以面向对象的观点、方法,说明数据库的概 念结构 可方便地直接转换成 OODBMS 的说明 经过努力,可以转换成 RDBMS 的说明
26
面向对象的设计
对象标识 — OID
对象与对象的区别
类
具有相同特性的对象归为一类 对象的归并必须有意义 属于同一类的对象其特性必须相同
3.数据库系统
是指在计算机系统中引入数据库后的系统
4
应用程序员
最终用户
应用系统 应用开发工具
DBMS 操作系统 数据库
数 据 库 系 统 构 成
数据库管理员 DBA
5
二、文件系统与数据库系统
保存信息的两种不同方法:
永久性的系统文件、数据库系统。
文件方式的问题:
数据的冗余和不一致 数据访问困难 数据孤立
inverse Star :: starredIn; //Star与Movie
的联系
};
32
联系的多重性
N:N
在联系中,每个C都和D的集合有关,而在反向 联系中,每个D都和C的集合有关
N:1
在联系中,每个C都和唯一的D有关,而在反向 联系中,每个D都和C的集合有关
1:1
在联系中,每个C都和唯一的D有关,而在反向 联系中,每个D都和唯一的C有关
10
§1.2 数据库管理系统的结构
DBMS的组成 模式更新 查询
数据、元数据
存储管理程序 事务管理程序
查询 处理程序
查询处理程序
存储 管理程序
数据 元数据
更新 事务
管理程序
11
DBMS的组成
数据、元数据
关于数据结构的信息(关于数据的数据) 索引(INDEX)
12
DBMS的组成
存储管理程序
文件管理程序 缓冲区管理查程序
13
DBMS的组成
查询处理程序
查询优化 磁盘访问,是查询的主要代价; 索引是查询优化的利器
14
DBMS的组成
事务管理程序
事务:是用户定义的一个数据库操作序列 事务的四个特性 原子性A 一致性C 隔离性I 持久性D
15
DBMS的组成
客户-服务器程序体系结构 浏览器-服务器体系结构
16
§1.3 未来的数据库系统
attribute string title; attribute integer year; attribute integer length; attribute enum Film { color, blackAndWhite } filmType; };
29
Interface Star { attribute stringname; attribute Struct Addr { string street,string city } address; };
20
数据库的设计步骤
需求收集和分析 设计概念结构
存什么 关系(联系)如何 ODL或E/R图,是各种数据模型的共同基础
设计逻辑结构 设计物理结构 物理实现
21
数据库的设计步骤
需求收集和分析 设计概念结构 设计逻辑结构
用什么数据模型 数据库的模式(database schema) 用户子模式
设计物理结构 物理实现
基于表的关系模型
物理无关、支持高级查询语言,
基于对象的面向对象模型
OO
OR
8
关系数据库系统
accountNO Balance Type
12345
1000.00 Savings
67890
2846.92 Che性
元组
9
关系数据库系统
关查询语言
SQL语言 SELECT balance FROM Accounts WHERE accountNO = 67890;
记录结构类型
30
联系
对象的引用 对象的关联 对象集合的引用(1:N) Relationship Set < Star > stars; 单一对象集合的引用(1:1) Relationship Star starOf;
31
反向联系
ODL要求显式表示存在的反向联系
Interface Movie { //Movie Class 的ODL说 明
§1.1 数据库系统的发展c
一、术语 1.数据库
是长期储存在计算机内的、 有组织的、可共享的数据 的集合。
1
2.数据库管理系统
2
息的环境
DBMS - DataBase Management
System 是处理数据库访问的软件。 提供数据库的用户接口。
DBMS的目的: 提供一个可以方便地、有效地存取数据库信
27
面向对象的设计
对象的三个特性
属性:特性 联系:引用 方法:函数
接口说明
interface < 名字 > { < 特性表 > }
28
属性
对象某方面的特征,属性就是数据 只由基本数据类型构成 属性的类型,不能是类、也不能从类中构造 Interface Movie { //Movie Class 的ODL说明
客户-服务器程序体系结构 浏览器-服务器体系结构
17
第二章 数据库建模 Database Modeling
数据库的设计步骤
需求收集和分析 设计概念结构 设计逻辑结构 设计物理结构 物理实现
19
数据库的设计步骤
需求收集和分析
用户关心什么 用户要什么结果
设计概念结构 设计逻辑结构 设计物理结构 物理实现
attribute stringtitle;
attribute integer year;
attribute integer length;
attribute enum Film { color, blackAndWhite } filmType;
relationship Set < Star > stars
22
数据库的设计步骤
需求收集和分析 设计概念结构 设计逻辑结构 设计物理结构
数据怎么存 根据DBMS产品、环境特点
物理实现
23
数据库的设计步骤
需求收集和分析 设计概念结构 设计逻辑结构 设计物理结构 物理实现
运行DDL 装入测试数据 应用程序
24
数据库的设计步骤
想法 需求
ODL E/R
关系
OODBMS RDBMS
完整性问题 原子性问题
并发访问异常
安全性问题
6
三、为什么用数据库
数据库方法能较好地解决以上的问题
数据的独立性 有效地访问数据 减少应用程序的开发时间 数据的一致性和安全性 统一的数据管理 并发的数据访问
7
四、数据库模型的发展
几种模型:
基于树的层次模型 基于图的网状模型
定长记录
物理相关、无高级查询语言