Вы находитесь на странице: 1из 13

CHAPTER 2: QUESTIONS AND ANSWERS

1. How is creating a table similar to writing a chapter of a book?


Ans: A table is composed of a heading (or definition) part and a content (or body) part as
a book chapter is.

2. With what terminology for relational databases are you most comfortable? Why?
Ans: Students' responses will vary. However, an example response might be: The
terminology I'm most comfortable with is Table for relational databases because of its
familiarity and simplicity. Tables are used to communicate ideas in many fields.

3. What is the difference between a primary key and a candidate key?


Ans: A primary key is chosen from among the candidate keys. While the primary key
cannot contain null values, the candidate key can have null values. Candidate keys that
are not primary keys are declared with the UNIQUE keyword.

4. What is the difference between a candidate key and a superkey?


Ans: A superkey may not be minimal. A candidate key is a minimal superkey. A
superkey is minimal if removing a column makes it no longer unique.

5. What is a null value?


Ans: A null value represents the absence of an actual value. A null value can mean that
the actual value is unknown or does not apply to the given row.

6. What is the motivation for the entity integrity rule?


Ans: Entity integrity ensures that entities (people, things, and events) are uniquely
identified in a database. For auditing and security reasons, it is often important that
business entities be easily traceable.

7. What is the motivation for the referential integrity rule?


Ans: Referential integrity ensures that a database contains valid connections. For
example, it is critical that each row of the Enrollment table contains a social security
number of a valid student. Otherwise, some enrollments can be meaningless, possibly
resulting in students denied enrollment because non-existing students took their places.

8. What is the relationship between the referential integrity rule and foreign keys?
Ans: Referential integrity involves the allowable values in foreign keys. A foreign key
can store a value matching a candidate key value in some row of the table containing the
associated candidate key or a null value.

9. How are candidate keys that are not primary keys indicated in the CREATE TABLE
statement?
Ans: The CREATE TABLE statement has a UNIQUE constraint for candidate keys that
are not primary keys.
10. What is the advantage of using constraint names when defining primary key,
candidate key, or referential constraints in CREATE TABLE statements?
Ans: The advantage of using a constraint name is that the constraint is easy to identify
when a violation occurs.

11. When is it not permissible for foreign keys to store null values?
Ans: When a foreign key is part of a primary key, null values are not permitted.

12. What is the purpose of a database diagram such as the Access Relationship window?
Ans: To make referential integrity easier to define and understand than the text
representation in the CREATE TABLE statement. In addition, a graphical representation
supports nonprocedural data access.

13. How is a 1-M relationship represented in the Relational Model?


Ans: In the CREATE TABLE statement, primary keys and related foreign keys along
with referential integrity constraints represent 1-M relationships. In the Access
Relationship window, a line connecting two tables represents a 1-M relationship. The
relationship line connects the parent or “1” table (the primary key table) with the child or
“M” table (the foreign key table). The symbol of “1” is presented at the end of the line
connected to the “1” table. The symbol of “8 ” is presented at the other end of the line
connected to the “M” table.

14. How is an M-N relationship represented in the Relational Model?


Ans: An M-N relationship cannot be directly represented in the Relational Model. Two
1-M relationships and a linking or associative table represent an M-N relationship.

15. What is a self-referencing relationship?


Ans: A self-referencing relationship is a relationship in which a foreign key refers to the
same table. Self-referencing relationships represent associations among members of the
same set.

16. How is a self- referencing relationship represented in the Relational Model?


Ans: In the CREATE TABLE statement, a foreign key referring to the primary key of the
same table along with a referential integrity constraint represents a self-referencing
relationship. In the Access Relationship window, a self-referencing relationship is
represented indirectly in the Relational Model. A relationship line of the self- referencing
relationship table is linked to itself in a copied table, which is not a real table as it is
created only inside the Relationship Window. For example, in the University Database,
the self-referencing relationship for Faculty is represented as a relationship between the
Faculty and Faculty_1 tables. Faculty_1 is not a real table.

