Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Database System Concepts - 7th Edition 3.2 ©Silberschatz, Korth and Sudarshan
History
Database System Concepts - 7th Edition 3.3 ©Silberschatz, Korth and Sudarshan
SQL Parts
DML -- provides the ability to query information from the database and to
insert tuples into, delete tuples from, and modify tuples in the database.
DDL – Provides commands for:
• Integrity – specifying integrity constraints.
• View definition -- defining views.
• Authorization – specifying access rights to relations and views
Transaction control –includes commands for specifying the beginning and
ending of transactions.
Embedded SQL and dynamic SQL -- define how SQL statements can
be embedded within general-purpose programming languages.
Database System Concepts - 7th Edition 3.4 ©Silberschatz, Korth and Sudarshan
Data Definition Language
Database System Concepts - 7th Edition 3.5 ©Silberschatz, Korth and Sudarshan
Domain Types in SQL
Database System Concepts - 7th Edition 3.6 ©Silberschatz, Korth and Sudarshan
Create Table Construct
Database System Concepts - 7th Edition 3.7 ©Silberschatz, Korth and Sudarshan
Integrity Constraints in Create Table
Database System Concepts - 7th Edition 3.8 ©Silberschatz, Korth and Sudarshan
And a Few More Relation Definitions
Database System Concepts - 7th Edition 3.9 ©Silberschatz, Korth and Sudarshan
And more still
Database System Concepts - 7th Edition 3.10 ©Silberschatz, Korth and Sudarshan
Updates to tables
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 - 7th Edition 3.11 ©Silberschatz, Korth and Sudarshan
Basic Query Structure
• Ai represents an attribute
• ri represents a relation
• P is a predicate.
The result of an SQL query is a relation.
Database System Concepts - 7th Edition 3.12 ©Silberschatz, Korth and Sudarshan
The select Clause
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 - 7th Edition 3.13 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)
Database System Concepts - 7th Edition 3.14 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)
Database System Concepts - 7th Edition 3.15 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)
Database System Concepts - 7th Edition 3.16 ©Silberschatz, Korth and Sudarshan
The where Clause
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.'
SQL allows the use of the logical connectives and, or, and not
The operands of the logical connectives can be expressions involving the
comparison operators <, <=, >, >=, =, and <>.
Comparisons can be applied to results of arithmetic expressions
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 - 7th Edition 3.17 ©Silberschatz, Korth and Sudarshan
The from Clause
Database System Concepts - 7th Edition 3.18 ©Silberschatz, Korth and Sudarshan
Examples
Find the names of all instructors who have taught some course and the
course_id
• select name, course_id
from instructor , teaches
where instructor.ID = teaches.ID
Find the names of all instructors in the Art department who have taught
some course and the course_id
• select name, course_id
from instructor , teaches
where instructor.ID = teaches.ID and instructor. dept_name = 'Art'
Database System Concepts - 7th Edition 3.19 ©Silberschatz, Korth and Sudarshan
The Rename Operation
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 - 7th Edition 3.20 ©Silberschatz, Korth and Sudarshan
Self Join Example
Relation emp-super
Database System Concepts - 7th Edition 3.21 ©Silberschatz, Korth and Sudarshan
String Operations
Database System Concepts - 7th Edition 3.22 ©Silberschatz, Korth and Sudarshan
String Operations (Cont.)
Database System Concepts - 7th Edition 3.23 ©Silberschatz, Korth and Sudarshan
Ordering the Display of Tuples
Database System Concepts - 7th Edition 3.24 ©Silberschatz, Korth and Sudarshan
Where Clause Predicates
Database System Concepts - 7th Edition 3.25 ©Silberschatz, Korth and Sudarshan
Set Operations
Database System Concepts - 7th Edition 3.26 ©Silberschatz, Korth and Sudarshan
Set Operations (Cont.)
Database System Concepts - 7th Edition 3.27 ©Silberschatz, Korth and Sudarshan
Null Values
It is possible for tuples to have a null value, denoted by null, for some of
their attributes
The value null signifies an unknown value or that a value does not exist.
The result of any arithmetic expression involving null is null
• Example: 5 + null returns null
The predicate is null can be used to check for null values.
• Example: Find all instructors whose salary is null.
select name
from instructor
where salary is null
The predicate is not null succeeds if the value on which it is applied is
not null.
Database System Concepts - 7th Edition 3.28 ©Silberschatz, Korth and Sudarshan
Null Values (Cont.)
Database System Concepts - 7th Edition 3.29 ©Silberschatz, Korth and Sudarshan
Examples
Example: Find the names of all instructors with salary value null
• select name
from instructor
where salary is null
Example: Find the names of all instructors with salary value greater
than100000 is unknown
• select name
from instructor
where salary > 100000 is unknown
Database System Concepts - 7th Edition 3.30 ©Silberschatz, Korth and Sudarshan
Aggregate Functions
Database System Concepts - 7th Edition 3.31 ©Silberschatz, Korth and Sudarshan
Aggregate Functions Examples
Database System Concepts - 7th Edition 3.32 ©Silberschatz, Korth and Sudarshan
Aggregate Functions Examples
Database System Concepts - 7th Edition 3.33 ©Silberschatz, Korth and Sudarshan
Aggregate Functions – Group By
Find the average salary of instructors in each department
• select dept_name, avg (salary) as avg_salary
from instructor
group by dept_name;
Database System Concepts - 7th Edition 3.34 ©Silberschatz, Korth and Sudarshan
Aggregation (Cont.)
Database System Concepts - 7th Edition 3.35 ©Silberschatz, Korth and Sudarshan
Aggregate Functions – Having Clause
Find the names and average salaries of all departments whose average
salary is greater than 42000
select dept_name, avg (salary) as avg_salary
from instructor
group by dept_name
having avg (salary) > 42000;
Note: predicates in the having clause are applied after the formation of
groups whereas predicates in the where clause are applied before forming
groups
Database System Concepts - 7th Edition 3.36 ©Silberschatz, Korth and Sudarshan
Nested Subqueries
SQL provides a mechanism for the nesting of subqueries. A subquery is
a select-from-where expression that is nested within another query.
The nesting can be done in the following SQL query
Database System Concepts - 7th Edition 3.37 ©Silberschatz, Korth and Sudarshan
Set Membership
Database System Concepts - 7th Edition 3.38 ©Silberschatz, Korth and Sudarshan
Set Membership (“in” ) Clause
Database System Concepts - 7th Edition 3.39 ©Silberschatz, Korth and Sudarshan
Set Membership (Cont.)
Find the total number of (distinct) students who have taken course
sections taught by the instructor with ID 10101
Database System Concepts - 7th Edition 3.40 ©Silberschatz, Korth and Sudarshan
Set Comparison
Database System Concepts - 7th Edition 3.41 ©Silberschatz, Korth and Sudarshan
Set Comparison – “some” Clause
Find names of instructors with salary greater than that of some (at least
one) instructor in the Biology department.
select distinct T.name
from instructor as T, instructor as S
where T.salary > S.salary and S.dept name = 'Biology';
select name
from instructor
where salary > some (select salary
from instructor
where dept name = 'Biology');
Database System Concepts - 7th Edition 3.42 ©Silberschatz, Korth and Sudarshan
Definition of “some” Clause
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 - 7th Edition 3.43 ©Silberschatz, Korth and Sudarshan
Set Comparison – “all” Clause
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 - 7th Edition 3.44 ©Silberschatz, Korth and Sudarshan
Definition of “all” Clause
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 - 7th Edition 3.45 ©Silberschatz, Korth and Sudarshan
Test for Empty Relations
The exists construct returns the value true if the argument subquery is
nonempty.
exists r r Ø
not exists r r = Ø
Database System Concepts - 7th Edition 3.46 ©Silberschatz, Korth and Sudarshan
Use of “exists” Clause
Yet another way of specifying the query “Find all courses taught in both the
Fall 2017 semester and in the Spring 2018 semester”
select course_id
from section as S
where semester = 'Fall' and year = 2017 and
exists (select *
from section as T
where semester = 'Spring' and year= 2018
and S.course_id = T.course_id);
Database System Concepts - 7th Edition 3.47 ©Silberschatz, Korth and Sudarshan
Use of “not exists” Clause
Find all students who have taken all courses offered in the Biology
department.
select distinct S.ID, S.name
from student as S
where not exists ( (select course_id
from course
where dept_name = 'Biology')
except
(select T.course_id
from takes as T
where S.ID = T.ID));
Database System Concepts - 7th Edition 3.48 ©Silberschatz, Korth and Sudarshan
Test for Absence of Duplicate Tuples
The unique construct tests whether a subquery has any duplicate tuples
in its result.
The unique construct evaluates to “true” if a given subquery contains no
duplicates
Find all courses that were offered at most once in 2017
select T.course_id
from course as T
where unique ( select R.course_id
from section as R
where T.course_id= R.course_id
and R.year = 2017);
• Can be done in simpler way. How?
Database System Concepts - 7th Edition 3.49 ©Silberschatz, Korth and Sudarshan
Subqueries in the From Clause
Database System Concepts - 7th Edition 3.50 ©Silberschatz, Korth and Sudarshan
Subqueries in the Form Clause
Database System Concepts - 7th Edition 3.51 ©Silberschatz, Korth and Sudarshan
With Clause
Database System Concepts - 7th Edition 3.52 ©Silberschatz, Korth and Sudarshan
Complex Queries using With Clause
Find all departments where the total salary is greater than the average of
the total salary at all departments
Database System Concepts - 7th Edition 3.53 ©Silberschatz, Korth and Sudarshan
Scalar Subquery
Database System Concepts - 7th Edition 3.54 ©Silberschatz, Korth and Sudarshan
Modification of the Database
Database System Concepts - 7th Edition 3.55 ©Silberschatz, Korth and Sudarshan
Deletion
Delete all tuples in the instructor relation for those instructors associated
with a department located in the Watson building.
delete from instructor
where dept name in (select dept name
from department
where building = 'Watson');
Database System Concepts - 7th Edition 3.56 ©Silberschatz, Korth and Sudarshan
Deletion (Cont.)
Delete all instructors whose salary is less than the average salary of
instructors
Database System Concepts - 7th Edition 3.57 ©Silberschatz, Korth and Sudarshan
Insertion
or equivalently
insert into course (course_id, title, dept_name, credits)
values ('CS-437', 'Database Systems', 'Comp. Sci.', 4);
Database System Concepts - 7th Edition 3.58 ©Silberschatz, Korth and Sudarshan
Insertion (Cont.)
Make each student in the Music department who has earned more than
144 credit hours an instructor in the Music department with a salary of
$18,000.
insert into instructor
select ID, name, dept_name, 18000
from student
where dept_name = 'Music' and total_cred > 144;
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 - 7th Edition 3.59 ©Silberschatz, Korth and Sudarshan
Updates
Database System Concepts - 7th Edition 3.60 ©Silberschatz, Korth and Sudarshan
Updates (Cont.)
Database System Concepts - 7th Edition 3.61 ©Silberschatz, Korth and Sudarshan
Case Statement for Conditional Updates
Database System Concepts - 7th Edition 3.62 ©Silberschatz, Korth and Sudarshan
Updates with Scalar Subqueries
Database System Concepts - 7th Edition 3.63 ©Silberschatz, Korth and Sudarshan
End of Chapter 3
Database System Concepts - 7th Edition 3.64 ©Silberschatz, Korth and Sudarshan