SlideShare a Scribd company logo
Prof P Sreenivasa Kumar
Department of CS&E, IITM
1
The SQL Standard
• SQL – Structured Query Language
a ‘standard’ that specifies how
a relational schema is created
data is inserted / updated in the relations
data is queried
transactions are started and stopped
programs access data in the relations
and a host of other things are done
• Every relational database management system (RDBMS) is
required to support / implement the SQL standard.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
2
History of SQL
SEQUEL
developed by IBM in early 70’s
relational query language as part of System-R project at
IBM San Jose Research Lab.
the earliest version of SQL
SQL evolution
SQL- 86/89
SQL- 92 - SQL2
SQL- 99/03 - SQL3
(includes object relational features)
And the evolution continues.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
3
Components of SQL Standard(1/2)
Data Definition Language (DDL)
Specifies constructs for schema definition, relation definition,
integrity constraints, views and schema modification.
Data Manipulation Language (DML)
Specifies constructs for inserting, updating and querying the
data in the relational instances ( or tables ).
Embedded SQL and Dynamic SQL
Specifies how SQL commands can be embedded in a high-level
host language such as C, C++ or Java for programmatic access
to the data.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
4
Transaction Control
Specifies how transactions can be started / stopped, how a set
of concurrently executing transactions can be managed.
Authorization
Specifies how to restrict a user / set of users to access only
certain parts of data, perform only certain types of queries etc.
Components of SQL Standard(2/2)
Prof P Sreenivasa Kumar
Department of CS&E, IITM
5
Data Definition in SQL
Defining the schema of a relation
create table r ( attributeDefinition-1, attributeDefinition-2,…,
attributeDefinition-n, [integrityConstraints-1],
[integrityConstraints-2],…,[integrityConstraints-m])
Attribute Definition –
attribute-name domain-type [NOT NULL] [DEFAULT v]
E.g.:
create table example1 ( A char(6) not null default “000000”,
B int, C char (1) default “F” );
name of the
relation
Prof P Sreenivasa Kumar
Department of CS&E, IITM
6
Domain Types in SQL-92 (1/2)
Numeric data types
• integers of various sizes – INT, SMALLINT
• real numbers of various precision – REAL, FLOAT,
DOUBLE PRECISION
• formatted numbers – DECIMAL ( i, j ) or NUMERIC ( i, j )
i – total number of digits ( precision )
j – number of digits after the decimal point ( scale )
Character string data types
• fixed length – CHAR(n) – n: no. of characters
• varying length – VARCHAR(n) – n: max.no. of characters
Bit string data types
• fixed length – BIT(n)
• varying length – BIT VARYING(n)
Prof P Sreenivasa Kumar
Department of CS&E, IITM
7
Date data type
DATE type has 10 position format – YYYY-MM-DD
Time data type
TIME type has 8 position format – HH : MM : SS
Others
There are several more data types whose details are
available in SQL reference books
Domain Types in SQL-92 (2/2)
Prof P Sreenivasa Kumar
Department of CS&E, IITM
8
Specifying Integrity Constraints in SQL
Also called Table Constraints
Included in the definition of a table
Key constraints
PRIMARY KEY (A1,A2,…,Ak)
specifies that {A1,A2,…,Ak} is the primary key of the table
UNIQUE (B1,B2,…,Bk)
specifies that {B1,B2,…,Bk} is a candidate key for the table
There can be more than one UNIQUE constraint but only one
PRIMARY KEY constraint for a table.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
9
Specifying Referential Integrity Constraints
FOREIGN KEY (A1) REFERENCES r2 (B1)
specifies that attribute A1 of the table being defined, say r1, is a
foreign key referring to attribute B1 of table r2
recall that this means:
each value of column A1 is either null or is one of the
values appearing in column B1 of r2
Prof P Sreenivasa Kumar
Department of CS&E, IITM
10
Specifying What to Do if RIC Violation Occurs
RIC violation
can occur if a referenced tuple is deleted or modified
action can be specified for each case using qualifiers
ON DELETE or ON UPDATE
Actions
three possibilities can be specified
SET NULL, SET DEFAULT, CASCADE
these are actions to be taken on the referencing tuple
SET NULL – foreign key attribute value to be set null
SET DEFAULT – foreign key attribute value to be set to its
default value
CASCADE – delete the referencing tuple if the referenced
tuple is deleted or update the FK attribute if the
referenced tuple is updated
Prof P Sreenivasa Kumar
Department of CS&E, IITM
11
Table Definition Example
create table students (
rollNo char(8) not null,
name varchar(15) not null,
degree char(5),
year smallint,
sex char not null,
deptNo smallint,
advisor char(6),
primary key(rollNo),
foreign key(deptNo) references
department(deptId)
on delete set null on update cascade,
foreign key(advisor) references
professor(empId)
on delete set null on update cascade
);
Prof P Sreenivasa Kumar
Department of CS&E, IITM
12
Modifying a Defined Schema
ALTER TABLE command can be used to modify a schema
Adding a new attribute
ALTER table student ADD address varchar(30);
Deleting an attribute
need to specify what needs to be done about views or
constraints that refer to the attribute being dropped
two possibilities
CASCADE – delete the views/constraints also
RESTRICT – do not delete the attributes if there are some
views/constraints that refer to it.
ALTER TABLE student DROP degree RESTRICT
Similarly, an entire table definition can be deleted
Prof P Sreenivasa Kumar
Department of CS&E, IITM
13
Data Manipulation in SQL
Basic query syntax
select A1,A2,…,Am a set of attributes
from relations R1,…,Rp that are
from R1,R2,…,Rp required in the output table.
the set of tables that
where θ contain the relevant
tuples to answer the query.
a boolean predicate that
specifies when a combined
tuple of R1,…,Rp contributes
to the output.
Equivalent to: Assuming that each attribute
name appears exactly once
in the table.
1 2 nA ,A ,....A θ 1 2 pπ (σ (R ×R ×.....×R ))
Prof P Sreenivasa Kumar
Department of CS&E, IITM
14
Meaning of the Basic Query Block
The cross product M of the tables in the from clause
would be considered.
Tuples in M that satisfy the condition θ are selected.
For each such tuple, values for the attributes A1,A2,….,Am
( mentioned in the select clause) are projected.
This is a conceptual description
- in practice more efficient methods are employed for
evaluation.
The word select in SQL should not be confused with select
operation of relational algebra.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
15
SQL Query Result
The result of any SQL query
a table with select clause attributes as column names.
duplicate rows may be present.
- differs from the definition of a relation.
duplicate rows can be eliminated by specifying DISTINCT
keyword in the select clause, if necessary.
SELECT DISTINCT name
FROM student
duplicate rows are essential while computing aggregate
functions ( average, sum etc ).
removing duplicate rows involves additional effort and is
done only when necessary.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
16
Example Relational Scheme
student (rollNo, name, degree, year, sex, deptNo, advisor)
department (deptId, name, hod, phone)
professor (empId, name, sex, startYear, deptNo, phone)
course (courseId, cname, credits, deptNo)
enrollment (rollNo, courseId, sem, year, grade)
teaching (empId, courseId, sem, year, classRoom)
preReq (preCourseId, courseId)
Prof P Sreenivasa Kumar
Department of CS&E, IITM
17
Example Relational Scheme with RIC’s shown
student (rollNo, name, degree, year, sex, deptNo, advisor)
department (deptId, name, hod, phone)
professor (empId, name, sex, startYear, deptNo, phone)
course (courseId, cname, credits, deptNo)
enrollment (rollNo, courseId, sem, year, grade)
teaching (empId, courseId, sem, year, classRoom)
preRequisite (preReqCourse, courseID)
Prof P Sreenivasa Kumar
Department of CS&E, IITM
18
Example Queries Involving a Single Table
Get the rollNo, name of all women students in the
dept no. 5.
select rollNo, name
from student
where sex = ‘F’ and deptNo = ‘5’;
Get the employee Id, name and phone number of
professors in the CS dept (deptNo = 3) who have
joined after 1999.
select empId, name, phone
from professor
where deptNo = 3 and startYear > 1999;
Prof P Sreenivasa Kumar
Department of CS&E, IITM
19
Examples Involving Two or More Relations (1/2)
Get the rollNo, name of students in the CSE
dept (deptNo = 3)along with their advisor’s
name and phone number.
select rollNo, s.name, f.name as advisorName,
phone as advisorPhone
from student as s, professor as f
where s.advisor = f.empId and
s.deptNo = ‘3’;
attribute
renaming in
the output
table aliases are
used to disambiguate
the common attributes
table aliases are required
if an attribute name
appears in more than
one table.
Also when same relation
appears twice in the from
clause.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
20
Get the names, employee ID’s, phone numbers
of professors in CSE dept who joined
before 1995.
select empId, f.name, phone
from professor as f, department as d
where f.deptNo = d.deptId and
d.name = ‘CSE’ and
f.startYear < 1995
Examples Involving Two or More Relations (2/2)
Prof P Sreenivasa Kumar
Department of CS&E, IITM
21
Nested Queries or Subqueries
While dealing with certain complex queries
beneficial to specify part of the computation as a
separate query & make use of its result to formulate
the main query.
such queries – nested / subqueries.
Using subqueries
makes the main query easy to understand / formulate
sometimes makes it more efficient also
• sub query result can be computed once and
used many times.
• not the case with all subqueries.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
22
Nested Query Example
Get the rollNo, name of students who have
a lady professor as their advisor.
select s.rollNo, s.name
from student s
where s.advisor IN
(select empId
from professor
where sex = ‘F’);
NOT IN can be used in the above query to get details of students
who don’t have a lady professor as their advisor.
IN Operator: One of the
ways of making use of
the subquery result
Subquery computes
the empId’s of
lady professors
Prof P Sreenivasa Kumar
Department of CS&E, IITM
23
Set Comparison Operators
SQL supports several operators to deal with subquery results or
in general with collection of tuples.
Combination of { =, <, ≤, ≥, >, < > } with keywords
{ ANY, ALL } can be used as set comparison operators.
Get the empId, name of the senior-most Professor(s):
select p.empId, p.name
from professors p
where p.startYear <= ALL ( select distinct startYear
from professor );
Prof P Sreenivasa Kumar
Department of CS&E, IITM
24
Semantics of Set Comparison Operators
v op ANY S
true if for some member x of S, v op x is true
false if for no member x of S, v op x is true
v op ALL S
true if for every member x of S, v op x is true
false if for some member x of S, v op x is not true
IN is equivalent to = ANY
NOT IN is equivalent to < > ALL
v is normally a single attribute, but while using IN or
NOT IN it can be a tuple of attributes
op is one of <, ≤, >, ≥, =, < >
S is a subquery
Prof P Sreenivasa Kumar
Department of CS&E, IITM
25
Correlated Nested Queries
If the nested query result is independent of the current tuple
being examined in the outer query,
nested query is called uncorrelated,
otherwise, nested query is called correlated.
Uncorrelated nested query
nested query needs to be computed only once.
Correlated nested query
nested query needs to be re-computed for each row
examined in the outer query.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
26
Example of a Correlated Subquery
Get the roll number and name of students
whose gender is same as their advisor’s.
select s.rollNo, s.name
from student s
where s.sex = ALL ( select f.sex
from professor f
where f.empId = s.advisor );
Prof P Sreenivasa Kumar
Department of CS&E, IITM
27
EXISTS Operator
Using EXISTS, we can check if a subquery result is non-empty
EXISTS ( S ) is true if S has at least one tuple / member
is false if S contain no tuples
Get the employee Id and name of professors
who advise at least one women student.
select f.empId, f.name
from professors f
where EXISTS ( select s.rollNo
from student s
where s.advisor = f.empId and
s.sex = ‘F’ );
SQL does not have an operator for universal quantification.
a correlated
subquery
Prof P Sreenivasa Kumar
Department of CS&E, IITM
28
Obtain the department Id and name of
departments that do not offer any 4 credit courses.
select d.deptId, d.name
from department d
where NOT EXISTS ( select courseId
from course c
where c.deptNo = d.deptId and
c.credits = ‘4’ );
Queries with existentially quantified predicates can be easily
specified using EXISTS operator.
Queries with universally quantified predicates can only be
specified after translating them to use existential quantifiers.
NOT EXISTS Operator
Prof P Sreenivasa Kumar
Department of CS&E, IITM
29
Example Involving Universal Quantifier
Obtain the department Id and name of departments
whose courses are all 3-credit courses.
Equivalently, obtain the department Id and name of departments
that do not offer a single course that is not 3-credit course
select d.deptNo, d.name
from department d
where NOT EXISTS ( select c.courseId
from course c
where c.deptNo = d.deptId and
c.credits ≠ 3);
Prof P Sreenivasa Kumar
Department of CS&E, IITM
30
Missing where Clause
If the where clause in an SQL query is not specified, it is treated
as the where condition is true for all tuple combinations.
Essentially no filtering is done on the cross product of from
clause tables.
Get the name and contact phone of all Departments.
select name,phone
from department
Prof P Sreenivasa Kumar
Department of CS&E, IITM
31
Union, Intersection and Difference Operations
In SQL, using operators UNION, INTERSECT and EXCEPT,
one can perform set union, intersection and difference
respectively.
Results of these operators are sets –
i.e duplicates are automatically removed.
Operands need to be union compatible and also have same
attributes in the same order.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
32
Example using UNION
Obtain the roll numbers of students who are currently
enrolled for either CS230 or CS232 courses.
(SELECT rollNo
FROM enrollment
WHERE courseId = ‘CS230’ and
sem = odd and year = 2005 ) UNION
(SELECT rollNo
FROM enrollment
WHERE courseId = ‘CS232’ and
sem = odd and year = 2005 );
Equivalent to:
(SELECT rollNo
FROM enrollment
WHERE (courseId = ‘CS230’ or courseID = ‘CS232’)
and sem = odd and year = 2005 )
Prof P Sreenivasa Kumar
Department of CS&E, IITM
33
Obtain the roll numbers of students who are currently
enrolled for both CS230 and CS232 Courses.
select rollNo
from enrollment
where courseId = ‘CS230’ and
sem = odd and
year = 2005
INTERSECT
select rollNo
from enrollment
where courseId = ‘CS232’ and
sem = odd and year = 2005;
Example using INTERSECTION
Prof P Sreenivasa Kumar
Department of CS&E, IITM
34
Example using EXCEPT
Obtain the roll numbers of students who are
currently not enrolled for CS230 course.
(SELECT rollNo
FROM enrollment
WHERE sem = odd and year = 2005 )
EXCEPT
(SELECT rollNo
FROM enrollment
WHERE courseId = ‘CS230’ and
sem = odd and year = 2005);
Prof P Sreenivasa Kumar
Department of CS&E, IITM
35
Aggregation of Data
Data analysis
need for computing aggregate values for data
total value, average value etc
Aggregate functions in SQL
five aggregate function are provided in SQL
AVG, SUM, COUNT, MAX, MIN
can be applied to any column of a table
can be used in the select clause of SQL queries
Prof P Sreenivasa Kumar
Department of CS&E, IITM
36
Aggregate functions
AVG ( [DISTINCT]A):
computes the average of (distinct) values in column A
SUM ( [DISTINCT]A):
computes the sum of (distinct) values in column A
COUNT ( [DISTINCT]A):
computes the number of (distinct) values in column A or no.
of tuples in result
MAX (A): computes the maximum of values in column A
MIN (A): computes the minimum of values in column A
Optional
keyword
Prof P Sreenivasa Kumar
Department of CS&E, IITM
37
Examples involving aggregate functions (1/2)
Suppose data about Gate in a particular year is available in a table
with schema
gateMarks(regNo,name,sex,branch,city,state,marks)
Obtain the number of students who have taken GATE in
CS and their average marks
Select count(regNo) as CsTotal avg(marks) as CsAvg
from gateMarks
where branch = ‘CS’
Get the maximum, minimum and average marks obtained
by Students from the city of Hyderabad
Select max(marks), min(marks), avg(marks)
from gateMarks
where city = ‘Hyderabad’;
CStotal CSavg
Output
Prof P Sreenivasa Kumar
Department of CS&E, IITM
38
Examples involving aggregate functions (2/2)
Get the names of students who obtained the
maximum marks in the branch of EC
Select name, max(marks)
from gateMarks
where branch = ‘EC’
Will not work
Only aggregate functions can be specified here. It does not
make sense to include normal attributes ! (unless they are
grouping attributes – to be seen later)
Select regNo, name, marks
from gateMarks
where branch = ‘EC’ and marks =
(select max(marks)
from gateMarks
where branch = ‘EC’);
Correct way of
specifying the query
Prof P Sreenivasa Kumar
Department of CS&E, IITM
39
Date Aggregation and Grouping
Grouping
Partition the set of tuples in a relation into groups based on
certain criteria and compute aggregate functions for each group
All tuples that agree on a set of attributes (i.e have the same
value for each of these attributes ) are put into a group
The specified aggregate functions are computed for each group
Each group contributes one tuple to the output
All the grouping attributes must also appear in the select clause
the result tuple of the group is listed along with the values of the
grouping attributes of the group
Called the grouping
attributes
Prof P Sreenivasa Kumar
Department of CS&E, IITM
40
Examples involving grouping(1/2)
Determine the maximum of the GATE CS marks
obtained by students in each city, for all
cities.
Select city, max(marks) as maxMarks
from gateMarks
where branch = ‘CS’
group by city;
Grouping attributes
must appear in the
select clause
Grouping
attribute
Result:
City maxMarks
Hyderabad
Chennai
Mysore
Bangalore
87
84
90
82
Prof P Sreenivasa Kumar
Department of CS&E, IITM
41
In the University database, for each department,
obtain the name, deptId and the total number of four
credit courses offered by the department
Select deptId, name, count(*) as totalCourses
from department, course
where deptId = deptNo and credits = 4
group by deptId, name;
Examples involving grouping(2/2)
Prof P Sreenivasa Kumar
Department of CS&E, IITM
42
Having clause
After performing grouping, is it possible to report information
about only a subset of the groups ?
Yes, with the help of having clause which is always used in
conjunction with Group By clause
Report the total enrollment in each course in the 2nd
semester of 2004; include only the courses with a
minimum enrollment of 10.
Select courseId, count(rollNo) as Enrollment
from enrollment
where sem = 2 and year = 2004
group by courseId
having count(rollNo) ≥ 10;
Prof P Sreenivasa Kumar
Department of CS&E, IITM
43
Where clause versus Having clause
• Where clause
• Performs tests on rows and eliminates rows not satisfying
the specified condition
• Performed before any grouping of rows is done
• Having clause
• Always performed after grouping
• Performs tests on groups and eliminates groups not
satisfying the specified condition
• Tests can only involve grouping attributes and aggregate
functions
Select courseId, count(rollNo) as Enrollment
from enrollment
where sem = 2 and year = 2004
group by courseId
having count(rollNo) ≥ 10;
Prof P Sreenivasa Kumar
Department of CS&E, IITM
44
String Operators in SQL
Specify strings by enclosing them in single quotes
e.g., ‘Chennai’
Common operations on strings –
• pattern matching – using ‘LIKE’ comparison operator
Specify patterns using special characters –
• character ‘%’ (percent) matches any Substring
e.g., ‘Jam%’ matches any string starting with “Jam”
• character ‘_’ (underscore) matches any single character
e.g., (a) ‘_ _ press’ matches with any string ending
with “press”, with any two characters before that.
(b) ‘_ _ _ _’ matches any string with exactly four
characters
Prof P Sreenivasa Kumar
Department of CS&E, IITM
45
Using the ‘LIKE’ operator
Obtain roll numbers and names of all students
whose names end with ‘Mohan’
Select rollNo, name
from student
where name like ‘%Mohan’;
Patterns are case sensitive.
Special characters (percent, underscore) can be included in
patterns using an escape character ‘’ (backslash)
Prof P Sreenivasa Kumar
Department of CS&E, IITM
46
Join Operation
In SQL, usually joining of tuples from different relations is
specified in ‘where’ clause
Get the names of professors working in CSE dept.
Select f.name
from professor as f, department as d
where f.deptNo = d.deptId and
d.name = ‘CSE’;
The above query specifies joining of professor and department
relations on condition f.deptNo = d.deptId and d.name = ‘CSE’
Prof P Sreenivasa Kumar
Department of CS&E, IITM
47
Explicit Specification of Joining in ‘From’ Clause
select f.name
from (professor as f join department as d on
f.deptNo = d.deptId)
where d.name = ‘CSE’;
Join types:
1. inner join (default):
from (r1 inner join r2 on <predicate>)
use of just ‘join’ is equivalent to ‘inner join’
2. left outer join:
from (r1 left outer join r2 on <predicate>)
3. right outer join:
from (r1 right outer join r2 on <predicate>)
4. full outer join:
from (r1 full outer join r2 on <predicate>)
Prof P Sreenivasa Kumar
Department of CS&E, IITM
48
Natural join
The adjective ‘natural’ can be used with any of the join types to
specify natural join.
FROM (r1 NATURAL <join type> r2 [USING <attr. list>])
• natural join by default considers all common attributes
• a subset of common attributes can be specified in an
optional using <attr. list> phrase
REMARKS
• Specifying join operation explicitly goes against the spirit of
declarative style of query specification
• But the queries may be easier to understand
• The feature is to be used judiciously
Prof P Sreenivasa Kumar
Department of CS&E, IITM
49
Views
Views provide virtual relations which contain data spread across
different tables. Used by applications.
• simplified query formulations
• data hiding
• logical data independence
Once created, a view is always kept up-to-date by the RDBMS
View is not part of conceptual schema
• created to give a user group, concerned with a certain aspect
of the information system, their view of the system
Storage
• Views need not be stored as permanent tables
• They can be created on-the-fly whenever needed
• They can also be materialized
Tables involved in the view definition – base tables
Prof P Sreenivasa Kumar
Department of CS&E, IITM
50
Creating Views
CREATE VIEW v AS <query expr>
creates a view ‘v’, with structure and data defined by the
outcome of the query expression
Create a view which contains name, employee Id and
phone number of professors who joined CSE dept
in or after the year 2000.
create view profAft2K as
(Select f.name, empId, phone
from professor as f, department as d
where f.depNo = d.deptId and
d.name = ‘CSE’ and
f.startYear >= 2000);
If the details of a new CSE professor are entered into professor table,
the above view gets updated automatically
name of the view
Prof P Sreenivasa Kumar
Department of CS&E, IITM
51
Queries on Views
Once created a view can be used in queries just like any other
table.
e.g. Obtain names of professors in CSE dept,
who joined after 2000 and whose name
starts with ‘Ram’
select name
from profAft2K
where name like ‘Ram%’;
The definition of the view is stored in DBMS, and executed to
create the temporary table (view), when encountered in query
Prof P Sreenivasa Kumar
Department of CS&E, IITM
52
Operations on Views
Querying is allowed
Update operations are usually restricted
because – updates on a view may modify many base tables
– there may not be a unique way of updating the
base tables to reflect the update on view
– view may contain some aggregate values
– ambiguity where primary key of a base table is not
included in view definition.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
53
Restrictions on Updating Views
Updates on views defined on joining of more than one table
are not allowed
For example, updates on the following view are not allowed
create a view Professor_Dept with professor
ID, department Name and department phone
create view profDept(profId,DeptName,DPhone) as
(select f.empId, d.name, d.phone
from professor f, department d
where f.depNo = d.depId);
Prof P Sreenivasa Kumar
Department of CS&E, IITM
54
Updates on views defined with ‘group by’ clause and aggregate
functions is not permitted, as a tuple in view will not have a
corresponding tuple in base relation.
For example, updates on the following view are not allowed
Create a view deptNumCourses which contains the
number of courses offered by a dept.
create view deptNumCourses(deptNo,numCourses)
as select deptNo, count(*)
from course
group by deptNo;
Restrictions on Updating Views
Prof P Sreenivasa Kumar
Department of CS&E, IITM
55
Updates on views which do not include Primary Key of
base table, are also not permitted
For example, updates on the following view are not allowed
Create a view StudentPhone with Student name and
phone number.
create view StudentPhone (sname,sphone) as
(select name, phone
from student);
View StudentPhone does not include Primary key of the
base table.
Restrictions on Updating Views
Prof P Sreenivasa Kumar
Department of CS&E, IITM
56
Updates to views are allowed only if
defined on single base table
not defined using ‘group by’ clause and aggregate functions
include Primary Key of base table
Allowed Updates on Views
Prof P Sreenivasa Kumar
Department of CS&E, IITM
57
Inserting data into a table
Specify a tuple(or tuples) to be inserted
INSERT INTO student VALUES
(‘CSO5D014’,‘Mohan’,‘PhD’,2005,‘M’,3,‘FCS008’),
(‘CSO5S031’,‘Madhav’,‘MS’,2005,‘M’,4,‘FCE009’);
Specify the result of query to be inserted
INSERT INTO r1 SELECT … FROM … WHERE …
Specify that a sub-tuple be inserted
INSERT INTO student(rollNo, name, sex)
VALUES (CS05M022, ‘Rajasri’, ‘F’),
(CS05B033, ‘Kalyan’, ‘M’);
the attributes that can be NULL or have
declared default values can be left-out to be
updated later
Prof P Sreenivasa Kumar
Department of CS&E, IITM
58
Deleting rows from a table
Deletion of tuples is possible ; deleting only part of a tuple is
not possible
Deletion of tuples can be done only from one relation at a time
Deleting a tuple might trigger further deletions due to
referentially triggered actions specified as part of RIC’s
Generic form: delete from r where <predicate>;
Delete tuples from professor relation with start year
as 1982.
delete from professor
where startYear = 1982;
If ‘where’ clause is not specified, then all the tuples of that
relation are deleted ( Be careful !)
Prof P Sreenivasa Kumar
Department of CS&E, IITM
59
A Remark on Deletion
The where predicate is evaluated for each of the tuples in the
relation to mark them as qualified for deletion before any
tuple is actually deleted from the relation
Note that the result may be different if tuples are deleted as and
when we find that they satisfy the where condition!
An example:
Delete all tuples of students that scored the least marks in the
CS branch:
DELETE
FROM gateMarks
WHERE branch = “CS” and
marks = ANY ( SELECT MIN(marks)
FROM gateMarks
WHERE branch = “CS”)
Prof P Sreenivasa Kumar
Department of CS&E, IITM
60
Updating tuples in a relation
update r
set <<attr = newValue> list>
where <predicates>;
Change phone number of all professors working in CSE
dept to “94445 22605”
update professors
set phone = ‘9444422605’
where deptNo = (select deptId
from department
where name = ‘CSE’);
If ‘where’ clause is not specified, values for the specified
attributes in all tuples is changed.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
61
Miscellaneous features in SQL (1/3)
Ordering of result tuples can be done using ‘order by’ clause
e.g., List the names of professors who joined
after 1980, in alphabetic order.
select name
from professor
where startYear > 1980
order by name;
Use of ‘null’ to test for a null value, if the attribute can take null
e.g., Obtain roll numbers of students who
don’t have phone numbers
select rollNo
from student
where phoneNumber is null;
Prof P Sreenivasa Kumar
Department of CS&E, IITM
62
Miscellaneous features in SQL (2/3)
Use of ‘between and’ to test the range of a value
e.g., Obtain names of professors who have
joined between 1980 and 1990
select name
from professor
where startYear between 1980 and 1990;
Change the column name in result relation
e.g.,
select name as studentName, rollNo as studentNo
from student;
Prof P Sreenivasa Kumar
Department of CS&E, IITM
63
Miscellaneous features in SQL (3/3)
Use of ‘distinct’ key word in ‘select’ clause to determine
duplicate tuples in result.
Obtain all distinct branches of study for students
select distinct d.name
from student as s, department as d
where s.deptNo = d.deptId;
Use of asterisk (*) to retrieve all the attribute values of
selected tuples.
Obtain details of professors along with their
department details.
select *
from professor as f, department as d
where f.deptNo = d.deptId;
Prof P Sreenivasa Kumar
Department of CS&E, IITM
64
Application Development Process
Host language (HL) – the high-level programming language in
which the application is developed (e.g., C, C++, Java etc.)
Database access – using embedded SQL is one approach
• SQL statements are interspersed in HL program.
Data transfer –
takes place through specially declared HL variables
Mismatch between HL data types and SQL data types
• SQL 92 standard specifies the corresponding SQL types for
many HLs.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
65
Declaring Variables
Variables that need to be used in SQL statements are declared in a
special section as follows:
EXEC SQL BEGIN DECLARE SECTION
char rollNo[9]; // HL is C language
char studName[20], degree[6];
int year; char sex;
int deptNo; char advisor[9];
EXEC SQL END DECLARE SECTION
Note that schema for student relation is
student(rollNo, name, degree, year, sex, deptNo, advisor)
Use in SQL statements: variable name is prefixed with a colon(:)
e.g., :ROLLNO in an SQL statement refers to rollNo variable
Prof P Sreenivasa Kumar
Department of CS&E, IITM
66
Handling Error Conditions
The HL program needs to know if an SQL statement has
executed successfully or otherwise
Special variable called SQLSTATE is used for this purpose
SQLSTATE is set to appropriate value by the RDBMS
run-time after executing each SQL statement
non-zero values indicate errors in execution
• different values indicate different types of error situations
SQLSTATE variable must be declared in the HL program and
HL program needs to check for error situations and handle them
appropriately.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
67
Embedding SQL statements
Suppose we collect data through user interface into variables
rollNo, studName, degree, year, sex, deptNo, advisor
A row in student table can be inserted –
EXEC SQL INSERT INTO STUDENT
VALUES (:rollNo,:studName,:degree,
:year,:sex,:deptNo,:advisor);
Prof P Sreenivasa Kumar
Department of CS&E, IITM
68
Impedance mismatch and cursors
Occurs because, HL languages do not support set-of-records
as supported by SQL
A‘cursor’ is a mechanism which allows us to retrieve one row
at a time from the result of a query
We can declare a cursor on any SQL query
Once declared, we use open, fetch, move and close commands
to work with cursors
We usually need a cursor when embedded statement is
a SELECT query
INSERT, DELETE and UPDATE don’t need a cursor.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
69
We don’t need a cursor if the query results in a single row.
e.g., EXEC SQL SELECT s.name, s.sex
INTO :name, :sex
FROM student s
WHERE s.rollNo = :rollNo;
Result row values name and phone are assigned to HL variables
:name and :phone, using ‘INTO’ clause
Cursor is not required as the result always contains only
one row ( rollNo is a key for student relation)
Embedded SQL (1/2)
Prof P Sreenivasa Kumar
Department of CS&E, IITM
70
If the result contains more than one row, cursor declaration
is needed
e.g., select s.name, s.degree
from student s
where s.sex = ‘F’;
Query results in a collection of rows
HL program has to deal with set of records.
The use of ‘INTO’ will not work here
We can solve this problem by using a ‘cursor’.
Embedded SQL (2/2)
Prof P Sreenivasa Kumar
Department of CS&E, IITM
71
Declaring a cursor on a query
declare studInfo cursor for
select name, degree
from student
where sex = ‘F’;
Command OPEN studInfo; opens the cursor and makes it point
to first record
To read current row of values into HL variables, we use the
command FETCH studInfo INTO :name, :degree;
After executing FETCH statement cursor is pointed to next
row by default
Cursor movement can be optionally controlled by the
programmer
After reading all records we close the cursor using the
CLOSE studInfo command.
Cursor name
Prof P Sreenivasa Kumar
Department of CS&E, IITM
72
Dynamic SQL
Useful for applications to generate and run SQL statements,
based on user inputs
Queries may not be known in advance
e.g., char sqlstring [ ] = {“select * from student”};
EXEC SQL PREPARE runQ FROM sqlstring;
EXEC SQL EXECUTE runQ;
‘Sqlstring’ is a ‘C’ variable that holds user submitted query
‘runQ’ is an SQL variable that holds the SQL statements.
Prof P Sreenivasa Kumar
Department of CS&E, IITM
73
Connecting to Database from HL
ODBC (Open Database Connectivity) and
JDBC (Java Database Connectivity)
accessing database and data is through an API
many DBMSs can be accessed
no restriction on number of connections
appropriate drivers are required
steps in accessing data from a HL program
• select the data source
• load the appropriate driver dynamically
• establish the connection
• work with database
• close the connection.