17. What is a referenced row?


Ans: Referenced rows are rows in a parent table of a 1-M relationship. A row is
referenced if there are rows in a child table with foreign key values identical to the
primary key value of the parent table row.
18. What two actions on referenced rows can affect related rows in a child table?
Ans: The two actions are deleting a referenced row and updating the primary key of a
referenced row.

19. What are the possible actions on related rows after a referenced row is deleted or its
primary key is updated?
Ans: The possible actions are: restrict (do not allow the operation on the referenced row),
cascade (perform the same action on the related rows), nullify (set the foreign values in
related rows to null), and default (set the foreign values in related rows to a default
value).

20. Why is the restrict action for referenced rows more common than the cascade action?
Ans: Because the restrict action helps maintain a degree of accuracy between tables. The
restrict action prevents a user from deleting records from a primary key (referenced) table
when there are associated records in a foreign key (referencing) table. For example, in the
University Database, a user cannot delete a course if there related no related offering. The
restrict action also prevents users from changing primary key values that would result in
orphan records. For example, a faculty primary key should not be changed if there are
related faculty in offering; otherwise the offering will result in a record with an
unassigned faculty.

21. When is the nullify action not allowed?


Ans: The nullify action is not permitted if the foreign key does not allow null values.

22. Why study the operators of relational algebra?


Ans: Knowledge of the operators can improve your query formulation skills. The
operators are the fundamental ways to retrieve data from a relational database. To help
you become proficient in query formulation, you should understand its purpose and
inputs of each operator.

23. Why are the restrict and project operators widely used?
Ans: Because users often want to see a subset rather than an entire table, these operators
are widely used. These operators are also popular because they are easy to understand.

24. Explain how the unio n, intersection, and difference operators for tables differ from
the traditional operators for sets.
Ans: The union, intersection, and difference operators for tables operate in the same way
as the traditional operators for sets. The only difference is tha t the table operators to rows
of a table. Compatibility is a new concept for the table operators as compared to the
traditional set operators. With the table operators, both tables must be union compatible
because all columns are compared.

25. Why is the join operator so important for retrieving useful information?
Ans: Because information resides in different tables, combining tables is important. The
join operator is used for combining tables to retrieve information from different tables.
26. What is the relationship between the join and the extended cross product operators?
Ans: The extended cross product operator is a building block for the join operator.

27. Why is the extended cross product operator used sparingly?


Ans: The extended cross product operator can combine any two tables. Other table
combining operators have conditions about the tables to combine. Because of its
unrestricted nature, the extended cross product operator can produce tables with
excessive data.

28. What happens to unmatched rows with the join operator?


Ans: Unmatched rows are removed from the result of the join operator. Unmatched rows
are included in the result of the full outer join operator.

29. What happens to unmatched rows with the full outer join operator?
Ans: Unmatched rows from both input tables are included in the result of the full outer
join operator.

30. What is the difference between the full outer join and the one-sided outer join?
Ans: Full outer join includes the nonmatching rows from both tables. The one-side outer
join includes the nonmatching rows from the designated input table.

31. Define a decision- making situation that might require the summarize operator.
Ans: Students' responses will vary. However, an example might be: The decision on
granting a scholarship to a student might require the summarized operator to retrieve all
students who have GPAs above 3.75.

32. What is an aggregate function?


Ans: An aggregrate function is a statistical function used in the summarize operator.
Common aggregate functions for databases are sum, count, min, max, and average.

33. How are grouping columns used in the summarize operator?


Ans: Grouping columns are used to organize data in the summarize operator. The result
of a summarize operation includes one row for each combination of grouping column
values.

34. Why is the divide operator not as widely used as the join operator?
Ans: Because the matching requirement in the divide operator is more stringent than the
join operator.
V. PROBLEM SOLUTIONS

