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

DBMS unit 4

Uploaded by

shyam bk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

DBMS unit 4

Uploaded by

shyam bk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Structure Query Language (SQL)

Background

SQL was developed in 1970‟s in an IBM laboratory “San Jose Research Laboratory” (now the Amaden
Research center). SQL is derived from the SEQUEL one of the database language popular during 1970‟s.
SQL established itself as the standard relational database language. Two standard organization (ANSI) and
International standards organization (ISO) currently promote SQL standards to industry. In 1986 ANSI &
ISO published an SQL standard called SQL-86. In 1987, IBM published its own corporate SQL standard,
the system application Architecture Database Interface (SAA-SQL). In 1989, ANSI published extended
standard for SQL called, SQL-89. The next version was SQL-92, and the recent version is SQL: 1999.

Basic Term and Terminology

Query: is a statement requesting the retrieval of information.

Query language: language through which user request information from database.

These languages are generally higher level language than programming language.

The two types of query language are:

(i) Procedural language

 User instructs the system to perform sequence of operation on the database to compete the desired
result.
 Example : relational algebra

ii) Non- procedural language

 User describes the desired information without giving a specific procedure for obtaining that desired
information.
 Examples: tuple relational calculus and domain relational calculus.

What is SQL?

 SQL is Structured Query Language, which is a computer language for storing, manipulating and
retrieving data stored in relational database.
 SQL is the standard language for Relation Database System. All relational database management
systems like MySQL, MS Access, and Oracle, Sybase, Informix, postgres and SQL Server use SQL
as standard database language

Why SQL?

 Allows users to access data in relational database management systems.


 Allows users to describe the data.
 Allows users to define the data in database and manipulate that data.
 Allows embedding within other languages using SQL modules, libraries & pre-compilers.
 Allows users to create and drop databases and tables.
 Allows users to create view, stored procedure, functions in a database.
 Allows users to set permissions on tables, procedures and views

Characteristics of SQL:

SQL usage by its very nature is extremely flexible. It uses a free from syntax that gives the user the ability to
structure SQL statements in a way best suited to him. Each SQL request is parsed by the RDMS before
execution, to check for proper syntax and to optimize the request. Unlike certain programming languages,
there is no need to start SQL statements in a particular column or be finished in a single line. The
same SQL request can be written in a variety of ways.

Advantages of SQL:

The various advantages of SQL are:

 SQL is a high level language that provides a greater degree of abstraction than procedural
languages.
 SQL enables the end-users and systems personnel to deal with a number of database
management systems where it is available. Increased acceptance and availability of SQL are also
in its favor.
 Applications written in SQL can be easily ported across systems. Such porting could be required
when the underlying DBMS needs to be upgraded or changed.
 SQL specifies what is required and not how it should be done.
 The language while being simple and easy to learn can handle situations.

Domain Types in SQL:

1. The SQL-92 standard supports a variety of built-in domain types:

 char(n) (or character(n)): fixed-length character string, with user-specified length.


 varchar(n) (or character varying): variable-length character string, with user-
specified maximum length.
 int or integer: an integer (length is machine-dependent).
 smallint: a small integer (length is machine-dependent).
 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.
 real or double precision: floating-point or double-precision floating-point numbers,
with machine-dependent precision.
 float(n): floating-point, with user-specified precision of at least n digits.
 date: a calendar date, containing four digit year, month, and day of the month.
 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)
SQL Commands:

The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT,
UPDATE, DELETE and DROP. These commands can be classified into groups based on their nature.

They are:

 DDL Commands
 DML Commands
 DCL Commands
 DRL/DQL Commands
 TCL Commands

Data Definition Language (DDL) :

 Data definition language is used to create, alter and delete database objects.
 The commands used are create, alter and drop.
 The principal data definition statements are :
 Create table, create view, create index
 Alter table
 Drop table, drop view, drop index

Data Definition Language (DDL) Commands :

Command Description
CREATE Creates a new table, a view of a table, or other object in
database

ALTER Modifies an existing database object, such as a table.