More Related Content

PPTX
Method Time Measurement
DOC
Rpp prota prosem pengembangan silabus matematika x sma 18 pulau temiang ilh...
PDF
1 introduction
PPTX
Managing your tech career
PDF
6 relational schema_design
PPT
Best Practices for Database Schema Design
PDF
5 data storage_and_indexing
Method Time Measurement
Rpp prota prosem pengembangan silabus matematika x sma 18 pulau temiang ilh...
1 introduction
Managing your tech career
6 relational schema_design
Best Practices for Database Schema Design
5 data storage_and_indexing

Viewers also liked (20)

PPTX
Webinar: Build an Application Series - Session 2 - Getting Started
PDF
3 relational model
PDF
MySQL Replication: Pros and Cons
PDF
Distributed Postgres
ZIP
Week3 Lecture Database Design
PPTX
Database Design
PPTX
English gcse final tips
PDF
2 entity relationship_model
PDF
Postgres-XC Write Scalable PostgreSQL Cluster
PDF
Escalabilidade, Sharding, Paralelismo e Bigdata com PostgreSQL? Yes, we can!
PPTX
Database design concept
PDF
Database Schema
PPT
Best Practices for Database Schema Design
PPT
Database design
PPT
Database design
PDF
Advanced Postgres Monitoring
PPT
The NoSQL Way in Postgres
 
