Sie sind auf Seite 1von 31

Database Management System 68

Module -03
STRUCTURED QUERY LANGUAGE (SQL)
3.1 Motivation
The knowledge SQL is essential for software developer hence SQL is an important subject
in computer programming around the world.
3.2 Objective
 To learn Structured Query Language of DBMS
 To learn how data can be handled with the help of tools (e.g SQL Server or
Oracle).
 To learn how to store and retrieve data.
3.3 Syllabus
Prerequisites: Syllabus: Duration: Self study:
Relational Background 12 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
o Tuple Variables DCL 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
o Test for the Absence of Duplicate
Tuples
• Derived Relations
o Views
• Modification of the Database
o Deletion
o Insertion
o Updates
o Update of a view
• Joined Relations
o Examples
o Join types and conditions
• Data-Definition Language
o Views in SQL
o Integrity Constraints
Database Management System 69
3.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.
3.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.
3.6 Key definition
SQL: SQL is a tool for organizing, managing, and retrieving data stored by a computer
database. SQL is a computer language that we use to interact with the database. In fact,
SQL works with one specific type of database, called a relational data base.
Relational Database: A relational database is a database that can be perceived as a set of
tables and manipulated in accordance with the relational model of data. It contains a set of
objects used to store, manage, and access data. Examples of such objects are tables, views,
indexes, functions, triggers, and packages
3.7 Module theory
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.
Module 3 : Structured Query Language (SQL) 70
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.

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.
2. Typical query has the form
Select A1,A2……..An
from R1,R2……………Rn
where P
where each Ai represents an attribute, each Ri a relation, and P is a predicate.
3. This is equivalent to the relational algebra expression
a1,a2……..an(p(R1*R2*…………*Rm))
o If the where clause is omitted, the predicate P is true.
o The list of attributes can be replaced with a * to select all.
o SQL forms the Cartesian product of the relations named, performs a selection
using the predicate, then projects the result onto the attributes named.
o The result of an SQL query is a relation.
o SQL may internally convert into more efficient expressions.
 The select Clause
 The where Clause
 The from Clause
 The Rename Operation
 Tuple Variables
 String Operations
 Ordering the Display of Tuples
 Duplicate Tuples
The select Clause
1. An example: Find the names of all branches in the account relation.
select bname
from account
2. distinct vs. all: elimination or not elimination of duplicates.
Find the names of all branches in the account relation.
Database Management System 71
select distinct bname
from account
By default, duplicates are not removed. We can state it explicitly using all.
select all bname
from account
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
from borrower, loan
where borrower.loan# = loan.loan#
and bname= ``SFU"
Tuple Variables
1. Tuple variables can be used in SQL, and are defined in the from clause:
select distinct cname, T.loan#
Module 3 : Structured Query Language (SQL) 72
from borrower as S, loan as T
where S.loan# = T.loan#
2. These variables can then be used throughout the expression. Think of it as being
something like the rename operator.
Finds the names of all branches that have assets greater than at least one branch
located in Burnaby.
select distinct T.bname
from branch S, branch T
where S.bcity=``Burnaby'' and T.assets > S.assets
String Operations
1. The most commonly used operation on strings is pattern matching using the
operator like.
2. String matching operators % (any substring) and _ (underscore, matching any
character).
E.g., ``___%'' matches any string with at least 3 characters.
3. Patterns are case sensitive, e.g., ``Jim" does not match ``jim".
4. Use the keyword escape to define the escape character.
E.g., like ``ab%tely % '' escape `` '' matches all the strings beginning with ``ab''
followed by a sequence of characters and then ``tely'' and then ``% ''.Backslash
overrides the special meaning of these symbols.
5. We can use not like for string mismatching.
Example. Find all customers whose street includes the substring ``Main''.
select cname
from customer
where street like ``%Main%''
6. SQL also permits a variety of functions on character strings, such as concatenating

(using `` ''), extracting substrings, finding the length of strings, converting between
upper case and lower case, and so on.
Ordering the Display of Tuples
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.
Database Management System 73
 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) .