DROP Deletes an entire table, a view of a table or other object in the


database.

TRUNCATE Truncates the table values without delete table structure


Schema definition in SQL:

An SQL relation is defined by:

create table r (A1,D1,A2,D2,......An,Dn

(integrity-constraint1),

.....(integrity-constraint1))

where r is the relation name, Ai is 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)

CREATE

 The SQL CREATE TABLE statement is used to create a new table.


 Creating a basic table involves naming the table and defining its columns and each column's data type

Syntax: Basic syntax of CREATE TABLE statement is as follows:


CREATE TABLE table name (column1 datatype (size), column2 datatype (size), column3 datatype (size) …….
columnN datatype (size), PRIMARY KEY (one or more columns));

Example:

create table branch (

bname char(15) not null

bcity char(30) ( Data Constraints(Primary Key Concept))

assets integer

primary key (bname) (Check Integrity Constraints)

check(assets >= 0));


 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.
 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")))

 Some checking (such as foreign-key constraints) could be costly, e.g.,

check (bname in (select bname from branch))

Example:
create table customers
(
id number (10) not null,
name varchar (20) not null,
age number (5) not null,
address char (25),
salary decimal (8, 2),
primary key (id)
);

Example:
 CREATE TABLE dept
(
dept no NUMBER(2) PRIMARY KEY,
dname VARCHAR2(20) NOT NULL
); (column level Primary Key Constraint)

 CREATE TABLE student


(roll_no numbers (5) PRIMARY KEY,
name varchar(25) NOT NULL,
address varchar(25) NOT NULL,
ph_no varchar (15)); (Table level primary key constraint)

 CREATE TABLE student1


(roll_no number(5),
name varchar(25) NOT NULL,
address varchar(25) NOT NULL,
ph_no varchar(15),
PRIMARY KEY(roll_no));
 CREATE TABLE special_customer
(customer_code number(5) PRIMARY KEY,
customer_name varchar(25) NOT NULL,
customer_address varcher (30) NOT NULL,
license_no varchar (15) constraint uk_license_no_UNIQUE); (Unique Key Concept)

 CREATE TABLE employee


(emp_code number(5),
emp_name varchar(25) NOT NULL,
ph_no varchar(15),
married char(1) DEFAULT ‘M’ , (Default Key Concept)
PRIMARY KEY (emp_coder));

 CREATE TABLE book


(ISBN varchar(25) PRIMARY KEY,
title varchar(25) ,
pub_year varchar(4), (The foreign key/ references constraint)
unit_price number(4),
author_name varchar(25) references author,
publisher_name varchar(25) references publisher);

ALTER:
SQL ALTER TABLE command is used to add, delete or modify columns in an existing table

Syntax:
1. The basic syntax of ALTER TABLE to add a new column in an existing table is as follows:
ALTER TABLE table_name ADD column_name datatype;
EX: ALTER TABLE CUSTOMERS ADD phno number (12);

2. The basic syntax of ALTER TABLE to DROP COLUMN in an existing table is as follows:
ALTER TABLE table_name DROP COLUMN column_name;
EX: ALTER TABLE CUSTOMERS DROP column phno;

3. The basic syntax of ALTER TABLE to change the DATA TYPE of a column in a table is as follow
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
Ex:ALTER TABLE customer MODIFY COLUMN phno number(12);

4. The basic syntax of ALTER TABLE to add a NOT NULL constraint to a column in a table is as follows:
ALTER TABLE table_name MODIFY column_name datatype NOT NULL;
Ex:ALTER TABLE customers MODIFY phno number (12); NOT NULL;

5. The basic syntax of ALTER TABLE to ADD PRIMARY KEY constraint to a table is as follows:
ALTER TABLE table_name ADD PRIMARY KEY (column1, column2...);
Ex:ALTER TABLE customer ADD PRIMARY KEY (id,phno);
TRUNCATE:
SQL TRUNCATE TABLE command is used to delete complete data from an existing table.
Syntax:
The basic syntax of TRUNCATE TABLE is as follows:
TRUNCATE TABLE table name;
EX:TRUNCATE TABLE student;