PDF
Postgres in Amazon RDS
PDF
Scaling postgres
PDF
How to analyze and tune sql queries for better performance webinar
Webinar: Build an Application Series - Session 2 - Getting Started
3 relational model
MySQL Replication: Pros and Cons
Distributed Postgres
Week3 Lecture Database Design
Database Design
English gcse final tips
2 entity relationship_model
Postgres-XC Write Scalable PostgreSQL Cluster
Escalabilidade, Sharding, Paralelismo e Bigdata com PostgreSQL? Yes, we can!
Database design concept
Database Schema
Best Practices for Database Schema Design
Database design
Database design
Advanced Postgres Monitoring
The NoSQL Way in Postgres
 
Postgres in Amazon RDS
Scaling postgres
How to analyze and tune sql queries for better performance webinar
Ad

Similar to 4 the sql_standard (20)

PPT
UNIT 2 Structured query language commands
PDF
3_Relational_Model.pdf
PPTX
Introduction to SQL- Database management System
PPT
04 Introduction to SQ(Structural Query Language)L.ppt
PPT
Db1 lecture4
PPT
Unit04 dbms
PPT
ER model and diagram in database system
PPT
The SQL data-definition language (DDL) allows the specification of informatio...
PPT
SQL manages and queries database data.ppt
PPT
SQL : introduction
PPTX
introduction to database system concepts 2
PPT
Dbms ii mca-ch4-relational model-2013
PPT
mysql.ppt
PPTX
Intonation to SQL using the query language postgresSQL.pptx
PPTX
SQL data types: INT, VARCHAR, CHAR,.pptx
PPT
ch3.ppt
PPTX
RDBMS
PDF
Introduction to sql.pdf Database Systems
PPT
Sql (DBMS)
UNIT 2 Structured query language commands
3_Relational_Model.pdf
Introduction to SQL- Database management System
04 Introduction to SQ(Structural Query Language)L.ppt
Db1 lecture4
Unit04 dbms
ER model and diagram in database system
The SQL data-definition language (DDL) allows the specification of informatio...
SQL manages and queries database data.ppt
SQL : introduction
introduction to database system concepts 2
Dbms ii mca-ch4-relational model-2013
mysql.ppt
Intonation to SQL using the query language postgresSQL.pptx
SQL data types: INT, VARCHAR, CHAR,.pptx
ch3.ppt
RDBMS
Introduction to sql.pdf Database Systems
Sql (DBMS)
Ad

