Banner

 

10 - SQL

Answers to exercises

1.
Why is it important that SQL was adopted as a standard by ANSI and ISO?

Since ISO and ANSI are two of the major international standards setting institutions, a standard that is adopted by both of them will be widely used by software manufacturers and thus, end users.

2.
What does it mean to say “SQL is a complete database language?"

SQL is a complete database language because it is both a data definition language (DDL) and a data manipulation language (DML). It can create a relational database, query it, and manipulate the data in the database.

3.
Is SQL a complete programming language? What are the implications of your answer?

SQL is not a complete programming language since it cannot be used to write computer programs and procedures. However, SQL can be embedded in standard programming languages such as COBOL and C.

4.
List some operational advantages of a relational DBMS.

One of the major operational advantages of a relational DBMS is that DDL statements (data definition statements such as creating a table) can be executed at any time. Earlier DBMSs often require halting the entire system to create a new table, views, and indexes.

5.
What is the difference between a base table and a view?

A base table is an autonomous, named table. It is autonomous because it exists in its own right. Views are derived from one or more base tables and actually do not exist independently of the base table. A view is also called a virtual table.

6.
What is the difference between a primary key and a unique key?

The primary key is used to uniquely identify any record of a table and is typically the primary index by which a table is searched. A unique key also uniquely identifies a table, but it is only used for additional indexes to the table.

7.
What is the purpose of an index?

An index is an ordered set of pointers to rows of a base table. It stores data about the position of the data in a certain column of a base table. Indexes are used to accelerate data access and ensure uniqueness.

8.
Consider the three choices for the ON DELETE clause associated with the foreign key constraint. What are the pros and cons of each option?

The three choices for the ON DELETE command are: restrict, cascade, set null. The restrict option does permit deletion of a primary key when there are matching foreign keys in a related table. The cascade option automaticallydeletes all rows in a related table with matching foreign keys. The set null option sets all corresponding rows to null. The following table shows the advantages and disadvantages of all three options.

Option Advantages Disadvantages
RESTRICT * referential integrity

* no accidental loss of records

* update is cumbersome: each value of the corresponding foreign key must be deleted before one value of a primary key can be deleted
CASCADE * referential integrity

* convenient update

* all related records are lost, some data might be lost accidentally
SET NULL * convenient update

* no accidental loss of records

* referential integrity: possibly many useless records with null values as foreign key
9.
Specify the data type (e.g., DECIMAL(6,2)) you would use for the following columns:

(To answer these questions we assume US data. In other countries the data format might vary slightly)

9a.
The selling price of a house.
DECIMAL(10,2)
9b.
A telephone number with area code.
CHAR(14) (xxx) xxx-xxxx
9c.
Hourly temperatures in Antarctica.
DECIMAL(6,2)
9d.
A numeric customer code.
INTEGER
9e.
A credit card number.
CHAR(16)
9f.
The distance between two cities.
SMALLINT
9g.
A sentence using Chinese characters.
VARGRAPHIC
9h.
The number of kilometers from the Earth to a given star.
FLOAT
9i.
The text of an advertisement in the classified section of a newspaper.
LONG VARCHAR
9j.
A basketball score.
SMALLINT
9k.
The title of a CD.
VARCHAR(50)
9l.
The X-ray of a patient.
BLOB
9m.
A U.S. zip code.
DECIMAL(5)
9n.
A British or Canadian postal code.
CHAR(6)
9o.
The photo of a customer.
BLOB
9p.
The date a person purchased a car.
DATE
9q.
The time of arrival of an e-mail message.
TIMESTAMP
9r.
The number of full-time employees in a small business.
SMALLINT
9s.
The text of a speech.
LONG VARCHAR
9t.
The thickness of a layer on a silicon chip.
FLOAT
10.
What is the difference between DROP table and deleting all the rows in a table?

The command DROP TABLE would erase the entire table. Deleting all rows, however, would leave an empty table.

11.
Give some reasons for creating a view.

A view can be created for a variety of reasons:

