CS3600.1 Introduction to Database System Fall 2016 Dr. Zhizhang Shen A Sample Solution to the Midterm Test 1. A couple of W s(10) (a) Why is it the case that, by default, there are no duplicated tuples in any table in a relational database? A solution: I found various answers. But, the fundamental reason is that a RDB table is a set, which does not allow duplicates. (b) What are the nine basic relational operations that we can use within a relational algebraic expression? Describe them briefly. A solution: Union, Intersection, Cartesian Product, Minus, Projection, Restriction, Join, Divided By, and Rename. You know what they do, don t you? (c) What is a View? Is it the same as, or different from, a table? Explain. A solution: A view is defined via a query, based on tables. It is similar to a table in the sense that they share the same structure, thus a view can be used in any context, where a table can be used. It is different from a table as it is not permanently stored in a database. (d) What does that mean by logic data independence? Give an example as when there might be a problem, and why is such an independence important? A solution: Logic data independence refers to the immunity of users and user programs to changes in the logic structure of the database. In other words, over a long period of time, even though the conceptual structure of the database will change, the application programs don et have to. As we mentioned in a class, we may consider to split the table Student table which contains supplier information, to two tables SNC: (SupplierId, SName, City), and ST: (SupplierId, SName, Status). Such a split changes the conceptual structure of the database, thus having an impact on application programs. On the other hand, we can create a view Student as SNC Join ST. Then, all the applications involved with the Student table will still be valid with the help of this view, even though that table no longer exists. (e) What are some of the issues we might run into when converting an ER relationship to a RDB relation? why? A solution: When we convert an ER entity to a RDB relation, if an attribute of this relationship might have a set as its value, we have to throw it in as part of the key for this relation. The reason is certainly the data atomicity requirement of REB. Another issue might be the key composition for such a relation, which consists of all the attributes, and keys of the participating roles. When we convert a part-of relationship, we might have to make a choice as if we should covert into one relation; or maybe combine a few.
When we convert an isa relationship, we might have to enforce the associated disjoint and covering constraints.... Problems 2 and 3 are based on the following tables, Supplier, Part, and SupplyPart. Below is an instance of such a structure. SupplierId SName Status City S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris PartId PName Color Weight City P1 Nut Red 12.0 London P2 Bolt Green 17.0 Paris P3 Screw Blue 17.0 Rome P4 Screw Red 14.0 London SupplierId PartId Quantity S1 P1 300 S1 P2 200 S1 P3 400 S1 P4 200 S2 P1 300 S2 P2 400 S3 P2 200 2. What do the following queries do? Write down the respective output based on the above instance, as well as an explanation.(5) (a) π SupplierId, Status, City (σ Status>15 (Supplier)) A solution: Find out the Supplier number, status and location of those suppliers whose status is more than 15. With the above instance, after a restriction on those tuples where thestatus is above 15, and then a projection of the relevant three attributes, the result should be the following: SupplierId Status City S1 20 London S3 30 Paris (b) Select * from parispart where Color= Red, where parispart is defined as follows: Create View parispart As Select PartId, PName, Color From P Where City= Paris ; 2
A solution: When there is a view in the query, DBMS will replace the occurrence of the view with its definition first. Thus, the above query becomes Select * From ( Select PartId, PName, Color From P Where City= Paris ; ) Where Color= Red ; It is clear that this query is to find the part number, its name and color of all the red parts stored in Paris. Since there are no such parts in this instance of the database, the answer will be empty for this database instance. 3. With the above table schema, come up with either an algebraic expression or ansql query that will, respectively, (5) (a) get full details of those parts that are supplied by S2. (b) get supplier s names for those who supply the largest quantity for each and every part as listed in the Part table. You can use either a relational algebraic expression, or an SQL statement, but you may not use a mixture of those structures. A solution: In Relational Algebra: (a) Everybody should be able to do it. Part PartId (π Part.PartId (σ SupplyPart.SupplierId= S2 (SupplyPart))) (b) Not applicable since RA does not support aggregation operators. The general idea is to find out those parts and their associated maximum quantity in the SupplyPart table, then dig out the ids for those suppliers who supply such a maximum quantity, also in the SupplyPart table, and finally dig out their names in the Supplier table, e.g., via a Join. In MySQL, an implementation of SQL. Again, everybody should be able to do it. (a) mysql> Select P.* -> From Part P, SupplyPart SP -> WHERE P.PartId=SP.PartId AND SP.SupplierId= S2 ; This query should lead to the following result, with the above database instance: PartId Name Color Weight City P1 Nut Red 12 London P2 Bolt Green 17 Paris 3
If you hate the expensive Join, you can use the following instead. Select P.* From Part P Where P.PartId In ( Select SP.SupplierId From SupplierPart SP Where SP.SupplierId= S2 ); (b) There are also few ways of doing this. You can do it in a nested query as follows: mysql> Select distinct S.SName, SP.PartId, SP.Quantity -> From SupplyPart SP, Suppliers S, -> #The following table C gets us -> #a table with each part and its maximum quantity. -> (Select PartId, max(quantity) As Num -> From SupplyPart -> Group by PartId) As C -> Where S.SupplierId=SP.SupplierId -> and SP.PartId=C.PartId And SP.Quantity=C.Num; The above query leads to the following output, with the above database instance. SName PartId Quantity Smith P1 300 Jones P1 300 Jones P2 400 Smith P3 400 Smith P4 200 On the other hand, if you prefer to cut this query into a few simpler steps, you can certainly use the second tier query to create a view, as we did somewhere in Lab 4. mysql> Create View QMax As -> (Select PartId, max(quantity) As Num -> From SupplyPart -> Group by PartId); Query OK, 0 rows affected (0.04 sec) This gives us the maximum quantity as associated with each and every of the parts: mysql> select * from QMax; PartId Num P1 300 P2 400 4
P3 400 P4 200 A bit simplified query gives us the desired information: mysql> Select distinct S.SName, SP.PartId, SP.Quantity -> From SupplyPart SP, Suppliers S, QMax -> Where S.SupplierId=SP.SupplierId -> And SP.PartId=QMax.PartId -> And SP.Quantity=QMax.Num; SName PartId Quantity Smith P1 300 Jones P1 300 Jones P2 400 Smith P3 400 Smith P4 200 5