Vous êtes sur la page 1sur 7

Concepts of Database Management, Sixth Edition

Solutions 3-1

Chapter 3
The Relational Model 2: SQL
Solutions
Answers to Review Questions
Note: Answers to odd-numbered review questions are found in Appendix D of the textbook.
1.

To create a table in SQL, use a CREATE TABLE command. The word TABLE is followed by the name of the
table to be created and then by the names and data types of the columns (fields) that make up the table. The
data types you can use are INTEGER (large negative and positive whole numbers), SMALLINT (whole
numbers from -32,768 to 32,767), DECIMAL (numbers that have a decimal part), CHAR (alphanumeric
strings), and DATE (date values).

2.

The WHERE clause lets you specify a condition. Only those rows on which a condition is satisfied will be
included in the query results. The comparison operators you can include in a WHERE clause are: = (equal to),
> (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to), and < > (not equal to).

3.

A compound condition is formed by connecting two or more simple conditions using one or both of the
following operators: AND and OR. You can also precede a single condition with the NOT operator to negate a
condition. When you connect simple conditions using the AND operator, all the simple conditions must be true
for the compound condition to be true. When you connect simple conditions using the OR operator, the
compound condition will be true whenever any of the simple conditions are true.

4.

A computed field is one whose values are calculated from other fields. To use a computed field in an SQL
query, type the desired calculation. To assign a name to a computed field, follow the calculation with the word
AS and then the desired name.

5.

To use the LIKE or IN operators in an SQL query, include them in the WHERE clause. A character string
containing one or more wildcards is included after the word LIKE. The word IN is followed by a list of values.

6.

To sort data, use the ORDER BY clause, which consists of the words ORDER BY followed by the sort key. If
there is more than one sort key, the major key is listed first. To sort data in descending order, follow the sort
key with the word DESC.

7.

Use an SQL built-in function (COUNT, SUM, AVG, MAX, and MIN) by including it in the SELECT clause
followed by the name of the field to which it applies.

8.

A subquery is a query that is included in another query. The subquery is evaluated before the query that
contains it.

9.

To group data in SQL, include the words GROUP BY followed by the field or fields on which the data is to be
grouped in the query results. If you group data, you only can include the fields on which you are grouping or
statistics in the SELECT clause.

10.

To join tables in SQL, use a SELECT command that lists two or more tables to be joined in the FROM clause,
and then specify the matching columns in the WHERE clause.

11.

To qualify the name of a field in an SQL query, precede the field with the name of the table to which it belongs,
followed by a period. It is necessary to qualify a field if the field name occurs in more than one of the tables
listed in the FROM clause.

12.

To take the union of two tables, create a SQL query for each table and then place the word UNION between the
queries. The two tables involved in a union must have the same structure, or be union compatible; in other
words, they must have the same number of fields and their corresponding fields must have the same data types.

Concepts of Database Management, Sixth Edition

Solutions 3-2

13.

The update commands in SQL are INSERT, which inserts new rows in a table; UPDATE, which changes all the
rows in a table that satisfy some condition; and DELETE, which deletes all the rows in a table that satisfy some
condition.

14.

Create a query to select the columns from the desired table. After the SELECT clause, include an INTO clause
followed by the name of the table to be created.

Answers to Premiere Products Exercises


Note: See the Instructors Resource Kit on CD-ROM for a copy of the Premiere Products database that contains the
solutions to these exercises. It also includes the SQL script files necessary to create the Premiere Products queries in
MySQL or Oracle.
1.
SELECT CustomerNum, CustomerName
FROM Customer;
2.
SELECT *
FROM Part;
3.
SELECT CustomerNum, CustomerName
FROM Customer
WHERE RepNum='35';
4.
SELECT CustomerNum, CustomerName
FROM Customer
WHERE RepNum='35'
AND CreditLimit=10000;
5.
SELECT CustomerNum, CustomerName
FROM Customer
WHERE RepNum='35'
OR CreditLimit=10000;
6.
SELECT OrderNum, OrderDate, Orders.CustomerNum, CustomerName
FROM Orders, Customer
WHERE Orders.CustomerNum=Customer.CustomerNum;
7.
SELECT CustomerNum, CustomerName
FROM Customer, Rep
WHERE Customer.RepNum=Rep.RepNum
AND LastName='Perez'
AND FirstName='Juan';
8.
SELECT COUNT(*)
FROM Orders
WHERE OrderDate='20-OCT-2010';
Access:
SELECT COUNT(*)
FROM Orders
WHERE OrderDate=#10/20/2010#;
9.
SELECT SUM(Balance)
FROM Customer

Concepts of Database Management, Sixth Edition

Solutions 3-3

WHERE RepNum='35';
10.
SELECT PartNum, Description, OnHand * Price
FROM Part
WHERE Class='HW';
11.
SELECT *
FROM Part
ORDER BY Description;
12.
SELECT *
FROM Part
ORDER BY Class, PartNum;
13.
SELECT Class, SUM(OnHand)
FROM Part
GROUP BY Class;
14.
SELECT PartNum, Description, OnHand, Warehouse, Price
INTO SportingGoods
FROM Part
WHERE Class='SG';
15.
UPDATE SportingGoods
SET Description='Fitness Gym'
WHERE PartNum='BV06';
16.
DELETE
FROM SportingGoods
WHERE Price>1000;

Answers to Henry Books Case


Note: See the Instructors Resource Kit on CD-ROM for a copy of the Henry Books database that contains the solutions
to these exercises. It also includes the SQL script files necessary to create the Henry Books queries in MySQL or
Oracle.
1.
SELECT AuthorNum, AuthorLast
FROM Author;
2.
SELECT *
FROM Branch;
3.
SELECT PublisherName
FROM Publisher
WHERE City='Boston';
4.
SELECT PublisherName
FROM Publisher
WHERE NOT City='Boston';
5.
SELECT BranchName

