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. Understanding how SQL works can help create better queries, and can make it easier for you to fix a query when it is not returning the results that you want.
Que2: 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.
Que: 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
Que. 4: Write an SQL CREATE TABLE statement to create the PET_OWNER table, with OwnerID as a surrogate key. Save as CreatePetOwner.
Ans: CreatePetOwner.
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));
Que. 5: Write a set of SQL INSERT statements to populate the PET_OWNER table with the data given above. Save as PopulatePetOwner.
Ans: PopulatePetOwner
SQL: Insert INTO PET_OWNER(OwnerID, OwnerLastName, OwnerFirstName, OwnerPhone, OwnerEmail) VALUES(1,’Downs’,’Marsha’,’555-537-8765’,’Marshadowns@somewhere.com’ );
Or
Insert INTO PET_OWNER(OwnerLastName, OwnerFirstName, OwnerPhone, OwnerEmail) VALUES(’Downs’,’Marsha’,’555-537-8765’,’Marshadowns@somewhere.com’ );
Note: Id will be added automatically.
Que. 6: Write an SQL CREATE TABLE statement to create the PET table, with PetID as a surrogate key. Save as CreatePet.
Ans: CreatePet:
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));
Que 7: Write a set of SQL INSERT statements to populate the PET table with the data given above. Save as PopulatePet.
Ans: PouplatePet
SQL: INSERT INTO PET(PetID, PetName, PetType, PetBreed, PetDOB, OwnerId) VALUES(1,’King’,’Dog’,’std.Poddle’,’27-feb-2011’,1);
Que. 8: Write an SQL statement to display the breed and type of all pets. Save as AllBreeds.
Ans: AllBreeds
SQL: Select PetType, PetBreed form PET;
Que:9 Write an SQL statement to display the breed, and DOB of all pets having the type Cat. Save as Cats.
Ans: Cats
SQL: Select PetBreed, PetDOB from PET where PetType=’Cat’;
Que: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: AlphaOwners
SQL: select OwnerFirstName, OwnerLastName, OwnerEmail from pet_owner order by OwnerLastName ASC ;
Que: 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: UpperLower
SQL: select LCASE(OwnerFirstName), UCASE(OwnerLastName) from pet_owner ;
Que:12 Write an SQL statement to display the total number of pets. Save as TotalPets.
Ans: TotalPets
SQL: SELECT COUNT(*) FROM PET ;
Que: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: PhoneNull
SQL: SELECT OwnerFirstName, OwnerLastName, OwnerEmail from PET_OWNER where OwnerPhone= '‘;
Que:14 Write an SQL statement to count the number of distinct breeds. Save as NumberOfBreeds.
Ans: NumberOfBreeds
SQL: Select Count( Distinct PetBreed) from PET ;
Que:15. Write an SQL statement to display the names of all the dogs. Save as Dogs.
Ans: Dogs
SQL: Select PetName from PET where PetType=’Dog’ ;

Комментариев нет:
Отправить комментарий