12.
When is a view created?

The view definition is stored in the systems catalogue and the view is actually created only when it is referenced in a query.

13.
Write SQL codes to create a unique index on firm name for the shr table defined in Chapter 3. Would it make sense to create a unique index for PE ratio in the same table?
CREATE UNIQUE INDEX firmname ON shr (shrfirm); 

a. No, it would not make sense to create a unique index for PE since two companies might have the same PE.

14.
What is the difference between product and join?

When two tables are connected using Product, each row of one table is concatenated with each row of the other table. Join combines only those rows with matching columns (typically a primary key - foreign key match).

15.
What is the difference between an equijoin and a natural join?

An equijoin table contains two identical columns. If one of these two columns is dropped, the remaining table is called a natural join.

16.
You have a choice between executing two queries that will both give the same result. One is written as a simple subquery and the other as a correlated subquery. Which one would you use and why?

It would be wise to choose the alternative with the simple subquery since it would take less time to execute. A correlated subquery must be executed more than once and thus takes more time.

17.
What function would you use for the following situations?
17a.
Computing the total value of a column.
SUM
17b.
Finding the minimum value of a column.
MIN
17c.
Counting the number of customers in the customer table.
COUNT
17d.
Displaying a number with specified precision.
DECIMAL(n,p,d)
17e.
Reporting the month part of a date.
MONTH
17f.
Displaying the second part of a time.
SECOND
17g.
Retrieving the first five characters of a city's name.
SUBSTR(cityname,1,5)
17h.
Reporting the distance to the sun in feet.
distance* 5280 (assuming distance is in miles)
18.
Write SQL statements for the following:
18a.
Let Hui-Tze query and add to the nation table.
GRANT SELECT, INSERT ON nation TO hui-tze; 
18b.
Give Lana permission to update the phone number column in the customer table.
GRANT UPDATE(phonenum) ON CUSTOMER TO lana; 
18c.
Remove all of William's privileges.
REVOKE ALL ON tablename FROM william; 
18d.
Give Chris permission to grant other users authority to select from the address table.

(This has to be repeated for each table for which Chris has been granted privileges)

GRANT SELECT ON address TO chris WITH GRANT OPTION;
18e.
Find the names of all tables that include the word sale.
SELECT TABLE_NAME FROM TABLES WHERE TABLE_NAME REGEXP 'Sale';
18f.
List all the tables created last year.
SELECT TABLE_NAME, CREATE_TIME FROM TABLES WHERE YEAR(CREATE_TIME) = YEAR(CURRENT_DATE) - 1;
18g.
What is the maximum length of the column city in the ClassicModels database? Why do you get multiple rows in the response?
SELECT CHARACTER_MAXIMUM_LENGTH FROM COLUMNS WHERE COLUMN_NAME ='City' AND TABLE_SCHEMA = 'ClassicModels';
18h.
Find all columns that have a data type of SMALLINT .
SELECT TABLE_NAME, COLUMN_NAME FROM COLUMNS WHERE DATA_TYPE ='SMALLINT';
19.
What are the two modes in which you can use SQL?

SQL can be used as an interactive query language or embedded in an application program.

20.
Using the Classic Models database, write an SQL procedure to change the credit limit of all customers in a specified country by a specified amount. Provide before and after queries to show your procedure works.
DELIMITER //
CREATE PROCEDURE increaseCredit (
IN amount DECIMAL(5),
IN countryIn CHARACTER(25))
LANGUAGE SQL
BEGIN
UPDATE Customers
SET creditLimit = creditLimit + amount
WHERE country = countryIn;
END //
21.
How do procedural programming languages and SQL differ in the way they process data? How is this difference handled in an application program? What is embedded SQL?

SQL processes one table at a time while COBOL processes one record at a time. Thus, COBOL uses a loop to process each record of a table.

 

This page is part of the promotional and support material for Data Management (open edition) by Richard T. Watson
For questions and comments please contact the author
Date revised: 10-Dec-2021