SELECT * FROM student;


Empty set (0.00 sec).

DROP:
SQL DROP TABLE statement is used to remove a table definition and all data, indexes, triggers,
constraints, and permission specifications for that table.
Syntax:
Basic syntax of DROP TABLE statement is as follows:
DROP TABLE table_name;
EX: DROP TABLE student;

Renaming a Table:
You can rename a table provided you are the owner of the table. The general syntax is:
Syntax:
RENAME old table name TO new table name;
EX: RENAME Test To Test_info;

Data Manipulation Language


A data manipulation language is a language that enables users to access or manipulate the
data in database. The data manipulation means :
 Retrieval of information stored in database.
 The insertion of new information into database.
 Deletion of information from database.
 Modification of data in database.

Two types of data manipulation languages are:


 Procedural DML: User need to specify what data are needed to retrieve (modify) and how to
retrieve those data.
 Non Procedural (Declarative DML) : User requires to specify what data are needed to
retrieve without specifying how to get (retrieve) those data.

Basic Structure
Basic structure of an SQL expression consists of select, from and where clauses.
 select clause lists attributes to be copied - corresponds to relational algebra project.
 from clause corresponds to Cartesian product - lists relations to be used.
 where clause corresponds to selection predicate in relational algebra.
SELECT Statement is used to fetch the data from a database table which returns data in the form of result
table. These result tables are called result-sets.
Syntax:
The Following Syntax is used to retrieve specific attributes from the table is as follows:
SELECT column1, column2, columnN FROM table_name;
Here, column1, column2...are the fields of a table whose values you want to fetch

The Following Syntax is used to retrieve all the attributes from the table is as follows:
SELECT * FROM table_name;
Ex: Select * from student;

FROM: The FROM list in the FROM clause is a list of table names. A Table name can be followed by a
range variable. A range variable is particularly useful when the same table name appears more than once in
the from-list.

WHERE: The qualification in the WHERE clause is a Boolean combination (i.e., an expression using the
logical connectives AND, OR, and NOT) of conditions of the form expression op expression, where op is
one of the comparison operators {<, <=, =, <>, >=,>}.

Typical query has the form


select A1,A2,….,An
from r1,r2,,,,,,rm
where P
where each Ai represents an attribute, each ri a relation, and P is a predicate.

This is equivalent to the relational algebra expression


∏A1,A2,......,An(P (r1 X r2 X......Xrm))
 If the where clause is omitted, the predicate P is true.
 The list of attributes can be replaced with a * to select all.
 SQL forms the Cartesian product of the relations named, performs a selection using the predicate,
then
 projects the result onto the attributes named.
 The result of an SQL query is a relation.
 SQL may internally convert into more efficient expressions.

The select Clause


 An example: Find the names of all branches in the account relation.
 select bname
 from account
 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
 select * means select all the attributes. Arithmetic operations can also be in the selection list.

The where Clause


 The predicates can be more complicated, and can involve
 Logical connectives and, or and not.
 Arithmetic expressions on constant or tuple values.
 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
∏cname;loan#(borrower 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"

These are defined in the form clause.

Tuple Variables:
Tuple Variables are mainly used to save typing effort.
They are useful for saving typing, but there are other reasons to use them:
 If you join a table to itself you must give it two different names otherwise referencing
the table would be ambiguous.
 It can be useful to give names to derived tables, and in some database systems it is
required... even if you never refer to the name.
Example1
select customer_name, T.loan_number,S.amount
from borrower as T, loan as S
where T.loan_number = S.loan_number

as is optional. Hence the above example can also be written as

select customer_name, T.loan_number,S.amount


from borrower T, loan S
where T.loan_number = S.loan_number

Example2
SELECT CUSTOMER_NAME, T.LOAN_NUMBER, S.AMOUNT
FROM BORROWER AS T, LOAN AS S
WHERE T.LOAN_NUMBER = S.LOAN_NUMBER

So the tuple variables here are T and S

