Module 4
Module 4
Module-04
4.2 Objective
➢ To learn Structured Query Language of DBMS
➢ To create relational database tables from the entities and their attributes in an
entity/relationship diagram (ERD)
➢ To declare primary keys and foreign keys based on the relationships in an ERD
➢ To establish NOT NULL, UNIQUE, and CHECK constraints using SQL
➢ To perform SQL queries with the SELECT command to retrieve data from a single table or
multiple tables
4.3 Syllabus
Prerequisites: Syllabus: Duration: Self study:
Relational • Background 8 hours create the college
model. • Basic Structure library database
o The select Clause using SQL
o The where Clause command then
o The from Clause and use all the
o The Rename Operation DML, DDL, and DCL
o Tuple Variables command.
o String Operations
o Ordering the Display of Tuples
o Duplicate Tuples
• Set Operations
• Aggregate Functions
• Null Values
• Nested Subqueries
o Set Membership
o Set Comparison
o Test for Empty Relations
Module 4 : Structured Query Language (SQL) 73
4.4 Definition
Data definition: SQL lets a user define the structure and organization of the stored data and
relationships among the stored data items.
Data retrieval: SQL allows a user or an application program to retrieve stored data from the
database and use it.
Data manipulation: SQL allows a user or an application program to update the database by adding
new data, removing old data, and modifying previously stored data.
Access control: SQL can be used to restrict a user’s ability to retrieve, add, and modify data,
protecting stored data against unauthorized access.
Data sharing: SQL is used to coordinate data sharing by concurrent users, ensuring that they do not
interface with one another.
Data integrity: SQL defines integrity constraints in the database, protecting in the database,
protecting it from corruption due to inconsistent updates or system failures.
4.5 Learning
With the help of this concept student can create the database of any organization or any system
and also can be manage in efficient manner.
Database Management System 74
All of the enterprise-level relational database vendors, as well as some of the workgroup-level and
desktop-level vendors, provide support for SQL. The syntax for SQL is based on the currently
emerging SQL3 standard and represents an improvement over the earlier International Standard
Database Language (1992), or SQL/92 (also known as SQL2). SQL3 provides support for complex
data types, procedures and a number of other useful features. The RDBMS vendors provide a
"superset of a subset" of the SQL standard by incorporating a subset of the overall standard and
then adding their own vendor enhancements.
Because SQL is a standard language it has become widely accepted in commercial database
applications. The SQL standard allows portability of the application software between different
RDBMSs. Of course this advantage may be restricted somewhat depending on the number of
vendor-specific features included in the application.
SQL not only allows the creation of database objects (e.g., tables, views, users, and so forth) but
also the specification of security controls, integrity constraints, and control of database
transactions. It is also possible to embed SQL into a host 3GL (third generation language), such as
Java or C. Some vendors (e.g., Oracle) also provide a proprietary 3GL for embedded SQL. By
embedding SQL into a 3GL we get the benefits of a 4GL along with the programming control
structures of a 3GL (e.g., sequence, looping, if-then-else, and case constructs).
Prior to SQL, there were other contenders for a relational database language, such as QUEL (Query
Language). The name SQL is actually derived from SEQUEL (Structured English Query Language). In
several desktop-level products (e.g., dBASE and Microsoft Access), a Query-By-Example (QBE) grid
is provided with the built-in user interface to specify values for columns of tables for which data are
to be updated, deleted, and retrieved.
There are two major types of SQL commands: data definition language (DDL) and data
manipulation language (DML). DDL-type SQL commands are used to create databases, tables,
users, and all other database objects. DML-type SQL commands are used to insert, update, delete,
and retrieve data from a relational database. Some vendors (e.g., Oracle) also include a third type
of SQL, data control language (DCL), which includes those DDL commands used for granting
permissions to users and for other security matters.
Module 4 : Structured Query Language (SQL) 75
Commercial database systems require more user-friendly query languages. We will look at SQL in
detail. Although referred to as query languages, SQL contains facilities for designing and modifying
the database.
The relation schemes for the banking example are:
Branch-scheme = (bname, bcity, assets)
Customer-scheme = (cname, street, ccity)
Depositor-scheme = (cname, account#)
Account-scheme = (bname, account#, balance)
Loan-scheme = (bname, loan#, amount)
Borrower-scheme = (cname, loan#)
Background
1. SQL has become the standard relational database language. It has several parts:
o Data definition language (DDL) - provides commands to
▪ Define relation schemes.
▪ Delete relations.
▪ Create indices.
▪ Modify schemes.
o Interactive data manipulation language (DML) - a query language based on both
relational algebra and tuple relational calculus, plus commands to insert, delete and
modify tuples.
o Embedded data manipulation language - for use within programming languages like
C, PL/1, Cobol, Pascal, etc.
o View Definition - commands for defining views
o Authorization - specifying access rights to relations and views.
o Integrity - a limited form of integrity checking.
o Transaction control - specifying beginning and end of transactions.
We will only look at basic DDL, the DML and views. Integrity features will be covered in
Chapter 5.
Basic Structure
1. Basic structure of an SQL expression consists of select, from and where clauses.
o select clause lists attributes to be copied - corresponds to relational algebra project.
o from clause corresponds to Cartesian product - lists relations to be used.
o where clause corresponds to selection predicate in relational algebra.
Database Management System 76
a1,a2……..an( p(R1*R2*…………*Rm))
3. select * means select all the attributes. Arithmetic operations can also be in the selection
list.
The where Clause
1. The predicates can be more complicated, and can involve
o Logical connectives and, or and not.
o Arithmetic expressions on constant or tuple values.
o The between operator for ranges of values.
Example: Find account number of accounts with balances between $90,000 and $100,000.
select account#
from account
where balance between 90000 and 100000
The from Clause
1. The from class by itself defines a Cartesian product of the relations in the clause.
2. SQL does not have a natural join equivalent. However, natural join can be expressed in
terms of a Cartesian product, selection, and projection.
3. For the relational algebra expression
Πename,loan#(borrower1 loan)
we can write in SQL,
select distinct cname, borrower.loan#
from borrower, loan
where borrower.loan# = loan.loan#
4. More selections with join: ``Find the names and loan numbers of all customers who have a
loan at the SFU branch,'' we can write in SQL,
select distinct cname, borrower.loan#
from borrower, loan
where borrower.loan# = loan.loan#
and bname=``SFU''
The Rename Operation
1. Rename: a mechanism to rename both relations and attributes.
2. as-clause can appear in both the select and from clauses:
old-name as new-name.
Example.
select distinct cname, borrower.loan# as loan_id
Database Management System 78
''), extracting substrings, finding the length of strings, converting between upper case and
lower case, and so on.
Ordering the Display of Tuples
Module 4 : Structured Query Language (SQL) 79
1. SQL allows the user to control the order in which tuples are displayed.
o order by makes tuples appear in sorted order (ascending order by default).
o desc specifies descending order.
o asc specifies ascending order.
select *
from loan
order by amount desc, loan# asc
Sorting can be costly, and should only be done when needed
Duplicate Tuples
• Formal query languages are based on mathematical relations. Thus no duplicates appear in
relations.
• As duplicate removal is expensive, SQL allows duplicates.
• To remove duplicates, we use the distinct keyword.
• To ensure that duplicates are not removed, we use the all keyword.
• Multiset (bag) versions of relational algebra operators.
o if there are c1 copies of tuples t1 in R1, and t1 satisfies selection θ, then there are c1
copies of t1 in σθ (R1) .
Aggregate Functions
1. In SQL we can compute functions on groups of tuples using the group by clause.
Attributes given are used to form groups with the same values. SQL can then compute
o average value -- avg
o minimum value -- min
o maximum value -- max
o total sum of values -- sum
o number in group -- count
These are called aggregate functions. They return a single value.
2. Some examples:
1. Find the average account balance at each branch.
select bname, avg (balance)
from account
group by bname
2. Find the number of depositors at each branch.
select bname, count (distinct cname)
from account, depositor
where account.account# = depositor.account#
group by bname
We use distinct so that a person having more than one account will not be counted
more than once.
3. Find branches and their average balances where the average balance is more than
$1200.
select bname, avg (balance)
from account
group by bname
having avg (balance) > 1200
Predicates in the having clause are applied after the formation of groups.
4. Find the average balance of each customer who lives in Vancouver and has at least
three accounts:
select depositor.cname, avg (balance)
from depositor, account, customer
where depositor.cname = customer.cname and
Database Management System 82
account.account# = depositor.account#
and ccity=``Vancouver''
group by depositor.cname
having count (distinct account#) >= 3
If a where clause and a having clause appear in the same query, the where clause
predicate is applied first.
o Tuples satisfying where clause are placed into groups by the group by clause.
o The having clause is applied to each group.
o Groups satisfying the having clause are used by the select clause to generate the
result tuples.
o If no having clause is present, the tuples satisfying the where clause are treated as a
single group.
Null Values
1. With insertions, we saw how null values might be needed if values were unknown. Queries
involving nulls pose problems.
2. If a value is not known, it cannot be compared or be used as part of an aggregate function.
3. All comparisons involving null are false by definition. However, we can use the keyword null
to test for null values:
select distinct loan#
from loan
where amount is null
4. All aggregate functions except count ignore tuples with null values on the argument
attributes.
Nested Subqueries
• Set Membership
• Set Comparison
• Test for Empty Relations
• Test for the Absence of Duplicate Tuples
We use the in and not in operations for set membership.
select distinct cname
from borrower
where cname in
(select cname
Module 4 : Structured Query Language (SQL) 83
from account
where bname=``SFU'')
Note that we can write the same query several ways in SQL.
We can also test for more than one attribute:
select distinct cname
from borrower, loan
where borrower.loan# = loan.loan#
and bname=``SFU''
and (bname, cname) in
from account, depositor where depositor.account# = account.account#)
This finds all customers who have a loan and an account at the SFU branch in yet another
way.
Finding all customers who have a loan but not an account, we can use the not in operation.
Set Comparison
1. To compare set elements in terms of inequalities, we can write
select distinct T.bname
from branch T,branch S
where T.assets > S.assets
and S.bcity=``Burnaby''
or we can write
select bname
from branch
where assets > some
(select assets
from branch
where bcity=``Burnaby'')
to find branches whose assets are greater than some branch in Burnaby.
2. We can use any of the equality or inequality operators with some. If we change > some to >
all, we find branches whose assets are greater than all branches in Burnaby.
3. Example. Find branches with the highest average balance. We cannot compose aggregate
functions in SQL, e.g. we cannot do max (avg ...)). Instead, we find the branches for which
average balance is greater than or equal to all average balances:
select bname
Database Management System 84
from account
group by bname
having avg (balance) >= all
(select avg (balance)
from account
group by bname)
Test for Empty Relations
1. The exists construct returns true if the argument subquery is nonempty.
2. Find all customers who have a loan and an account at the bank.
select cname
from borrower
where exists (select *
from depositor
where depositor.cname = borrower.cname)
Test for the Absence of Duplicate Tuples
1. The unique construct returns true if the argument subquery contains no duplicate tuples.
2. Find all customers who have only one account at the SFU branch.
aaaaa aa - select T.cname
from depositor as T
where unique (select R.cname
from account, depositor as R
where T.cname = R.cname and
R.account# = account.account# and
account.bname = ``SFU")
Derived Relations
1. SQL-92 allows a subquery expression to be used in the from clause.
2. If such an expression is used, the result relation must be given a name, and the attributes
can be renamed.
3. Find the average account balance of those branches where the average account balance is
greater than $1,000.
select bname, avg-balance
from (select bname, avg(balance)
from account
Module 4 : Structured Query Language (SQL) 85
group by bname)
as result(bname, avg-balance)
where avg-balance > 1000
Views
1. A view in SQL is defined using the create view command:
Insertion
1. To insert data into a relation, we either specify a tuple, or write a query whose result is the
set of tuples to be inserted. Attribute values for inserted tuples must be members of the
attribute's domain.
2. Some examples:
1. To insert a tuple for Smith who has $1200 in account A-9372 at the SFU branch.
insert into account
values (``SFU'', ``A-9372'', 1200)
2. To provide each loan that the customer has in the SFU branch with a $200 savings
account.
insert into account
select bname, loan#, 200
from loan
where bname=``SFU''
Here, we use a select to specify a set of tuples.
It is important that we evaluate the select statement fully before carrying out any
insertion. If some insertions were carried out even as the select statement were
being evaluated, the insertion
insert into account
select *
from account
might insert an infinite number of tuples. Evaluating the select statement completely
before performing insertions avoids such problems.
3. It is possible for inserted tuples to be given values on only some attributes of the
schema. The remaining attributes are assigned a null value denoted by null.
We can prohibit the insertion of null values using the SQL DDL
Updates
1. Updating allows us to change some values in a tuple without necessarily changing all.
2. Some examples:
1. To increase all balances by 5 percent.
update account
set balance=balance * 1.05
This statement is applied to every tuple in account.
Database Management System 88
This insertion is represented by an insertion into the actual relation loan, from which the
view is constructed. However, we have no value for amount.
This insertion results in (``SFU'', ``L-307'', null) being inserted into the loan relation.
As we saw, when a view is defined in terms of several relations, serious problems can result.
As a result, many SQL-based systems impose the constraint that a modification is permitted
through a view only if the view in question is defined in terms of one relation in the
database.
Joined Relations
• Examples
• Join types and conditions
Examples
1. Two given relations: loan and borrower.
Figure 4.5: Result of loan natural full outer join borrower using (loan#).
5. Ex. Find all customers who have either an account or a loan (but not both) at the bank.
select cname
from (natural full outer join borrower)
where account# is null or
loan# is null
Data-Definition Language
The SQL DDL (Data Definition Language) allows specification of not only a set of relations, but also
the following information for each relation:
Module 4 : Structured Query Language (SQL) 91
where r is the relation name, A1is the name of an attribute, and Di is the
domain of that attribute. The allowed integrity-constraints include
primary key (Aj1,………Ajm)
and
check(P)
2. Example.
create table branch (
bname char(15) not null
bcity char(30)
assets integer
primary key (bname)
check (assets >= 0))
3.The values of primary key must be not null and unique. SQL-92 consider not null in primary
key specification is redundant but SQL-89 requires to define it explicitly.
4.Check creates type checking functionality which could be quite useful. E.g.,
create table student (
name char(15) not null
student-id char(10) not null
degree-level char(15) not null
check (degree-level in (``Bachelors'', ``Masters'', ``Doctorate'')))
6. Some checking (such as foreign-key constraints) could be costly, e.g.,
check (bname in (select bname from branch))
7. A newly loaded table is empty. The insert command can be used to load it, or use special
bulk loader untilities.
8. To remove a relation from the database, we can use the drop table command:
drop table r
This is not the same as
delete r
which retains the relation, but deletes all tuples in it.
9. The alter table command can be used to add or drop attributes to an existing relation r:
alter table r add A D
where A is the attribute and D is the domain to be added.
alter table r drop A
Module 4 : Structured Query Language (SQL) 93
Embedded SQL
1. SQL provides a powerful declarative query language. However, access to a database from a
general-purpose programming language is required because,
o SQL is not as powerful as a general-purpose programming language. There are
queries that cannot be expressed in SQL, but can be programmed in C, Fortran,
Pascal, Cobol, etc.
o Nondeclarative actions -- such as printing a report, interacting with a user, or
sending the result to a GUI -- cannot be done from within SQL.
2. The SQL standard defines embedding of SQL as embedded SQL and the language in which
SQL queries are embedded is referred as host language.
3. The result of the query is made available to the program one tuple (record) at a time.
4. To identify embedded SQL requests to the preprocessor, we use EXEC SQL statement:
EXEC SQL embedded SQL statement END-EXEC
Note: A semi-colon is used instead of END-EXEC when SQL is embedded in C or Pascal.
5. Embedded SQL statements: declare cursor, open, and fetch statements.
EXEC SQL
declare c cursor for
select cname, ccity
from deposit, customer
where deposit.cname = customer.cname
and deposit.balance > :amount
END-EXEC
where amount is a host-language variable.
6. EXEC SQL open c END-EXEC
This statement causes the DB system to execute the query and to save the results within a
temporary relation.
A series of fetch statement are executed to make tuples of the results available to the
program.
EXEC SQL fetch c into :cn, :cc END-EXEC
Database Management System 94
The program can then manipulate the variable cn and cc using the features of the host
programming language.
A single fetch request returns only one tuple. We need to use a while loop (or equivalent) to
process each tuple of the result until no further tuples (when a variable in the SQLCA is set).
We need to use close statement to tell the DB system to delete the temporary relation that
held the result of the query.
EXEC SQL close c END-EXEC
6. Embedded SQL can execute any valid update, insert, or delete statements.
7. Dynamic SQL component allows programs to construct and submit SQL queries ar run time
(see p. 147 of the textbook for details).
8. SQL-92 also contains a module language, which allows procedures to be defined in SQL (see
pp. 147-148 of the textbook for details).
Other SQL Features
1. 4GL: Most commercial database products include a special language to assist application
programmers in creating templates on the screen for a user interface, and in formatting
data for report generating.
No single accepted standard currently exists for 4GL.
2. SQL-92 standard defined SQL sessions and SQL environments.
o SQL sessions: client/server abstraction (connect, disconnect, commit, rollback).
o SQL environments: provide user-id and schema for each user.
4.8 References
5. Microsoft SQL Server 2000 Bible, Wiley.
6. BALTER, MS SQL SERVER 2005 EXPRESS IN 24 Hours, Pearson
Education
Case Study:
1) Write the following queries in SQL, using the university schema. (We suggest you actually run these
queries on a database, using the sample data that we provide on the Web site of the book, db-book.com.
Instructions for setting up a database, and loading sample data, are provided on the above Web site.)
a. Find the titles of courses in the Comp. Sci. department that have 3 credits.
b. Find the IDs of all students who were taught by an instructor named Einstein; make sure there are no
duplicates in the result.
c. Find the highest salary of any instructor.
d. Find all instructors earning the highest salary (there may be more than one with the same salary).
e. Find the enrollment of each section that was offered in Autumn 2009.
f. Find the maximum enrollment, across all sections, in Autumn 2009.
Module 4 : Structured Query Language (SQL) 95
g. Find the sections that had the maximum enrollment in Autumn 2009.
a. Find the titles of courses in the Comp. Sci. department that have 3
credits.
select title
from course
where dept name = ’Comp. Sci.’
and credits = 3
b. Find the IDs of all students who were taught by an instructor named Einstein; make sure there are no
duplicates in the result. This query can be answered in several different ways. One way is as follows.
select distinct student.ID
from (student join takes using(ID))
join (instructor join teaches using(ID))
using(course id, sec id, semester, year)
where instructor.name = ’Einstein’
As an alternative to th join .. using syntax above the query can be written by enumerating relations in
the fromclause, and adding the corresponding join predicates on ID, course id, section id, semester, and year
to the where clause. Note that using natural join in place of join .. using would result inequating student
ID with instructor ID, which is incorrect.
d. Find all instructors earning the highest salary (there may be more
than one with the same salary).
select ID, name
from instructor
where salary = (select max(salary) from instructor)
Note that if a section does not have any students taking it, it would not appear in the result. One way of
ensuring such a section appears with a count of 0 is to replace natural join by the natural left outer join
operation, covered later in Chapter 4. Another way is to use a subquery in the select clause, as follows.
g. Find the sections that had the maximum enrollment in Autumn 2009.
The following answer uses a with clause to create a temporary view,
simplifying the query.
with sec enrollment as (
select course id, sec id, count(ID) as enrollment
from section natural join takes
where semester = ’Autumn’
and year = 2009
group by course id, sec id)
select course id, sec id
from sec enrollment
where enrollment = (select max(enrollment) from sec enrollment)
It is also possible to write the query without the with clause, but the subquery to find enrollment would
get repeated twice in the query.
A) Condition.
B) Entity.
C) Attribute.
D) Set of tuple .
4) Sigma operator in SQL represents.
A) Select row.
B) Select column.
C) Select set of tuple.
D) All of these.
5) Roll back command is……
A) DDL
B) DML.
C) DCL.
D) None of these.
6) Command is use to erase the all the data of table.
A) DELET.
B) TRUNCATE.
C) UPDATE
D) WHERE
7) From clause in SQL refer to
A) ROW.
B) COLUMN.
C) TABLE.
D) DATABASE.
8) Following function is not aggregate function.
A) Min.
B) Max.
C) Count.
D) All of these.
9)’*’ in SQL is use for selection of
A) All rows.
B) All columns.
C) All table.
Database Management System 98
D) All of these.
10) Aggregate function can be use only in.
A) Select clause.
B) WHERE cause.
C) FROM clause.
D) All of these.
4.10 Subjective question:
1) List and explain the name of all the DDL command.
2) List and explain all the DML command.
3) Give the name of all aggregate function and explain.
4) Explain the group by having clause with example.
5) Give the short note with example.
A) UNION B) INTERSECTION
6) List the set of all operators in SQL.
7) What is the DCL command explains with example.
8) Create the any five table of library system with 10 entries in each table.
9) Explain COMMIT and ROLLBACK command in SQL.
10) What is SQL? Explain the following structures of SQL queries with
appropriate Example
i) Select clause iv) Aggregate function
ii) Where clause v) Assignment
iii) From clause.
11) For the given employee database give an expression in SQL for the following.
Employee (empname, street, city)
Works (empname, company-name, salary)
Company (company-name, City)
Manages (empname, manager-name)
(i) Create table,instert values for all the table given
(ii) Modify the database so that ‘jones’ now lives in ‘newtown’
(iii) Give all employees of first bank corporation a 10 percent raise.
12 ) For the given database with SQL query:
Employee (eid, employee_name, street, city)
Works (eid, cid,salary)
Module 4 : Structured Query Language (SQL) 99