Assignment Chapter 4
Assignment Chapter 4
Student’s Name:
Group:
Submit to: Lec. Mr. Sun Sophorn
Deadline: One Week
I. Review Questions
1. What is a view? How do you define a view? Does the data described in a view definition
ever exist in that form? What happens when a user accesses a database through a view?
2. Using data from the Premiere Products database, define a view named TopLevelCust. It
consists of the number, name, address, balance, and credit limit of all customers with
credit limits that are greater than or equal to $10,000.
a. Using SQL, write the view definition for TopLevelCust.
b. Write an SQL query to retrieve the number and name of all customers in the
TopLevelCust view with balances that exceed their credit limits.
c. Convert the query you wrote in Question 2b to the query that the DBMS will
actually execute.
3. Define a view named PartOrder. It consists of the part number, description, price, order
number, order date, number ordered, and quoted price for all order lines currently on
file.
a. Using SQL, write the view definition for PartOrder.
b. Write an SQL query to retrieve the part number, description, order number, and
quoted price for all orders in the PartOrder view for parts with quoted prices
that exceed $100.
c. Convert the query you wrote in Question 3b to the query that the DBMS will
actually execute.
4. What is an index? What are the advantages and disadvantages of using indexes? How
do you use SQL to create an index?
5. Describe the GRANT statement and explain how it relates to security. What types of
privileges may be granted? How are they revoked?
6. Write the SQL commands to grant the following privileges:
a. User Stillwell must be able to retrieve data from the Part table.
b. Users Webb and Bradley must be able to add new orders and order lines.
7. Write the SQL command to revoke user Stillwell’s privilege.
8. What is the system catalog? Name three items about which the catalog maintains
information.
9. Write the SQL commands to obtain the following information from the system catalog:
a. List every table that you created.
b. List every field in the Customer table and its associated data type.
c. List every table that contains a field named PartNum.
10. Why is it a good idea for the DBMS to update the catalog automatically when a change
is made in the database structure? Could users cause problems by updating the catalog
themselves? Explain.
11. What are nulls? Which field cannot accept null values? Why?
12. State the three integrity rules. Indicate the reasons for enforcing each rule.
13. The Orders table contains a foreign key, CustomerNum that must match the primary key
of the Customer table. What type of update to the Orders table would violate
In the following exercises, you will use the data in the Premiere Products database
In MySQL. Check with your instructor if you are not certain about which approach to take.
1. Create the TopLevelCust view described in Review Question 2. Display the data in the
view.
2. Create the PartOrder view described in Review Question 3. Display the data in the view.
3. Create a view named OrdTot. It consists of the order number and order total for each
order currently on file. (The order total is the sum of the number ordered multiplied by
the quoted price on each order line for each order.) Display the data in the view.
4. Create the following indexes. If it is necessary to name the index in your DBMS, use the
indicated name.
a. Create an index named PartIndex1 on the PartNum field in the OrderLine table.
b. Create an index named PartIndex2 on the Warehouse field in the Part table.
c. Create an index named PartIndex3 on the Warehouse and Class fields in the Part
table.
d. Create an index named PartIndex4 on the Warehouse and OnHand fields in the
Part table and list units on hand in descending order.
5. Drop the PartIndex3 index.
6. Assume the Part table has been created, but there are no integrity constraints. Create
the necessary integrity constraint to ensure that the only allowable values for the Class
field are AP, HW, and SG. Ensure that the PartNum field is the primary key and that the
PartNum field in the OrderLine table is a foreign key that must match the primary key of
the Part table.
7. Add a field named Allocation to the Part table. The allocation is a number representing
the number of units of each part that have been allocated to each customer. Set all
Allocation values to zero. Calculate the number of units of part number KV29 currently
on order. Change the Allocation value for part number KV29 to this number. Display all
the data in the Part table.
8. Increase the length of the Warehouse field in the Part table to two characters. Change
the warehouse number for warehouse 1 to 1a. Display all the data in the Part table.
9. Delete the Allocation field from the Part table. Display all the data in the Part table.
10. What command would you use to delete the Part table from the Premiere Products
database? (Do not delete the Part table.)
11. Write a stored procedure that will change the price of a part with a given part number.
How would you use this stored procedure to change the price of part AT94 to $26.95?
12. Write the code for the following triggers following the style shown in the text.
a. When adding a customer, add the customer’s balance times the sales rep’s
commission rate to the commission for the corresponding sales rep.
Ray Henry would like you to complete the following tasks to help him maintain his database. In
the following exercises, you will use the data in the Henry Books database. Check with your
instructor if you are uncertain about which approach to take.
1. Create a view named PenguinBooks. It consists of the book code, book title, book type,
and book price for every book published by Penguin USA. Display the data in the view.
2. Create a view named Paperback. It consists of the book code, book title, publisher
name, branch number, copy number, and price for every book copy that is available in
paperback. Display the data in the view.
3. Create a view named BookAuthor. It consists of the book code, book title, book type,
author number, first name, last name, and sequence number for each book. Display the
data in the view.
4. Create the following indexes. If it is necessary to name the index in your DBMS, use the
indicated name.
a. Create an index named BookIndex1 on the PublisherName field in the Publisher
table.
b. Create an index named BookIndex2 on the Type field in the Book table.
c. Create an index named BookIndex3 on the BookCode and Type fields in the Book
table and list the book codes in descending order.
5. Drop the BookIndex3 index.
6. Specify the integrity constraint that the price of any book must be less than $90.
7. Ensure that the following are foreign keys (that is, specify referential integrity) within
the Henry Books database.
a. PublisherCode is a foreign key in the Book table.
b. BranchNum is a foreign key in the Copy (Inventory) table.
c. AuthorNum is a foreign key in the Wrote table.
8. Add to the Book table a new character field named Classic that is one character in
length.
9. Change the Classic field in the Book table to Y for the book titled The Grapes of Wrath.
10. Change the length of the Title field in the Book table to 60.
11. What command would you use to delete the Books table from the Henry Books
database? (Do not delete the Book table.)
12. Write a stored procedure that will change the price of a book with a given book code
and quality. How would you use this stored procedure to change the price of any copy
of book 1351 whose quality is excellent to $22.95?
13. Assume the Branch table contains a column called TotalValue that represents the total
price for all books at that branch. Following the style shown in the text, write the code
for the following triggers.
a. When inserting a row in the Copy table, add the price to the total value for the
appropriate branch.