06 Ch6 4slot
06 Ch6 4slot
TO
DATABASE
L E A R N B Y E X A M P L E S
relational algebra.
There are many different dialects of SQL
Ansi SQL (or SQL-86), SQL-92, SQL-99
SQL:2003, SQL:2006, SQL:2008, SQL:2009
Transact-SQL (T-SQL) is Microsoft's and Sybase's proprietary
extension to SQL.
PL/SQL (Procedural Language/Structured Query Language) is
Oracle Corporation's procedural extension for SQL and the Oracle
relational database.
Today, SQL is accepted as the standard RDBMS language
I2DB: Algebraic Query Language
tblLocation
locNum
tblDependent
tblWorksOn depName
locName
I2DB: Algebraic Query Language
empSSN empSSN
proNum depSex
workHours depBirthdate
depRelationship
tblDepLocation
depNum
locNum
tblProject tblEmployee
proNum empSSN
proName empName
tblDepartment
locNum depNum empAddress
depNum depName empSalary
mgrSSN empSex
mgrAssDate empBirthdate
depNum
supervisorSSN
Syntax
I2DB: Algebraic Query Language
SELECT L
FROM R L(C(R))
WHERE C
[FROM table]
[WHERE conditions]
SELECT identifies what columns
ALL: Specifies that duplicate rows can appear in the result set. ALL is the default
DISTINCT: Specifies that only unique rows can appear in the result set. Null values are considered
equal for the purposes of the DISTINCT keyword
TOP n [ PERCENT ]:Specifies that only the first n rows are to be output from the query result set. n is
an integer between 0 and 4294967295. If PERCENT is also specified, only the first n percent of the
rows are output from the result set. When specified with PERCENT, n must be an integer between 0
and 100
FROM identifies which table
The WHERE clause follows the FROM clause. Condition: is composed of column names,
expressions, constants, and a comparison operator
Example 1: Listing all employees whose salary exceed at 50000
I2DB: Algebraic Query Language
Arithmetic operators
▪ +, -, *, /
Comparison operators
Logical operators
Example 4
I2DB: Algebraic Query Language
male employees
Two strings are equal (=) if they are the same sequence of
characters
I2DB: Algebraic Query Language
ak+1<bk+1
Example
fodder < foo
bar < bargain
SELECT SELECT
FROM FROM
WHERE s LIKE p; WHERE s NOT LIKE p;
Example 5.2
Find all employees whose name is ended at ‘Anh’
DATE ‘1948-05-14’
A time constant’s presentation
TIME ‘15:00:02.5’
A combination of dates and times
TIMESTAMP ‘1948-05-14 12:00:00’
Operations on date and time
Arithmetic operations
Comparison operations
Value withheld: we are not entitled to know the value that belongs here
Null is not a constant
Two rules for operating upon a NULL value in WHERE clause
Arithmetic operators on NULL values will return a NULL value
Comparisons with NULL values will return UNKNOWN
Truth table for True, False, and Unknown
We can think of TRUE=1; FALSE=0; UNKNOWN=1/2, so
I2DB: Algebraic Query Language
x y x AND y x OR y NOT x
TRUE TRUE TRUE TRUE FALSE
TRUE UNKNOWN UNKNOWN TRUE FALSE
TRUE FALSE FALSE TRUE FALSE
UNKNOWN TRUE UNKNOWN TRUE UNKNOWN
UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN
UNKNOWN FALSE FALSE UNKNOWN UNKNOWN
FALSE TRUE FALSE TRUE TRUE
FALSE UNKNOWN FALSE UNKNOWN TRUE
FALSE FALSE FALSE FALSE TRUE
Syntax
SELECT <list of attributes>
FROM <list of tables>
WHERE <conditions>
ORDER BY <list of attributes>
Order by clause follows Where and any other clauses. The ordering is
performed on the result of the From, Where, and other clauses, just
before Select clause
Using keyword ASC for ascending order and DESC for descending
order
Example 6:
I2DB: Algebraic Query Language
differences
When data from more than one table in the
database is required, a join condition is used.
I2DB: Algebraic Query Language
Example 7:
I2DB: Algebraic Query Language
Example 9:
Find all those project numbers which have more than
two members
We combine relations using the set operations of
relational algebra: union, intersection, and
I2DB: Algebraic Query Language
difference
SQL provides corresponding operators with
UNION, INTERSECT, and EXCEPT for , , and
-, respectively
Example 10.1
I2DB: Algebraic Query Language
Example 10.3
I2DB: Algebraic Query Language
department
Example 11:
I2DB: Algebraic Query Language
department
Example 11:
I2DB: Algebraic Query Language
department
unary R
value in unary R
A tuple in SQL is represented by a list of scalar
values between ()
I2DB: Algebraic Query Language
Example 12:
I2DB: Algebraic Query Language
number 1
To now, sub-queries can be evaluated once and
for all, the result used in a higher-level query
I2DB: Algebraic Query Language
Example 13:
I2DB: Algebraic Query Language
projectA
Another example:
I2DB: Algebraic Query Language
Find the titles that have been used for two or movies
SELECT title
(SELECT year
FROM Movies
Example 15.1
I2DB: Algebraic Query Language
Example 15.2
Find departments and employees who work in those
departments, respectively
More Example
I2DB: Algebraic Query Language
values
When padding dangling tuples from both of its
arguments, we use full outer join
When padding from left/right side, we use left
outer join/right outer join
Example 17.1:
I2DB: Algebraic Query Language
in it
Example 17.2:
I2DB: Algebraic Query Language
R UNION ALL S;
R INTERSECT ALL S;
R EXCEPT ALL S;
or more attributes
After grouping the tuples of relation, we are able
to aggregate certain other columns of relation
We use GROUP BY clause in SELECT statement
Five aggregation operators
SUM acts on single numeric column
I2DB: Algebraic Query Language
Example 18.1
I2DB: Algebraic Query Language
Example 18.2
Find number of employees
To partition the tuples of relation into groups
Syntax
I2DB: Algebraic Query Language
Example 19.1:
I2DB: Algebraic Query Language
Example 19.2
List number of employees for each department number
There are two kinds of terms in SELECT clause
Aggregations, that applied to an attribute or expression involving
I2DB: Algebraic Query Language
attributes
Grouping Attributes, that appear in GROUP BY clause
A query with GROUP BY is interpreted as follow:
Evaluate the relation R expressed by the FROM and WHERE
clauses
Group the tuples of R according to the attributes in GROUP BY
clause
Produce as a result the attributes and aggregation of the SELECT
clause
Example 20
I2DB: Algebraic Query Language
A B
NULL NULL
clause
If we want to apply conditions to groups of tuples
after grouping, those conditions are based on
some aggregations, how can we do?
In that case, we follow the GROUP BY clause
with a HAVING clause
Syntax:
I2DB: Algebraic Query Language
FROM tblWorksOn
GROUP BY proNum
HAVING AVG(workHours)>20
tuples
Basic form of insertion statement
INSERT INTO R(A1,…,An) VALUES (v1 ,…,vn );
I2DB: Algebraic Query Language
Example:
remove a department named ‘Phòng Kế Toán’
remove a department which depNum is 7
User 1 finds a
time seat empty
User2 finds a seat
empty
User1 sets seat
22A occupied
User2 sets seat
22A occupied
ACC: 3209
$500
ACC: 3208
executed atomically
That is, either all operations are performed or
none are
In SQL, each statement is a transaction by itself
SQL allows to group several statements into a
single transaction
Transaction begins by SQL command START
TRANSACTION
I2DB: Algebraic Query Language
end successfully
Consistency
Isolation
Durability
Atomicity: a transaction is an atomic unit of processing; it
should either be performed in its entirety or not performed at
I2DB: Algebraic Query Language
all.
At the end of the transaction, either all statements of the transaction is
successful or all statements of the transaction fail.
If a partial transaction is written to the disk then the Atomic property is
violated
Consistency: a transaction should be consistency preserving,
meaning that if it is completely executed from beginning to end
without interference from other transactions, it should take the
database from one consistent state to another.