0% found this document useful (0 votes)
124 views4 pages

Assumptions: 1. Ownerid Is A Surrogate Key (Starts at 1 and Increments by 1) For SQL Server

The document describes SQL statements to create tables for a pet owner database with columns for owner and pet details. It includes statements to create tables PET_OWNER and PET with appropriate data types and constraints, insert sample data, create a foreign key constraint between the tables, and provides rational for the better table design.

Uploaded by

kam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views4 pages

Assumptions: 1. Ownerid Is A Surrogate Key (Starts at 1 and Increments by 1) For SQL Server

The document describes SQL statements to create tables for a pet owner database with columns for owner and pet details. It includes statements to create tables PET_OWNER and PET with appropriate data types and constraints, insert sample data, create a foreign key constraint between the tables, and provides rational for the better table design.

Uploaded by

kam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

3.

7 Write a SQL CREATE TABLE statement to create the PET_OWNER table, with
OwnerID as a surrogate key. Justify your choices of column properties.
Assumptions:
1. OwnerID is a surrogate key (starts at 1 and increments by 1)
For SQL Server:
CREATE TABLE PET_OWNER(
OwnerID Int NOT NULL IDENTITY(1, 1),
OwnerLastName Char(25) NOT NULL,
OwnerFirstName Char(25) NOT NULL,
OwnerPhone Char(12) NULL,
OwnerEmail VarChar(100) NULL,
CONSTRAINT OWNER_PK PRIMARY KEY(OwnerID)
);

INSERT INTO PET_OWNER


VALUES('Downs', 'Marsha', '555-537-8765', '[email protected]');
INSERT INTO PET_OWNER
VALUES('James', 'Richard', '555-537-7654', '[email protected]');
INSERT INTO PET_OWNER
VALUES('Frier', 'Liz', '555-537-6543', '[email protected]');
INSERT INTO PET_OWNER (OwnerLastName, OwnerFirstName, OwnerEmail)
VALUES('Trent', 'Miles', '[email protected]');

3.9 Create a referential integrity constraint on OwnerID in PET. Assume that deletions
should not cascade.
CREATE TABLE PET(
For SQL Server:
CREATE TABLE PET(
PetID Int NOT NULL IDENTITY(1,1),
PetName Char (50) NOT NULL,
PetType Char (25) NOT NULL,
PetBreed VarChar(100) NULL,
PetDOB Date NULL,
OwnerID Int NOT NULL,
CONSTRAINT PET_PK PRIMARY KEY(PetID),
CONSTRAINT OWNER_FK FOREIGN KEY(OwnerID)
REFERENCES PET_OWNER(OwnerID)
ON DELETE NO ACTION
);

3.11 Write the required SQL statements to create the PET_2 table.
PET_2 (PetName, PetType, PetBreed, PetDOB, OwnerID)
For Access, SQL Server Oracle Database and MySQL:
CREATE TABLE PET_2(
PetName Char(50) NOT NULL,
PetType Char(25) NOT NULL,
PetBreed VarChar(100) NULL,
PetDOB Date NULL,
OwnerID Int NOT NULL
CONSTRAINT PET_2_PK PRIMARY KEY(PetName)
);

This table will NOT be used for data; see question 3.12 below.
3.12 Is PET or PET_2 a better design? Explain your rationale.
PET is the better design because of the PetID field. Using the dog’s name as the primary key
is not a good idea; there are many dogs named “Spot.”
3.13 Write the SQL statements necessary to remove the PET_OWNER table from the
database. Assume that the referential integrity constraint is to be removed. Do not
run these commands in an actual database!
ALTER TABLE PET
DROP CONSTRAINT OWNER_FK;
DROP TABLE PET_OWNER;

3.14 Write the SQL statements necessary to remove the PET_OWNER table from the
database. Assume that the PET table is to be removed. Do not run these
commands in an actual database!
DROP TABLE PET;
DROP TABLE PET_OWNER;

(and only in that order.)


3.15 Write a SQL statement to display all columns of all rows of PET. Do not use the
asterisk (*) notation.
SELECT PetID, PetName, PetType, PetBreed, PetDOB, OwnerID
FROM PET;
3.16 Write a SQL statement to display all columns of all rows of PET. Use the asterisk (*)
notation.
SELECT *
FROM PET;

3.17 Write a SQL statement to display the breed and type of all pets.
SELECT PetBreed, PetType
FROM PET;

3.18 Write a SQL statement to display the breed, type, and DOB of all pets having the type
Dog.
SELECT PetBreed, PetType, PetDOB
FROM PET
WHERE PetType = 'Dog';
3.19 Write an SQL statement to display the PetBreed column of PET.
SELECT PetBreed
FROM PET;

3.20 Write a SQL statement to display the PetBreed column of PET. Do not show
duplicates.
SELECT DISTINCT PetBreed
FROM PET;

3.21 Write a SQL statement to display the breed, type, and DOB for all pets having the
type Dog and the breed Std. Poodle.
SELECT PetBreed, PetType, PetDOB
FROM PET
WHERE PetType = 'Dog' AND PetBreed = 'Std. Poodle';

You might also like