The problems use the Customer, OrderTbl, and Employee tables of the simplified Order
Entry database. Other chapters of Part I extend the database to increase its usefulness.
The Customer table records clients who have placed orders. The OrderTbl contains the
basic facts about customer orders. The Employee table contains facts about employees
who take orders. The primary keys of the tables are CustNo for Customer, EmpNo for
Employee, and OrdNo for OrderTbl.

1. Write a CREATE TABLE statement for the Customer table. Choose data types
appropriate for the DBMS used in your course. Note that the CustBal column
contains numeric data. The currency symbols are not stored in the database. The
CustFirstName and CustLastName columns are required (not null).
Ans: The CREATE TABLE statement solution uses the standard SQL:1999 data types.
Your DBMS may provide a different collection of data types.

CREATE TABLE Customer


( CustNo CHAR(8),
CustFirstName VARCHAR(20) NOT NULL,
CustLastName VARCHAR(30) NOT NULL,
CustCity VARCHAR(30),
CustState CHAR(2),
CustZip CHAR(10),
CustBal DECIMAL(12,2),
CONSTRAINT PKCustomer PRIMARY KEY (CustNo) )

2. Write a CREATE TABLE statement for the Employee table. Choose data types
appropriate for the DBMS used in your course.
Ans:

CREATE TABLE Employee


( EmpNo CHAR(8),
EmpFirstName VARCHAR(20) NOT NULL,
EmpLastName VARCHAR(30) NOT NULL,
EmpPhone CHAR(15),
CONSTRAINT PKEmployee PRIMARY KEY (EmpNo) )

3. Write a CREATE TABLE statement for the OrderTbl table. Choose data types
appropriate for the DBMS used in your course. The OrdDate column is required (not
null).
Ans:
CREATE TABLE OrderTbl
( OrdNo CHAR(8),
OrdDate DATE NOT NULL,
CustNo CHAR(8),
EmpNo CHAR(8),
CONSTRAINT PKOrderTbl PRIMARY KEY (OrdNo) )

4. Identify the foreign keys and draw a relationship diagram for the simplified Order
Entry database. The CustNo column references the Customer table and the EmpNo
column references the Employee table. For each relationship, identify the “1” table
and the “Many” table.

Ans: In the relationship diagram, primary keys are underlined and foreign keys are
italicized. The “M” denotes the many side of a relationship.

Customer
CustNo
CustFirstName
CustLastName Employee
CustCity EmpNo
CustState EmpFirstName
CustZip EmpLastName
CustBal EmpPhone
1 1
OrderTbl
OrdNo
CustNo
M EmpNo
OrdDate
M

5. Extend your CREATE TABLE statement from problem 3 with referential integrity
constraints. Updates and deletes on related rows are restricted.

Ans: In most database systems, the default for the ON DELETE and ON UPDATE
clauses are NO ACTION. Thus, the clauses are not strictly necessary. Some products
such as Oracle 8i do not support the ON UPDATE clause.
CREATE TABLE OrderTbl
( OrdNo CHAR(8),
OrdDate DATE NOT NULL,
CustNo CHAR(8),
EmpNo CHAR(8),
CONSTRAINT PKOrderTbl PRIMARY KEY (OrdNo) ,
CONSTRAINT FKCustNo FOREIGN KEY (CustNo) REFERENCES
Customer
ON DELETE NO ACTION ON UPDATE RESTRICT,
CONSTRAINT FKEmpNo FOREIGN KEY (EmpNo) REFERENCES
Employee
ON DELETE NO ACTION ON UPDATE RESTRICT )

6. From examination of the sample data and your common understanding of order entry
businesses, are null values allowed for the foreign keys in the OrderTbl table? Why
or why not? Extend the CREATE TABLE statement in problem 5 to enforce the null
value restrictions if any.
Ans: Null values are not allowed for CustNo. The sample data shows that each order has
a related customer. In addition, common practice indicates that an order requires a
customer. Fraud could result if orders are stored without a related customer. Null values
are allowed for the EmpNo column. The sample data shows rows without an EmpNo
value. The null values may correspond to internet orders where no employee takes the
order.