String Operations:
The string operators in SQL are used to perform important operations such as pattern
matching, concatenation, etc... using the operators. The pattern matching are described using
two special characters (wildcard characters) (1) percent(%) (2) and underscore( _ ) in
concurrence with the Like operator to search for the specific patterns in strings and by the
usage of concatenation operation one or more strings or columns of the tables can be
combined together.
For example:
i) “MUM”: It matches any string beginning with MUM.
ii) “%abc%”: It matches any string of exactly 3 characters.
iii) “_ _ _”: It matches any string having at least 3 characters.

1. Concatenation Operator
SELECT 'Hello' + 'World!' AS StringConcatenated;
->HelloWorld!

SELECT 'Hello' + ' ' + 'World!' AS StringConcatenated;


->Hello World!

2 Like Operator
i) SELECT * FROM STUDENTS WHERE FIRSTNAME='Preety';

Sid Roll FirstName LastName Age


1 14 Preety Sharma 24
ii) SELECT * FROM STUDENTS WHERE FIRSTNAME LIKE 'p%';
In the above query, the FIRSTNAME column is compared with the pattern „p%‟ and then it finds the Student
name which starts with „p‟ as shown below.

Sid Roll FirstName LastName Age


1 14 Preety Sharma 24

iii) SELECT * FROM STUDENTS WHERE FIRSTNAME LIKE '%j';


In the above query, it can be seen that the wildcard character % is used before „j‟ and this will find the values
which end with „j‟.

Sid Roll FirstName LastName Age


2 16 Raj B.K 25

In the above query, it can be seen that the wildcard character % is used before „j‟ and this will find the values
which end with „j‟.

iv) SELECT FIRSTNAME FROM STUDENTS WHERE FIRSTNAME LIKE 'p%y';

Sid FirstName
1 Preety

v) SELECT * FROM STUDENTS WHERE FIRSTNAME LIKE '%a%';

Sid Roll FirstName LastName Age


1 16 Raj B.K 25
2 20 Harry Poudel 28

The „%a%‟ finds any value which has „a‟ in any position of the first name.

vi) SELECT FIRSTNAME FROM STUDENTS WHERE FIRSTNAME LIKE '_a%';

Sid FirstName
1 Raj
2 Harry

The above query will display the FIRSTNAME of the students „Raj‟ and „Harry‟ as they have „a‟ at the
second position in their first names.

vii) SELECT FIRSTNAME FROM STUDENTS WHERE FIRSTNAME LIKE 'p_%';

Sid FirstName
1 Preety

In a result, it can be seen that the first name „Preeti‟ starts with „p‟ and has a length of at least two characters.
Ordering the Display of Tuples:
SQL allows the user to control the order in which tuples are displayed.
order by makes tuples appear in sorted order (ascending order by default).
desc specifies descending order.
asc specifies ascending order.

Syntax:
SELECT column-list|* FROM table-name ORDER BY ASC | DESC;
select *

from loan

order by amount desc, loan# asc

Sorting can be costly, and should only be done when needed.

SELECT * FROM Emp ORDER BY salary;


The above query will return the resultant data in ascending order of the salary.

Eid Name Age Salary


11 Shyam 27 10000
12 Ram 30 11000
13 Hari 23 12000
10 Indira 24 12000
29 Tiger 25 50000

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.
To find duplicate tuples, use GROUP BY HAVING clause.

In terms of the general approach for either scenario, finding duplicates values in SQL comprises two key
steps:
1. Using the GROUP BY clause to group all rows by the target column(s) – i.e. the column(s) you want
to check for duplicate values on.
2. Using the COUNT function in the HAVING clause to check if any of the groups have more than 1
entry; those would be the duplicate values.
select * from StudentTable;

Id Name
100 Aahan
101 Ram
101 Hari
100 Binod
100 Aahan
100 Aahan

Following is the query to find duplicate tuples

select Id,Name,count(*)
from StudentTable
group by Id,Name
having count(*) > 1;

This query here will find the count of records that have the same values. The having clause ensures that only
rows with a count greater than one are shown. The columns in the select clause and the group by clause are
those that indicate a row as a duplicate.

