0% found this document useful (0 votes)
62 views

Module 4

This document discusses structured query language (SQL) and its use in relational database management systems. It covers the objectives of learning SQL, the syllabus for SQL topics including data definition, retrieval, manipulation, and control. Key definitions are provided for SQL, relational databases, and the components of SQL including data definition language, data manipulation language, and data control language. The document also provides background on SQL and its basic structure using select, from, and where clauses.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Module 4

This document discusses structured query language (SQL) and its use in relational database management systems. It covers the objectives of learning SQL, the syllabus for SQL topics including data definition, retrieval, manipulation, and control. Key definitions are provided for SQL, relational databases, and the components of SQL including data definition language, data manipulation language, and data control language. The document also provides background on SQL and its basic structure using select, from, and where clauses.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Database Management System 72

Module-04

STRUCTURED QUERY LANGUAGE (SQL)


4.1 Motivation
The knowledge SQL is essential for software developer hence SQL is an important subject in
computer programming around the world.

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

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 Domain Types in SQL
o Schema definition in SQL
• Embedded SQL

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

4.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

4.7 Module theory


SQL, or Structured Query Language, as it was originally called, is the standard language used in
commercial database applications today to communicate with relational databases. SQL is used by
database developers, database administrators, and sophisticated end users.

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

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.
select distinct bname
from account
By default, duplicates are not removed. We can state it explicitly using all.
select all bname
from account
Module 4 : Structured Query Language (SQL) 77

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

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#
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
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) .

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
Database Management System 80

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.
Module 4 : Structured Query Language (SQL) 81

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:

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.
Database Management System 86

2. A deletion in SQL is of the form


delete from r
where P
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!
Module 4 : Structured Query Language (SQL) 87

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

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
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'')
Module 4 : Structured Query Language (SQL) 89

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:
loan natural inner join borrower
Database Management System 90

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:
Module 4 : Structured Query Language (SQL) 91

• 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
• 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)
Database Management System 92

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

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
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.

c. Find the highest salary of any instructor.


select max(salary)
from instructor

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)

e. Find the enrollment of each section thatwas offered inAutumn 2009.


One way of writing the query is as follows.
select course id, sec id, count(ID)
from section natural join takes
where semester = ’Autumn’
and year = 2009
group by course id, sec id

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.

select course id, sec id,


(select count(ID)
from takes
where takes.year = section.year
and takes.semester = section.semester
and takes.course id = section.course id
and takes.section id = section.section id)
from section
Database Management System 96

where semester = ’Autumn’


and year = 2009
Note that if the result of the subquery is empty, the aggregate function count returns a value of 0.
f. Find the maximum enrollment, across all sections, in Autumn 2009.
One way of writing this query is as follows:
select max(enrollment)
from (select count(ID) as enrollment
from section natural join takes
where semester = ’Autumn’
and year = 2009
group by course id, sec id)
As an alternative to using a nested subquery in the from clause, it is possible to use a with clause, as
illustrated in the answer to the next part of this question.
A subtle issue in the above query is that if no section had any enrollment, the answer would be empty,
not 0.We can use the alternative using a subquery, from the previous part of this question, to ensure the
count is 0 in this case.

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.

4.9 Objective question


1) SQL stand for.
A) Syntax query language
B) Structure Query Language.
C) Structure quantum language.
D) None of these.
2) Create is………command.
A) DML.
B) DCL.
C) DDL
D) All of them.
3) Where clause in SQL is use to define?
Module 4 : Structured Query Language (SQL) 97

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

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.
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

You might also like