Answers to Review Questions

advertisement
Answers to Review Questions – Ch 3 SQL
1,2,4,5,6,12,14,15,16,17,20,21
James River Jewelry A,B,C (using INSERT INTO),D,E,F,P,R
3.1
What does SQL stand for?
Structured Query Language
3.2
What is a data sublanguage?
A language that has only constructs for defining and processing a database. To
have full language capability, it needs to be embedded in a such as COBOL, Visual
Basic or C#.
3.4
Why is it important to learn SQL?
You cannot do everything with graphical tools, it’s the only way to create SQL
programmatically, and you’ll be a stronger database developer if you know it.
3.5
Describe in your own words the purpose of the two business rules listed on page
56.
Use the following tables for your answers to questions 3.6–4.4:
PET_OWNER (OwnerID, Name, Phone, Email)
PET (PetID, Name, Type, Breed, DOB, OwnerID)
(See Figure 1-27 on page 26 for sample data)
3.6
Code a CREATE TABLE command to create the PET_OWNER table. Justify
your choices of column properties.
CREATE TABLE PET_OWNER (
OwnerID
Integer
Primary Key,
Name
Char (50)
Not Null,
Phone
Char (12),
Email
VarChar(100));
OwnerID is integer because it will be a surrogate key. (By the way, the means by
which such keys are created varies from DBMS to DBMS, and is not discussed in
this text.) Name is Char rather than varchar because we may search on it. Neither
Phone nor Email are required – they’re not keys and we may want to record a
customer even if we have neither.
3.12
Code the SQL statements necessary to remove the PET_OWNER table from the
database. Assume that the referential integrity constraint is to be removed.
ALTER TABLE PET DROP CONSTRAINT OwnerFK;
DROP TABLE PET_OWNER;
3.14
Code a SQL statement to display all columns of all rows of PET. Do not use the
* notation.
SELECT
FROM
3.15
Code a SQL statement to display all columns of all rows of PET. Use the *
notation.
SELECT
FROM
3.16
*
PET;
Code SQL to display the Breed and Type of all pets.
SELECT
FROM
3.17
PetID, Name, Type, Breed, DOB, OwnerID
PET;
Breed, Type
PET;
Code SQL to display the Breed, Type, and DOB of all pets having the Type Dog.
SELECT
FROM
WHERE
Breed, Type, DOB
PET
Type = ‘Dog’;
Note: cannot be Type = ‘DOG’.
3.20
Code SQL to display the Breed, Type, and DOB for all pets having the Type Dog
and the Breed Std. Poodle.
SELECT
FROM
WHERE
3.21
Breed, Type, DOB
PET
Type = ‘Dog’ AND Breed = ‘Std. Poodle’;
Code SQL to display the Name, Breed, and Type for all pets that are not of Type
Cat, Dog, or Fish.
SELECT
FROM
WHERE
Name, Breed, Type
PET
Type NOT IN (‘Cat’, ‘Dog’, ‘Fish’);
Answers to James River Jewelry Project Questions
Assume that James River designs a database with the following tables:
CUSTOMER (CustomerID, Name, Phone, Email)
PURCHASE (InvoiceNumber, Date, PreTaxAmount, CustomerID)
PURCHASE_ITEM (InvoiceNumber, ItemNumber, RetailPrice)
ITEM (ItemNumber, Description, Cost, ArtistName)
Answer the following questions for this database:
A.
Code CREATE TABLE statements for each of these tables.
For this example, I’ll use SQL Server data types. Also note creation of surrogate
key CustomerID using SQL Server syntax
CREATE TABLE CUSTOMER (
CustomerID
Integer
Primary Key IDENTITY (100,10),
Name
Char (50)
Not Null,
Phone
Char (12),
Email
VarChar(100));
CREATE TABLE PURCHASE (
InvoiceNumber Integer
Date
Date
City
Char (35)
PreTaxAmount Money
CustomerID
Integer
Primary Key,
Not Null,
Not Null,
Not Null,
Not Null);
CREATE TABLE PURCHASE_ITEM (
InvoiceNumber Integer
Not Null,
ItemNumber Integer
Not Null,
RetailPrice
Money
Not Null);
ALTER TABLE PURCHASE_ITEM ADD CONSTRAINT PurchaseItemPK PRIMARY
KEY (InvoiceNumber, ItemNumber);
CREATE TABLE ITEM
ItemNumber
Description
Cost
ArtistName
B.
(
Integer
Char (100)
Money
Char (100));
Primary Key,
Not Null,
Not Null,
Write foreign key constrains for the relationships in each of these tables. Make
your own assumptions regarding cascading deletions, and justify those
assumptions.
ALTER table PURCHASE ADD CONSTRAINT CustomerFK FOREIGN KEY
(CustomerID) REFERENCES CUSTOMER;
(No cascade delete here because I don’t want any purchase data to be
inadvertently deleted.)
ALTER table PURCHASE_ITEM ADD CONSTRAINT PurchaseFK FOREIGN KEY
(InvoiceNumber) REFERENCES PURCHASE ON DELETE CASCADE;
(Cascade here because want line items to be deleted when purchase invoice is.)
ALTER table PURCHASE_ITEM ADD CONSTRAINT ItemFK FOREIGN KEY
(ItemNumber) REFERENCES PURCHASE;
(No cascade delete here because don’t want to be able to remove items if there are
purchases that use them.)
C.
Code SQL statements to insert three rows of data into each of these tables.
Assume that CustomerID of CUSTOMER is a surrogate key column whose
values will be supplied by the DBMS.
All will be in format like:
INSERT INTO CUSTOMER VALUES (Mary Jones’, ‘2320090’,‘Mary@somewhere.com’);
D.
E.
Code SQL statements to list all columns of all tables.
SELECT
FROM
*
CUSTOMER;
SELECT
FROM
*
PURCHASE;
SELECT
FROM
*
PURCHASE_ITEM;
SELECT
FROM
*
ITEM;
Code SQL statements to list the ItemNumber and Description for all items that
cost more than $100.
SELECT
FROM
WHERE
F.
Code SQL statements to list the ItemNumber and Description for all items that
cost more than $100 and were produced by an artist with a name ending with the
letters son.
SELECT
FROM
WHERE
AND
P.
ItemNumber, Description
ITEM
Cost > 100;
ItemNumber, Description
ITEM
Cost > 100
ArtistName LIKE ‘%son’;
Code SQL statements to modify all ITEM rows with a name of Baker to Rex
Baker.
UPDATE
SET
WHERE
ITEM
ArtistName = ‘Rex Baker’
ArtistName = ‘Baker’;
R. Given your assumptions about cascading deletions in your answer to question B,
write the fewest number of DELETE statements possible to remove all of the data
in your database, but leave the table structures intact.
DELETE
FROM
DELETE
FROM
DELETE
FROM
PURCHASE;
ITEM;
CUSTOMER;
Note the delete of PURCHASE will delete all rows in PURCHASE_ITEM as well.
Download