CREATE TABLE OrderTbl


( OrdNo CHAR(8),
OrdDate DATE NOT NULL,
CustNo CHAR(8) NOT NULL,
EmpNo CHAR(8),
CONSTRAINT PKOrderTbl PRIMARY KEY (OrdNo) ,
CONSTRAINT FKCustNo FOREIGN KEY (CustNo) REFERENCES
Customer
ON DELETE NO ACTION ON UPDATE RESTRICT,
CONSTRAINT FKEmpNo FOREIGN KEY (EmpNo) REFERENCES
Employee
ON DELETE NO ACTION ON UPDATE RESTRICT )

7. Show the result of a restrict operation that lists the orders in February 2004.

Ans: The result of a restriction operation that lists the orders in February 2004

OrdNo OrdDate CustNo EmpNo


O165677 02/11/200 C854332
O795989 02/19/200 C854332 E854439
8. Show the result of a project operation that lists the CustNo, CustFirstName, and
CustLastName columns of the Customer table.

Ans: The result of a project operation to list the CustNo, CustFirstName, and
CustLastName columns

CustNo CustFirstNa CustLastNa


C095432 Sheri Gordon
C101039 Jim Glussman
C238859 Beth Taylor
C334095 Betty Wise
C349950 Bob Mann
C854332 Ron Thompson

9. Show the result of a project operation that lists the CustCity and CustState columns
of the Customer table.

Ans: The result of a project operation to list the CustCity and CustState columns of the
Customer table; The result contains only five rows because two Customer rows contain
the same city and state.

CustCity CustStat
Littleton CO
Denver CO
Seattle WA
Monroe WA
Renton WA

10. Show the result of a natural join that combines the Customer and OrderTbl tables.

Ans: The result of a natural join of the Customer and OrderTbl tables

OrdNo OrdDate EmpNo CustNo CustFirst CustLast CustStreet Cust CustZip CustBal
Name Name City
Cu
st
State
O2334661 01/14/2004 E1329594 C0954327 Sheri Gordon 336 Hill St. Littleton CO 80129-5543 $230.00
O1116324 01/23/2004 E8544399 C0954327 Sheri Gordon 336 Hill St. Littleton CO 80129-5543 $230.00
O3331222 01/13/2004 C1010398 Jim Glussman 1432 E. Ravenna Denver CO 80111-0033 $200.00
O2233457 01/12/2004 E9884325 C2388597 Beth Taylor 2396 Rafter Rd Seattle WA 98103-1121 $500.00
O4714645 01/11/2004 E1329594 C2388597 Beth Taylor 2396 Rafter Rd Seattle WA 98103-1121 $500.00
O5511365 01/22/2004 E9884325 C3340959 Betty Wise 4334 153rd NW Seattle WA 98178-3311 $200.00
O7989497 01/16/2004 E9345771 C3499503 Bob Mann 1190 Lorraine Cir. Monroe WA 98013-1095 $0.00
O7959898 02/19/2004 E8544399 C8543321 Ron Thompson 789 122nd St. Renton WA 98666-1289 $85.00
O1656777 02/11/2004 C8543321 Ron Thompson 789 122nd St. Renton WA 98666-1289 $85.00
11. Show the steps to derive the natural join for problem 10. How many rows and
columns are in the extended cross product step?
Ans: Perform a cross product, followed by a restriction, and finally a projection to
remove patient.patssn. The size of the cross product is the product of the rows (9
OrderTbl rows * 6 Customer rows = 54 rows).

12. Show the result of a natural join of the Employee and OrderTbl tables.

Ans: The result of a natural join of the Employee and OrderTbl tables

