Beruflich Dokumente
Kultur Dokumente
Jan-Apr, 2018
ppd@cse.iitkgp.ernet.in
Srijoni Majumdar
Himadri B G S Bhuyan
Gurunath Reddy M
Week 01 Recap
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 06.2 ©Silberschatz, Korth and Sudarshan
PPD
Module Objectives
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 06.3 ©Silberschatz, Korth and Sudarshan
PPD
Module Outline
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
History of SQL
Data Definition Language (DDL)
Basic Query Structure (DML)
Database System Concepts - 6th Edition 06.4 ©Silberschatz, Korth and Sudarshan
PPD
History of SQL
Data Definition
Language (DDL)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
HISTORY OF SQL
Database System Concepts - 6th Edition 06.5 ©Silberschatz, Korth and Sudarshan
History of Query Language
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
IBM Sequel language developed as part of System R project at the IBM San Jose Research
Laboratory
Renamed Structured Query Language (SQL)
ANSI and ISO standard SQL:
SQL-86
SQL-89
SQL-92
SQL:1999 (language name became Y2K compliant!)
SQL:2003
Commercial systems offer most, if not all, SQL-92 features, plus varying feature sets from later
standards and special proprietary features.
Not all examples here may work on your particular system
Database System Concepts - 6th Edition 06.6 ©Silberschatz, Korth and Sudarshan
PPD
History of SQL
Data Definition
Language (DDL)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 06.7 ©Silberschatz, Korth and Sudarshan
Data Definition Language
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 06.8 ©Silberschatz, Korth and Sudarshan
Domain Types in SQL
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 06.9 ©Silberschatz, Korth and Sudarshan
PPD
Database System Concepts - 6th Edition 06.10 ©Silberschatz, Korth and Sudarshan
PPD
Example:
create table instructor (
ID char(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2))
Database System Concepts - 6th Edition 06.11 ©Silberschatz, Korth and Sudarshan
PPD
not null
primary key (A1, ..., An )
foreign key (Am, ..., An ) references r
Example:
Database System Concepts - 6th Edition 06.12 ©Silberschatz, Korth and Sudarshan
PPD
Database System Concepts - 6th Edition 06.13 ©Silberschatz, Korth and Sudarshan
PPD
Database System Concepts - 6th Edition 06.14 ©Silberschatz, Korth and Sudarshan
Updates to tables
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Insert
insert into instructor values (‘10211’, ‘Smith’, ‘Biology’, 66000);
Delete
Remove all tuples from the student relation
delete from student
Drop Table
drop table r
Alter
alter table r add A D
where A is the name of the attribute to be added to relation
r and D is the domain of A
All exiting tuples in the relation are assigned null as the
value for the new attribute
alter table r drop A
where A is the name of an attribute of relation r
Dropping of attributes not supported by many databases
Database System Concepts - 6th Edition 06.15 ©Silberschatz, Korth and Sudarshan
PPD
History of SQL
Data Definition
Language (DDL)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 06.16 ©Silberschatz, Korth and Sudarshan
Basic Query Structure
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Ai represents an attribute
Ri represents a relation
P is a predicate
The result of an SQL query is a relation
Database System Concepts - 6th Edition 06.17 ©Silberschatz, Korth and Sudarshan
The select Clause
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
The select clause lists the attributes desired in the result of a query
corresponds to the projection operation of the relational algebra
Example: find the names of all instructors:
select name
from instructor
NOTE: SQL names are case insensitive (i.e., you may use upper- or
lower-case letters.)
E.g., Name ≡ NAME ≡ name
Some people use upper case wherever we use bold font
Database System Concepts - 6th Edition 06.18 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 06.19 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 06.20 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 06.21 ©Silberschatz, Korth and Sudarshan
The where Clause
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
The where clause specifies conditions that the result must satisfy
Corresponds to the selection predicate of the relational algebra
To find all instructors in Comp. Sci. dept
select name
from instructor
where dept_name = ‘Comp. Sci.'
Comparison results can be combined using the logical connectives
and, or, and not
To find all instructors in Comp. Sci. dept with salary > 80000
select name
from instructor
where dept_name = ‘Comp. Sci.' and salary > 80000
Database System Concepts - 6th Edition 06.22 ©Silberschatz, Korth and Sudarshan
The from Clause
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 06.23 ©Silberschatz, Korth and Sudarshan
Cartesian Product
instructor teaches
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 06.24 ©Silberschatz, Korth and Sudarshan
Module Summary
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 06.25 ©Silberschatz, Korth and Sudarshan
PPD
Database System Concepts - 6th Edition 06.26 ©Silberschatz, Korth and Sudarshan
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
ppd@cse.iitkgp.ernet.in
Srijoni Majumdar
Himadri B G S Bhuyan
Gurunath Reddy M
Module Recap
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
History of SQL
Data Definition Language (DDL)
Basic Query Structure (DML)
Database System Concepts - 6th Edition 07.2 ©Silberschatz, Korth and Sudarshan
PPD
Module Objectives
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.3 ©Silberschatz, Korth and Sudarshan
PPD
Module Outline
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.4 ©Silberschatz, Korth and Sudarshan
PPD
Additional Basic
Operations
Set Operations
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Null Values
Aggregate Functions
Database System Concepts - 6th Edition 07.5 ©Silberschatz, Korth and Sudarshan
Cartesian Product
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.6 ©Silberschatz, Korth and Sudarshan
Cartesian Product
instructor teaches
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.7 ©Silberschatz, Korth and Sudarshan
PPD
Examples
Find the names of all instructors who have taught some course and the
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
course_id
select name, course_id
from instructor, teaches
where instructor.ID = teaches.ID
Equi-Join, Natural Join
Database System Concepts - 6th Edition 07.8 ©Silberschatz, Korth and Sudarshan
PPD
Examples
Find the names of all instructors in the Art department who have taught
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.9 ©Silberschatz, Korth and Sudarshan
The Rename Operation
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
The SQL allows renaming relations and attributes using the as clause:
old-name as new-name
Find the names of all instructors who have a higher salary than
some instructor in ‘Comp. Sci’.
select distinct T.name
from instructor as T, instructor as S
where T.salary > S.salary and S.dept_name = ‘Comp. Sci.’
Database System Concepts - 6th Edition 07.10 ©Silberschatz, Korth and Sudarshan
Cartesian Product Example*
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Relation emp-super
person supervisor
Bob Alice
Mary Susan
Alice David
David Mary
Database System Concepts - 6th Edition 07.11 ©Silberschatz, Korth and Sudarshan
String Operations
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.12 ©Silberschatz, Korth and Sudarshan
String Operations (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.13 ©Silberschatz, Korth and Sudarshan
Ordering the Display of Tuples
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.14 ©Silberschatz, Korth and Sudarshan
Where Clause Predicates
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.15 ©Silberschatz, Korth and Sudarshan
Duplicates*
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.16 ©Silberschatz, Korth and Sudarshan
Duplicates (Cont.)*
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
A ,A ,,A ( P (r1 r2 rm ))
1 2 n
Database System Concepts - 6th Edition 07.17 ©Silberschatz, Korth and Sudarshan
PPD
Additional Basic
Operations
Set Operations
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Null Values
Aggregate Functions
SET OPERATIONS
Database System Concepts - 6th Edition 07.18 ©Silberschatz, Korth and Sudarshan
Set Operations
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Find courses that ran in Fall 2009 but not in Spring 2010
(select course_id from section where sem = ‘Fall’ and year = 2009)
except
(select course_id from section where sem = ‘Spring’ and year = 2010)
Database System Concepts - 6th Edition 07.19 ©Silberschatz, Korth and Sudarshan
Set Operations (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Find the salaries of all instructors that are less than the largest salary
select distinct T.salary
from instructor as T, instructor as S
where T.salary < S.salary
Database System Concepts - 6th Edition 07.20 ©Silberschatz, Korth and Sudarshan
Set Operations (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.21 ©Silberschatz, Korth and Sudarshan
PPD
Additional Basic
Operations
Set Operations
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Null Values
Aggregate Functions
NULL VALUES
Database System Concepts - 6th Edition 07.22 ©Silberschatz, Korth and Sudarshan
Null Values
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.23 ©Silberschatz, Korth and Sudarshan
Null Values and Three Valued Logic
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.24 ©Silberschatz, Korth and Sudarshan
PPD
Additional Basic
Operations
Set Operations
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Null Values
Aggregate Functions
AGGREGATE FUNCTIONS
Database System Concepts - 6th Edition 07.25 ©Silberschatz, Korth and Sudarshan
Aggregate Functions
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.26 ©Silberschatz, Korth and Sudarshan
Aggregate Functions (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.27 ©Silberschatz, Korth and Sudarshan
Aggregate Functions – Group By
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
avg_salary
Database System Concepts - 6th Edition 07.28 ©Silberschatz, Korth and Sudarshan
Aggregation (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Attributes in select clause outside of aggregate functions must appear in group by list
/* erroneous query */
select dept_name, ID, avg (salary)
from instructor
group by dept_name;
Database System Concepts - 6th Edition 07.29 ©Silberschatz, Korth and Sudarshan
Aggregate Functions – Having Clause
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.30 ©Silberschatz, Korth and Sudarshan
Null Values and Aggregates
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.31 ©Silberschatz, Korth and Sudarshan
Module Summary
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 07.32 ©Silberschatz, Korth and Sudarshan
PPD
Database System Concepts - 6th Edition 07.33 ©Silberschatz, Korth and Sudarshan
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
ppd@cse.iitkgp.ernet.in
Srijoni Majumdar
Himadri B G S Bhuyan
Gurunath Reddy M
Module Recap
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.2 ©Silberschatz, Korth and Sudarshan
PPD
Module Objectives
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.3 ©Silberschatz, Korth and Sudarshan
PPD
Module Outline
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Nested Subqueries
Modification of the Database
Database System Concepts - 6th Edition 08.4 ©Silberschatz, Korth and Sudarshan
PPD
Nested Subqueries
Modification of the
Database
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
NESTED SUB-QUERIES
Database System Concepts - 6th Edition 08.5 ©Silberschatz, Korth and Sudarshan
Nested Subqueries
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
as follows:
Ai can be replaced be a subquery that generates a single value
ri can be replaced by any valid subquery
P can be replaced with an expression of the form:
B <operation> (subquery)
where B is an attribute and <operation> to be defined later
Database System Concepts - 6th Edition 08.6 ©Silberschatz, Korth and Sudarshan
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.7 ©Silberschatz, Korth and Sudarshan
Subqueries in the Where Clause
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.8 ©Silberschatz, Korth and Sudarshan
Set Membership
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.9 ©Silberschatz, Korth and Sudarshan
Set Membership (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Find the total number of (distinct) students who have taken course
sections taught by the instructor with ID 10101
Database System Concepts - 6th Edition 08.10 ©Silberschatz, Korth and Sudarshan
Set Comparison – “some” Clause
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Find names of instructors with salary greater than that of some (at
least one) instructor in the Biology department
select name
from instructor
where salary > some (select salary
from instructor
where dept name = ’Biology’);
Database System Concepts - 6th Edition 08.11 ©Silberschatz, Korth and Sudarshan
Definition of “some” Clause*
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
0
(5 < some 5 ) = true
(read: 5 < some tuple in the relation)
6
0
(5 < some 5 ) = false
0
(5 = some 5 ) = true
0
(5 some 5 ) = true (since 0 5)
(= some) in
However, ( some) not in
Database System Concepts - 6th Edition 08.12 ©Silberschatz, Korth and Sudarshan
Set Comparison – “all” Clause
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Find the names of all instructors whose salary is greater than the
salary of all instructors in the Biology department
select name
from instructor
where salary > all (select salary
from instructor
where dept name = ’Biology’);
Database System Concepts - 6th Edition 08.13 ©Silberschatz, Korth and Sudarshan
Definition of “all” Clause*
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
0
(5 < all 5 ) = false
6
6
(5 < all 10 ) = true
4
(5 = all 5 ) = false
4
(5 all 6 ) = true (since 5 4 and 5 6)
( all) not in
However, (= all) in
Database System Concepts - 6th Edition 08.14 ©Silberschatz, Korth and Sudarshan
Test for Empty Relations
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.15 ©Silberschatz, Korth and Sudarshan
Use of “exists” Clause
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Yet another way of specifying the query “Find all courses taught in both the Fall 2009 semester
and in the Spring 2010 semester”
select course_id
from section as S
where semester = ’Fall’ and year = 2009 and
exists (select *
from section as T
where semester = ’Spring’ and year= 2010
and S.course_id = T.course_id);
Database System Concepts - 6th Edition 08.16 ©Silberschatz, Korth and Sudarshan
Use of “not exists” Clause
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Find all students who have taken all courses offered in the Biology
department.
Note: X – Y = Ø X Y
Note: Cannot write this query using = all and its variants
Database System Concepts - 6th Edition 08.17 ©Silberschatz, Korth and Sudarshan
Test for Absence of Duplicate Tuples
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.18 ©Silberschatz, Korth and Sudarshan
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.19 ©Silberschatz, Korth and Sudarshan
Subqueries in the From Clause
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.20 ©Silberschatz, Korth and Sudarshan
With Clause
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.21 ©Silberschatz, Korth and Sudarshan
Complex Queries using With Clause*
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Find all departments where the total salary is greater than the
average of the total salary at all departments
Database System Concepts - 6th Edition 08.22 ©Silberschatz, Korth and Sudarshan
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.23 ©Silberschatz, Korth and Sudarshan
Scalar Subquery
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.24 ©Silberschatz, Korth and Sudarshan
PPD
Nested Subqueries
Modification of the
Database
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.25 ©Silberschatz, Korth and Sudarshan
Modification of the Database
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.26 ©Silberschatz, Korth and Sudarshan
Deletion
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.27 ©Silberschatz, Korth and Sudarshan
Deletion (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Delete all instructors whose salary is less than the average salary of
instructors
Database System Concepts - 6th Edition 08.28 ©Silberschatz, Korth and Sudarshan
Insertion
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
or equivalently
insert into course (course_id, title, dept_name, credits)
values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);
Database System Concepts - 6th Edition 08.29 ©Silberschatz, Korth and Sudarshan
Insertion (Cont.)
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
The select from where statement is evaluated fully before any of its
results are inserted into the relation
Otherwise queries like
insert into table1 select * from table1
would cause problem
Database System Concepts - 6th Edition 08.30 ©Silberschatz, Korth and Sudarshan
Updates
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.31 ©Silberschatz, Korth and Sudarshan
Case Statement for Conditional Updates
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.32 ©Silberschatz, Korth and Sudarshan
Updates with Scalar Subqueries
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.33 ©Silberschatz, Korth and Sudarshan
Module Summary
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 08.34 ©Silberschatz, Korth and Sudarshan
PPD
Database System Concepts - 6th Edition 08.35 ©Silberschatz, Korth and Sudarshan
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
ppd@cse.iitkgp.ernet.in
Srijoni Majumdar
Himadri B G S Bhuyan
Gurunath Reddy M
Module Recap
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Nested Subqueries
Modification of the Database
Database System Concepts - 6th Edition 09.2 ©Silberschatz, Korth and Sudarshan
PPD
Module Objectives
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.3 ©Silberschatz, Korth and Sudarshan
PPD
Module Outline
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Join Expressions
Views
Transactions
Database System Concepts - 6th Edition 09.4 ©Silberschatz, Korth and Sudarshan
PPD
Join Expressions
Views
Transactions
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
JOIN EXPRESSIONS
Database System Concepts - 6th Edition 09.5 ©Silberschatz, Korth and Sudarshan
Joined Relations
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.6 ©Silberschatz, Korth and Sudarshan
PPD
Cross join
Inner join
Equi-join
Natural join
Outer join
Left outer join
Right outer join
Full outer join
Self-join
Database System Concepts - 6th Edition 09.7 ©Silberschatz, Korth and Sudarshan
PPD
Cross Join
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
CROSS JOIN returns the Cartesian product of rows from tables in the join
Explicit
select *
from employee cross join department;
Implicit
select *
from employee, department;
Database System Concepts - 6th Edition 09.8 ©Silberschatz, Korth and Sudarshan
Join operations – Example
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Relation course
Relation prereq
Observe that
prereq information is missing for CS-315 and
course information is missing for CS-437
Database System Concepts - 6th Edition 09.9 ©Silberschatz, Korth and Sudarshan
PPD
Inner Join
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.10 ©Silberschatz, Korth and Sudarshan
Outer Join
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.11 ©Silberschatz, Korth and Sudarshan
PPD
Database System Concepts - 6th Edition 09.12 ©Silberschatz, Korth and Sudarshan
PPD
Database System Concepts - 6th Edition 09.13 ©Silberschatz, Korth and Sudarshan
Joined Relations
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.14 ©Silberschatz, Korth and Sudarshan
PPD
Database System Concepts - 6th Edition 09.15 ©Silberschatz, Korth and Sudarshan
Joined Relations – Examples
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.16 ©Silberschatz, Korth and Sudarshan
Joined Relations – Examples
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.17 ©Silberschatz, Korth and Sudarshan
PPD
Join Expressions
Views
Transactions
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
VIEWS
Database System Concepts - 6th Edition 09.18 ©Silberschatz, Korth and Sudarshan
Views
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
In some cases, it is not desirable for all users to see the entire
logical model (that is, all the actual relations stored in the
database.)
Consider a person who needs to know an instructors name
and department, but not the salary. This person should see a
relation described, in SQL, by
Database System Concepts - 6th Edition 09.19 ©Silberschatz, Korth and Sudarshan
View Definition
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.20 ©Silberschatz, Korth and Sudarshan
Example Views
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.21 ©Silberschatz, Korth and Sudarshan
Views Defined Using Other Views
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.22 ©Silberschatz, Korth and Sudarshan
View Expansion
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.23 ©Silberschatz, Korth and Sudarshan
Views Defined Using Other Views
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.24 ©Silberschatz, Korth and Sudarshan
View Expansion*
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.25 ©Silberschatz, Korth and Sudarshan
PPD
Recursive View
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.26 ©Silberschatz, Korth and Sudarshan
PPD
Database System Concepts - 6th Edition 09.27 ©Silberschatz, Korth and Sudarshan
PPD
Database System Concepts - 6th Edition 09.28 ©Silberschatz, Korth and Sudarshan
The Power of Recursion
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Recursive views make it possible to write queries, such as transitive closure queries, that
cannot be written without recursion or iteration
Intuition: Without recursion, a non-recursive non-iterative program can perform only a
fixed number of joins of flights with itself
This can give only a fixed number of levels of reachable destinations
Given a fixed non-recursive query, we can construct a database with a greater
number of levels of reachable destinations on which the query will not work
Database System Concepts - 6th Edition 09.29 ©Silberschatz, Korth and Sudarshan
The Power of Recursion
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.30 ©Silberschatz, Korth and Sudarshan
Example of Fixed-Point Computation
source destination carrier cost
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.31 ©Silberschatz, Korth and Sudarshan
Update of a View
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.32 ©Silberschatz, Korth and Sudarshan
Some Updates cannot be Translated Uniquely
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.33 ©Silberschatz, Korth and Sudarshan
And Some Not at All
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.34 ©Silberschatz, Korth and Sudarshan
Materialized Views
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Materializing a view: create a physical table containing all the tuples in the result of the query
defining the view
If relations used in the query are updated, the materialized view result becomes out of date
Need to maintain the view, by updating the view whenever the underlying relations are
updated
Database System Concepts - 6th Edition 09.35 ©Silberschatz, Korth and Sudarshan
PPD
Join Expressions
Views
Transactions
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
TRANSACTIONS
Database System Concepts - 6th Edition 09.36 ©Silberschatz, Korth and Sudarshan
Transactions
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Unit of work
Atomic transaction
either fully executed or rolled back as if it never occurred
Isolation from concurrent transactions
Transactions begin implicitly
Ended by commit work or rollback work
But default on most databases: each SQL statement commits
automatically
Can turn off auto commit for a session (e.g. using API)
In SQL:1999, can use: begin atomic …. end
Not supported on most databases
Database System Concepts - 6th Edition 09.37 ©Silberschatz, Korth and Sudarshan
Module Summary
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 09.38 ©Silberschatz, Korth and Sudarshan
PPD
Database System Concepts - 6th Edition 09.39 ©Silberschatz, Korth and Sudarshan
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
ppd@cse.iitkgp.ernet.in
Srijoni Majumdar
Himadri B G S Bhuyan
Gurunath Reddy M
Module Recap
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Join Expressions
Views
Transactions
Database System Concepts - 6th Edition 10.2 ©Silberschatz, Korth and Sudarshan
PPD
Module Objectives
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 10.3 ©Silberschatz, Korth and Sudarshan
PPD
Module Outline
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Integrity Constraints
SQL Data Types and Schemas
Authorization
Database System Concepts - 6th Edition 10.4 ©Silberschatz, Korth and Sudarshan
PPD
Integrity Constraints
SQL Data Types and
Schemas
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Authorization
INTEGRITY CONSTRAINTS
Database System Concepts - 6th Edition 10.5 ©Silberschatz, Korth and Sudarshan
Integrity Constraints
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 10.6 ©Silberschatz, Korth and Sudarshan
Integrity Constraints on a Single Relation
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
not null
primary key
unique
check (P), where P is a predicate
Database System Concepts - 6th Edition 10.7 ©Silberschatz, Korth and Sudarshan
Not Null and Unique Constraints
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
not null
Declare name and budget to be not null
name varchar(20) not null
budget numeric(12,2) not null
unique (A1, A2, …, Am)
The unique specification states that the attributes A1, A2, …
Am form a candidate key
Candidate keys are permitted to be null (in contrast to primary
keys).
Database System Concepts - 6th Edition 10.8 ©Silberschatz, Korth and Sudarshan
The check clause
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
check (P)
where P is a predicate
Database System Concepts - 6th Edition 10.10 ©Silberschatz, Korth and Sudarshan
PPD
Database System Concepts - 6th Edition 10.11 ©Silberschatz, Korth and Sudarshan
Integrity Constraint Violation During Transactions
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 10.12 ©Silberschatz, Korth and Sudarshan
PPD
Integrity Constraints
SQL Data Types and
Schemas
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Authorization
Database System Concepts - 6th Edition 10.13 ©Silberschatz, Korth and Sudarshan
Built-in Data Types in SQL
date: Dates, containing a (4 digit) year, month and date
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 10.14 ©Silberschatz, Korth and Sudarshan
Index Creation
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 10.15 ©Silberschatz, Korth and Sudarshan
User-Defined Types
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 10.16 ©Silberschatz, Korth and Sudarshan
Domains
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 10.17 ©Silberschatz, Korth and Sudarshan
Large-Object Types
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Large objects (photos, videos, CAD files, etc.) are stored as a large object:
blob: binary large object -- object is a large collection of uninterpreted binary data
(whose interpretation is left to an application outside of the database system)
clob: character large object -- object is a large collection of character data
When a query returns a large object, a pointer is returned rather than the large
object itself
Database System Concepts - 6th Edition 10.18 ©Silberschatz, Korth and Sudarshan
PPD
Integrity Constraints
SQL Data Types and
Schemas
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Authorization
AUTHORIZATION
Database System Concepts - 6th Edition 10.19 ©Silberschatz, Korth and Sudarshan
Authorization
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 10.20 ©Silberschatz, Korth and Sudarshan
Authorization Specification in SQL
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 10.21 ©Silberschatz, Korth and Sudarshan
Privileges in SQL
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 10.22 ©Silberschatz, Korth and Sudarshan
Revoking Authorization in SQL
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 10.23 ©Silberschatz, Korth and Sudarshan
Roles
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 10.24 ©Silberschatz, Korth and Sudarshan
Authorization on Views
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 10.25 ©Silberschatz, Korth and Sudarshan
Other Authorization Features*
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 10.26 ©Silberschatz, Korth and Sudarshan
Module Summary
SWAYAM: NPTEL-NOC MOOCs Instructor: Prof. P P Das, IIT Kharagpur. Jan-Apr, 2018
Database System Concepts - 6th Edition 10.27 ©Silberschatz, Korth and Sudarshan
PPD
Database System Concepts - 6th Edition 10.28 ©Silberschatz, Korth and Sudarshan