Structured Query Language
SQL
Basics
- Each SQL command is separated by a semicolon.
- Ignores redundant whitespaces (recommend to use them for visualizing).
- Two dashes (
--
) introduce comments.- Whatever follows them is ignored up to the end of the line.
- Single-quoted string literals.
- Case insensitive about keywords and identifiers
- Except when identifiers are double-quoted to preserve the case.
Domain Types
char(n)
: fixed length(n) stringvarchar(n)
: variable length(up to n) stringint
: integersmallint
: small integernumeric(m, d)
: fixed point ($m \times 10^{-d}$)real
,double precision
: floating pointfloat(n)
: floating point of at least n digit precision- Date and Time (ISO 8601) - dependent to dbms
date
:yyyy-mm-dd
datetime
:yyyy-mm-dd hh:mm:ss[.mmm]
time
:hh:mm:ss[.mmmmmm]
Create Table
1
2
3
4
CREATE TABLE table_name (
{ column_name domain_type { column_constraint }* },+
{ , table_constraint }*
);
Where column constraints are:
1
2
3
4
5
NOT NULL
UNIQUE
PRIMARY KEY
CHECK ( condition )
REFERENCES table_name { ( { column_name },+ ) }? { ON DELETE referential_action }? { ON UPDATE referential_action }?
Where table constraints are:
1
2
3
4
UNIQUE ( { column_name },+ )
PRIMARY KEY ( { column_name },+ )
CHECK ( condition )
FOREIGN KEY ( { column_name },+ ) REFERENCES table_name ( { column_name },+ ) { ON DELETE referential_action }? { ON UPDATE referential_action }?
Where referential actions are:
1
2
3
CASCADE
SET NULL
SET DEFAULT
Integrity Constraints
Integrity constraints ensures correctness of data under specific business logic and guard against accidental damage to the database.
NOT NULL
: value of the column must not be nullUNIQUE
: specified column(s) form candidate keyPRIMARY KEY
: specified column(s) form primary key- implies
NOT NULL
andUNIQUE
- implies
CHECK
: value of column or tuple must ensure the condition- referential integrity: value of the column(s) must appear in the column(s) of another relation
Referential Action
Actions to perform on event is called referential action.
ON DELETE
and ON UPDATE
.
CASCADE
: delete/update on referenced relation is cascadedSET NULL
: set nullSET DEFAULT
: set default
Drop Table
1
DROP TABLE table_name;
Alter Table
1
2
ALTER TABLE table_name ADD column_name domain_type;
ALTER TABLE table_name DROP column_name;
Select (Query)
Result of SQL Query is analogous to relation, except that duplicated row is allowed.
1
2
3
4
5
6
SELECT { DISTINCT }? { * | { expression { { AS }? output_name }? },+ }
FROM { from_item },+
{ WHERE condition }?
{ GROUP BY { grouping_element },+ }?
{ HAVING condition }?
{ ORDER BY { expression { ASC | DESC } },+ }?;
Expression (SELECT
Clause)
A select clause can contain any expressions, such as arithmetic expressions.
Aggregate Function
- Takes values of a column of a relation and return single value.
NULL
values are ignored, exceptcount (*)
avg
,min
,max
,sum
,count
count
is little differentcount (DISTINCT column_name)
count (*)
counts the number of columns, includingNULL
.
From Item (FROM
Clause)
From item
includes but not limited to table.- List of
from item
s corresponds to cartesian product. - Joining two
from item
s are available - Result of a subquery can be
from item
.- In this case the result of subquery requires an alias.
1
2
3
table_name { { AS }? alias { ( { column_alias },+ ) }? }?
from_item { NATURAL }? join_type from_item { ON join_condition }? { USING ( { column_name },+ ) }?
( subquery ) { AS } alias { ( { column_alias },+ ) }?
Rename (AS
Clause)
AS
clause can rename expressions, from item
s, etc.
AS
keyword is optional.
- Called tuple variables
- Renaming its columns is also possible.
- Called correlation variables when used in correlated subquery.
Tuple variables are included in the local context of query, so that later it can be referenced.
Condition (WHERE
Clause)
Corresponds to selection predicate of the relational algebra. Various operators, functions and predicates are supported.
Logical Operators
- Three predicates
AND
,OR
,NOT
- Three valued logic -> Three boolean values
TRUE
,FALSE
,NULL
.- As mentioned,
NULL
is a member of ever domain, including boolean.
- As mentioned,
- Logical operation with
NULL
returnsNULL
exceptFALSE AND NULL
(=FALSE
)TRUE OR NULL
(=TRUE
)
- Where clause treats
NULL
asFALSE
Comparison Operators
<
,>
,=
,<=
,>=
,!=
(or<>
)- Comparison with
NULL
returnsNULL
, sinceNULL
represents unknown.
Comparison Predicates
e1 BETWEEN e2 AND e3
: equivalent toe1 >= e2 AND e1 <= e3
- Range inclusive
IS
: always returnTRUE
orFALSE
by treatingNULL
as comparable value- e.g.,
e IS NULL
- e.g.,
String Predicates
LIKE
: pattern matching%
: any substring_
: any character- e.g.,
title LIKE '%data%'
: any pattern which includes ‘data’ substring
Grouping (GROUP BY
Clause)
Groups the tuples.
- Aggregate function aggregates column values by group.
- Non-grouping attributes must appear in
SELECT
with aggregate function.
HAVING
Clause
Condition applied after grouping. WHERE
clause is applied before grouping.
HAVING
clause can be replaced by subquery in FROM
clause. (Better performance)
1
2
3
4
5
6
7
8
9
10
SELECT a, avg(b)
FROM tbl
GROUP BY a
HAVING avg(b) > 500;
-- is equivalent to
SELECT a, avg_b
FROM (SELECT a, avg(b) AS avg_b
FROM tbl
GROUP BY a)
WHERE avg_b > 500;
Ordering
Sort the result. The last step of query operation.
Nested Query (Subquery)
Subquery is a nested select-from-where expression.
Common usecase: from item
, set operation / membership / comparison
Set Operations
- Idential to set operations in relational algebra.
UNION
,INTERSECT
,EXCEPT
- Operates on relation(query result).
- e.g.,
(select name from instructors) union (select name from students)
- e.g.,
- Automatically eliminates duplicate rows
UNION ALL
,INTERSECT ALL
,EXCEPT ALL
preserves duplicates.- when m, n duplcate rows,
UNION ALL
: m + nINTERSECT ALL
: min(m, n)EXCEPT ALL
: min(0, m - n)
Set Membership
- Logical operation whether tests a tuple is in a relation (usually subquery).
in
,not in
- e.g.,
(id, name) in (select id, name from dept where no = '301')
Set Comparison
some
: tuple exists in a relation which satisfies condition.all
: all tuple in a relation satisifies condition.- e.g.,
score > some (select score from student where name like '%ab%')
- e.g.,
exists
: whether relation is not empty.- e.g.,
exists (select * from student where name like '%ab%')
- e.g.,
not exists
: whether relation is empty.
Join
1
2
3
from_item { NATURAL }? join_type from_item
{ ON join_condition }?
{ USING ( { column_name },+ ) }?
Join Condition
Which tuples in each relations match, and what attributes are present in the result.
Natural
: use all columns with same name, retain only one copy- Must be careful
ON
Clause: specify the join conditionUSING
Clause: specify which common columns to test for equality
Join Type
How tuples in each relation that don’t match any in the other are treated.
- (Inner) Join: Unmatched tuples are discarded
- Left Outer Join: Unmatched tuples from left relation are filled with null
- Right Outer Join: Unmatched tuples from right relation are filled with null
- Full Outer Join: Unmatched tuples from both relation are filled with null
Insert Into
1
INSERT INTO table_name { ( { column_name },+ ) }? insert_values;
Where insert values are:
1
2
VALUES { ( { expression },+ ) },+
subquery
Update
1
2
3
UPDATE table_name
SET { { column_name = expression | ( { column_name },+ ) = ( { expression },+ ) } },?
{ WHERE condition }?;
Delete From
1
2
DELETE FROM table_name
{ WHERE condition }?;
Role and Privilege
Roles represent the group of users and their privileges.
Grant/Revoke:
- privilege to/from role
- privilege to/from user
- role_A to/from role_B (role_B implies role_A)
- role to/from user
Privileges:
select
insert
update
delete
resources
: create tableindex
: create indexalteration
: alterdrop
all { privileges }?
1
2
3
4
5
CREATE ROLE role_name;
GRANT { { privilege },+ ON table_name | role_name }
TO { public | { user_or_role_name },+ };
REVOKE { { privilege },+ ON table_name | role_name }
FROM { user_or_role_name },+;
Discussion
Integrity Constraint
Q. What is an integrity constraint?
- relation의 tuple들이 항상 만족해야 하는 조건
- constraint가 깨지면 의도하지 않은 일이 발생할 수 있음
Integrity Constraint w/ Create
Q. Why are integrity constraint declarations, such as primary key and foreign key, part of the create table statement instead of, say, the select or insert statement?
A. Integrity constraint는 relation의 속성이기 때문. 반면 Select나 Insert는 relation의 tuple과 관련 있는 command.