Concepts of Database Management, Sixth Edition


FROM Branch
WHERE NumEmployees>=9;
6.
SELECT BookCode, Title
FROM Book
WHERE Type='SFI';
7.
SELECT BookCode, Title
FROM Book
WHERE Type='SFI'
AND Paperback;
or
SELECT BookCode, Title
FROM Book
WHERE Type='SFI'
AND Paperback=Yes;
or
SELECT BookCode, Title
FROM Book
WHERE Type='SFI'
AND Paperback=True;
8.
SELECT BookCode, Title
FROM Book
WHERE Type='SFI'
OR PublisherCode='PE';
9.
SELECT BookCode, Title, Price
FROM Book
WHERE Price BETWEEN 5 and 10;
10.
SELECT BookCode, Title
FROM Book
WHERE Type='FIC'
AND Price<10;
11.
SELECT BookCode, Title, .85 * Price AS DiscountedPrice
FROM Book;
12.
SELECT BookCode, Title
FROM Book
WHERE Type IN ('SFI', 'HOR', 'ART');
13.
SELECT BookCode, Title, PublisherCode
FROM Book
ORDER BY PublisherCode, Title;
14.
SELECT COUNT(*)
FROM BOOK
WHERE Type='SFI';
15.
SELECT Type, AVG(Price)
FROM Book

Solutions 3-4

Concepts of Database Management, Sixth Edition

Solutions 3-5

GROUP BY Type
ORDER BY Type;
16.
SELECT BookCode, Title, Book.PublisherCode, PublisherName
FROM Book, Publisher
WHERE Book.PublisherCode=Publisher.PublisherCode;
17.
SELECT Title, Price
FROM Book, Publisher
WHERE Book.PublisherCode=Publisher.PublisherCode
AND PublisherName='Taunton Press';
18.
SELECT Title, BookCode
FROM Book, Publisher
WHERE Book.PublisherCode=Publisher.PublisherCode
AND PublisherName='Putnam Publishing Group'
AND Price>15;
19.
SELECT BookCode, Title, PublisherCode, Price
INTO Fiction
FROM Book
WHERE Type='FIC';
20.
UPDATE Fiction
SET Price=14.50
WHERE Price=14.00;
21.
DELETE
FROM Fiction
WHERE PublisherCode=VB;

Answers to Alexamara Marina Group Case


Note: See the Instructors Resource Kit on CD-ROM for a copy of the Alexamara database that contains the solutions to
these exercises. It also includes the SQL script files necessary to create the Henry Books queries in MySQL or Oracle.
1.
SELECT OwnerNum, LastName, FirstName
FROM Owner;
2.
SELECT *
FROM Marina;
3.
SELECT LastName, FirstName
FROM Owner
WHERE City = 'Bowton';
4.
SELECT LastName, FirstName
FROM Owner
WHERE City <> 'Bowton';
5.
SELECT MarinaNum, SlipNum
FROM MarinaSlip
WHERE Length <= 30;
6.

Concepts of Database Management, Sixth Edition

Solutions 3-6

SELECT MarinaNum, SlipNum


FROM MarinaSlip
WHERE BoatType='Dolphin 28';
7.
SELECT SlipNum
FROM MarinaSlip
WHERE MarinaNum = '1'
AND BoatType = 'Dolphin 28';
8.
SELECT BoatName
FROM MarinaSlip
WHERE Length >= 25
AND Length <= 30;
9.
SELECT SlipNum
FROM MarinaSlip
WHERE MarinaNum = '1'
AND RentalFee < 3000;
10.
.
SELECT SlipId, CategoryNum, EstHours, 60 * EstHours AS EstimatedCost
FROM ServiceRequest;
11.
SELECT MarinaNum, SlipNum
FROM MarinaSlip
WHERE BoatType IN ('Sprite 4000', 'Sprite 3000', 'Ray 4025');
12.
SELECT MarinaNum, SlipNum, BoatName
FROM MarinaSlip
ORDER BY MarinaNum, BoatName;
13.
SELECT COUNT(*)
FROM MarinaSlip
WHERE BoatType='Dolphin 28';
14.
SELECT SUM(RentalFee)
FROM MarinaSlip
GROUP BY Length;
15.
SELECT MarinaNum, SlipNum, BoatName, Owner.OwnerNum, LastName, FirstName
FROM MarinaSlip, Owner
WHERE MarinaSlip.OwnerNum=Owner.OwnerNum;
16.
SELECT SlipId, Description, Status
FROM ServiceRequest, ServiceCategory
WHERE ServiceRequest.CategoryNum = ServiceCategory.CategoryNum
AND CategoryDescription='Routine engine maintenance';
17.
SELECT ServiceRequest.SlipID, MarinaNum, SlipNum, EstHours, SpentHours,
Owner.OwnerNum, LastName
FROM ServiceRequest, MarinaSlip, ServiceCategory, Owner
WHERE ServiceRequest.CategoryNum = ServiceCategory.CategoryNum
AND ServiceRequest.SlipID = MarinaSlip.SlipID
AND MarinaSlip.OwnerNum = Owner.OwnerNum
AND CategoryDescription='Routine engine maintenance';

Concepts of Database Management, Sixth Edition


18.
SELECT MarinaNum, SlipNum, RentalFee, BoatName, OwnerNum
INTO LargeSlip
FROM MarinaSlip
WHERE Length=40;
19.
UPDATE LargeSlip
SET RentalFee=3900
WHERE RentalFee=3800;
20.
DELETE
FROM LargeSlip
WHERE RentalFee=3600;

Solutions 3-7

Vous aimerez peut-être aussi