o for each copy of tuple t1 in R1 , there is a copy of tuple a(t1) in πa(R1) .
o if there are c1 copies of tuple t1 in R1, and C2 copies of tuple t2 in R2, there is C1×
C2 copies of tuple t1,t2 in R1×R2.
 An SQL query of the form
select A1,A2……….An
from R1,R2…………Rn
where P
is equivalent to the algebra expression
A1,A2……..An(σp(R1×R2×………×Rm))
using the multiset versions of the relational operators σ ,  and .
Set Operations
1. SQL has the set operations union, intersect and except.
2. Find all customers having an account.
select distinct cname
from depositor
3. union: Find all customers having a loan, an account, or both. branch.
(select cname
from depositor)
union
(select cname
from borrower)
4. intersect: Find customers having a loan and an account.
(select distinct cname
from depositor)
intersect
(select distinct cname
from borrower)
5. except: Find customers having an account, but not a loan.
(select distinct cname
from depositor)
except
(select cname
from borrower)
6. Some additional details:
o union eliminates duplicates, being a set operation. If we want to retain
duplicates, we may use union all, similarly for intersect and except.
o Not all implementations of SQL have these set operations.
o except in SQL-92 is called minus in SQL-86.
o It is possible to express these queries using other operations.
Aggregate Functions
Module 3 : Structured Query Language (SQL) 74
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
account.account# = depositor.account#
and ccity=``Vancouver''
group by depositor.cname
having count (distinct account#) >= 3
2 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.
Database Management System 75
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
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
(select bname, cname
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
Module 3 : Structured Query Language (SQL) 76
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
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.
Database Management System 77
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
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:

2. create view v as query expression

where query expression is any legal query expression.


The view created is given the name v.
To create a view all-customer of all branches and their customers:
create view all-customer as
(select bname, cname
from depositor, account
where depositor.account# = account.account#)
union
(select bname, cname
from borrower, loan
where borrower.loan# = loan.loan#)
3. Having defined a view, we can now use it to refer to the virtual relation it creates.
View names can appear anywhere a relation name can.
4. We can now find all customers of the SFU branch by writing
select cname
from all-customer
where bname=``SFU'
Modification of the Database
Up until now, we have looked at extracting information from the database.
 Deletion
 Insertion
 Updates
 Update of a view
Deletion
1. Deletion is expressed in much the same way as a query. Instead of displaying, the
selected tuples are removed from the database. We can only delete whole tuples.
2. A deletion in SQL is of the form
delete from r
where P
Module 3 : Structured Query Language (SQL) 78
Tuples in r for which P is true are deleted. If the where clause is omitted, all tuples
are deleted.
3. The request delete from loan deletes all tuples from the relation loan.
4. Some more examples:
1. Delete all of Smith's account records.
delete from depositor
where cname=``Smith''
2.Delete all loans with loan numbers between 1300 and 1500.
delete from loan
where loan# between 1300 and 1500
3.Delete all accounts at branches located in Surrey.
delete from account
where bname in
(select bname
from branch
where bcity=``Surrey'')
5. We may only delete tuples from one relation at a time, but we may reference any
number of relations in a select-from-where clause embedded in the where clause of
a delete.
6. However, if the delete request contains an embedded select that references the
relation from which tuples are to be deleted, ambiguities may result.
For example, to delete the records of all accounts with balances below the average,
we might write
delete from account
where balance <
(select avg(balance) from account)
You can see that as we delete tuples from account, the average balance changes!
Solution: The delete statement first test each tuple in the relation account to check
whether the account has a balance less than the average of the bank. Then all tuples
that fail the test are deleted. Perform all the tests (and mark the tuples to be deleted)
before any deletion then delete them en masse after the evaluations!
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
Database Management System 79
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.
To ke two different rates of interest payment, depending on balance
amount:
update account
2. set balance=balance * 1.06
where balance > 10,000
update account
set balance=balance * 1.05
where balance 10,000
Note: in this example the order of the two operations is important. (Why?)
3. In general, where clause of update statement may contain any construct legal in a
where clause of a select statement (including nesting).
4. A nested select within an update may reference the relation that is being updated.
As before, all tuples in the relation are first tested to see whether they should be
updated, and the updates are carried out afterwards.
For example, to pay 5% interest on account whose balance is greater than average,
we have
update account
set balance=balance * 1.05
where balance >
select avg (balance)
from account
Module 3 : Structured Query Language (SQL) 80
Update of a view
1. The view update anomaly previously mentioned in Chapter 3 exists also in SQL.
2. An example will illustrate: consider a clerk who needs to see all information in the
loan relation except amount.
Let the view branch-loan be given to the clerk:
create view branch-loan as
select bname, loan#
from loan
Since SQL allows a view name to appear anywhere a relation name may appear, the
clerk can write:
insert into branch-loan
values (``SFU'', ``L-307'')

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.1: The loan and borrower relations.


2. inner join:
loan inner join borrower on loan.loan# = borrower.loan#
Notice that the loan# will appear twice in the inner joined relation.

Figure 4.2: Result of loan inner join borrower.


3. left outer join:
loan left outer join borrower on loan.loan# = borrower.loan#

Figure 4.3: Result of loan left outer join borrower.


4. natural inner join:
Database Management System 81
loan natural inner join borrower

Figure 4.4: Result of loan natural inner join borrower


Join types and conditions
1. Each variant of the join operations in SQL-92 consists of a join type and a join
condition.
2. Join types: inner join, left outer join, right outer join, full outer join.
The keyword inner and outer are optional since the rest of the join type enables us to
deduce whether the join is an inner join or an outer join.
SQL-92 also provides two other join types:
1. cross join: an inner join without a join condition.
2. union join: a full outer join on the ``false'' condition, i.e., where the inner join
is empty.
3. Join conditions: natural, on predicate, using (A1,A2,……….An)
The use of join condition is mandatory for outer joins, but is optional for inner joins
(if it is omitted, a Cartesian product results).
4. Ex. A natural full outer join:
loan natural full outer join borrower
using (loan#)

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:
 The schema for each relation.
 The domain of values associated with each attribute.
 Integrity constraints.
 The set of indices for each relation.
 Security and authorization information.
 Physical storage structure on disk.
 Domain Types in SQL
Module 3 : Structured Query Language (SQL) 82
 Schema definition in SQL
Domain Types in SQL
1. The SQL-92 standard supports a variety of built-in domain types:
o char(n) (or character(n)): fixed-length character string, with user-specified
length.
o varchar(n) (or character varying): variable-length character string, with user-
specified maximum length.
o int or integer: an integer (length is machine-dependent).
o smallint: a small integer (length is machine-dependent).
o numeric(p, d): a fixed-point number with user-specified precision, consists of
p digits (plus a sign) and d of p digits are to the right of the decimal point. E.g.,
numeric(3, 1) allows 44.5 to be stored exactly but not 444.5.
o real or double precision: floating-point or double-precision floating-point
numbers, with machine-dependent precision.
o float(n): floating-point, with user-specified precision of at least n digits.
o date: a calendar date, containing four digit year, month, and day of the
month.
o time: the time of the day in hours, minutes, and seconds.
2. SQL-92 allows arithmetic and comparison operations on various numeric domains,
including, interval and cast (type coercion) such as transforming between smallint and
int. It considers strings with different length are compatible types as well.
3. SQL-92 allows create domain statement, e.g.,
create domain person-name char(20)
Schema definition in SQL
1. An SQL relation is defined by:
create table r ( A1,D1,A2,D2,………..An,Dn)
integrity-cnstraint1 , ..., integrity- constraint)
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
Database Management System 83
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
where A is the attribute to be dropped.

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
Module 3 : Structured Query Language (SQL) 84
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
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.
SUBQUERY:
A Subquery or Inner query or Nested query is a query within another SQL query and
embedded within the WHERE clause.
A subquery is used to return data that will be used in the main query as a condition to
further restrict the data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements
along with the operators like =, <, >, >=, <=, IN, BETWEEN etc.
There are a few rules that subqueries must follow:
 Subqueries must be enclosed within parentheses.
 A subquery can have only one column in the SELECT clause, unless multiple
columns are in the main query for the subquery to compare its selected columns.
 An ORDER BY cannot be used in a subquery, although the main query can use an
ORDER BY. The GROUP BY can be used to perform the same function as the
ORDER BY in a subquery.
Database Management System 85
 Subqueries that return more than one row can only be used with multiple value
operators, such as the IN operator.
 The SELECT list cannot include any references to values that evaluate to a BLOB,
ARRAY, CLOB, or NCLOB.
 A subquery cannot be immediately enclosed in a set function.
 The BETWEEN operator cannot be used with a subquery; however, the BETWEEN
operator can be used within the subquery.
Subqueries with the SELECT Statement:
Subqueries are most frequently used with the SELECT statement. The basic syntax is as
follows:
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 ,[ table2 ]
[WHERE])
INSERT With Subquery: Subqueries also can be used with INSERT statements. The INSERT
statement uses the data returned from the subquery to insert into another table. The
selected data in the subquery can be modified with any of the character, date or number
functions.
The basic syntax is as follows:
INSERT INTO table_name [ (column1 [, column2 ]) ]
SELECT [ *|column1 [, column2 ]
FROM table1 [, table2 ]
[ WHERE VALUE OPERATOR ]
Example: Consider a table CUSTOMERS_BKP with similar structure as CUSTOMERS table.
Now to copy complete CUSTOMERS table into CUSTOMERS_BKP, following is the syntax:
SQL> INSERT INTO CUSTOMERS_BKP
SELECT * FROM CUSTOMERS
WHERE ID IN (SELECT ID
FROM CUSTOMERS) ;
Subqueries with the UPDATE Statement:
The subquery can be used in conjunction with the UPDATE statement. Either single or
multiple columns in a table can be updated when using a subquery with the UPDATE
statement.
The basic syntax is as follows:
UPDATE table
SET column_name = new_value
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
Example:
Module 3 : Structured Query Language (SQL) 86
Assuming, we have CUSTOMERS_BKP table available which is backup of CUSTOMERS
table.
Following example updates SALARY by 0.25 times in CUSTOMERS table for all the
customers whose AGE is greater than or equal to 27:
SQL> UPDATE CUSTOMERS
SET SALARY = SALARY * 0.25
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
WHERE AGE >= 27 );
Subqueries with the DELETE Statement:
The subquery can be used in conjunction with the DELETE statement like with any other
statements mentioned above.
The basic syntax is as follows:
DELETE FROM TABLE_NAME
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
Example:
Assuming, we have CUSTOMERS_BKP table available which is backup of CUSTOMERS
table.
Following example deletes records from CUSTOMERS table for all the customers whose
AGE is greater than or equal to 27:
SQL> DELETE FROM CUSTOMERS
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
WHERE AGE > 27 );
There may be a situation when you have multiple duplicate records in a table. While
fetching such records, it makes more sense to fetch only unique records instead of fetching
duplicate records.
The SQL DISTINCT keyword, which we already have discussed, is used in conjunction
with SELECT statement to eliminate all the duplicate records and fetching only unique
records.
Syntax:
The basic syntax of DISTINCT keyword to eliminate duplicate records is as follows:
SELECT DISTINCT column1, column2,.....columnN
FROM table_name
WHERE [condition]
Example:
Consider the CUSTOMERS table having the following records:
Database Management System 87

First, let us see how the following SELECT query returns duplicate salary records:
SQL> SELECT SALARY FROM CUSTOMERS
ORDER BY SALARY;
This would produce the following result where salary 2000 is coming twice which is a
duplicate record from the original table.

Now, let us use DISTINCT keyword with the above SELECT query and see the result:
SQL> SELECT DISTINCT SALARY FROM CUSTOMERS
ORDER BY SALARY;
This would produce the following result where we do not have any duplicate entry:

What Is Data Integrity?


To preserve the consistency and correctness of its stored data, a relational DBMS typically
imposes one or more data integrity constraints. These constraints restrict the data values
that can be inserted into the database or created by a database update. Data integrity is
enforced in both hierarchical and relational database models. The following three integrity
constraints are used in a relational database structure to achieve data integrity:
 Entity Integrity: This is concerned with the concept of primary keys. The rule states
that every table must have its own primary key and that each has to be unique and
not null.
Module 3 : Structured Query Language (SQL) 88
 Referential Integrity: This is the concept of foreign keys. The rule states that the
foreign key value can be in two states. The first state is that the foreign key value
would refer to a primary key value of another table, or it can be null. Being null
could simply mean that there are no relationships, or that the relationship is
unknown.

When you enforce referential integrity, SQL Server prevents users from doing the
following:

 Adding or changing rows to a related table if there is no associated row in the


primary table.
 Changing values in a primary table that causes orphaned rows in a related table.

 Deleting rows from a primary table if there are matching related rows.

For example, with the Sales.SalesOrderDetail and Production.Product tables referential


integrity is based on the relationship between the foreign key (ProductID) in the
Sales.SalesOrderDetail table and the primary key (ProductID) in the Production.Product
table. This relationship makes sure that a sales order can never reference a product that
does not exist in the Production.Product table.

 Domain Integrity: This states that all columns in a relational database are in a
defined domain.
The concept of data integrity ensures that all data in a database can be traced and
connected to other data. This ensures that everything is recoverable and searchable.
Having a single, well-defined and well-controlled data integrity system increases
stability, performance, reusability and maintainability. If one of these features cannot
be implemented in the database, it must be implemented through the software.
Database Management System 89

• Other data relationships. The real-world situation modeled by a database will often have
additional constraints that govern the legal data values that may appear in the database.
For example, in the sample database, the sales vice president may want to insure that the
quota target for each office does not exceed the total of the quota targets for the salespeople
in that office. The DBMS can be asked to check modifications to the office and salesperson
quota targets to make sure that their values are constrained in this way.
• Business rules. Updates to a database may be constrained by business rules governing
the real-world transactions that are represented by the updates. For example, the company
using the sample database may have a business rule that forbids accepting an order for
which there is an inadequate product inventory. The DBMS can be asked to check each new
row added to the ORDERS table to make sure
that the value in its QTY column does not violate this business rule.
• Consistency. A transaction reaching its normal end, thereby committing its results,
preserves the consistency of the database. In other words, each successful transaction by
definition commits only legal results.

Essentially, consistency means that database systems have to enforce business rules defined
for their databases.

consistency means that any defined checks and constraints are satisfied after any
transaction:

 Columns only store values of a particular type (int columns store only ints, etc…)
 Primary keys and unique keys are unique
Module 3 : Structured Query Language (SQL) 90
 Check constraints are satisfied

 Foreign key constraints are satisfied

When SQL Server finds inconsistencies. It handles it in one of a few ways.

 If a foreign key is defined properly, a change to one row can cascade to other other
rows.
 If a value of a particular datatype is inserted into a column which is defined to hold
a different datatype, SQL Server may sometimes implicitly convert the value to the
target datatype.

 Most often, SQL Server gives up and throws an error, rolling back all effects of that
statement.

Required Data
The simplest data integrity constraint requires that a column contain a non-NULL value.
The ANSI/ISO standard and most commercial SQL products support this constraint by
allowing you to declare that a column is NOT NULL when the table containing the column
is first created. The NOT NULL constraint is specified as part of the CREATE TABLE when
a column is declared NOT NULL; the DBMS enforces the constraint by ensuring the
following:
• Every INSERT statement that adds a new row or rows to the table must specify a non-
NULL data value for the column. An attempt to insert a row containing a NULL value
(either explicitly or implicitly) results in an error.
• Every UPDATE statement that updates the column must assign it a non-NULL data
value. Again, an attempt to update the column to a NULL value results in an error.
One disadvantage of the NOT NULL constraint is that it must usually be specified when a
table is first Created. Typically, you cannot go back to a previously created table and
disallow NULL values for a column. Usually this disadvantage is not serious because it's
obvious when the table is first created which columns should allow NULLs and which
should not. The inability to add a NOT NULL constraint to an existing table is a result of
the way most DBMS brands implement NULL values internally. Usually a DBMS reserves
an extra byte in every stored row of data for each column that permits NULL values. The
extra byte serves as a "null indicator" for the column and is set to some specified value to
indicate a NULL value. When a column is defined as NOT NULL, the indicator byte is not
present, saving disk storage space. Dynamically adding and removing NOT NULL
constraints would thus require "on the fly" reconfiguration of the stored rows on the disk,
which is not practical in a large database.
Simple Validity Checking
The SQL1 standard provides limited support for restricting the legal values that can appear
in a column. When a table is created, each column in the table is assigned a data type, and
the DBMS ensures that only data of the specified type is introduced into the column. For
example, the EMPL_NUM column in the SALESREPS table is defined as an INTEGER, and
Database Management System 91
the DBMS will produce an error if an INSERT or UPDATE statement tries to store a
character string or a decimal number in the column.
However, the SQL1 standard and many commercial SQL products do not provide a way to
restrict a column to certain specific data values. The DBMS will happily insert a
SALESREPS row with an employee number of 12345, even though employee numbers in
the sample database have three digits by convention. A hire date of December 25 would
also be accepted, even though the company is closed on Christmas day.
Column Check Constraints (SQL2)
A SQL2 check constraint is a search condition, like the search condition in a WHERE clause,
that produces a true/false value. When a check constraint is specified for a column, the
DBMS automatically checks the value of that column each time a new row is inserted or a
row is updated to insure that the search condition is true. If not, the INSERT or UPDATE
statement fails. A column check constraint is specified as part of the column definition
within the CREATE TABLE statement
Consider this excerpt from a CREATE TABLE statement, modified from the definition of
the demo database to include three check constraints:
CREATE TABLE SALESREPS
(EMPL_NUM INTEGER NOT NULL
CHECK (EMPL_NUM BETWEEN 101 AND 199),
AGE INTEGER
CHECK (AGE >= 21),
.
.
.
QUOTA MONEY
CHECK (MONEY >= 0.0)
.
.
.
The first constraint (on the EMPL_NUM column) requires that valid employee numbers be
three-digit numbers between 101 and 199. The second constraint (on the AGE column)
similarly prevents hiring of minors. The third constraint (on the QUOTA column) prevents
a salesperson from having a quota target less than $0.00. All three of these column check
constraints are very simple examples of the capability
specified by the SQL2 standard. In general, the parentheses following the keyword CHECK
can contain any valid search condition that makes sense in the context of a column
definition. With this flexibility, a check constraint can compare values from two different
columns of the table, or even compare a proposed data value against other values from the
database.
Entity Integrity
A table's primary key must have a unique value for each row of the table, or the database
will lose its integrity as a model of the outside world. For example, if two rows of the
SALESREPS table both had value 106 in their EMPL_NUM column, it would be impossible
to tell which row really represented the real-world entity associated with that key value—
Module 3 : Structured Query Language (SQL) 92
Bill Adams, who is employee number 106. For this reason the requirement that primary
keys have unique values is called the entity integrity constraint.
Other Uniqueness Constraints
It is sometimes appropriate to require a column that is not the primary key of a table to
contain a unique value in every row. For example, suppose you wanted to restrict the data
in the SALESREPS table so that no two salespeople could have exactly the same name in
the table. You could achieve this goal by imposing a uniqueness constraint on the NAME
column. The DBMS enforces a uniqueness constraint in the same way that it enforces the
primary key constraint. Any attempt to insert or update a row in the table
that violates the uniqueness constraint will fail.
Uniqueness and NULL Values
NULL values pose a problem when they occur in the primary key of a table or in a column
that is specified in a uniqueness constraint .Suppose you tried to insert a row with a
primary key that was NULL (or partially NULL, if the primary key is composed of more
than one column). Because of the NULL value, the DBMS cannot conclusively decide
whether the primary key does or does not duplicate one that is already in the table. The
answer must be "maybe," depending on the "real" value of the missing (NULL) data.
For this reason, the SQL standard requires that every column that is part of a primary key
must be declared NOT NULL. The same restriction applies for every column that is named
in a uniqueness constraint. Together, these restrictions ensure that columns that are
"supposed to" contain unique data values in each row of a table actually do contain unique
values.
Referential Integrity
As we have discussed primary keys, foreign keys, and the parent/child relationships that
they create between tables. Figure shows the SALESREPS and OFFICES tables and
illustrates once again how foreign keys and primary keys work. The OFFICE column is the
primary key for the OFFICES table, and it uniquely identifies each row. The REP_OFFICE
column, in the SALESREPS table, is a foreign key for the OFFICES table. It identifies the
office where each salesperson is assigned.
Database Management System 93

The REP_OFFICE and OFFICE columns create a parent/child relationship between the
OFFICES and SALESREPS rows. Each OFFICES (parent) row has zero or more SALESREPS
(child) rows with matching office numbers. Similarly, each SALESREPS (child) row has
exactly one OFFICES (parent) row with a matching office number. Suppose you tried to
insert a new row into the SALESREPS table that contained an invalid office number, as in
this example:
INSERT INTO SALESREPS (EMPL_NUM, NAME, REP_OFFICE, AGE, HIRE_DATE,
SALES)
VALUES (115, 'George Smith', 31, 37, '01-APR-90', 0.00)

On the surface, there's nothing wrong with this INSERT statement. In fact, many SQL
implementations will successfully add the row. The database will show that George Smith
works in office number 31, even though no office number 31 is listed in the OFFICES table.
The newly inserted row clearly "breaks" the parent/child relationship between the
OFFICES and SALESREPS tables. In fact, the office number in the INSERT statement is
probably an error—the user may have intended office number 11, 21, or 13.
It seems clear that every legal value in the REP_OFFICE column should be forced to match
some value that appears in the OFFICE column. This rule is known as a referential integrity
constraint. It ensures the integrity of the parent/child relationships created by foreign keys
and primary keys.
Triggers and Referential Integrity

You can use triggers to enforce referential integrity and cascading actions when you need a self-
maintained solution—you don't need to modify the data through special stored procedures. You
can use the same INSERT, UPDATE, and DELETE statements to modify the base tables as you
would normally use, and they will cause the triggers to fire and enforce referential integrity and
cascade your modifications.
Module 3 : Structured Query Language (SQL) 94
Triggers provide an alternative way to implement the referential integrity constraints
provided by foreign keys and primary keys. In fact, advocates of the trigger feature point
out that the trigger mechanism is more flexible than the strict referential integrity provided
by DB2 and the ANSI/ISO standard.
Creating Triggers
The syntax for creating a trigger is:
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;
 Where,
 CREATE [OR REPLACE] TRIGGER trigger_name: Creates or replaces an existing
trigger with the trigger_name.
 {BEFORE | AFTER | INSTEAD OF} : This specifies when the trigger would be
executed. The INSTEAD OF clause is used for creating trigger on a view.
 {INSERT [OR] | UPDATE [OR] | DELETE}: This specifies the DML operation.
 [OF col_name]: This specifies the column name that would be updated.
 [ON table_name]: This specifies the name of the table associated with the trigger.
 [REFERENCING OLD AS o NEW AS n]: This allows you to refer new and old values
for various DML statements, like INSERT, UPDATE, and DELETE.
 [FOR EACH ROW]: This specifies a row level trigger, i.e., the trigger would be
executed for each row being affected. Otherwise the trigger will execute just once
when the SQL statement is executed, which is called a table level trigger.
 WHEN (condition): This provides a condition for rows for which the trigger would
fire. This clause is valid only for row level triggers.

When users modify data in a table, SQL Server needs to check the data in the related table or tables
to verify that the modification doesn’t violate the referential integrity rules. SQL Server checks data
by adding steps to the execution plan. Therefore, performance decreases as the number of table
references increases. To enforce referential integrity rules, you can create a foreign key and
references constraint, as the statement in Listing 1 shows.
Trigger Advantages and Disadvantages
A complete discussion of triggers is beyond the scope of this book, but even these simple
examples shows the power of the trigger mechanism. The major advantage of triggers is
Database Management System 95
that business rules can be stored in the database and enforced consistently with each
update to the database.

Triggers can be written for the following purposes:

 Generating some derived column values automatically


 Enforcing referential integrity

 Event logging and storing information on table access

 Auditing

 Synchronous replication of tables

 Imposing security authorizations

 Preventing invalid transactions

This can dramatically reduce the complexity of application programs that access the
database. Triggers also have some disadvantages, including these:

• Database complexity. When the rules are moved into the database, setting up the
database becomes a more complex task. Users who could reasonably be expected to create
small, ad hoc applications with SQL will find that the programming logic of triggers makes
the task much more difficult.
• Hidden rules. With the rules hidden away inside the database, programs that appear to
perform straightforward database updates may, in fact, generate an enormous amount of
database activity. The programmer no longer has total control over what happens to the
database. Instead, a program-initiated database action may cause other, hidden actions.
• Hidden performance implications. With triggers stored inside the database, the
consequences of executing a SQL statement are no longer completely visible to the
programmer. In particular, an apparently simple SQL statement could, in concept, trigger a
process that involves a sequential scan of a very large database table, which would take a
long time to complete. These performance implications of any given SQL statement are
invisible to the programmer.

3.8 References
5. Microsoft SQL Server 2000 Bible, Wiley.
6. BALTER, MS SQL SERVER 2005 EXPRESS IN 24 Hours, Pearson
Education
3.9 Objective question
1) SQL stand for.
A) Syntax query language
B) Structure Query Language.
C) Structure quantum language.
D) None of these.
Module 3 : Structured Query Language (SQL) 96
2) Create is………command.
A) DML.
B) DCL.
C) DDL
D) All of them.
3) Where clause in SQL is use to define?
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.
D) All of these.
10) Aggregate function can be use only in.
A) Select clause.
B) WHERE cause.
C) FROM clause.
Database Management System 97
D) All of these.
11) The view of total database content is
(A) Conceptual view.
(B) Internal view.
(C) External view.
12) DML is provided for
(A) Description of logical structure of database.
(B) Addition of new structures in the database system.
(C) Manipulation & processing of database
13) Architecture of the database can be viewed as
(A) two levels.
(B) four levels.
(C) three levels.
3.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)
Company (cid,company_name,and city)
Manager (eid,manager_name)
i) Find the name, street and city of all employee who work for “ABC” and more than rs.200
ii) Find the name of all employee having “i” as the second letter in there name.
iii) Display the annual salary of all employees.
Module 3 : Structured Query Language (SQL) 98
13) How many cods rule must a DBMS satisfy to quality of DBMS?
14) For the following given database, write a sql query.
Person (driver_id#, name, address)
Car (license, model, year)
Accident (report_no, date, location)
Owns (driver_id,car,report_no,damage_amt)
i) Find the total number of people who owned cars that were involved in accident
in 1995.
ii) Find the number of accident in which the cars belonging to “Sunil K”.were
involved.
iii) Update damage amount for car with license no. “Mum2022”
In the accident with report number “AR2197” to Rs.5000.
15) Give the name of all aggregate function and explain.
16) Explain the group by having clause with example.
17) Give the short note with example.
A) UNION B) INTERSECTION
18) ) For the following given database, write SQL querues:
Person (driver_id#, name, address)
Car (licence, model, year)
Accident (report-no, date, location)
Owns (driver_id#, lisence)
Participated (drivercid, car, report_number, damage_amount)
(i) Find the total number of people who owned cars that were involved in accident 2004
(ii) Find the number of accidents in which the cars belonging to “HT” were involved.
(iii) Update the damage amount for car with licence number “Mum2011” in the accident with
report number “AR120” to Rs. 4000.