In this example if a row has the same Id and Name as another row it‟s a duplicate in the table.

Id Name Count(*)
100 Aahan 3

SQL delete duplicate Rows using Common Table Expressions (CTE)


We can use Common Table Expressions commonly known as CTE to remove duplicate rows in SQL Server.
It is available starting from SQL Server 2005.

Set Operations:
SQL has the set operations union, intersect and except.
1) union:
In SQL the UNION clause combines the results of two SQL queries into a single table of all
matching rows. The two queries must result in the same number of columns and compatible data
types in order to unite. Any duplicate records are automatically removed unless UNION ALL is
used.

Sales1

person amount
shyam 1000
ram 2000
hari 3000
Sales2

Person amount
shyam 2000
ram 2000
kiran 4000
mohan 5000

Select * FROM Sales1


UNION
Select * FROM Sales2;

OUTPUT

Person amount
shyam 1000
shyam 2000
ram 2000
Hari 3000
kiran 4000
mohan 5000

SELECT * FROM sales1


UNION ALL
SELECT * FROM sales2;

Person amount
shyam 1000
shyam 2000
ram 2000
ram 2000
hari 3000
kiran 4000
mohan 5000

2) intersect:
The SQL INTERSECT operator takes the results of two queries and returns only rows that appear in both
result sets. For purposes of duplicate removal the INTERSECT operator does not distinguish between
NULLs. The INTERSECT operator removes duplicate rows from the final result set. The INTERSECT ALL
operator does not remove duplicate rows from the final result set, but if a row appears X times in the first
query and Y times in the second, it will appear min(X, Y) times in the result set.
One Two
Id Name Id Name
1 Ram 2 Adam
2 Adam 3 Kamal

Intersect query will be,

SELECT * FROM First


INTERSECT
SELECT * FROM Second;

The result table will look like


Id Name
2 Adam

3) except:
The SQL EXCEPT operator takes the distinct rows of one query and returns the rows that do not appear
in a second result set. For purposes of row elimination and duplicate removal, the EXCEPT operator
does not distinguish between NULLs. The EXCEPT ALL operator does not remove duplicates, but if a
row appears X times in the first query and Y times in the second, it will appear max(X - Y, 0) times in
the result set.

Notably, the Oracle platform provides a MINUS operator which is functionally equivalent to the SQL
standard EXCEPT and DISTINCT operator.

Find customers having an account, but not a loan.


(select distinct cname

from depositor)

except

(select cname

from borrower)

 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.
Aggregate Functions:

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

 average value – avg


 minimum value – min
 maximum value – max
 total sum of values – sum
 number in group – count

COUNT counts how many rows are in a particular column.


SUM adds together all the values in a particular column.
MIN and MAX return the lowest and highest values in a particular column, respectively.
AVG calculates the average of a group of selected values.

These are called aggregate functions. They return a single value.

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

If a where clause and a having clause appear in the same query, the where clause predicate is
applied first.

 Tuples satisfying where clause are placed into groups by the group by clause.
 The having clause is applied to each group.
 Groups satisfying the having clause are used by the select clause to generate the
result tuples.
 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:

Subquery:

A subquery, also known as a nested query or subselect, is a SELECT query embedded within the
WHERE or HAVING clause of another SQL query.

Subqueries provide an easy and efficient way to handle the queries that depend on the results from
another query. They are almost identical to the normal SELECT statements, but there are few
restrictions. The most important ones are listed below:

 A subquery must always appear within parentheses.


 A subquery must return only one column. This means you cannot use SELECT * in a
subquery unless the table you are referring has only one column. You may use a subquery
that returns multiple columns, if the purpose is row comparison.
 You can only use subqueries that return more than one row with multiple value operators,
such as the IN or NOT IN operator.
 A subquery cannot be a UNION. Only a single SELECT statement is allowed.

Subqueries are most frequently used with the SELECT statement, however you can use them
within a INSERT, UPDATE, or DELETE statement as well, or inside another subquery.

