Documente Academic
Documente Profesional
Documente Cultură
3.2
History
IBM Sequel language developed as part of System R project at
3.3
3.4
maximum length n.
dependent).
domain type).
3.5
Example:
create table instructor (
ID
char(5),
name
varchar(20),
dept_name varchar(20),
salary
numeric(8,2))
insert into instructor values (10211, Smith, Biology, 66000);
insert into instructor values (10211, null, Biology, 66000);
3.6
3.7
ID
varchar(5) primary key,
name
varchar(20) not null,
dept_name
varchar(20),
tot_cred
numeric(3,0),
foreign key (dept_name) references department) );
ID
varchar(5),
course_id
varchar(8),
sec_id
varchar(8),
semester
varchar(6),
year
numeric(4,0),
grade
varchar(2),
primary key (ID, course_id, semester, year),
foreign key (ID) references student,
foreign key (course_id, sec_id, semester, year) references section );
3.8
course_id
varchar(8) primary key,
title
varchar(50),
dept_name
varchar(20),
credits
numeric(2,0),
foreign key (dept_name) references department) );
3.9
alter table
All
Dropping
3.10
3.11
select name
from instructor
NOTE: SQL names are case insensitive (i.e., you may use upper- or
lower-case letters.)
3.12
after select.
duplicates
3.13
select *
from instructor
The select clause can contain arithmetic expressions involving
The query:
3.14
satisfy
To find all instructors in Comp. Sci. dept with salary > 80000
select name
from instructor
where dept_name = Comp. Sci.' and salary > 80000
3.15
select
from instructor, teaches
3.16
teaches
3.17
Joins
For all instructors who have taught some course, find their names
Find the course ID, semester, year and title of each course offered
3.18
3.19
Quiz
Quiz Q4: The query select name, course_id
from instructor, teaches
(1) returns all matching instructor/teaches pairs
(2) is a syntax error
(3) returns all pairs of instructor, teaches tuples
(4) none of the above
3.20
Natural Join
Natural join matches tuples with the same values for all
select *
3.21
they taught.
3.22
List the names of instructors along with the the titles of courses that they
teach
Correct version
3.23
old-name as new-name
E.g.
Find the names of all instructors who have a higher salary than
instructor as T instructor T
3.24
String Operations
SQL includes a string-matching operator for comparisons on character
strings. The operator like uses patterns that are described using two
special characters:
Find the names of all instructors whose name includes the substring dar.
select name
from instructor
where name like '%dar%'
Match the string 100 %
3.25
3.26
Duplicates
Multiset: set with duplicates
In relations with duplicates, SQL can define how many copies of
r1
r2
B(r1)
C>2(r2)
B(r1) x r2
3.27
3.
A ,A ,,A ( P (r1 r2 rm ))
1
3.28
Set Operations
Find courses that ran in Fall 2009 or in Spring 2010
(select course_id from section where sem = Fall and year = 2009)
union
(select course_id from section where sem = Spring and year = 2010)
A
B
A
intersect
B
Find courses that ran in Fall 2009 but not in Spring 2010
A
except
B
3.29
Set Operations
Set operations union, intersect, and except
3.30
Null Values
It is possible for tuples to have a null value, denoted by null, for
or
null = null
to unknown
3.32
Quiz
Quiz Q6: Consider the where clause predicates
where r.A <> 5
and
where not (r.A = 5)
(1) the two are equivalent
(2) the two are equivalent only if r.a is declared as not null
(3) the two are equivalent only if r.a is null
(4) the two are never equivalent
3.33
Aggregate Functions
These functions operate on the multiset of values of a
3.34
department
3.35
3.36
Aggregation (Cont.)
Attributes in select clause outside of aggregate functions must
/* erroneous query */
select dept_name, ID, avg (salary)
from instructor
group by dept_name;
3.37
3.38
count returns 0
3.39
3.40
Nested Subqueries
SQL provides a mechanism for the nesting of subqueries.
A subquery is a select-from-where expression that is nested
3.41
Example Query
Find courses offered in Fall 2009 and in Spring 2010
select distinct course_id
from section
where semester = Fall and year= 2009 and
course_id in (select course_id
from section
where semester = Spring and year= 2010);
Find courses offered in Fall 2009 but not in Spring 2010
select distinct course_id
from section
where semester = Fall and year= 2009 and
course_id not in (select course_id
from section
where semester = Spring and year= 2010);
3.42
Example Query
Find the total number of (distinct) students who have taken
3.43
Set Comparison
Find names of instructors with salary greater than that of some
3.44
Example Query
Find the names of all instructors whose salary is greater than
3.45
Example Query
Find the names of all instructors whose salary is greater than
3.46
subquery is nonempty.
exists r r
not exists r r =
3.47
Correlation Variables
Yet another way of specifying the query Find all courses taught
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);
Correlated subquery
Correlation name or correlation variable
3.48
Not Exists
Find all students who have taken all courses offered in the
Biology department.
SQA
SQB
Note that X Y = X Y
Note: Cannot write this query using = all and its variants
3.49
Silberschatz, Korth and Sudarshan
3.50
3.51
With Clause
The with clause provides a way of defining a temporary view
3.52
Find all departments where the total salary is greater than the
3.53
Scalar Subquery
Scalar subquery is one which is used where a single value is
expected
select dept_name,
(select count(*)
from instructor
where department.dept_name = instructor.dept_name)
as num_instructors
from department;
Runtime error if subquery returns more than one result tuple
3.54
3.55
Example Query
Delete all instructors whose salary is less than the average
salary of instructors
3.56
3.57
its results are inserted into the relation (otherwise queries like
insert into table1 select * from table1
would cause problems, if table1 did not have any primary key
defined.
3.58
update instructor
set salary = case
when salary <= 100000 then salary * 1.05
else salary * 1.03
end
3.59
update student S
set tot_cred = ( select sum(credits)
from takes natural join course
where S.ID= takes.ID and
takes.grade <> F and
takes.grade is not null);
Sets tot_creds to null for students who have not taken any course
Instead of sum(credits), use:
case
when sum(credits) is not null then sum(credits)
else 0
end
Or
coalesce(sum(credits), 0)
3.60
End of Chapter 3
select name
from instructor
where salary between 90000 and 100000
Tuple comparison
3.62
Division Operator
Given relations r(R) and s(S), such that S R, r s is the largest
Can write r s as
temp1 R-S (r )
temp2 R-S ((temp1 x s ) R-S,S (r ))
result = temp1 temp2
3.63