Escolar Documentos
Profissional Documentos
Cultura Documentos
SQL
(Structured Query Language)
Contents
SQL
SQL
SQL
Name
1986
SQL-86
1989
SQL-89
1992
SQL-92
SQL2
1999
SQL:1999
SQL3
Added regular expression matching, recursive queries, triggers, nonscalar types and some object-oriented features. (The last two are
somewhat controversial and not yet widely supported)
2003
SQL:2003
2006
SQL:2006
SQL
Alias
SQL-87
Comments
First published by ANSI. Ratified by ISO in 1987
Minor revision
Contents
SQL
SQL
DDL: Data Definition Language
Create
Alter
Drop
Select
Insert
Update
Delete
SQL
Commit
Rollback
Grant,
Revoke
7
AUTHORIZATION AuthorizationIdentifier;
To create a relational database schema:
started with SQL-92
CREATE SCHEMA Company AUTHORIZATION
JSmith;
SQL
or
CREATE TABLE TableName
SQL
SQL
10
DOMAIN
[CHECK conditions];
SQL
DomainName
AS
DataType
11
Do create tables
& constraints !!
SQL
12
SQL
13
SQL
14
CHECK clause:
DNUMBER INT NOT NULL CHECK (DNUMBER>0 AND
DNUMBER<21),
CREATE DOMAIN can also be used in conjunction with the
CHECK clause:
CREATE DOMAIN D_NUM AS INTEGER CHECK (D_NUM>0
AND D_NUM<21);
SQL
15
SQL
16
Referential integrity:
FOREIGN KEY (list_of_attr)
SQL
17
An example
SQL
18
This is optional
The name is unique within a particular DB
schema
Used to identify a particular constraint in case it
must be dropped later and replaced with
another one
SQL
19
SQL
20
More
general
ASSERTION
SQL
constraints:
<
CREATE
21
SQL
22
DROP
TABLE
Dependent
CASCADE;
(RESTRICT)
RESTRICT option: dropped on if it is not
referenced in any constraints or views
CASCADE option: all such constraints and
views that reference the table are dropped
automatically from the schema along with the
table itself
Similarly, we can drop constraints & domains
SQL
23
SQL
24
http://download-
west.oracle.com/docs/cd/B14117_01/server.10
1/b10759/toc.htm
http://www.java2s.com/Tutorial/Oracle/Catalog
Oracle.htm
SQL
25
SQL
26
a SELECT-FROM-WHERE block
SELECT
FROM
WHERE
<attribute list>
<table list>
<condition>
SQL
27
SQL
28
SQL
appear in
FROM
WHERE
GROUP BY
column
HAVING
condition
ORDER BY
29
SQL
30
BDATE, ADDRESS
EMPLOYEE
FNAME='John' AND MINIT='B AND
LNAME='Smith
SQL
31
SQL
32
SQL
33
SQL
34
SQL
35
SQL
36
SQL
37
Aliases
Some queries need to refer to the same relation twice: aliases are given
SQL
38
Aliases
Aliases can also be used in any SQL query for
SQL
39
Unspecified WHERE-clause
A missing WHERE-clause indicates no condition;
Q5:
SQL
SELECT
FROM
SSN
EMPLOYEE
40
Unspecified WHERE-clause
If more than one relation is specified in the FROM-clause and
SSN, DNAME
EMPLOYEE, DEPARTMENT
SQL
41
SQL
Q7: SELECT
FROM
WHERE
*
EMPLOYEE
DNO=5
Q8: SELECT
FROM
WHERE
*
EMPLOYEE, DEPARTMENT
DNAME='Research' AND
DNO=DNUMBER
42
USE OF DISTINCT
SQL does not treat a relation as a set: duplicate tuples
Q9A:
SQL
SELECT
FROM
DISTINCT SALARY
EMPLOYEE
43
Set Operations
Set union (UNION), set difference (EXCEPT) and set
SQL
44
Set Operations
Query 10: Make a list of all project numbers for projects that
SQL
DISTINCT PNUMBER
PROJECT, DEPARTMENT, EMPLOYEE
DNUM=DNUMBER AND MGRSSN=SSN
AND LNAME='Smith')
DISTINCT PNUMBER
PROJECT, WORKS_ON, EMPLOYEE
PNUMBER=PNO AND ESSN=SSN AND
LNAME='Smith')
45
SQL
Q11:
SELECT *
FROM
Employee
WHERE Address LIKE %HCMC%
Q12:
SELECT *
FROM
Employee
WHERE BDate LIKE _ _8_ _ _ _ _ _ _
46
SQL
47
Contents
SQL
48
True
False
Unknown
True
False
Unknown
OR
True
False
Unknown
True
False
Unknown
NOT
True
False
Unknown U
SQL
50
Nested Queries
Complete select-from-where blocks within WHERE clause of
another query
Comparison operator IN
Compares value v with a set (or multiset) of values V
Evaluates to TRUE if v is one of the elements in V
Query 14: Retrieve the name and address of all employees who
Q14:SELECT
FROM
WHERE
SQL
51
SQL
52
SQL
53
SELECT
FROM
WHERE
E.FNAME, E.LNAME
EMPLOYEE E, DEPENDENT D
E.SSN=D.ESSN AND
E.FNAME=D.DEPENDENT_NAME
SQL
54
Q16: SELECT
FROM
WHERE
DISTINCT
ESSN
Works_on
(PNO, HOURS) IN
(SELECT
FROM
WHERE
SQL
PNO, HOURS
Works_on
ESSN=123456789)
55
value v
= ANY (or = SOME) operator
Returns TRUE if the value v is equal to some value in the set
V and is hence equivalent to IN
Other operators that can be combined with ANY (or
56
SQL
57
E.FNAME, E.LNAME
EMPLOYEE
EXISTS (SELECT *
FROM DEPENDENT
WHERE SSN=ESSN AND
FNAME=DEPENDENT_NAME)
SQL
58
dependents
Q18:
SELECT
FROM
WHERE
FNAME, LNAME
EMPLOYEE
NOT EXISTS (SELECT *
FROM DEPENDENT
WHERE SSN=ESSN)
SQL
59
Enumerated Sets
It is also possible to use an explicit (enumerated) set
Q19:
SQL
SELECT
FROM
WHERE
DISTINCT ESSN
WORKS_ON
PNO IN (1, 2, 3)
60
clause
Allows the user to specify different types of
joins (EQUIJOIN, NATURAL JOIN, LEFT
OUTER JOIN, RIGHT OUTER JOIN)
SQL
61
SQL
62
NATURAL JOIN
Various types of OUTER JOIN
NATURAL JOIN on two relations R and S
No join condition specified
Implicit EQUIJOIN condition for each pair of
attributes with same name from R and S
SQL
63
SQL
64
SQL
65
SELECT
FROM
WHERE
SQL
66
SELECT
FNAME, LNAME, ADDRESS
FROM
EMPLOYEE, DEPARTMENT
WHERE
DNAME='Research' AND DNUMBER=DNO
could be written as:
SELECT
FNAME, LNAME, ADDRESS
FROM
(EMPLOYEE JOIN DEPARTMENT ON
DNUMBER=DNO)
WHERE
DNAME='Research
or as:
SELECT
FNAME, LNAME, ADDRESS
FROM
(EMPLOYEE NATURAL JOIN (DEPARTMENT
AS DEPT(DNAME, DNO, MSSN, MSDATE)))
WHERE
DNAME='Research
SQL
67
SELECT
FROM
SQL
68
AGGREGATE FUNCTIONS
COUNT, SUM, MAX, MIN, AVG
Query 20: Find the max, min, & average salary
Q20:SELECT
AVG(SALARY)
FROM
SQL
MAX(SALARY), MIN(SALARY),
EMPLOYEE
69
AGGREGATE FUNCTIONS
Queries 21 and 22: Retrieve the total number of
Q21:SELECT
FROM
COUNT (*)
EMPLOYEE
Q22:SELECT
FROM
WHERE
COUNT (*)
EMPLOYEE, DEPARTMENT
DNO=DNUMBER AND
DNAME='Research
Note: NULL values are discarded wrt. aggregate
functions as applied to a particular column
SQL
70
GROUPING
In many cases, we want to apply the aggregate
71
GROUPING
Query 23: For each department, retrieve the department number, the
SQL
72
Result of Q23
SQL
73
SQL
74
Q24:
SQL
SELECT
FROM
WHERE
GROUP BY
HAVING
75
ORDER BY
The ORDER BY clause is used to sort the tuples in a
SQL
76
SELECT summarization
SELECT [DISTINCT | ALL]
{* | [columnExpression [AS newName]] [,...] }
FROM TableName [alias] [, ...]
[WHERE condition]
[GROUP BY columnList] [HAVING condition]
[ORDER BY columnList]
SQL
77
HAVING
condition
ORDER BY
FROM
WHERE
GROUP BY
SQL
78
SQL
79
Contents
SQL
80
SQL
81
correspond to the values in the new tuple, attributes with NULL values
can be left out
Example: Insert a tuple for a new EMPLOYEE for whom we only know
the FNAME, LNAME, and SSN attributes.
SQL
82
SQL
83
SQL
84
SQL
85
SQL
U4A:
DELETE FROM
WHERE
EMPLOYEE
LNAME='Brown
U4B:
DELETE FROM
WHERE
EMPLOYEE
SSN='123456789
U4C:
DELETE FROM
WHERE
U4D:
DELETE FROM
EMPLOYEE
DNO IN
(SELECT
DNUMBER
FROM
DEPARTMENT
WHERE
DNAME='Research')
EMPLOYEE
86
SQL
selected tuples
A WHERE-clause selects the tuples to be modified
An additional SET-clause specifies the attributes to be
modified and their new values
Each command modifies tuples in the same relation
Referential integrity should be enforced
87
SQL
PROJECT
PLOCATION = 'Bellaire', DNUM = 5
PNUMBER=10
88
SQL
EMPLOYEE
SALARY = SALARY *1.1
DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research')
89
SQL
90
SQL
91
SQL
92
VIEWs
A view is a virtual table that is derived from
other tables
Allows for limited update operations (since the
table may not physically be stored)
Allows full query operations
A convenience for expressing certain
operations
SQL
93
VIEWs
SQL
WORKS_ON_NEW AS
FNAME, LNAME, PNAME, HOURS
EMPLOYEE, PROJECT, WORKS_ON
SSN=ESSN AND PNO=PNUMBER
94
VIEWs
We can specify SQL queries on a newly create
table (view):
SELECT FNAME, LNAME FROM WORKS_ON_NEW
WHERE PNAME=Seena;
dropped:
DROP VIEW WORKS_ON_NEW;
SQL
95
SQL
96
SQL
97
Summary
SQL developments: an overview
SQL
SQL
98
SQL
99
1.
2.
3.
4.
5.
6.
SQL
Do create tables
& constraints !!
CREATE TABLE TableName
{(colName dataType [NOT NULL]
[UNIQUE]
[DEFAULT defaultOption]
[CHECK searchCondition] [,...]}
[PRIMARY KEY (listOfColumns),]
{[UNIQUE (listOfColumns),] [,]}
{[FOREIGN KEY (listOfFKColumns)
REFERENCES ParentTableName
[(listOfCKColumns)],
[ON UPDATE referentialAction]
[ON DELETE referentialAction ]]
[,]}
{[CHECK (searchCondition)] [,] })
SQL
102
SQL
103