A subquery can be nested inside other subqueries. SQL has an ability to nest queries within one
another. A subquery is a SELECT statement that is nested within another SELECT statement and
which return intermediate results. SQL executes innermost subquery first, then next level.

1) Set Membership

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'')

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.
2) Set Comparison

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.

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.

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)
3) Test for Empty Relations
 The exists construct returns true if the argument subquery is nonempty.
 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)

4) Test for the Absence of Duplicate Tuples


The unique construct returns true if the argument subquery contains no duplicate tuples.
Find all customers who have only one account at the SFU branch.

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
 SQL-92 allows a subquery expression to be used in the from clause.
 If such an expression is used, the result relation must be given a name, and the attributes
can be renamed.
 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:
 A view in SQL is defined using the create view command:

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

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

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.

The request delete from loan deletes all tuples from the relation loan.

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'')

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

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.

examples:

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)


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.

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:

Updating allows us to change some values in a tuple without necessarily changing all.

examples:

To increase all balances by 5 percent.


update account

set balance=balance * 1.05

This statement is applied to every tuple in account.

To make two different rates of interest payment, depending on balance amount:

update account

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?)

In general, where clause of update statement may contain any construct legal in a where clause of
a select statement (including nesting).

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:

The view update anomaly previously mentioned in Chapter 3 exists also in SQL.

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

The SQL Joins clause is used to combine records from two or more tables in a database. A JOIN is
a means for combining fields from two tables by using values common to each.

Join types and Conditions

SQL provides the following joins:

 Cartesian-product
 natural joins
 inner join
 natural inner join
 left outer join
 right outer join
 full outer join
 cross join
 union join

These operations are typically used as subquery expressions in the from clause.

To show the results of some of the joins, the following tables will be use:
 left outer join

The left outer join is computed with the results of the inner join as above. Then, for every tuple t in the
left-hand-side relation that did not match any tuple in the right-hand-side relation borrower in the inner join, a
tuple r is added to the result of the join, where the left-side is filled in from loan and the remainder is filled in
with null values for each attribute that appears on the right-hand-side. For any tuple in borrower that does not
have a match in loan is ignored.

Note: Again, the attribute loan-number appears twice, because it is on both the left and the right sides.

 natural right outer join


 Cross join:

CROSS JOIN returns the Cartesian product of rows from tables in the join. In other words, it will produce
rows which combine each row from the first table with each row from the second table.

Eg:
SELECT *
FROM employee CROSS JOIN department;

 Natural join
The natural join is a special case of equi-join. Natural join (⋈) is a binary operator that is written as (R ⋈
S) where R and S are relations. The result of the natural join is the set of all combinations of tuples in R and
S that are equal on their common attribute names. For an example consider the tables Employee and Dept
and their natural join:

A natural join is a type of equi-join where the join predicate arises implicitly by comparing all columns in
both tables that have the same column-names in the joined tables. The resulting joined table contains only
one column for each pair of equally named columns. In the case that no columns with the same names are
found, the result is a cross join.

SELECT *
FROM
Employee NATURAL JOIN Dept;

 Right outer join:


A right outer join returns all the values from the right table and matched values from the left table
(NULL in the case of no matching join predicate).

Eg: self

 Full outer join:


A full outer join combines the effect of applying both left and right outer joins. Where rows in the FULL
OUTER JOINed tables do not match, the result set will have NULL values for every column of the table
that lacks a matching row.
Embedded/Static SQL:

 SQL is known as the Structured Query Language. It is the language that we use to perform operations and
transactions on the databases.
 Static SQL statements do not change from execution to execution. The full text of static SQL statements
are known at compilation.
 When we talk about industry-level applications we need properly connected systems which could draw
data from the database and present to the user. In such cases, the embedded SQL comes to our rescue.
 The sql standard defines embedding of sql in a variety of programming language.
 We embed SQL queries into high-level languages such that they can easily perform the logic part of our
analysis.
 Some of the prominent examples of languages with which we embed SQL are as follows:

 C++
 Java
 Python etc.

 A language to which SQL queries embedded is referred to as a host language, and SQL structure permitted