OrdNo OrdDate CustNo EmpNo EmpFirstNa EmpLastNa EmpPhone


O233466 01/14/200 C095432 E132959 Landi Santos (303) 789-
O471464 01/11/200 C238859 E132959 Landi Santos (303) 789-
O111632 01/23/200 C095432 E854439 Joe Jenkins (303) 221-
O551136 01/22/200 C334095 E988432 Thomas Johnson (303) 556-
O798949 01/16/200 C349950 E934577 Colin White (303) 221-
O795989 02/19/200 C854332 E854439 Joe Jenkins (303) 221-
O223345 01/12/200 C238859 E988432 Thomas Johnson (303) 556-

13. Show the result of a one-sided outer join between the Employee and OrderTbl tables.
Preserve the rows of the OrderTbl table in the result.

Ans: The result of a one-sided outer join between the Employee and OrderTbl tables.
Preserve the rows of the OrderTbl table in the result.

OrdNo OrdDate CustNo EmpNo EmpFirstNa EmpLastNa EmpPhone


O333122 01/13/200 C101039
O165677 02/11/200 C854332
O233466 01/14/200 C095432 E132959 Landi Santos (303) 789-
O471464 01/11/200 C238859 E132959 Landi Santos (303) 789-
O111632 01/23/200 C095432 E854439 Joe Jenkins (303) 221-
O551136 01/22/200 C334095 E988432 Thomas Johnson (303) 556-
O798949 01/16/200 C349950 E934577 Colin White (303) 221-
O795989 02/19/200 C854332 E854439 Joe Jenkins (303) 221-
O223345 01/12/200 C238859 E988432 Thomas Johnson (303) 556-

14. Show the result of a full outer join between the Employee and OrderTbl tables.
Ans: The result of a full outer join between the Employee and OrderTbl tables; The first
two rows are added in the result as compared to problem 13.

OrdNo OrdDate CustNo EmpNo EmpFirstNa EmpLastNa EmpPhone


E884321 Amy Tang (303) 556-
E995430 Mary Hill (303) 556-
O333122 01/13/200 C101039
O165677 02/11/200 C854332
O233466 01/14/200 C095432 E132959 Landi Santos (303) 789-
O471464 01/11/200 C238859 E132959 Landi Santos (303) 789-
O111632 01/23/200 C095432 E854439 Joe Jenkins (303) 221-
O551136 01/22/200 C334095 E988432 Thomas Johnson (303) 556-
O798949 01/16/200 C349950 E934577 Colin White (303) 221-
O795989 02/19/200 C854332 E854439 Joe Jenkins (303) 221-
O223345 01/12/200 C238859 E988432 Thomas Johnson (303) 556-

15. Show the result of the restrict operation on Customer where the condition is CustCity
equals “Denver” or “Seattle” followed by a project operation to retain the CustNo,
CustFirstName, CustLastName, and CustCity columns.

Ans: The result of restrict operation on Customer where the condition is CustCity equals
“Denver” or “Seattle” followed by a project operation to retain the CustNo,
CustFirstName, CustLastName, and CustCity columns

CustNo CustFirstNa CustLastNa CustCity


C101039 Jim Glussman Denver
C238859 Beth Taylor Seattle
C334095 Betty Wise Seattle

16. Show the result of a summarize operation on Customer. The grouping column is
CustState and the aggregate calculation is COUNT. COUNT shows the number of
rows with the same value for the grouping column.

Ans: The result of a summarize operation on Customer. The grouping column is


CustState and the aggregate calculation is COUNT

CustState COUNT
CO 2
WA 4

17. Show the result of a summarize operation on Customer. The grouping column is
CustState and the aggregate calculations are the minimum and maximum CustBal
values.

Ans: The result of a summarize operation on Customer where the grouping column is
CustState and the aggr egate calculations are the minimum and maximum CustBal values

