数据库系统基础教程第六章答案
- 格式:docx
- 大小:25.21 KB
- 文档页数:26
Database Systems: The Complete BookSolutions for Chapter 2Solutions for Section 2.1Exercise 2.1.1The E/R Diagram.Exercise 2.1.8(a)The E/R DiagramKobvxybzSolutions for Section 2.2Exercise 2.2.1The Addresses entity set is nothing but a single address, so we would prefer to make address an attribute of Customers. Were the bank to record several addresses for a customer, then it might make sense to have an Addresses entity set and make Lives-at a many-many relationship.The Acct-Sets entity set is useless. Each customer has a unique account set containing his or her accounts. However, relating customers directly to their accounts in a many-many relationship conveys the same information and eliminates the account-set concept altogether.Solutions for Section 2.3Exercise 2.3.1(a)Keys ssNo and number are appropriate for Customers and Accounts, respectively. Also, we think it does not make sense for an account to be related to zero customers, so we should round the edgeconnecting Owns to Customers. It does not seem inappropriate to have a customer with 0 accounts;they might be a borrower, for example, so we put no constraint on the connection from Owns to Accounts. Here is the The E/R Diagram,showing underlined keys and the numerocity constraint.Exercise 2.3.2(b)If R is many-one from E1 to E2, then two tuples (e1,e2) and (f1,f2) of the relationship set for R must be the same if they agree on the key attributes for E1. To see why, surely e1 and f1 are the same. Because R is many-one from E1 to E2, e2 and f2 must also be the same. Thus, the pairs are the same.Solutions for Section 2.4Exercise 2.4.1Here is the The E/R Diagram.We have omitted attributes other than our choice for the key attributes of Students and Courses. Also omitted are names for the relationships. Attribute grade is not part of the key for Enrollments. The key for Enrollements is studID from Students and dept and number from Courses.Exercise 2.4.4bHere is the The E/R Diagram Again, we have omitted relationship names and attributes other than our choice for the key attributes. The key for Leagues is its own name; this entity set is not weak. The key for Teams is its own name plus the name of the league of which the team is a part, e.g., (Rangers, MLB) or (Rangers, NHL). The key for Players consists of the player's number and the key for the team on which he or she plays. Since the latter key is itself a pair consisting of team and league names, the key for players is the triple (number, teamName, leagueName). e.g., JeffGarcia is (5, 49ers, NFL).Database Systems: The Complete BookSolutions for Chapter 3Solutions for Section 3.1Exercise 3.1.2(a)We can order the three tuples in any of 3! = 6 ways. Also, the columns can be ordered in any of 3! = 6 ways. Thus, the number of presentations is 6*6 = 36.Solutions for Section 3.2Exercise 3.2.1Customers(ssNo, name, address, phone)Flights(number, day, aircraft)Bookings(ssNo, number, day, row, seat)Being a weak entity set, Bookings' relation has the keys for Customers and Flights and Bookings' own attributes.Notice that the relations obtained from the toCust and toFlt relationships are unnecessary. They are:toCust(ssNo, ssNo1, number, day)toFlt(ssNo, number, day, number1, day1)That is, for toCust, the key of Customers is paired with the key for Bookings. Since both include ssNo, this attribute is repeated with two different names, ssNo and ssNo1. A similar situation exists for toFlt.Exercise 3.2.3Ships(name, yearLaunched)SisterOf(name, sisterName)Solutions for Section 3.3Exercise 3.3.1Since Courses is weak, its key is number and the name of its department. We do not havearelation for GivenBy. In part (a), there is a relation for Courses and a relation for LabCourses that has only the key and the computer-allocation attribute. It looks like:Depts(name, chair)Courses(number, deptName, room)LabCourses(number, deptName, allocation)For part (b), LabCourses gets all the attributes of Courses, as:Depts(name, chair)Courses(number, deptName, room)LabCourses(number, deptName, room, allocation)And for (c), Courses and LabCourses are combined, as:Depts(name, chair)Courses(number, deptName, room, allocation)Exercise 3.3.4(a)There is one relation for each entity set, so the number of relations is e. The relation for the root entity set has a attributes, while the other relations, which must include the key attributes, have a+k attributes.Solutions for Section 3.4Exercise 3.4.2Surely ID is a key by itself. However, we think that the attributes x, y, and z together form another key. The reason is that at no time can two molecules occupy the same point.Exercise 3.4.4The key attributes are indicated by capitalization in the schema below:Customers(SSNO, name, address, phone)Flights(NUMBER, DAY, aircraft)Bookings(SSNO, NUMBER, DAY, row, seat)Exercise 3.4.6(a)The superkeys are any subset that contains A1. Thus, there are 2^{n-1} such subsets, since each of the n-1 attributes A2 through An may independently be chosen in or out.Solutions for Section 3.5Exercise 3.5.1(a)We could try inference rules to deduce new dependencies until we are satisfied we have them all.A more systematic way is to consider the closures of all 15 nonempty sets of attributes.For the single attributes we have A+ = A, B+ = B, C+ = ACD, and D+ = AD. Thus, the only new dependency we get with a single attribute on the left is C->A.Now consider pairs of attributes:AB+ = ABCD, so we get new dependency AB->D. AC+ = ACD, and AC->D is nontrivial. AD+ = AD, so nothing new. BC+ = ABCD, so we get BC->A, and BC->D. BD+ = ABCD, giving usBD->A and BD->C. CD+ = ACD, giving CD->A.For the triples of attributes, ACD+ = ACD, but the closures of the other sets are each ABCD. Thus, we get new dependencies ABC->D, ABD->C, and BCD->A.Since ABCD+ = ABCD, we get no new dependencies.The collection of 11 new dependencies mentioned above is: C->A, AB->D, AC->D, BC->A, BC->D, BD->A, BD->C, CD->A, ABC->D, ABD->C, and BCD->A.Exercise 3.5.1(b)From the analysis of closures above, we find that AB, BC, and BD are keys. All other sets either do not have ABCD as the closure or contain one of these three sets.Exercise 3.5.1(c)The superkeys are all those that contain one of those three keys. That is, a superkey that is not a key must contain B and more than one of A, C, and D. Thus, the (proper) superkeys are ABC, ABD, BCD, and ABCD.Exercise 3.5.3(a)We must compute the closure of A1A2...AnC. Since A1A2...An->B is a dependency, surely B is in this set, proving A1A2...AnC->B.Exercise 3.5.4(a)Consider the relationThis relation satisfies A->B but does not satisfy B->A.Exercise 3.5.8(a)If all sets of attributes are closed, then there cannot be any nontrivial functional dependencies. For suppose A1A2...An->B is a nontrivial dependency. Then A1A2...An+ contains B and thus A1A2...An is not closed.Exercise 3.5.10(a)We need to compute the closures of all subsets of {ABC}, although there is no need to think about the empty set or the set of all three attributes. Here are the calculations for the remaining six sets: A+ = AB+ = BC+ = ACEAB+ = ABCDEAC+ = ACEBC+ = ABCDEWe ignore D and E, so a basis for the resulting functional dependencies for ABC are: C->A and AB->C. Note that BC->A is true, but follows logically from C->A, and therefore may be omitted from our list.Solutions for Section 3.6Exercise 3.6.1(a)In the solution to Exercise 3.5.1 we found that there are 14 nontrivial dependencies, including the three given ones and 11 derived dependencies. These are: C->A, C->D, D->A, AB->D, AB-> C, AC->D, BC->A, BC->D, BD->A, BD->C, CD->A, ABC->D, ABD->C, and BCD->A.We also learned that the three keys were AB, BC, and BD. Thus, any dependency above that does not have one of these pairs on the left is a BCNF violation. These are: C->A, C->D, D->A, AC->D, and CD->A.One choice is to decompose using C->D. That gives us ABC and CD as decomposed relations. CD is surely in BCNF, since any two-attribute relation is. ABC is not in BCNF, since AB and BC are its only keys, but C->A is a dependency that holds in ABCD and therefore holds in ABC. We must further decompose ABC into AC and BC. Thus, the three relations of the decomposition are AC, BC, and CD.Since all attributes are in at least one key of ABCD, that relation is already in 3NF, and no decomposition is necessary.Exercise 3.6.1(b)(Revised 1/19/02) The only key is AB. Thus, B->C and B->D are both BCNF violations. The derived FD's BD->C and BC->D are also BCNF violations. However, any other nontrivial, derived FD will have A and B on the left, and therefore will contain a key.One possible BCNF decomposition is AB and BCD. It is obtained starting with any of the four violations mentioned above. AB is the only key for AB, and B is the only key for BCD.Since there is only one key for ABCD, the 3NF violations are the same, and so is the decomposition.Solutions for Section 3.7Exercise 3.7.1Since A->->B, and all the tuples have the same value for attribute A, we can pair the B-value from any tuple with the value of the remaining attribute C from any other tuple. Thus, we know that R must have at least the nine tuples of the form (a,b,c), where b is any of b1, b2, or b3, and c is any of c1, c2, or c3. That is, we can derive, using the definition of a multivalued dependency, that each of the tuples (a,b1,c2), (a,b1,c3), (a,b2,c1), (a,b2,c3), (a,b3,c1), and (a,b3,c2) are also in R.Exercise 3.7.2(a)First, people have unique Social Security numbers and unique birthdates. Thus, we expect the functional dependencies ssNo->name and ssNo->birthdate hold. The same applies to children, so we expect childSSNo->childname and childSSNo->childBirthdate. Finally, an automobile has a unique brand, so we expect autoSerialNo->autoMake.There are two multivalued dependencies that do not follow from these functional dependencies. First, the information about one child of a person is independent of other information about that person. That is, if a person with social security number s has a tuple with cn,cs,cb, then if there isany other tuple t for the same person, there will also be another tuple that agrees with t except that it has cn,cs,cb in its components for the child name, Social Security number, and birthdate. That is the multivalued dependencyssNo->->childSSNo childName childBirthdateSimilarly, an automobile serial number and make are independent of any of the other attributes, so we expect the multivalued dependencyssNo->->autoSerialNo autoMakeThe dependencies are summarized below:ssNo -> name birthdatechildSSNo -> childName childBirthdateautoSerialNo -> autoMakessNo ->-> childSSNo childName childBirthdatessNo ->-> autoSerialNo autoMakeExercise 3.7.2(b)We suggest the relation schemas{ssNo, name, birthdate}{ssNo, childSSNo}{childSSNo, childName childBirthdate}{ssNo, autoSerialNo}{autoSerialNo, autoMake}An initial decomposition based on the two multivalued dependencies would give us {ssNo, name, birthDate}{ssNo, childSSNo, childName, childBirthdate}{ssNo, autoSerialNo, autoMake}Functional dependencies force us to decompose the second and third of these.Exercise 3.7.3(a)Since there are no functional dependencies, the only key is all four attributes, ABCD. Thus, each of the nontrvial multivalued dependencies A->->B and A->->C violate 4NF. We must separate out the attributes of these dependencies, first decomposing into AB and ACD, and then decomposing the latter into AC and AD because A->->C is still a 4NF violation for ACD. The final set of relations are AB, AC, and AD.Exercise 3.7.7(a)Let W be the set of attributes not in X, Y, or Z. Consider two tuples xyzw and xy'z'w' in the relation R in question. Because X ->-> Y, we can swap the y's, so xy'zw and xyz'w' are in R. Because X ->-> Z, we can take the pair of tuples xyzw and xyz'w' and swap Z's to get xyz'w and xyzw'. Similarly, we can take the pair xy'z'w' and xy'zw and swap Z's to get xy'zw' and xy'z'w.In conclusion, we started with tuples xyzw and xy'z'w' and showed that xyzw' and xy'z'w must also be in the relation. That is exactly the statement of the MVD X ->-> Y-union-Z. Note that the above statements all make sense even if there are attributes in common among X, Y, and Z.Exercise 3.7.8(a)Consider a relation R with schema ABCD and the instance with four tuples abcd, abcd', ab'c'd, and ab'c'd'. This instance satisfies the MVD A ->-> BC. However, it does not satisfy A ->-> B. For example, if it did satisfy A ->-> B, then because the instance contains the tuples abcd and ab'c'd, we would expect it to contain abc'd and ab'cd, neither of which is in the instance.Solutions for Chapter 4Solutions for Section 4.2Exercise 4.2.1class Customer {attribute string name;attribute string addr;attribute string phone;attribute integer ssNo;relationship Set<Account> ownsAcctsinverse Account::ownedBy;}class Account {attribute integer number;attribute string type;attribute real balance;relationship Set<Customer> ownedByinverse Customer::ownsAccts}Exercise 4.2.4class Person {attribute string name;relationship Person motherOfinverse Person::childrenOfFemalerelationship Person fatherOfinverse Person::childrenOfMalerelationship Set<Person> childreninverse Person::parentsOfrelationship Set<Person> childrenOfFemaleinverse Person::motherOfrelationship Set<Person> childrenOfMaleinverse Person::fatherOfrelationship Set<Person> parentsOfinverse Person::children}Notice that there are six different relationships here. For example, the inverse of the relationship that connects a person to their (unique) mother is a relationship that connects a mother (i.e., a female person) to the set of her children. That relationship, which we call childrenOfFemale, is different from the children relationship, which connects anyone -- male or female -- to their children.Exercise 4.2.7A relationship R is its own inverse if and only if for every pair (a,b) in R, the pair (b,a) is also in R. In the terminology of set theory, the relation R is ``symmetric.''Solutions for Section 4.3Exercise 4.3.1We think that Social Security number should me the key for Customer, and account number should be the key for Account. Here is the ODL solution with key and extent declarations.class Customer(extent Customers key ssNo){attribute string name;attribute string addr;attribute string phone;attribute integer ssNo;relationship Set<Account> ownsAcctsinverse Account::ownedBy;}class Account(extent Accounts key number){attribute integer number;attribute string type;attribute real balance;relationship Set<Customer> ownedByinverse Customer::ownsAccts}Solutions for Section 4.4Exercise 4.4.1(a)Since the relationship between customers and accounts is many-many, we should create a separate relation from that relationship-pair.Customers(ssNo, name, address, phone)Accounts(number, type, balance)CustAcct(ssNo, number)Exercise 4.4.1(d)Ther is only one attribute, but three pairs of relationships from Person to itself. Since motherOf and fatherOf are many-one, we can store their inverses in the relation for Person. That is, for each person, childrenOfMale and childrenOfFemale will indicate that persons's father and mother. The children relationship is many-many, and requires its own relation. This relation actually turns out to be redundant, in the sense that its tuples can be deduced from the relationships stored with Person. The schema:Persons(name, childrenOfFemale, childrenOfMale)Parent-Child(parent, child)Exercise 4.4.4You get a schema like:Studios(name, address, ownedMovie)Since name -> address is the only FD, the key is {name, ownedMovie}, and the FD has a left side that is not a superkey.Exercise 4.4.5(a,b,c)(a) Struct Card { string rank, string suit };(b) class Hand {attribute Set theHand;};For part (c) we have:Hands(handId, rank, suit)Notice that the class Hand has no key, so we need to create one: handID. Each hand has, in the relation Hands, one tuple for each card in the hand.Exercise 4.4.5(e)Struct PlayerHand { string Player, Hand theHand };class Deal {attribute Set theDeal;}Alternatively, PlayerHand can be defined directly within the declaration of attribute theDeal. Exercise 4.4.5(h)Since keys for Hand and Deal are lacking, a mechanical way to design the database schema is to have one relation connecting deals and player-hand pairs, and another to specify the contents of hands. That is:Deals(dealID, player, handID)Hands(handID, rank, suit)However, if we think about it, we can get rid of handID and connect the deal and the player directly to the player's cards, as:Deals(dealID, player, rank, suit)Exercise 4.4.5(i)First, card is really a pair consisting of a suit and a rank, so we need two attributes in a relation schema to represent cards. However, much more important is the fact that the proposed schema does not distinguish which card is in which hand. Thus, we need another attribute that indicates which hand within the deal a card belongs to, something like:Deals(dealID, handID, rank, suit)Exercise 4.4.6(c)Attribute b is really a bag of (f,g) pairs. Thus, associated with each a-value will be zero or more (f,g) pairs, each of which can occur several times. We shall use an attribute count to indicate the number of occurrences, although if relations allow duplicate tuples we could simply allow duplicate (a,f,g) triples in the relation. The proposed schema is:C(a, f, g, count)Solutions for Section 4.5Exercise 4.5.1(b)Studios(name, address, movies{(title, year, inColor, length,stars{(name, address, birthdate)})})Since the information about a star is repeated once for each of their movies, there is redundancy. To eliminate it, we have to use a separate relation for stars and use pointers from studios. That is: Stars(name, address, birthdate)Studios(name, address, movies{(title, year, inColor, length,stars{*Stars})})Since each movie is owned by one studio, the information about a movie appears in only one tuple of Studios, and there is no redundancy.Exercise 4.5.2Customers(name, address, phone, ssNo, accts{*Accounts})Accounts(number, type, balance, owners{*Customers})Solutions for Section 4.6Exercise 4.6.1(a)We need to add new nodes labeled George Lucas and Gary Kurtz. Then, from the node sw (which represents the movie Star Wars), we add arcs to these two new nodes, labeled directedBy and producedBy, respectively.Exercise 4.6.2Create nodes for each account and each customer. From each customer node is an arc to a node representing the attributes of the customer, e.g., an arc labeled name to the customer's name. Likewise, there is an arc from each account node to each attribute of that account, e.g., an arc labeled balance to the value of the balance.To represent ownership of accounts by customers, we place an arc labeled owns from each customer node to the node of each account that customer holds (possibly jointly). Also, we placean arc labeled ownedBy from each account node to the customer node for each owner of that account.Exercise 4.6.5In the semistructured model, nodes represent data elements, i.e., entities rather than entity sets. In the E/R model, nodes of all types represent schema elements, and the data is not represented at all. Solutions for Section 4.7Exercise 4.7.1(a)<STARS-MOVIES><STAR starId = "cf" starredIn = "sw, esb, rj"><NAME>Carrie Fisher</NAME><ADDRESS><STREET>123 Maple St.</STREET><CITY>Hollywood</CITY></ADDRESS><ADDRESS><STREET>5 Locust Ln.</STREET><CITY>Malibu</CITY></ADDRESS></STAR><STAR starId = "mh" starredIn = "sw, esb, rj"><NAME>Mark Hamill</NAME><ADDRESS><STREET>456 Oak Rd.<STREET><CITY>Brentwood</CITY></ADDRESS></STAR><STAR starId = "hf" starredIn = "sw, esb, rj, wit"><NAME>Harrison Ford</NAME><ADDRESS><STREET>whatever</STREET><CITY>whatever</CITY></ADDRESS></STAR><MOVIE movieId = "sw" starsOf = "cf, mh"><TITLE>Star Wars</TITLE><YEAR>1977</YEAR></MOVIE><MOVIE movieId = "esb" starsOf = "cf, mh"><TITLE>Empire Strikes Back</TITLE><YEAR>1980</YEAR></MOVIE><MOVIE movieId = "rj" starsOf = "cf, mh"><TITLE>Return of the Jedi</TITLE><YEAR>1983</YEAR></MOVIE><MOVIE movieID = "wit" starsOf = "hf"><TITLE>Witness</TITLE><YEAR>1985</YEAR></MOVIE></STARS-MOVIES>Exercise 4.7.2<!DOCTYPE Bank [<!ELEMENT BANK (CUSTOMER* ACCOUNT*)><!ELEMENT CUSTOMER (NAME, ADDRESS, PHONE, SSNO)><!A TTLIST CUSTOMERcustId IDowns IDREFS><!ELEMENT NAME (#PCDATA)><!ELEMENT ADDRESS (#PCDA TA)><!ELEMENT PHONE (#PCDATA)><!ELEMENT SSNO (#PCDA TA)><!ELEMENT ACCOUNT (NUMBER, TYPE, BALANCE)><!A TTLIST ACCOUNTacctId IDownedBy IDREFS><!ELEMENT NUMBER (#PCDA TA)><!ELEMENT TYPE (#PCDA TA)><!ELEMENT BALANCE (#PCDATA)>]>BookSolutions for Chapter 5Solutions for Section 5.2Exercise 5.2.1(a)PI_model( SIGMA_{speed >= 1000} ) (PC)Exercise 5.2.1(f)The trick is to theta-join PC with itself on the condition that the hard disk sizes are equal. That gives us tuples that have two PC model numbers with the same value of hd. However, these two PC's could in fact be the same, so we must also require in the theta-join that the model numbers be unequal. Finally, we want the hard disk sizes, so we project onto hd.The expression is easiest to see if we write it using some temporary values. We start by renaming PC twice so we can talk about two occurrences of the same attributes.R1 = RHO_{PC1} (PC)R2 = RHO_{PC2} (PC)R3 = R1 JOIN_{PC1.hd = PC2.hd AND PC1.model <> PC2.model} R2R4 = PI_{PC1.hd} (R3)Exercise 5.2.1(h)First, we find R1, the model-speed pairs from both PC and Laptop. Then, we find from R1 those computers that are ``fast,'' at least 133Mh. At the same time, we join R1 with Product to connect model numbers to their manufacturers and we project out the speed to get R2. Then we join R2 with itself (after renaming) to find pairs of different models by the same maker. Finally, we get our answer, R5, by projecting onto one of the maker attributes. A sequence of steps giving the desired expression is: R1 = PI_{model,speed} (PC) UNION PI_{model,speed} (Laptop)R2 = PI_{maker,model} (SIGMA_{speed>=700} (R1) JOIN Product)R3 = RHO_{T(maker2, model2)} (R2)R4 = R2 JOIN_{maker = maker2 AND model <> model2} (R3)R5 = PI_{maker} (R4)Exercise 5.2.2Here are figures for the expression trees of Exercise 5.2.1 Part (a)Part (f)Part (h). Note that the third figure is not really a tree, since it uses a common subexpression. We could duplicate the nodes to make it a tree, but using common subexpressions is a valuable form of query optimization. One of the benefits one gets from constructing ``trees'' for queries is the ability to combine nodes that represent common subexpressions.Exercise 5.2.7The relation that results from the natural join has only one attribute from each pair of equated attributes. The theta-join has attributes for both, and their columns are identical.Exercise 5.2.9(a)If all the tuples of R and S are different, then the union has n+m tuples, and this number is the maximum possible.The minimum number of tuples that can appear in the result occurs if every tuple of one relation also appears in the other. Surely the union has at least as many tuples as the larger of R and that is, max(n,m) tuples. However, it is possible for every tuple of the smaller to appear in the other, so it is possible that there are as few as max(n,m) tuples in the union.Exercise 5.2.10In the following we use the name of a relation both as its instance (set of tuples) and as its schema (set of attributes). The context determines uniquely which is meant.PI_R(R JOIN S) Note, however, that this expression works only for sets; it does not preserve the multipicity of tuples in R. The next two expressions work for bags.R JOIN DELTA(PI_{R INTERSECT S}(S)) In this expression, each projection of a tuple from S onto the attributes that are also in R appears exactly once in the second argument of the join, so it preserves multiplicity of tuples in R, except for those thatdo not join with S, which disappear. The DELTA operator removes duplicates, as described in Section 5.4.R - [R - PI_R(R JOIN S)] Here, the strategy is to find the dangling tuples of R and remove them.Solutions for Section 5.3Exercise 5.3.1As a bag, the value is {700, 1500, 866, 866, 1000, 1300, 1400, 700, 1200, 750, 1100, 350, 733}. The order is unimportant, of course. The average is 959.As a set, the value is {700, 1500, 866, 1000, 1300, 1400, 1200, 750, 1100, 350, 733}, and the average is 967. H3>Exercise 5.3.4(a)As sets, an element x is in the left-side expression(R UNION S) UNION Tif and only if it is in at least one of R, S, and T. Likewise, it is in the right-side expressionR UNION (S UNION T)under exactly the same conditions. Thus, the two expressions have exactly the same members, and the sets are equal.As bags, an element x is in the left-side expression as many times as the sum of the number of times it is in R, S, and T. The same holds for the right side. Thus, as bags the expressions also have the same value.Exercise 5.3.4(h)As sets, element x is in the left sideR UNION (S INTERSECT T)if and only if x is either in R or in both S and T. Element x is in the right side(R UNION S) INTERSECT (R UNION T)if and only if it is in both R UNION S and R UNION T. If x is in R, then it is in both unions. If x is in both S and T, then it is in both union. However, if x is neither in R nor in both of S and T, then it cannot be in both unions. For example, suppose x is not in R and not in S. Then x is not in R UNION S. Thus, the statement of when x is in the right side is exactly the same as when it is in the left side: x is either in R or in both of S and T.Now, consider the expression for bags. Element x is in the left side the sum of the number of times it is in R plus the smaller of the number of times x is in S and the number of times x is in T. Likewise, the number of times x is in the right side is the smaller ofThe sum of the number of times x is in R and in S.The sum of the number of times x is in R and in T.A moment's reflection tells us that this minimum is the sum of the number of times x is in R plus the smaller of the number of times x is in S and in T, exactly as for the left side.Exercise 5.3.5(a)For sets, we observe that element x is in the left side(R INTERSECT S) - T。
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 ;。
(完整版)数据库原理及其应⽤教程课后答案第⼀章1.2.从程序和数据之间的关系分析⽂件系统和数据库系统之间的区别和联系?(1)⽂件系统与数据库系统之间的区别⽂件系统⽤⽂件将数据长期保存在外存上,数据库系统则⽤数据库统⼀存储数据;⽂件系统中程序和数据有⼀定的联系,⼆数据库系统中程序和数据分离;⽂件系统⽤操作系统中的存取⽅法对数据进⾏管理,数据库系统则⽤DBMS 统⼀管理和控制数据;⽂件系统实现以⽂件为单位的数据共享,⼆数据库系统实现以记录和字段为单位的数据共享。
(2)⽂件系统和数据库系统之间的联系均为数据组织的管理技术;均由数据管理软件管理数据,程序与数据之间⽤存取⽅法进⾏转换;数据库系统是在⽂件系统的基础上发展起来的。
1.8.什么是数据库的数据独⽴性?它包含了哪些内容? 物理独⽴性?所谓数据的独⽴性是指数据库中的数据与应⽤程序间相互独⽴,即数据的逻辑结构、存储结构以及存取⽅式的改变不影响应⽤程序。
数据独⽴性分两级:物理独⽴性和逻辑独⽴性物理独⽴性是指当数据的物理结构改变时,通过修改映射,使数据库整体逻辑结构不受影响,进⽽⽤户的逻辑结构以及应⽤程序不⽤改变。
逻辑独⽴性是指当数据库的整体逻辑结构发⽣改变时,通过修改映射,使⽤户的逻辑结构以及应⽤程序不⽤改变。
1.11.解释实体、属性、实体键、实体集、实体型、实体联系类型、记录、数据项、字段、记录型、⽂件、实体模型、数据模型的含义。
实体:客观存在并且可以相互区别的“事物”称为实体。
属性:实体所具有的某⼀特性称为属性。
实体键:在实体型中,能唯⼀标识⼀个实体的属性或属性集称为实体的键。
实体集:同型实体的集合称为实体集。
实体型:具有相同属性的实体必然具有共同的特征,所以,⽤实体名及其属性名来抽象和描述同类实体,称为实体型。
实体联系类型:⼀对⼀联系(1:1);⼀对多联系(1:n);多对多联系(m:n)记录:(record)字段的有序集合称为记录。
数据项:标记实体属性的命名单位称为字段,也称为数据项。
第六章习题一、选择题:形框代替形框表示实体的属性。
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.关系规范化中的操作异常有哪些?是由什么引起的?解决方法是什么?操作异常有数据冗余问题、数据更新问题、数据插入问题、数据删除问题。
其产生的原因是关系模式没有设计好,某些属性之间存在不良的函数依赖。
解决方法是进行模式分解,把一个不良的关系模式分解为两个或多个关系模式。
2.第一、二、三范式的定义分别是什么?第一范式:不包含重复组的关系,即不包含非原子项的属性。
第二范式:属于第一范式,且每个非主属性都完全函数依赖于主码。
第三范式:属于第二范式,且所有非主属性都不传递依赖于主码。
3.什么是部分函数依赖?什么是传递函数依赖?举例说明。
部分函数依赖:在关系模式R(U)中,如果X→Y,并且存在X的一个真子集X0,使得X0→Y,则称Y对X部分函数依赖。
传递函数依赖:在关系模式R(U)中,设X,Y,Z是U的不同的属性子集,如果X确定Y、Y确定Z,且有X不包含Y,Y不确定X,(X∪Y)∩Z=空集合,则称Z传递函数依赖于X。
4.第三范式的关系模式是否一定不包含部分函数依赖?不一定。
可列举以下关系模式:关系模式(A,B,C,D)中AB和BC为候选码,AC相互依赖,D完全依赖于AB或BC,则ABC为主属性。
令AB为主码,此关系模式中各属性不可再分,且非主属性D完全依赖于主码,并不传递依赖于主码,则其为第三范式,但主属性C部分依赖于主码AB。
实际例子:(学号,课程号,身份证号,成绩)候选码:(学号,课程号)和(课程号,身份证号)主码:(学号,课程号)主属性:学号,课程号,身份证号非主属性:成绩5.对于主码只有一个属性组成的关系模式,如果它是第一范式,则它是否也一定是第二范式?第一范式主码只有一个属性,则不存在部分依赖关系,即一定是第二范式。
6.学生修课(学号,姓名,所在系,性别,课程号,课程名,学分,成绩)关系模式…。
指出候选码,判断是第几范式,转为第三范式并指出主码与外码。
第六章习题答案一、选择填空1、A2、C3、D4、B5、D6、A7、C8、A9、D 10、A11、C 12、A 13、B 14、C 15、C 16、D 17、B 18、C 19、A 20、D21、C 22、B二、判断下列描述的正确性,对者划√,错者划×。
1、√2、×3、×4、×5、√6、√7、×8、√9、×10、√11、√12、√13、√14、√15、×16、√17、√18、√19、√20、×21、×22、×三、分析下列程序的输出结果。
1、运行该程序输出结果如下所示。
Default constructor calledConstructor calleda=0,b=0a=4,b=82、运行该程序输出结果如下所示。
a=7,b=93、运行该程序输出结果如下所示。
1044、运行该程序输出结果如下所示。
1035,789.5045、运行该程序输出结果如下所示。
1{}{0,1,2,3,4,5,6,7,8}1{11,12,13,14,15,16,17,18,19}{19,18,17,16,15,14,13,12,11}6、运行该程序输出结果如下所示。
Starting1:Default constructor called.Default constructor called.Default constructor called.Eding1:Starting2:Constructor: a=5,b=6Constructor: a=7,b=8Constructor: a=9,b=10Ending2:Destructor called.a=9,b=10Destructor called.a=7,b=8Destructor called.a=5,b=6Destructor called.a=5,b=6Destructor called.a=3,b=4Destructor called.a=1,b=27、运行该程序输出结果如下所示。
第六章三、设计题1.一个图书管理系统中有如下信息。
图书:书号、书名、数量、位置借书人:借书证号、姓名、单位出版社:出版社名、邮编、地址、电话、E-mail其中约定:任何人可以借多种书,任何一种书可以被多个人借,借书和还书时,要登记相应的借书日期和还书日期;一个出版社可以出版多种书籍,同一本书仅为一个出版社所出版,出版社名具有唯一性。
根据以上情况,完成如下设计。
(1)设计该系统的E-R图。
(2)将E-R图转换为关系模式。
(3)指出转换后的每个关系模式的主码。
关系模式及主码图书(书号,书名,数量,位置,出版社名)借书人(借书证号,姓名,单位)出版社(出版社名,邮编,地址,电话,E-mail)2.图6-26(a)、(b)和(c)给出某企业管理系统三个不同的局部E-R图,将其合成一个全局E-R图,并设置各个实体以及联系的属性(允许增加必要的属性,也可将实体的属性改为联系的属性)。
(a ) (b ) (c ) 图6-26 局部E-R 图各实体的属性如下。
部门:部门号、部门名、电话、地址职员:职员号、职员名、职务、年龄、性别设备:设备号、名称、规格、价格零件:零件号、名称、规格、价格3.经过需求分析可知,某医院病房计算机管理系统中需要管理以下信息。
科室:科室名、科室地址、科室电话、医生姓名病房:病房号、床位号、所属科室医生:工作证号、姓名、性别、出生日期、联系电话、职称、所属科室名病人:病历号、姓名、性别、出生日期、诊断记录、主管医生、病房号其中,一个科室有多个病房、多名医生,一个病房只属于一个科室,一个医生只属于一个科室,但可负责多个病人的诊治,一个病人的主管医生只有一个。
根据以上需求分析的情况,完成以下有关的设计。
(1)画出该计算机管理系统中有关信息的E-R 图。
(2)将该E-R 图转换为对应的关系模式。
(3)指出转换以后的各关系模式的范式等级和对应的候选码。
科室(科室名,科室地址,科室电话)病房(病房号,床位号,科室名)医生(工作证号,姓名,性别,出生日期,联系电话,职称,科室名)病人(病历号,姓名,性别,出生日期,诊断记录,主管医生,病房号)4. 排课是教学环节中的重要过程,该过程包括以下实体。
8.4 答案一、单选题1. ②2. ③3. ④4. ④5. ③6. ②7. ②8. ③9. ① 10.②11.① 12.① 13.② 14.② 15.① 16.① 17.③ 18.③ 19.④ 20.①21.① 22.② 23.④ 24.① 25.① 26.③ 27.② 28.③ 29.② 30.①31.④ 32.① 33.③ 34.③ 35.② 36.④ 37.④ 38.① 39.③ 40.①41.② 42.④ 43.④ 44.③ 45.④ 46.② 47.④ 48.④ 49.④ 50.①51.④ 52.④ 53.④ 54.① 55.③ 56.② 57.④ 58.② 59.③ 60.③61.③二、是非题1. Y2. N3. N4. Y5. N6. Y7. Y8. Y9. N 10.Y 11.Y 12.Y 13.Y 14.Y 15.Y 16.N 17.Y 18.N 19.N 20.N 21.Y 22.Y 23.N 24.Y 25.Y 26.Y 27.Y 28.Y 29.Y 30.N 31.N 32.Y 33.N 34.Y 35.Y 36.Y 37.Y 38.N 39.N 40.Y 41.Y 42.Y 43.N 44.Y 45.Y 46.Y三、多选题1. ②④2. ①②④3. ①②④4. ①②④5. ①②6. ②③7. ①④8. ②③④9. ①③ 10.①③④11.②③④ 12.②③ 13.①② 14.①②③④ 15.①④16.①② 17.①② 18.①②③四、填空题1. 顺序、选择、循环、模块2. MODIFY COMMAND <文件名>3. ?|?? <表达式>、@ 行,列 SAY <表达式>4. (编号,8)=15. zkzh6. 17. 68. &nh.B&dbn9. 100以内自然数的和、Y=***10. p="Y".OR.p="y"(或UPPER(p)= "Y")11. 15.000 4.000 312. >0、j=j+1、LTRIM(STR(n))13. .NOT.EOF()、SKIP14. "B"+STR(n,1)15. REPLACE 等级 WITH "优秀"16. 命令窗口、Windows操作系统17. 018. 3.5 519. 2、120. 2 1 4 5 3321. 6 4 222. Q=***、Q=***23. WITH sfgz,tax、SKIP、PARA a,x24. s<=100.AND.s>10、s<=10.AND.s>1、s<=125. SET PROC TO ABC、DO ZZ26. SET PROC TO <过程文件名>、SET PROC TO、CLOSE PROC27. 过程文件、程序28. 变量作用的范围、全局变量、局部变量、私有变量29. .F.30. 声明式、隐含式31. RETURN、RETURN TO MASTER32. 调用语句、最后执行的那一条33. 嵌套34. 形式参数、变量35. 调用程序、<表达式>36. 837. 10 2010 238. Y=200、Y=10039. 自然数: 奇数40. 041. -22942. S=108943. "同学们"、300五、问答题1. 将需要执行的命令按照一定的要求有序地编排在一起,并以文件的方式存储在磁盘上,这种文件称为命令文件。
第6章习题与参考答案一.单项选择题1.下列关于视图的说法,正确的是(B)。
A.视图与基本表一样,也存储数据B.对视图的操作最终都转换为对基本表的操作C.视图的数据源只能是基本表D.所有视图都可以实现对数据的增、删、改、查操作2.在视图的定义语句中,只能包含(A)。
A.数据查询语句B.数据增、删、改语句C.创建表的语句D.全部都可以3.视图对应数据库三级模式中的(A)。
A.外模式B.内模式C.模式D.其他4.下列关于视图的说法,正确的是(B)。
A.通过视图可以提高数据查询效率B.视图提供了数据的逻辑独立性C.视图只能建立在基本表上D.定义视图的语句可以包含数据更改语句5.创建视图的主要作用是(D)。
A.提高数据查询效率B.维护数据的完整性约束C.维护数据的一致性D.提供用户视角的数据6.设有学生表(学号,姓名,所在系)。
下列建立统计每个系的学生人数的视图语句中,正确的是(D)。
A.CREATE VIEW v1ASSELECT 所在系, COUNT(*) FROM 学生表GROUP BY 所在系B.CREATE VIEW v1ASSELECT 所在系, SUM(*) FROM 学生表GROUP BY 所在系C.CREATE VIEW v1(系名,人数) ASSELECT 所在系, SUM(*) FROM 学生表GROUP BY 所在系D.CREATE VIEW v1(系名,人数) ASSELECT 所在系, COUNT(*) FROM 学生表GROUP BY 所在系7.设用户在某数据库中经常需要进行如下查询操作:SELECT * FROM T WHERE C1='A' ORDER BY C2设T表中已在C1列上建立了主键约束,且该表只建有该约束。
为提高该查询的执行效率,下列方法中可行的是(C)。
A.在C1列上建立一个聚集索引,在C2列上建立一个非聚集索引B.在C1和C2列上分别建立一个非聚集索引C.在C2列上建立一个非聚集索引D.在C1和C2列上建立一个组合的非聚集索引8.下列关于索引的说法,正确的是(C)。
数据库系统原理版课后习题参考答案答案仅供参考第一章数据库系统概述选择题B、B、A简答题1.请简述数据,数据库,数据库管理系统,数据库系统的概念。
P27数据是描述事物的记录符号,是指用物理符号记录下来的,可以鉴别的信息。
数据库即存储数据的仓库,严格意义上是指长期存储在计算机中的有组织的、可共享的数据集合。
数据库管理系统是专门用于建立和管理数据库的一套软件,介于应用程序和操作系统之间。
数据库系统是指在计算机中引入数据库技术之后的系统,包括数据库、数据库管理系统及相关实用工具、应用程序、数据库管理员和用户。
2.请简述早数据库管理技术中,与人工管理、文件系统相比,数据库系统的优点。
数据共享性高数据冗余小易于保证数据一致性数据独立性高可以实施统一管理与控制减少了应用程序开发与维护的工作量3.请简述数据库系统的三级模式和两层映像的含义。
P31答:数据库的三级模式是指数据库系统是由模式、外模式和内模式三级工程的,对应了数据的三级抽象。
两层映像是指三级模式之间的映像关系,即外模式/模式映像和模式/内模式映像。
4.请简述关系模型与网状模型、层次模型的区别。
P35使用二维表结构表示实体及实体间的联系建立在严格的数学概念的基础上概念单一,统一用关系表示实体和实体之间的联系,数据结构简单清晰,用户易懂易用存取路径对用户透明,具有更高的数据独立性、更好的安全保密性。
.第二章关系数据库选择题C、C、D简答题1.请简述关系数据库的基本特征。
P48答:关系数据库的基本特征是使用关系数据模型组织数据。
2.请简述什么是参照完整性约束。
P55答:参照完整性约束是指:若属性或属性组F是基本关系R的外码,与基本关系S的主码K相对应,则对于R中每个元组在F上的取值只允许有两种可能,要么是空值,要么与S中某个元组的主码值对应。
3.请简述关系规范化过程。
答:对于存在数据冗余、插入异常、删除异常问题的关系模式,应采取将一个关系模式分解为多个关系模式的方法进行处理。
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中的许多管理性或信息性的活动都可以被顺利地完成。
一、选择题60(选择一个最适宜的答案,在答题纸上涂黑)1.一个事务中的一组更新操作是一个整体,要么全部执行,要么全部不执行。
这是事务的:A.原子性B.一致性 C.隔离性 D.持久性2.在数据库的三级模式结构中,描述一个数据库中全体数据的全局逻辑结构和特性的是:A.外模式 B.内模式 C.存储模式D.模式3.关于联系的多重性,下面哪种说法不正确?A.一个多对多的联系中允许多对一的情形。
B.一个多对多的联系中允许一对一的情形。
C.一个多对一的联系中允许一对一的情形。
D.一个多对一的联系中允许多对多的情形。
4.考虑学校里的"学生"和"课程"之间的联系,该联系的多重性应该是:A. 一对一B. 多对一C. 一对多D. 多对多5.下面哪种约束要求一组属性在同一实体集任意两个不同实体上的取值不同。
A. 键(key)约束。
B. 单值约束。
C. 参照完整性。
D. 域(domain)约束6.关系模型要求各元组的每个分量的值必须是原子性的。
对原子性,下面哪种解释不正确:A.每个属性都没有内部结构。
B.每个属性都不可再分解。
C.各属性值应属于某种根本数据类型。
D.属性值不允许为NULL。
7.对于一个关系的属性(列)集合和元组(行)集合,下面哪种说法不正确:A.改变属性的排列次序不影响该关系。
B.改变元组的排列次序不影响该关系。
C.改变元组的排列次序会改变该关系。
D.关系的模式包括其名称及其属性集合。
8.假设R是实体集R1与R2间的一个多对多联系,将其转换为关系R',哪种说法不正确:A.R'属性应包括R1与R2的所有属性。
B.R'属性应包括R1与R2的键属性。
C.R1与R2的键属性共同构成R'的键。
D.R'的属性应包括R自身定义的属性。
9.关于函数依赖的判断,下面哪种说法不正确?A.假设任意两元组在属性A上一致,在B上也一致,那么有A → B成立。
ORACLE 10g 课后参考答案(答案仅供参考)第一章ORACLE 10g简介一、选择题1.下面不属于ORACLE 10g产品系列的是(D)A.Oracle数据库 10g标准版1B.Oracle数据库 10g标准版C.Oracle数据库 10g企业版D.Oracle数据库 10g网络版2.ORACLE 10g中的g表示(D)A.版本B.网络C.数据库D.网格计算3.下面关于ORACLE 10g数据库逻辑结构的描述错误的是(C)A.数据库由若干个表空间组成B.表空间由表组成C.表由数据块组成D.段由区间组成4.ORACLE管理数据库存储空间的最小存储单位是(A)A.数据块B.表空间C.表D.区间5.ORACLE分配磁盘空间的最小单位是(D)A.数据块B.表空间C.表D.区间6.下列不属于ORACLE表空间的是(D)A.大文件表空间B.系统表空间C.撤销表空间D.网格表空间7.当数据库服务器上的一个数据库启动时,ORACLE将分配一块内存区间,叫做系统全局区,英文缩写为(B)A.VGAB.SGAC.PGAD.GLOBAL二、填空题1.__视图__ 是虚拟的表,它在物理上并不存在。
可以把它看成是一个存储的查询。
2.创建___索引__可以提高读取数据的效率。
它的功能类似于书的目录,读者可以通过目录很快的在书中找到需要的内容。
3.有些表共享公共的列,并经常被同时访问,为了提高数据存取效率,把这些表在物理上存储在一起,得到的表的组合就是____簇____。
4.一个数据块对应磁盘上的一定数量的数据库空间,标准的数据块大小由初始参数(DB_BLOCK_SIZE )指定。
5.每个数据库都至少有一个系统表空间,被称为__SYSTEM _表空间。
6.每个ORACLE数据库都由3种类型的文件组成:数据文件、日志文件、控制文件。
7.ORACLE有两种内存结构,即_系统全局区_和_程序全局区_。
三、简答题1.简述ORACLE数据库逻辑结构中各要素之间的关系。
数据库系统基础教程第六章答案数据库系统基础教程第六章答案【篇一:数据库系统基础教程答案ch7】>数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案数据库系统基础教程答案a)create table movies ( titlechar(100), yearint, length int,gee char(10),studioname char(30), producerc# int,primary key (title, year),foreign key (producerc#) references movieexec(cert#) ); orcreate table movies ( titlechar(100), yearint, length int,gee char(10),studioname char(30),producerc# int references movieexec(cert#), primary key (title, year) ); b)create table movies ( titlechar(100), yearint, length int,gee char(10),studioname char(30),producerc# int references movieexec(cert#) on delete set null on update set null,primary key (title, year) );c)create table movies ( titlechar(100), yearint, length int,gee char(10),studioname char(30),producerc# int references movieexec(cert#) on delete cascade on update cascade,primary key (title, year) ); d)create table starsin (movietitle char(100)references movie(title), movieyear int,starname char(30),primary key (movietitle, movieyear, starname) );e)create table starsin (movietitle char(100)references movie(title) on delete cascade, movieyear int,starname char(30),primary key (movietitle, movieyear, starname) ); to declare such a foreign-key constraint between the relations movie and starsin, values of the referencing attributes in movie should appear inmoviestar as unique values. however, based on primary key declaration in relation starin, the uniqueness of movies is guaranteed with movietitle, movieyear, and starname attributes. even with title and year as referencing attributes there is no way of referencing unique movie from starsinwithout starname information. therefore, such a constraint can not be expressed using a foreign-key constraint.alter table productadd primary key (model);alter table pcadd foreign key (model) references product (model); alter table laptopadd foreign key (model) references product(model); alter table printeradd foreign key (model) references product (model);alter table classesadd primary key (class); alter table shipsadd primary key (name); alter table shipsadd foreign key (class) references classes(calss); alter table battlesadd primary key (name); alter table outcomes add foreign key (ship) references ships (name); alter table outcomesadd foreign key (battle) references battles (name);a)alter table shipsadd foreign key (class) references classes (class) on delete set null on update set null;in addition to the above declaration, class must be declared the primary key for classes. b)alter table outcomeadd foreign key (battle) references battles (name) on delete set null on update set null; c)alter table outcomesadd foreign key (ship) references ships (name) on delete set null on update set null; a)yearint check (year = 1915)b)length int check (length = 60 and length = 250) c)studioname char(30)check (studioname in (?disney?, fox?, ?mgm?, ?paramount?) ) a)create table laptop ( …speed decimal(4,2) check (speed = ) … ); b) create table printer ( …type varchar(10)check (type in (?laser?, ?ink-jet?, ?bubble-jet?)) … ); c)create table product ( …type varchar(10)check (type in(?pc?, ?laptop?, ?printer?)) … );d)create table product ( …model char(4)check (model in (select model from pc union all select model from laptop union allselect model from printer)) … );* note this doesn?t check the attribute constraint violation caused by deletions from pc, laptop, or printera)【篇二:数据库原理教程习题答案(全)】t>第1章数据库系统概述习题参考答案税务局使用数据库存储纳税人信息、纳税人缴纳税款信息等。
典型的数据处理包括纳税、退税处理、统计各类纳税人纳税情况等。
银行使用数据库存储客户基本信息、客户存贷款信息等。
典型的数据处理包括处理客户存取款等。
超市使用数据库存储商品的基本信息、会员客户基本信息、客户每次购物的详细清单。
典型的数据处理包括收银台记录客户每次购物的清单并计算应交货款。
dbms是数据库管理系统的简称,是一种重要的程序设计系统。
它一个相互关联的数据集合和一组访问这些数据的程序组成。
数据库是持久储存在计算机中、有组织的、可共享的大量数据的集合。
数据库中的数据按一定的数据模型组织、描述和存储,可以被各种用户共享,具有较小的冗余度、较高的数据独立性,并且易于扩展。
数据库系统数据库、dbms、应用系统和数据库管理员组成。
数据模型是一种形式机制,用于数据建模,描述数据、数据之间的联系、数据的语义、数据上的操作和数据的完整性约束条件。
数据库模式是数据库中使用数据模型对数据建模所产生设计结果。
对于关系数据库而言,数据库模式一组关系模式构成。
数据字典是dbms维护的一系列内部表,用来存放元数据。
所谓元数据是关于数据的数据。
dbms提供如下功能:数据定义:提供数据定义语言ddl,用于定义数据库中的数据对象和它们的结构。
数据操纵:提供数据操纵语言dml,用于操纵数据,实现对数据库的基本操作。
事务管理和运行管理:统一管理数据、控制对数据的并发访问,保证数据的安全性、完整性,确保故障时数据库中数据不被破坏,并且能够恢复到一致状态。