Recently uploaded (20)

PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
PPT on Performance Review to get promotions
PDF
Well-logging-methods_new................
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
web development for engineering and engineering
PPTX
Construction Project Organization Group 2.pptx
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
composite construction of structures.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
UNIT 4 Total Quality Management .pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPT on Performance Review to get promotions
Well-logging-methods_new................
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Model Code of Practice - Construction Work - 21102022 .pdf
web development for engineering and engineering
Construction Project Organization Group 2.pptx
Strings in CPP - Strings in C++ are sequences of characters used to store and...
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
composite construction of structures.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
CH1 Production IntroductoryConcepts.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
UNIT-1 - COAL BASED THERMAL POWER PLANTS
UNIT 4 Total Quality Management .pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk

4 the sql_standard

  • 1. Prof P Sreenivasa Kumar Department of CS&E, IITM 1 The SQL Standard • SQL – Structured Query Language a ‘standard’ that specifies how a relational schema is created data is inserted / updated in the relations data is queried transactions are started and stopped programs access data in the relations and a host of other things are done • Every relational database management system (RDBMS) is required to support / implement the SQL standard.
  • 2. Prof P Sreenivasa Kumar Department of CS&E, IITM 2 History of SQL SEQUEL developed by IBM in early 70’s relational query language as part of System-R project at IBM San Jose Research Lab. the earliest version of SQL SQL evolution SQL- 86/89 SQL- 92 - SQL2 SQL- 99/03 - SQL3 (includes object relational features) And the evolution continues.
  • 3. Prof P Sreenivasa Kumar Department of CS&E, IITM 3 Components of SQL Standard(1/2) Data Definition Language (DDL) Specifies constructs for schema definition, relation definition, integrity constraints, views and schema modification. Data Manipulation Language (DML) Specifies constructs for inserting, updating and querying the data in the relational instances ( or tables ). Embedded SQL and Dynamic SQL Specifies how SQL commands can be embedded in a high-level host language such as C, C++ or Java for programmatic access to the data.
  • 4. Prof P Sreenivasa Kumar Department of CS&E, IITM 4 Transaction Control Specifies how transactions can be started / stopped, how a set of concurrently executing transactions can be managed. Authorization Specifies how to restrict a user / set of users to access only certain parts of data, perform only certain types of queries etc. Components of SQL Standard(2/2)
  • 5. Prof P Sreenivasa Kumar Department of CS&E, IITM 5 Data Definition in SQL Defining the schema of a relation create table r ( attributeDefinition-1, attributeDefinition-2,…, attributeDefinition-n, [integrityConstraints-1], [integrityConstraints-2],…,[integrityConstraints-m]) Attribute Definition – attribute-name domain-type [NOT NULL] [DEFAULT v] E.g.: create table example1 ( A char(6) not null default “000000”, B int, C char (1) default “F” ); name of the relation
  • 6. Prof P Sreenivasa Kumar Department of CS&E, IITM 6 Domain Types in SQL-92 (1/2) Numeric data types • integers of various sizes – INT, SMALLINT • real numbers of various precision – REAL, FLOAT, DOUBLE PRECISION • formatted numbers – DECIMAL ( i, j ) or NUMERIC ( i, j ) i – total number of digits ( precision ) j – number of digits after the decimal point ( scale ) Character string data types • fixed length – CHAR(n) – n: no. of characters • varying length – VARCHAR(n) – n: max.no. of characters Bit string data types • fixed length – BIT(n) • varying length – BIT VARYING(n)
  • 7. Prof P Sreenivasa Kumar Department of CS&E, IITM 7 Date data type DATE type has 10 position format – YYYY-MM-DD Time data type TIME type has 8 position format – HH : MM : SS Others There are several more data types whose details are available in SQL reference books Domain Types in SQL-92 (2/2)
  • 8. Prof P Sreenivasa Kumar Department of CS&E, IITM 8 Specifying Integrity Constraints in SQL Also called Table Constraints Included in the definition of a table Key constraints PRIMARY KEY (A1,A2,…,Ak) specifies that {A1,A2,…,Ak} is the primary key of the table UNIQUE (B1,B2,…,Bk) specifies that {B1,B2,…,Bk} is a candidate key for the table There can be more than one UNIQUE constraint but only one PRIMARY KEY constraint for a table.
  • 9. Prof P Sreenivasa Kumar Department of CS&E, IITM 9 Specifying Referential Integrity Constraints FOREIGN KEY (A1) REFERENCES r2 (B1) specifies that attribute A1 of the table being defined, say r1, is a foreign key referring to attribute B1 of table r2 recall that this means: each value of column A1 is either null or is one of the values appearing in column B1 of r2
  • 10. Prof P Sreenivasa Kumar Department of CS&E, IITM 10 Specifying What to Do if RIC Violation Occurs RIC violation can occur if a referenced tuple is deleted or modified action can be specified for each case using qualifiers ON DELETE or ON UPDATE Actions three possibilities can be specified SET NULL, SET DEFAULT, CASCADE these are actions to be taken on the referencing tuple SET NULL – foreign key attribute value to be set null SET DEFAULT – foreign key attribute value to be set to its default value CASCADE – delete the referencing tuple if the referenced tuple is deleted or update the FK attribute if the referenced tuple is updated
  • 11. Prof P Sreenivasa Kumar Department of CS&E, IITM 11 Table Definition Example create table students ( rollNo char(8) not null, name varchar(15) not null, degree char(5), year smallint, sex char not null, deptNo smallint, advisor char(6), primary key(rollNo), foreign key(deptNo) references department(deptId) on delete set null on update cascade, foreign key(advisor) references professor(empId) on delete set null on update cascade );
  • 12. Prof P Sreenivasa Kumar Department of CS&E, IITM 12 Modifying a Defined Schema ALTER TABLE command can be used to modify a schema Adding a new attribute ALTER table student ADD address varchar(30); Deleting an attribute need to specify what needs to be done about views or constraints that refer to the attribute being dropped two possibilities CASCADE – delete the views/constraints also RESTRICT – do not delete the attributes if there are some views/constraints that refer to it. ALTER TABLE student DROP degree RESTRICT Similarly, an entire table definition can be deleted
  • 13. Prof P Sreenivasa Kumar Department of CS&E, IITM 13 Data Manipulation in SQL Basic query syntax select A1,A2,…,Am a set of attributes from relations R1,…,Rp that are from R1,R2,…,Rp required in the output table. the set of tables that where θ contain the relevant tuples to answer the query. a boolean predicate that specifies when a combined tuple of R1,…,Rp contributes to the output. Equivalent to: Assuming that each attribute name appears exactly once in the table. 1 2 nA ,A ,....A θ 1 2 pπ (σ (R ×R ×.....×R ))
  • 14. Prof P Sreenivasa Kumar Department of CS&E, IITM 14 Meaning of the Basic Query Block The cross product M of the tables in the from clause would be considered. Tuples in M that satisfy the condition θ are selected. For each such tuple, values for the attributes A1,A2,….,Am ( mentioned in the select clause) are projected. This is a conceptual description - in practice more efficient methods are employed for evaluation. The word select in SQL should not be confused with select operation of relational algebra.
  • 15. Prof P Sreenivasa Kumar Department of CS&E, IITM 15 SQL Query Result The result of any SQL query a table with select clause attributes as column names. duplicate rows may be present. - differs from the definition of a relation. duplicate rows can be eliminated by specifying DISTINCT keyword in the select clause, if necessary. SELECT DISTINCT name FROM student duplicate rows are essential while computing aggregate functions ( average, sum etc ). removing duplicate rows involves additional effort and is done only when necessary.
  • 16. Prof P Sreenivasa Kumar Department of CS&E, IITM 16 Example Relational Scheme student (rollNo, name, degree, year, sex, deptNo, advisor) department (deptId, name, hod, phone) professor (empId, name, sex, startYear, deptNo, phone) course (courseId, cname, credits, deptNo) enrollment (rollNo, courseId, sem, year, grade) teaching (empId, courseId, sem, year, classRoom) preReq (preCourseId, courseId)
  • 17. Prof P Sreenivasa Kumar Department of CS&E, IITM 17 Example Relational Scheme with RIC’s shown student (rollNo, name, degree, year, sex, deptNo, advisor) department (deptId, name, hod, phone) professor (empId, name, sex, startYear, deptNo, phone) course (courseId, cname, credits, deptNo) enrollment (rollNo, courseId, sem, year, grade) teaching (empId, courseId, sem, year, classRoom) preRequisite (preReqCourse, courseID)
  • 18. Prof P Sreenivasa Kumar Department of CS&E, IITM 18 Example Queries Involving a Single Table Get the rollNo, name of all women students in the dept no. 5. select rollNo, name from student where sex = ‘F’ and deptNo = ‘5’; Get the employee Id, name and phone number of professors in the CS dept (deptNo = 3) who have joined after 1999. select empId, name, phone from professor where deptNo = 3 and startYear > 1999;
  • 19. Prof P Sreenivasa Kumar Department of CS&E, IITM 19 Examples Involving Two or More Relations (1/2) Get the rollNo, name of students in the CSE dept (deptNo = 3)along with their advisor’s name and phone number. select rollNo, s.name, f.name as advisorName, phone as advisorPhone from student as s, professor as f where s.advisor = f.empId and s.deptNo = ‘3’; attribute renaming in the output table aliases are used to disambiguate the common attributes table aliases are required if an attribute name appears in more than one table. Also when same relation appears twice in the from clause.
  • 20. Prof P Sreenivasa Kumar Department of CS&E, IITM 20 Get the names, employee ID’s, phone numbers of professors in CSE dept who joined before 1995. select empId, f.name, phone from professor as f, department as d where f.deptNo = d.deptId and d.name = ‘CSE’ and f.startYear < 1995 Examples Involving Two or More Relations (2/2)
  • 21. Prof P Sreenivasa Kumar Department of CS&E, IITM 21 Nested Queries or Subqueries While dealing with certain complex queries beneficial to specify part of the computation as a separate query & make use of its result to formulate the main query. such queries – nested / subqueries. Using subqueries makes the main query easy to understand / formulate sometimes makes it more efficient also • sub query result can be computed once and used many times. • not the case with all subqueries.
  • 22. Prof P Sreenivasa Kumar Department of CS&E, IITM 22 Nested Query Example Get the rollNo, name of students who have a lady professor as their advisor. select s.rollNo, s.name from student s where s.advisor IN (select empId from professor where sex = ‘F’); NOT IN can be used in the above query to get details of students who don’t have a lady professor as their advisor. IN Operator: One of the ways of making use of the subquery result Subquery computes the empId’s of lady professors
  • 23. Prof P Sreenivasa Kumar Department of CS&E, IITM 23 Set Comparison Operators SQL supports several operators to deal with subquery results or in general with collection of tuples. Combination of { =, <, ≤, ≥, >, < > } with keywords { ANY, ALL } can be used as set comparison operators. Get the empId, name of the senior-most Professor(s): select p.empId, p.name from professors p where p.startYear <= ALL ( select distinct startYear from professor );
  • 24. Prof P Sreenivasa Kumar Department of CS&E, IITM 24 Semantics of Set Comparison Operators v op ANY S true if for some member x of S, v op x is true false if for no member x of S, v op x is true v op ALL S true if for every member x of S, v op x is true false if for some member x of S, v op x is not true IN is equivalent to = ANY NOT IN is equivalent to < > ALL v is normally a single attribute, but while using IN or NOT IN it can be a tuple of attributes op is one of <, ≤, >, ≥, =, < > S is a subquery
  • 25. Prof P Sreenivasa Kumar Department of CS&E, IITM 25 Correlated Nested Queries If the nested query result is independent of the current tuple being examined in the outer query, nested query is called uncorrelated, otherwise, nested query is called correlated. Uncorrelated nested query nested query needs to be computed only once. Correlated nested query nested query needs to be re-computed for each row examined in the outer query.
  • 26. Prof P Sreenivasa Kumar Department of CS&E, IITM 26 Example of a Correlated Subquery Get the roll number and name of students whose gender is same as their advisor’s. select s.rollNo, s.name from student s where s.sex = ALL ( select f.sex from professor f where f.empId = s.advisor );
  • 27. Prof P Sreenivasa Kumar Department of CS&E, IITM 27 EXISTS Operator Using EXISTS, we can check if a subquery result is non-empty EXISTS ( S ) is true if S has at least one tuple / member is false if S contain no tuples Get the employee Id and name of professors who advise at least one women student. select f.empId, f.name from professors f where EXISTS ( select s.rollNo from student s where s.advisor = f.empId and s.sex = ‘F’ ); SQL does not have an operator for universal quantification. a correlated subquery
  • 28. Prof P Sreenivasa Kumar Department of CS&E, IITM 28 Obtain the department Id and name of departments that do not offer any 4 credit courses. select d.deptId, d.name from department d where NOT EXISTS ( select courseId from course c where c.deptNo = d.deptId and c.credits = ‘4’ ); Queries with existentially quantified predicates can be easily specified using EXISTS operator. Queries with universally quantified predicates can only be specified after translating them to use existential quantifiers. NOT EXISTS Operator
  • 29. Prof P Sreenivasa Kumar Department of CS&E, IITM 29 Example Involving Universal Quantifier Obtain the department Id and name of departments whose courses are all 3-credit courses. Equivalently, obtain the department Id and name of departments that do not offer a single course that is not 3-credit course select d.deptNo, d.name from department d where NOT EXISTS ( select c.courseId from course c where c.deptNo = d.deptId and c.credits ≠ 3);
  • 30. Prof P Sreenivasa Kumar Department of CS&E, IITM 30 Missing where Clause If the where clause in an SQL query is not specified, it is treated as the where condition is true for all tuple combinations. Essentially no filtering is done on the cross product of from clause tables. Get the name and contact phone of all Departments. select name,phone from department
  • 31. Prof P Sreenivasa Kumar Department of CS&E, IITM 31 Union, Intersection and Difference Operations In SQL, using operators UNION, INTERSECT and EXCEPT, one can perform set union, intersection and difference respectively. Results of these operators are sets – i.e duplicates are automatically removed. Operands need to be union compatible and also have same attributes in the same order.
  • 32. Prof P Sreenivasa Kumar Department of CS&E, IITM 32 Example using UNION Obtain the roll numbers of students who are currently enrolled for either CS230 or CS232 courses. (SELECT rollNo FROM enrollment WHERE courseId = ‘CS230’ and sem = odd and year = 2005 ) UNION (SELECT rollNo FROM enrollment WHERE courseId = ‘CS232’ and sem = odd and year = 2005 ); Equivalent to: (SELECT rollNo FROM enrollment WHERE (courseId = ‘CS230’ or courseID = ‘CS232’) and sem = odd and year = 2005 )
  • 33. Prof P Sreenivasa Kumar Department of CS&E, IITM 33 Obtain the roll numbers of students who are currently enrolled for both CS230 and CS232 Courses. select rollNo from enrollment where courseId = ‘CS230’ and sem = odd and year = 2005 INTERSECT select rollNo from enrollment where courseId = ‘CS232’ and sem = odd and year = 2005; Example using INTERSECTION
  • 34. Prof P Sreenivasa Kumar Department of CS&E, IITM 34 Example using EXCEPT Obtain the roll numbers of students who are currently not enrolled for CS230 course. (SELECT rollNo FROM enrollment WHERE sem = odd and year = 2005 ) EXCEPT (SELECT rollNo FROM enrollment WHERE courseId = ‘CS230’ and sem = odd and year = 2005);
  • 35. Prof P Sreenivasa Kumar Department of CS&E, IITM 35 Aggregation of Data Data analysis need for computing aggregate values for data total value, average value etc Aggregate functions in SQL five aggregate function are provided in SQL AVG, SUM, COUNT, MAX, MIN can be applied to any column of a table can be used in the select clause of SQL queries
  • 36. Prof P Sreenivasa Kumar Department of CS&E, IITM 36 Aggregate functions AVG ( [DISTINCT]A): computes the average of (distinct) values in column A SUM ( [DISTINCT]A): computes the sum of (distinct) values in column A COUNT ( [DISTINCT]A): computes the number of (distinct) values in column A or no. of tuples in result MAX (A): computes the maximum of values in column A MIN (A): computes the minimum of values in column A Optional keyword
  • 37. Prof P Sreenivasa Kumar Department of CS&E, IITM 37 Examples involving aggregate functions (1/2) Suppose data about Gate in a particular year is available in a table with schema gateMarks(regNo,name,sex,branch,city,state,marks) Obtain the number of students who have taken GATE in CS and their average marks Select count(regNo) as CsTotal avg(marks) as CsAvg from gateMarks where branch = ‘CS’ Get the maximum, minimum and average marks obtained by Students from the city of Hyderabad Select max(marks), min(marks), avg(marks) from gateMarks where city = ‘Hyderabad’; CStotal CSavg Output
  • 38. Prof P Sreenivasa Kumar Department of CS&E, IITM 38 Examples involving aggregate functions (2/2) Get the names of students who obtained the maximum marks in the branch of EC Select name, max(marks) from gateMarks where branch = ‘EC’ Will not work Only aggregate functions can be specified here. It does not make sense to include normal attributes ! (unless they are grouping attributes – to be seen later) Select regNo, name, marks from gateMarks where branch = ‘EC’ and marks = (select max(marks) from gateMarks where branch = ‘EC’); Correct way of specifying the query
  • 39. Prof P Sreenivasa Kumar Department of CS&E, IITM 39 Date Aggregation and Grouping Grouping Partition the set of tuples in a relation into groups based on certain criteria and compute aggregate functions for each group All tuples that agree on a set of attributes (i.e have the same value for each of these attributes ) are put into a group The specified aggregate functions are computed for each group Each group contributes one tuple to the output All the grouping attributes must also appear in the select clause the result tuple of the group is listed along with the values of the grouping attributes of the group Called the grouping attributes
  • 40. Prof P Sreenivasa Kumar Department of CS&E, IITM 40 Examples involving grouping(1/2) Determine the maximum of the GATE CS marks obtained by students in each city, for all cities. Select city, max(marks) as maxMarks from gateMarks where branch = ‘CS’ group by city; Grouping attributes must appear in the select clause Grouping attribute Result: City maxMarks Hyderabad Chennai Mysore Bangalore 87 84 90 82
  • 41. Prof P Sreenivasa Kumar Department of CS&E, IITM 41 In the University database, for each department, obtain the name, deptId and the total number of four credit courses offered by the department Select deptId, name, count(*) as totalCourses from department, course where deptId = deptNo and credits = 4 group by deptId, name; Examples involving grouping(2/2)
  • 42. Prof P Sreenivasa Kumar Department of CS&E, IITM 42 Having clause After performing grouping, is it possible to report information about only a subset of the groups ? Yes, with the help of having clause which is always used in conjunction with Group By clause Report the total enrollment in each course in the 2nd semester of 2004; include only the courses with a minimum enrollment of 10. Select courseId, count(rollNo) as Enrollment from enrollment where sem = 2 and year = 2004 group by courseId having count(rollNo) ≥ 10;
  • 43. Prof P Sreenivasa Kumar Department of CS&E, IITM 43 Where clause versus Having clause • Where clause • Performs tests on rows and eliminates rows not satisfying the specified condition • Performed before any grouping of rows is done • Having clause • Always performed after grouping • Performs tests on groups and eliminates groups not satisfying the specified condition • Tests can only involve grouping attributes and aggregate functions Select courseId, count(rollNo) as Enrollment from enrollment where sem = 2 and year = 2004 group by courseId having count(rollNo) ≥ 10;
  • 44. Prof P Sreenivasa Kumar Department of CS&E, IITM 44 String Operators in SQL Specify strings by enclosing them in single quotes e.g., ‘Chennai’ Common operations on strings – • pattern matching – using ‘LIKE’ comparison operator Specify patterns using special characters – • character ‘%’ (percent) matches any Substring e.g., ‘Jam%’ matches any string starting with “Jam” • character ‘_’ (underscore) matches any single character e.g., (a) ‘_ _ press’ matches with any string ending with “press”, with any two characters before that. (b) ‘_ _ _ _’ matches any string with exactly four characters
  • 45. Prof P Sreenivasa Kumar Department of CS&E, IITM 45 Using the ‘LIKE’ operator Obtain roll numbers and names of all students whose names end with ‘Mohan’ Select rollNo, name from student where name like ‘%Mohan’; Patterns are case sensitive. Special characters (percent, underscore) can be included in patterns using an escape character ‘’ (backslash)
  • 46. Prof P Sreenivasa Kumar Department of CS&E, IITM 46 Join Operation In SQL, usually joining of tuples from different relations is specified in ‘where’ clause Get the names of professors working in CSE dept. Select f.name from professor as f, department as d where f.deptNo = d.deptId and d.name = ‘CSE’; The above query specifies joining of professor and department relations on condition f.deptNo = d.deptId and d.name = ‘CSE’
  • 47. Prof P Sreenivasa Kumar Department of CS&E, IITM 47 Explicit Specification of Joining in ‘From’ Clause select f.name from (professor as f join department as d on f.deptNo = d.deptId) where d.name = ‘CSE’; Join types: 1. inner join (default): from (r1 inner join r2 on <predicate>) use of just ‘join’ is equivalent to ‘inner join’ 2. left outer join: from (r1 left outer join r2 on <predicate>) 3. right outer join: from (r1 right outer join r2 on <predicate>) 4. full outer join: from (r1 full outer join r2 on <predicate>)
  • 48. Prof P Sreenivasa Kumar Department of CS&E, IITM 48 Natural join The adjective ‘natural’ can be used with any of the join types to specify natural join. FROM (r1 NATURAL <join type> r2 [USING <attr. list>]) • natural join by default considers all common attributes • a subset of common attributes can be specified in an optional using <attr. list> phrase REMARKS • Specifying join operation explicitly goes against the spirit of declarative style of query specification • But the queries may be easier to understand • The feature is to be used judiciously
  • 49. Prof P Sreenivasa Kumar Department of CS&E, IITM 49 Views Views provide virtual relations which contain data spread across different tables. Used by applications. • simplified query formulations • data hiding • logical data independence Once created, a view is always kept up-to-date by the RDBMS View is not part of conceptual schema • created to give a user group, concerned with a certain aspect of the information system, their view of the system Storage • Views need not be stored as permanent tables • They can be created on-the-fly whenever needed • They can also be materialized Tables involved in the view definition – base tables
  • 50. Prof P Sreenivasa Kumar Department of CS&E, IITM 50 Creating Views CREATE VIEW v AS <query expr> creates a view ‘v’, with structure and data defined by the outcome of the query expression Create a view which contains name, employee Id and phone number of professors who joined CSE dept in or after the year 2000. create view profAft2K as (Select f.name, empId, phone from professor as f, department as d where f.depNo = d.deptId and d.name = ‘CSE’ and f.startYear >= 2000); If the details of a new CSE professor are entered into professor table, the above view gets updated automatically name of the view
  • 51. Prof P Sreenivasa Kumar Department of CS&E, IITM 51 Queries on Views Once created a view can be used in queries just like any other table. e.g. Obtain names of professors in CSE dept, who joined after 2000 and whose name starts with ‘Ram’ select name from profAft2K where name like ‘Ram%’; The definition of the view is stored in DBMS, and executed to create the temporary table (view), when encountered in query
  • 52. Prof P Sreenivasa Kumar Department of CS&E, IITM 52 Operations on Views Querying is allowed Update operations are usually restricted because – updates on a view may modify many base tables – there may not be a unique way of updating the base tables to reflect the update on view – view may contain some aggregate values – ambiguity where primary key of a base table is not included in view definition.
  • 53. Prof P Sreenivasa Kumar Department of CS&E, IITM 53 Restrictions on Updating Views Updates on views defined on joining of more than one table are not allowed For example, updates on the following view are not allowed create a view Professor_Dept with professor ID, department Name and department phone create view profDept(profId,DeptName,DPhone) as (select f.empId, d.name, d.phone from professor f, department d where f.depNo = d.depId);
  • 54. Prof P Sreenivasa Kumar Department of CS&E, IITM 54 Updates on views defined with ‘group by’ clause and aggregate functions is not permitted, as a tuple in view will not have a corresponding tuple in base relation. For example, updates on the following view are not allowed Create a view deptNumCourses which contains the number of courses offered by a dept. create view deptNumCourses(deptNo,numCourses) as select deptNo, count(*) from course group by deptNo; Restrictions on Updating Views
  • 55. Prof P Sreenivasa Kumar Department of CS&E, IITM 55 Updates on views which do not include Primary Key of base table, are also not permitted For example, updates on the following view are not allowed Create a view StudentPhone with Student name and phone number. create view StudentPhone (sname,sphone) as (select name, phone from student); View StudentPhone does not include Primary key of the base table. Restrictions on Updating Views
  • 56. Prof P Sreenivasa Kumar Department of CS&E, IITM 56 Updates to views are allowed only if defined on single base table not defined using ‘group by’ clause and aggregate functions include Primary Key of base table Allowed Updates on Views
  • 57. Prof P Sreenivasa Kumar Department of CS&E, IITM 57 Inserting data into a table Specify a tuple(or tuples) to be inserted INSERT INTO student VALUES (‘CSO5D014’,‘Mohan’,‘PhD’,2005,‘M’,3,‘FCS008’), (‘CSO5S031’,‘Madhav’,‘MS’,2005,‘M’,4,‘FCE009’); Specify the result of query to be inserted INSERT INTO r1 SELECT … FROM … WHERE … Specify that a sub-tuple be inserted INSERT INTO student(rollNo, name, sex) VALUES (CS05M022, ‘Rajasri’, ‘F’), (CS05B033, ‘Kalyan’, ‘M’); the attributes that can be NULL or have declared default values can be left-out to be updated later
  • 58. Prof P Sreenivasa Kumar Department of CS&E, IITM 58 Deleting rows from a table Deletion of tuples is possible ; deleting only part of a tuple is not possible Deletion of tuples can be done only from one relation at a time Deleting a tuple might trigger further deletions due to referentially triggered actions specified as part of RIC’s Generic form: delete from r where <predicate>; Delete tuples from professor relation with start year as 1982. delete from professor where startYear = 1982; If ‘where’ clause is not specified, then all the tuples of that relation are deleted ( Be careful !)
  • 59. Prof P Sreenivasa Kumar Department of CS&E, IITM 59 A Remark on Deletion The where predicate is evaluated for each of the tuples in the relation to mark them as qualified for deletion before any tuple is actually deleted from the relation Note that the result may be different if tuples are deleted as and when we find that they satisfy the where condition! An example: Delete all tuples of students that scored the least marks in the CS branch: DELETE FROM gateMarks WHERE branch = “CS” and marks = ANY ( SELECT MIN(marks) FROM gateMarks WHERE branch = “CS”)
  • 60. Prof P Sreenivasa Kumar Department of CS&E, IITM 60 Updating tuples in a relation update r set <<attr = newValue> list> where <predicates>; Change phone number of all professors working in CSE dept to “94445 22605” update professors set phone = ‘9444422605’ where deptNo = (select deptId from department where name = ‘CSE’); If ‘where’ clause is not specified, values for the specified attributes in all tuples is changed.
  • 61. Prof P Sreenivasa Kumar Department of CS&E, IITM 61 Miscellaneous features in SQL (1/3) Ordering of result tuples can be done using ‘order by’ clause e.g., List the names of professors who joined after 1980, in alphabetic order. select name from professor where startYear > 1980 order by name; Use of ‘null’ to test for a null value, if the attribute can take null e.g., Obtain roll numbers of students who don’t have phone numbers select rollNo from student where phoneNumber is null;
  • 62. Prof P Sreenivasa Kumar Department of CS&E, IITM 62 Miscellaneous features in SQL (2/3) Use of ‘between and’ to test the range of a value e.g., Obtain names of professors who have joined between 1980 and 1990 select name from professor where startYear between 1980 and 1990; Change the column name in result relation e.g., select name as studentName, rollNo as studentNo from student;
  • 63. Prof P Sreenivasa Kumar Department of CS&E, IITM 63 Miscellaneous features in SQL (3/3) Use of ‘distinct’ key word in ‘select’ clause to determine duplicate tuples in result. Obtain all distinct branches of study for students select distinct d.name from student as s, department as d where s.deptNo = d.deptId; Use of asterisk (*) to retrieve all the attribute values of selected tuples. Obtain details of professors along with their department details. select * from professor as f, department as d where f.deptNo = d.deptId;
  • 64. Prof P Sreenivasa Kumar Department of CS&E, IITM 64 Application Development Process Host language (HL) – the high-level programming language in which the application is developed (e.g., C, C++, Java etc.) Database access – using embedded SQL is one approach • SQL statements are interspersed in HL program. Data transfer – takes place through specially declared HL variables Mismatch between HL data types and SQL data types • SQL 92 standard specifies the corresponding SQL types for many HLs.
  • 65. Prof P Sreenivasa Kumar Department of CS&E, IITM 65 Declaring Variables Variables that need to be used in SQL statements are declared in a special section as follows: EXEC SQL BEGIN DECLARE SECTION char rollNo[9]; // HL is C language char studName[20], degree[6]; int year; char sex; int deptNo; char advisor[9]; EXEC SQL END DECLARE SECTION Note that schema for student relation is student(rollNo, name, degree, year, sex, deptNo, advisor) Use in SQL statements: variable name is prefixed with a colon(:) e.g., :ROLLNO in an SQL statement refers to rollNo variable
  • 66. Prof P Sreenivasa Kumar Department of CS&E, IITM 66 Handling Error Conditions The HL program needs to know if an SQL statement has executed successfully or otherwise Special variable called SQLSTATE is used for this purpose SQLSTATE is set to appropriate value by the RDBMS run-time after executing each SQL statement non-zero values indicate errors in execution • different values indicate different types of error situations SQLSTATE variable must be declared in the HL program and HL program needs to check for error situations and handle them appropriately.
  • 67. Prof P Sreenivasa Kumar Department of CS&E, IITM 67 Embedding SQL statements Suppose we collect data through user interface into variables rollNo, studName, degree, year, sex, deptNo, advisor A row in student table can be inserted – EXEC SQL INSERT INTO STUDENT VALUES (:rollNo,:studName,:degree, :year,:sex,:deptNo,:advisor);
  • 68. Prof P Sreenivasa Kumar Department of CS&E, IITM 68 Impedance mismatch and cursors Occurs because, HL languages do not support set-of-records as supported by SQL A‘cursor’ is a mechanism which allows us to retrieve one row at a time from the result of a query We can declare a cursor on any SQL query Once declared, we use open, fetch, move and close commands to work with cursors We usually need a cursor when embedded statement is a SELECT query INSERT, DELETE and UPDATE don’t need a cursor.
  • 69. Prof P Sreenivasa Kumar Department of CS&E, IITM 69 We don’t need a cursor if the query results in a single row. e.g., EXEC SQL SELECT s.name, s.sex INTO :name, :sex FROM student s WHERE s.rollNo = :rollNo; Result row values name and phone are assigned to HL variables :name and :phone, using ‘INTO’ clause Cursor is not required as the result always contains only one row ( rollNo is a key for student relation) Embedded SQL (1/2)
  • 70. Prof P Sreenivasa Kumar Department of CS&E, IITM 70 If the result contains more than one row, cursor declaration is needed e.g., select s.name, s.degree from student s where s.sex = ‘F’; Query results in a collection of rows HL program has to deal with set of records. The use of ‘INTO’ will not work here We can solve this problem by using a ‘cursor’. Embedded SQL (2/2)
  • 71. Prof P Sreenivasa Kumar Department of CS&E, IITM 71 Declaring a cursor on a query declare studInfo cursor for select name, degree from student where sex = ‘F’; Command OPEN studInfo; opens the cursor and makes it point to first record To read current row of values into HL variables, we use the command FETCH studInfo INTO :name, :degree; After executing FETCH statement cursor is pointed to next row by default Cursor movement can be optionally controlled by the programmer After reading all records we close the cursor using the CLOSE studInfo command. Cursor name
  • 72. Prof P Sreenivasa Kumar Department of CS&E, IITM 72 Dynamic SQL Useful for applications to generate and run SQL statements, based on user inputs Queries may not be known in advance e.g., char sqlstring [ ] = {“select * from student”}; EXEC SQL PREPARE runQ FROM sqlstring; EXEC SQL EXECUTE runQ; ‘Sqlstring’ is a ‘C’ variable that holds user submitted query ‘runQ’ is an SQL variable that holds the SQL statements.
  • 73. Prof P Sreenivasa Kumar Department of CS&E, IITM 73 Connecting to Database from HL ODBC (Open Database Connectivity) and JDBC (Java Database Connectivity) accessing database and data is through an API many DBMSs can be accessed no restriction on number of connections appropriate drivers are required steps in accessing data from a HL program • select the data source • load the appropriate driver dynamically • establish the connection • work with database • close the connection.