Professional Documents
Culture Documents
Sarajane Marques Peres, Ph.D. - University of São Paulo Based On Elsmari, Navathe / Silberschatz, Korth, Sudarshan S Books
Sarajane Marques Peres, Ph.D. - University of São Paulo Based On Elsmari, Navathe / Silberschatz, Korth, Sudarshan S Books
SQL
SQL
Data
DefiniLon
and
Data
Types
• SQL
uses
the
terms:
– table,
row
and
column
for
the
formal
relaLon
model
terms
– relaLon,
tuple
and
aOribute
respecLvely.
• The
main
SQL
command
for
data
definiLon
is
the
CREATE
statement.
In
a
basic
context,
we
have:
– CREATE
SCHEMA
– CREATE
TABLE
– CREATE
DOMAIN
SQL
Data
DefiniLon
and
Data
Types
• Schema:
group
together
tables
and
other
constructs
that
belong
to
the
same
database
applicaLon.
L
J
SQL
Data
DefiniLon
and
Data
Types
SQL
Data
DefiniLon
and
Data
Types
• The
basic
data
types
available
for
aOributes
include
numeric,
character
string,
bit
string,
Boolean,
date
and
Lme.
– CLOB
(a
special
type
of
character
string)
– BLOB
(a
special
type
of
bit
string)
• Domains:
this
makes
it
easier
to
change
the
data
type
for
a
domain
that
is
used
by
numerous
aOributes
in
a
schema,
and
improves
schema
readability.
PostgreSQL
9.3.5
DocumentaLon
Specifying
Constraints
in
SQL
• Specifying
AOribute
Constraints
and
AOribute
Defaults
– Constraint
NOT
NULL
– DEFAULT
<value>
– CHECK
Specifying
Constraints
in
SQL
• Specifying
Key
and
ReferenLal
Integrity
Constraints
– PRIMARY
KEY
– UNIQUE
– FOREIGN
KEY
• ReferenLal
triggered
acLon
(ON
DELETE
or
ON
UPDATE)
– SET
NULL
– CASCAFE
– SET
DEFAULT
Specifying
Constraints
in
SQL
PostgreSQL
9.3.5
DocumentaLon
PostgreSQL
9.3.5
DocumentaLon
PostgreSQL
9.3.5
DocumentaLon
PostgreSQL
9.3.5
DocumentaLon
Tests
PostgreSQL
9.3.5
DocumentaLon
Test
Test
Test
Test
Basic
Retrieval
Queries
in
SQL
• IMPORTANT:
SQL
allows
a
table
(relaLon)
to
have
two
or
more
tuples
that
are
idenLcal
in
all
their
aOribute
values.
• The
SELECT-‐FROM-‐WHERE
Structure
of
Basic
SQL
Queries
Basic
Retrieval
Queries
in
SQL
Retrieve
the
birth
date
and
address
of
the
employee(s)
whose
name
is
‘John
B.
Smith’.
Retrieve
the
name
and
address
of
all
employees
who
work
for
the
‘Research’
department.
Basic
Retrieval
Queries
in
SQL
For
every
project
located
in
‘Stafford’,
list
the
project
number,
the
controlling
department
number,
and
the
department
manager’s
last
name,
address,
and
birth
date.
Basic
Retrieval
Queries
in
SQL
• Ambiguous
AOribute
Names,
Aliasing,
Renaming,
and
Tuple
Variables
For
each
employee,
retrieve
the
employee’s
first
and
last
name
and
the
first
and
last
name
of
his
or
her
immediate
supervisor.
Basic
Retrieval
Queries
in
SQL
• Unspecified
WHERE
Clause
and
Use
of
the
Asterisk
Retrieve
all
employees
in
department
5
whose
salary
is
between
$30,000
and
$40,000.
Basic
Retrieval
Queries
in
SQL
• Ordering
of
Query
Results
Retrieve
a
list
of
employee
and
the
projects
they
are
working
on,
ordered
by
department
and,
within
each
department,
ordered
alphabeLcally
by
last
name,
then
first
name.
INSERT,
DELETE
and
UPDATE
Statements
in
SQL
• The
INSERT
Command
INSERT,
DELETE
and
UPDATE
Statements
in
SQL
• The
DELETE
Command
INSERT,
DELETE
and
UPDATE
Statements
in
SQL
• The
UPDATE
Command
More
SQL
Retrieve
the
names
of
all
employees
who
do
not
have
supervisors
More
SQL
• Nested
queries
More
SQL
• Nested
queries
Make
a
list
of
all
project
numbers
for
projects
that
involve
an
employee
whose
last
name
is
‘Smith’,
either
as
a
worker
or
as
a
manager
of
the
department
that
controls
the
project.
More
SQL
• Nested
queries
More
SQL
• Nested
queries
Select
the
Essns
of
all
employees
who
work
the
same
(project,
hours)
combinaLon
on
some
project
that
employee
‘John
Smith’
(whose
Ssn=
“123456789”
works
on.
More
SQL
• Nested
queries
More
SQL
• Nested
queries
List
the
names
of
employees
whose
salary
is
greater
than
the
salary
of
all
the
employees
in
department
5.
More
SQL
• Nested
queries
More
SQL
• Nested
queries
Retrieve
the
name
of
each
employee
who
has
a
dependent
with
the
same
first
name
and
is
the
same
sex
as
the
employee.
More
SQL
• EXISTS
and
UNIQUE
FuncLons
More
SQL
• EXISTS
and
UNIQUE
FuncLons
Retrieve
the
name
of
each
employee
who
has
a
dependent
with
the
same
first
name
and
is
the
same
sex
as
the
employee.
More
SQL
• EXISTS
and
UNIQUE
FuncLons
More
SQL
• EXISTS
and
UNIQUE
FuncLons
List
the
names
of
managers
who
have
at
least
one
dependent.
More
SQL
• EXISTS
and
UNIQUE
FuncLons
More
SQL
• EXISTS
and
UNIQUE
FuncLons
Retrieve
the
name
of
each
employee
who
works
on
all
the
projects
controlled
by
department
number
5.
More
SQL
• EXISTS
and
UNIQUE
FuncLons
More
SQL
• EXISTS
and
UNIQUE
FuncLons
Does
E.Essn
work
in
that
project?
EMP
Project
Project
A
1
5
A
1
B
2
5
A
2
C
3
3
B
1
SSN
=
B
(?)
C
3
A,1
False
SSN
=
A
(?)
A,2
True
A,1
Is
project
1
in
depto
5?
IN
B,1
False
YES
C,3
False
Does
A
work
in
project
1
NOT
YES
EXIST
SSN
=
C
(?)
True
AND
False
A,1
True
False
A,2
True
A,2
False
B,1
True
B,1
False
C,3
False
C,3
False
NOT
EXIST
=
True
YES
=
A
More
SQL
• EXISTS
and
UNIQUE
FuncLons
Retrieve
the
name
of
each
employee
who
works
on
all
the
projects
controlled
by
department
number
5.
More
SQL
• Joins
More
SQL
• Joins
More
SQL
• Aggregate
FuncLons
Aggregate
FuncLons
and
NULL
values
More
SQL
• Group
by
and
Having
More
SQL
More
SQL
More
SQL
• Group
by
and
Having
PostgreSQL
-‐
SELECT
PostgreSQL
-‐
WITH
PostgreSQL
–
OVER
/
PARTITION
Window
FuncLon
PostgreSQL
–
Limit
/
Offset
PostgreSQL
-‐
Lateral
Review
Queries
(consultas)
• Liste
os
nomes
dos
instrutores
junto
com
os
wtulos
dos
cursos
que
eles
ministram.
• Encontre
os
nomes
de
todos
instrutores
que
recebem
um
salário
mais
alto
do
que
algum
instrutor
de
“Ciência
da
Computação”.
• Encontre
os
nomes
de
todos
os
instrutores
cujos
nomes
incluem
a
substring
“dar”.
• Liste
os
nomes
de
todos
os
instrutores,
em
ordem
alfabéLca.
• Encontre
os
cursos
que
ocorream
durante
o
Inverno
de
2009
ou
a
Primavera
de
2010.
• Encontre
o
número
total
de
instrutores
que
ministram(ministraram)
um
curso
durante
a
primavera
de
2010.
• Encontre
os
nomes
e
médias
de
salários
de
todos
departamentos
cuja
média
de
salários
é
maior
que
42000.
• Encontre
todos
os
estudantes
que
estão
cursando
todos
os
cursos
ofertados
pelo
departamento
de
Biologia.
Review
List
the
names
of
instructors
along
with
the
Ltles
of
courses
that
they
teach.
select name, title
from instructor natural join teaches, course
where teaches.course_id = course.course_id;"
(select
course_id
from
sec+on
where
sem
=
‘Fall’ and
year
=
2009)
union
(select
course_id
from
sec+on
where
sem
=
‘Spring’ and
year
=
2010)
Review
• Find
the
total
number
of
instructors
who
teach
a
course
in
the
Spring
2010
semester.
∏instructor.ID,course_id (σinstructor.ID=teaches.ID (
σ dept_name=“Physics” (instructor) x teaches))"
license
Review
• Find
the
total
number
of
people
who
owned
cars
that
were
involved
in
accidents
in
1989
• Ou?
select
count
(disGnct
name)
from
(((car
join
parLcipated
on
car.license
=
parLcipated.car)
natural
join
owns)
natural
join
person)
join
accident
on
accident.report_number
=
parLcipated.report_number
where
date
between
‘1989-‐01-‐01’
and
‘1989-‐12-‐31’
Review
• Add
a
new
accident
to
the
database;
assume
any
values
for
required
aOributes.
Name
=
Jones
model
=
Toyota
Review
• Delete
the
Mazda
belonging
to
‘John
Smith’.
Review
• RelaLonal
Model
Review
• Find
the
company
that
has
the
most
employees.
Review
• Write
an
efficient
relaLonal-‐algebra
expression
that
is
equivalent
to
this
query:
Sarajane
Marques
Peres,
Ph.D.
–
University
of
São
Paulo
www.each.usp.br/sarajane
Based
on
Elsmari,
Navathe
/
Silberschatz,
Korth,
Sudarshan‘s
books
SQL