in the host language comprise embedded SQL.
 Embedded SQL starts with identifier, usually EXEC SQL.
 Ends with terminator dependent on host language:Ada, C: terminator is semicolon (;), COBOL: terminator
is END-EXEC etc…

Example: CREATE TABLE

EXEC SQL CREATE TABLE Viewing

(propertyNo VARCHAR2(5) NOT NULL,

clientNo VARCHAR2(5) NOT NULL,

viewDate DATE NOT NULL,

comment VARCHAR2(40));

if (sqlca.sqlcode >= 0)printf(“Creation successful\n”);

With static SQL, all of the data definition information, such as table definitions, referenced by the SQL
statements in your program must be known at compilation. If the data definition changes, you must change
and recompile the program. Dynamic SQL programs can handle changes in data definition information,
because the SQL statements can change "on the fly" at runtime. Therefore, dynamic SQL is much more
flexible than static SQL. Dynamic SQL enables you to write application code that is reusable because the code
defines a process that is independent of the specific SQL statements used.

Performance of static SQL is generally better than dynamic SQL.


Dynamic SQL

As the name indicates, it is a technique that allows professionals to build SQL statements that can be changed
dynamically at the runtime. A dynamic query is a statement that can be constructed at execution or runtime;
for example, the application may allow users to run their own queries at execution.

TCL (Transaction control language) Commands in SQL:

What are Transactions?

Transactions group a set of tasks into a single execution unit. Each transaction begins with a specific task and
ends when all the tasks in the group successfully complete. If any of the tasks fail, the transaction fails.
Therefore, a transaction has only two results: success or failure.

Incomplete steps result in the failure of the transaction. A database transaction, by definition, must be atomic,
consistent, isolated and durable. These are popularly known as ACID properties.

The TCL commands are:

COMMIT

ROLLBACK

SAVEPOINT

These commands are only used with the DML Commands such as – INSERT, UPDATE and DELETE.

 COMMIT

COMMIT command in SQL is used to save all the transaction-related changes permanently to the disk.
Whenever DDL commands such as INSERT, UPDATE and DELETE are used, the changes made by these
commands are permanent only after closing the current session. So before closing the session, one can easily
roll back the changes made by the DDL commands. Hence, if we want the changes to be saved permanently to
the disk without closing the session, we will use the commit command.

Syntax:

COMMIT;
Examle;

Following is an example which would delete those records from the table which have age = 20 and then
COMMIT the changes in the database.

Queries:

DELETE FROM Student WHERE AGE = 20;

COMMIT;
 SAVEPOINT:
We can divide the database operations into parts. For example, we can consider all the insert related
queries that we will execute consecutively as one part of the transaction and the delete command as the
other part of the transaction. Using the SAVEPOINT command in SQL, we can save these different parts
of the same transaction using different names. For example, we can save all the delete related queries with
the savepoint named SP1. To save all the delete related queries in one savepoint, we have to execute the
SAVEPOINT query followed by the savepoint name after finishing the delete command execution.

Syntax:

SAVEPOINT SAVEPOINT_NAME;

DELETE FROM Student WHERE AGE = 20;

SAVEPOINT SP1;

This command is used to save the data at a particular point temporarily, so that whenever needed can be
rollback to that particular point.

 ROLLBACK:
While carrying a transaction, we must create savepoints to save different parts of the transaction.
According to the user's changing requirements, he/she can roll back the transaction to different savepoints.
Consider a scenario: We have initiated a transaction followed by the table creation and record insertion
into the table. After inserting records, we have created a savepoint student (let it be above student table).
Then we executed a delete query, but later we thought that mistakenly we had removed the useful record.
Therefore in such situations, we have an option of rolling back our transaction. In this case, we have to roll
back our transaction using the ROLLBACK command to the savepoint student, which we have created
before executing the DELETE query.

In general ROLLBACK is used to undo a group of transactions.

Syntax for rolling back to Savepoint command:

ROLLBACK TO SAVEPOINT_NAME;
ROLLBACK TO student;

You might also like