Please include the questions with your answers. Answer in complete sentences whe
ID: 3680923 • Letter: P
Question
Please include the questions with your answers. Answer in complete sentences where applicable.
1. Does all standard SQL work in Microsoft Access? Explain.
2. List and describe the four basic SQL data types.
3. List and describe five SQL built-in functions.
The best way to learn SQL is by actually using it. In the following problems, we will use SQL to create, populate, and query a small database. Use SQL in Microsoft Access to complete the problems. Save all queries as instructed in the problem. Submit the database file (save as Pet_Database.accdb and KEEP THIS FILE for use with a later assignment) with all your queries in addition to the Word document containing the questions and answers for numbers 1, 2, and 3.
Use the following information for problems 4 – 10:
Tables:
PET_OWNER (OwnerID, OwnerLastName, OwnerFirstName, OwnerPhone, OwnerEmail)
PET (PetID, PetName, PetType, PetBreed, PetDOB, OwnerID)
**Note: OwnerID is italicized to indicate the Foreign Key**
Data:
4. Write an SQL CREATE TABLE statement to create the PET_OWNER table, with OwnerID as a surrogate key. Save as CreatePetOwner.
5. Write a set of SQL INSERT statements to populate the PET_OWNER table with the data given above. Save as PopulatePetOwner.
6. Write an SQL CREATE TABLE statement to create the PET table, with PetID as a surrogate key. Save as CreatePet.
7. Write a set of SQL INSERT statements to populate the PET table with the data given above. Save as PopulatePet.
8. Write an SQL statement to display the breed and type of all pets. Save as AllBreeds.
9. Write an SQL statement to display the breed, and DOB of all pets having the type Cat. Save as Cats.
10. Write an SQL statement to display the first name, last name, and email of all owners, sorted in alphabetical order by last name. Save as AlphaOwners.
11. Write an SQL statement to display all the owners’ names, with the first name in all lower case and the last name in all upper case. Save as UpperLower.
12. Write an SQL statement to display the total number of pets. Save as TotalPets.
13. Write an SQL statement to display the last name, first name and email of any owner who has a NULL value for OwnerPhone. (Note: there should be one owner who has a NULL value for OwnerPhone.) Save as PhoneNull.
14. Write an SQL statement to count the number of distinct breeds. Save as NumberOfBreeds.
15. Write an SQL statement to display the names of all the dogs. Save as Dogs
Explanation / Answer
Que. 1 Does all standard SQL work in Microsoft Access? Explain.
Ans: Nearly all SQL Query works with the MS Access. We need to modify some statement to execute
them as they as executed in SQL.When you want to retrieve data from a database, you ask for the data by using Structured Query Language, or SQL. SQL is a computer language that closely resembles English that database programs understand. Knowing SQL is important because every query in Microsoft Access uses SQL.
2. List and describe the four basic SQL data types.
Ans: Four general SQL data Types:
1. char(n)
Description: Fixed width character string. Maximum 8,000 characters.
Storage: Defined width.
2. varchar(n):
Description: Variable width character string. Maximum 8,000 characters
Storage: 2 bytes + number of chars
3. int
Description: Allows whole numbers between -2,147,483,648 and 2,147,483,647
Storage: 4 bytes
4. bigint
Description: Allows whole numbers between -9,223,372,036,854,775,808 and
9,223,372,036,854,775,807
Storage: 8 bytes.
3. List and describe five SQL built-in functions.
Ans.
1. SQL Aggregate Functions
AVG() - Returns the average value
COUNT() - Returns the number of rows
FIRST() - Returns the first value
LAST() - Returns the last value
MAX() - Returns the largest value
MIN() - Returns the smallest value
SUM() - Returns the sum
2. SQL Scalar Functions
UCASE() - Converts a field to upper case
LCASE() - Converts a field to lower case
MID() - Extract characters from a text field
LEN() - Returns the length of a text field
ROUND() - Rounds a numeric field to the number of decimals specified
NOW() - Returns the current system date and time
FORMAT() - Formats how a field is to be displayed
4. Write an SQL CREATE TABLE statement to create the PET_OWNER table, with OwnerID as a surrogate key. Save as CreatePetOwner.
Ans:
SQL:CREATE TABLE PET_OWNER( OwnerID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
OwnerLastName varchar(50), OwnerFirstName varchar(50), OwnerPhone varchar(15), OwnerEmail
varchar(255));
5. Write a set of SQL INSERT statements to populate the PET_OWNER table with the data given above. Save as PopulatePetOwner.
Ans:
SQL:Insert INTO PET_OWNER(OwnerID, OwnerLastName, OwnerFirstName, OwnerPhone,
OwnerEmail) VALUES(1,’Downs’,’Marsha’,’555-537-8765’,’Marshadowns@somewhere.com’ );
Or
SQL:Insert INTO PET_OWNER(OwnerLastName, OwnerFirstName, OwnerPhone, OwnerEmail)
VALUES(’Downs’,’Marsha’,’555-537-8765’,’Marshadowns@somewhere.com’ );
Note: Id will be added automatically.
6. Write an SQL CREATE TABLE statement to create the PET table, with PetID as a surrogate key. Save as CreatePet.
Ans:
SQL:CREATE TABLE PET( PetID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, PetName
varchar(50), PetType varchar(50),PetBreed varchar(30),PetDOB varchar(15),OwnerId int NOT NULL,
FOREIGN KEY (OwnerID) REFERENCES PET_OWNER(OwnerID));
7. Write a set of SQL INSERT statements to populate the PET table with the data given above. Save as PopulatePet.
Ans:
SQL:INSERT INTO PET(PetID, PetName, PetType, PetBreed, PetDOB, OwnerId)
VALUES(1,’King’,’Dog’,’std.Poddle’,’27-feb-2011’,1);
8. Write an SQL statement to display the breed and type of all pets. Save as AllBreeds.
Ans:
SQL:Select PetType, PetBreed form PET;
9. Write an SQL statement to display the breed, and DOB of all pets having the type Cat. Save as Cats.
Ans:
SQL: Select PetBreed, PetDOB from PET where PetType=’Cat’;
10. Write an SQL statement to display the first name, last name, and email of all owners, sorted in alphabetical order by last name. Save as AlphaOwners.
Ans:
SQL:select OwnerFirstName, OwnerLastName, OwnerEmail from pet_owner order by
OwnerLastName ASC ;
11. Write an SQL statement to display all the owners’ names, with the first name in all lower case and the last name in all upper case. Save as UpperLower.
Ans:
SQL: select LCASE(OwnerFirstName), UCASE(OwnerLastName) from pet_owner ;
12. Write an SQL statement to display the total number of pets. Save as TotalPets.
Ans:
SQL: SELECT COUNT(*) FROM PET ;
13. Write an SQL statement to display the last name, first name and email of any owner who has a NULL value for OwnerPhone. (Note: there should be one owner who has a NULL value for OwnerPhone.) Save as PhoneNull.
Ans:
SQL:SELECT OwnerFirstName, OwnerLastName, OwnerEmail from PET_OWNER where
OwnerPhone= '‘;
14. Write an SQL statement to count the number of distinct breeds. Save as NumberOfBreeds.
Ans:
SQL:Select Count( Distinct PetBreed) from PET ;
15. Write an SQL statement to display the names of all the dogs. Save as Dogs
Ans:
SQL: Select PetName from PET where PetType=’Dog’ ;