CustState MIN(CustBa Max(CustBal


CO 200 230
WA 0 500

18. What tables are required to show the CustLastName, EmpLastName, and OrdNo
columns in the result table?
Ans: The Customer, Employee, and OrderTbl tables are necessary to show the
CustLastName, EmpLastName, and OrdNo columns in the result table

19. Extend your relationship diagram from problem 4 by adding two tables (OrdLine and
Product). Partial CREATE TABLE statements for the primary keys and referential
integrity constraints are shown below:
CREATE TABLE Product ... PRIMARY KEY (ProdNo)
CREATE TABLE OrdLine ... PRIMARY KEY (OrdNo, ProdNo)
FOREIGN KEY (OrdNo) REFERENCES Order
FOREIGN KEY (ProdNo) REFERENCES Product

Ans: Relationship diagram from problem (4) extended with two tables (OrdLine and
Product). Partial CREATE TABLE statements for the primary keys and referential
integrity constraints are shown below:
CREATE TABLE Product … PRIMARY KEY (ProdNo)
CREATE TABLE OrdLine … PRIMARY KEY (OrdNo, ProdNo)
FOREIGN KEY (OrdNo) REFERENCES Order
FOREIGN KEY (ProdNo) REFERENCES Product
Customer
CustNo
CustFirstName
CustLastName Employee
CustCity EmpNo
CustState EmpFirstName
CustZip EmpLastName
CustBal EmpPhone
1 1
OrderTbl
OrdNo
Product
CustNo
ProdNo
M EmpNo
OrdDate
M
1 1

OrdLine
OrdNo
M ProdNo M

20. Extend your relationship diagram from problem 19 by adding another foreign key in
the Employee table. The foreign key SupEmpNo is the employee number of the
supervising employee. Thus, the SupEmpNo column references the Employee table.
Ans: Extended relationship diagram from problem (19) by adding another foreign key in
the Employee table. The foreign key SupEmpNo is the employee number of the
supervising employee. Thus, the SupEmpNo column references the Employee table.

Customer
CustNo M
Employee
CustFirstName
EmpNo
CustLastName
EmpFirstName
CustCity
EmpLastName
CustState 1
EmpPhone
CustZip
SupEmpNo
CustBal
1 1
OrderTbl
OrdNo
Product
CustNo
ProdNo
M EmpNo
OrdDate
M
1 1

OrdLine
OrdNo
M ProdNo M

21. What relational algebra operator do you use to find products contained in every
order? What relational algebra operator do you use to find products contained in any
order?

Ans: The division operator is necessary because the problem requires products contained
in every order. In the second part, the join operator is required.

22. Are the Customer and Employee tables union compatible? Why or why not?

Ans: They are not union compatible because some columns are not compatible. The
Customer and Employee tables have some compatible columns (for example
EmpLastName and CustLastName) but also some non compatible columns. In addition,
the primary keys (CustNo and EmpNo), although data type compatible, will never be
equal even for the same person.

23. Using the database after problem 20, what tables must be combined to list the
product names on order number 01116324?
Ans: The OrdLine and Product tables are required to list the product names on order
number “O1116324”. The OrdLine table contains the matching ProdNo values and the
Product table contains the ProdName values.
24. Using the database after problem 20, what tables must be combined to list the
product names ordered by customer number C0954327?

Ans: The Order, OrdLine, and Product tables are required to list the product names
ordered by customer number “C0954327”. The Order table contains the matching OrdNo
values, the OrdLine table contains the matching ProdNo values, and the Product table
contains the ProdName values.

25. Using the database after problem 20, what tables must be combined to list the
product names ordered by the customer named Sheri Gordon?

Ans: The Customer, Order, OrdLine, and Product tables are required to list the product
names ordered by the customer named “Sheri Gordon”. The Customer table contains the
matching CustNo value, the Order table contains the matching OrdNo values, the
OrdLine table contains the matching ProdNo values, and the Product table contains the
ProdName values.

Вам также может понравиться