0% found this document useful (0 votes)
18 views49 pages

06 Ch6 4slot

The document introduces SQL syntax and concepts, including SELECT statements, WHERE clauses, comparison operators, LIKE/NOT LIKE patterns, date/time data types, and NULL values.

Uploaded by

t1souu.studio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views49 pages

06 Ch6 4slot

The document introduces SQL syntax and concepts, including SELECT statements, WHERE clauses, comparison operators, LIKE/NOT LIKE patterns, date/time data types, and NULL values.

Uploaded by

t1souu.studio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

INTRODUCTION

TO
DATABASE
L E A R N B Y E X A M P L E S

 SQL (sequel) is a database language designed for managing data in


relational database management systems, and originally based upon
I2DB: Algebraic Query Language

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

SELECT [ ALL | DISTINCT ]


[ TOP n [ PERCENT ] ]
* | {column_name | expression [alias],…}
I2DB: Algebraic Query Language

[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

 Example 2: Listing name and salary of all employees whose income


exceed 50000

 Using alias name in select clause


Example 3:
I2DB: Algebraic Query Language

 Listing full name and salary of all employees whose

income exceed 50000


 Condition expression in where clause may use
 Constants and attributes
I2DB: Algebraic Query Language

 Arithmetic operators

▪ +, -, *, /

 Comparison operators

▪ =, <>, <, >, , ≥

 Logical operators

▪ and, or, not

 The result of condition must be True or False

 Example 4
I2DB: Algebraic Query Language

 List all under 40 year-old female or under 50 year-old

male employees
 Two strings are equal (=) if they are the same sequence of
characters
I2DB: Algebraic Query Language

 Other comparisons: <, >, , , ≠


 Suppose a=a1a2…an and b=b1b2…bm are two strings, the
first is less than the second if kmin(n,m):
 i, 1ik: ai = bi, and

 ak+1<bk+1
 Example
 fodder < foo
 bar < bargain

 Like or Not Like


I2DB: Algebraic Query Language

SELECT SELECT
FROM FROM
WHERE s LIKE p; WHERE s NOT LIKE p;

 Two special characters


 % means any sequence of 0 or more characters

 _ means any one character


 Example 5.1:
I2DB: Algebraic Query Language

 Find all employees named as ‘Võ Việt Anh’

 Example 5.2
 Find all employees whose name is ended at ‘Anh’

 USING ESCAPE keyword


 SQL allows us to specify any one character we like as
I2DB: Algebraic Query Language

the escape character for a single pattern


 Example
▪ WHERE s LIKE ‘%20!%%’ ESCAPE !
▪ Or WHERE s LIKE ‘%20@%%’ ESCAPE @
 Matching any s string contains the 20% string
▪ WHERE s LIKE ‘x%%x%’ ESCAPE x
 Matching any s string that begins and ends with the character %
 Dates and times are special data types in SQL
 A date constant’s presentation
I2DB: Algebraic Query Language

 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

 Null value: special value in SQL


 Some interpretations
I2DB: Algebraic Query Language

 Value unknown: there is, but I don’t know what it is

 Value inapplicable: there is no value that makes sense here

 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 AND y = MIN(x,y); x OR y = MAX(x, y); NOT x = 1-x

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

 SQL conditions in Where clause produce three


truth values: True, False, and Unknown
I2DB: Algebraic Query Language

 Those tuples which condition has the value True


become part of the answer
 Those tuples which condition has the value False
or Unknown are excluded from the answer
 Presenting the tuples produced by a query in sorted order
 The order may be based on the value of any attribute
I2DB: Algebraic Query Language

 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

 Listing all employee by department number

ascreasingly, then by salary descreasingly


I2DB: Algebraic Query Language

 SQL allows we combine two or more relations


through joins, products, unions, intersections, and
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

 Simple way to couple relations: list each relation


in the From clause
 Other clauses in query can refer to the attributes
of any of the relations in the From clause

 Example 7:
I2DB: Algebraic Query Language

 List all employees who work on ‘Phòng Phần mềm

trong nước’ department


 … a query involves several relations, and there
are two or more attributes with the same name?
I2DB: Algebraic Query Language

 We may list a relation R as many times as we


need
I2DB: Algebraic Query Language

 We use tuple variables to refer to each


occurrence of R
 Example 8:
I2DB: Algebraic Query Language

 Find all cities in which our company is

 … a query involves two or more tuples from the


same relation?
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

 Find all those employees whose name is begun by ‘H’

or salary exceed 80000


 Example 10.2
I2DB: Algebraic Query Language

 Find all those normal employees, that is who do not

supervise any other employees

 Example 10.3
I2DB: Algebraic Query Language

 Find all employees who work on projectB and projectC


I2DB: Algebraic Query Language

 One query can be used to help in the evaluation of


another
I2DB: Algebraic Query Language

 A query that is part of another is called a sub-query


 Sub-queries return a single constant, this constant can be
compared with another value in a WHERE clause
 Sub-queries return relations, that can be used in WHERE
clause
 Sub-queries can appear in FROM clauses, followed by a
tuple variable
 An atomic value that can appear as one
component of a tuple is referred to as a scalar
I2DB: Algebraic Query Language

 Let’s compare two queries for the same request

 Example 7: Find the employees of Phòng Phần


mềm trong nước department
I2DB: Algebraic Query Language
 Example 11:
I2DB: Algebraic Query Language

 Find the employees of Phòng Phần mềm trong nước

department

 Example 11:
I2DB: Algebraic Query Language

 Find the employees of Phòng Phần mềm trong nước

department
 Example 11:
I2DB: Algebraic Query Language

 Find the employees of Phòng Phần mềm trong nước

department

 Some SQL operators can be applied to a relation


R and produce a bool result
I2DB: Algebraic Query Language

 (EXISTS R = True)  R is not empty

 (s IN R = True)  s is equal to one of the values of R

 (s > ALL R = True)  s is greater than every values in

unary R

 (s > ANY R = True)  s is greater than at least one

value in unary R
 A tuple in SQL is represented by a list of scalar
values between ()
I2DB: Algebraic Query Language

 If a tuple t has the same number of components


as a relation R, then we may compare t and R
with IN, ANY, ALL

 Example 12:
I2DB: Algebraic Query Language

 Find the dependents of all employees of department

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

 But, some sub-queries are required to be


evaluated many times
 That kind of sub-queries is called correlated sub-
query
 Note: Scoping rules for names

 Example 13:
I2DB: Algebraic Query Language

 Find all those projects have the same location with

projectA
 Another example:
I2DB: Algebraic Query Language

 Find the titles that have been used for two or movies
SELECT title

FROM Movies Old

WHERE year < ANY

(SELECT year

FROM Movies

WHERE title =Old.title)

 In a FROM list we can use a parenthesized sub-query


 We must give it a tuple-variable alias
I2DB: Algebraic Query Language

 Example: Find the employees of Phòng Phần mềm trong


nước
SELECT *
FROM tblEmployee e,
(SELECT depNum
FROM tblDepartment
WHERE depName=N'Phòng phần mềm trong nước') d
WHERE e.depNum=d.depNum
 SQL Join Expressions can be stand as a query itself
or can be used as sub-queries in FROM clauses
I2DB: Algebraic Query Language

 Cross Join in SQL= Cartesian Product


 Syntax: R CROSS JOIN S;

 Meaning: Each tuple of R connects to each tuple of S

 Theta Join with ON keyword


 Systax: R JOIN S ON R.A=S.A;

 Meaning: Each tuple of R connects to those tuples of S,


which satisfy the condition after ON keyword

 Example 15.1
I2DB: Algebraic Query Language

 Product two relations Department and Employee

 Example 15.2
 Find departments and employees who work in those

departments, respectively
 More Example
I2DB: Algebraic Query Language

 A natural join differs from a theta-join in that:


I2DB: Algebraic Query Language

 The join condition: all pairs of attributes from the two

relations having a common name are equated, and


there are no other condition

 One of each pair of equated attributes is projected out

 Syntax : Table1 NATURAL JOIN Table2

 Microsoft SQL SERVER DONOT SUPPORT

NATURAL JOINS AT ALL


 The outer join is a way to augment the result of
join by the dangling tuples, padded with null
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

 For each location, listing the projects that are processed

in it
 Example 17.2:
I2DB: Algebraic Query Language

 For each department, listing the projects that it controls

 Study some operations that acts on relations as


whole, rather than on tuples individually
I2DB: Algebraic Query Language
 A relation, being a set, cannot have more than
one copy of any given tuple
I2DB: Algebraic Query Language

 But, the SQL response to a query may list the


same tuple several times, that is, SELECT
preserves duplicates as a default
 So, by DISTINCT we can eliminate a duplicates
from SQL relations

 Example 17.3: List all location in which the


projects are processed.
I2DB: Algebraic Query Language

 Location name is repeated many times


SELECT DISTINCT l.locNum, l.locName
FROM tblLocation l JOIN tblProject p ON l.locNum=p.locNum

SELECT DISTINCT l.locNum, l.locName


FROM tblLocation l JOIN tblProject p ON l.locNum=p.locNum
 Set operations on relations will eliminate
duplicates automatically
I2DB: Algebraic Query Language

 Use ALL keyword after Union, Intersect, and


Except to prevent elimination of duplicates
 Syntax:

R UNION ALL S;
R INTERSECT ALL S;
R EXCEPT ALL S;

 Grouping operator partitions the tuples of relation


into groups, based on the values of tuples in one
I2DB: Algebraic Query Language

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

 AVG acts on single numeric column

 MIN acts on single numeric column

 MAX acts on single numeric column

 COUNT act on one or more columns or all of columns

 Eliminating duplicates from the column before


applying the aggregation by DISTINCT keyword

 Example 18.1
I2DB: Algebraic Query Language

 Find average salary of all employees

 Example 18.2
 Find number of employees
 To partition the tuples of relation into groups
Syntax
I2DB: Algebraic Query Language

SELECT <list of attributes>


FROM <list of tables>
WHERE <condition>
GROUP BY <list of attributes>

 Example 19.1:
I2DB: Algebraic Query Language

 Group employees by department number

 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

 Compute the number of employees for each project


 When tuples have nulls, there are some rules:
 The value NULL is ignored in any aggregation
I2DB: Algebraic Query Language

▪ Count(*): a number of tuples in a relation


▪ Count(A): a number of tuples with non-NULL values for A
attribute

 NULL is treated as an ordinary value when forming


groups
 The count of empty bag is 0, other aggregation of empty
bag is NULL

 Example: Suppose R(A,B) as followed


I2DB: Algebraic Query Language

A B
NULL NULL

The result of query The result of query


SELECT A, count(B) SELECT A, sum(B)
FROM R FROM R
GROUP BY A; GROUP BY A;
is one tuple (NULL,0) is one tuple (NULL,NULL)
 If we want to apply conditions to tuples of
relations, we put those conditions in WHERE
I2DB: Algebraic Query Language

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

SELECT <list of attributes>


FROM <list of tables>
WHERE <conditions on tuples>
GROUP BY <list of attributes>
HAVING <conditions on groups>
 Example 21:
I2DB: Algebraic Query Language

 Print the number of employees for each those

department, whose average salary exceeds 80000

 Some rules about HAVING clause


I2DB: Algebraic Query Language

 An aggregation in a HAVING clause applies only to the

tuples of the group being tested

 Any attribute of relations in the FROM clause may be

aggregated in the HAVING clause, but only those


attributes that are in the GROUP BY list may appear un-
aggregated in the HAVING clause (the same rule as for
the SELECT clause)
 Example:
SELECT proNum, COUNT(empSSN) AS Number_Of_Employees,
I2DB: Algebraic Query Language

FROM tblWorksOn
GROUP BY proNum
HAVING AVG(workHours)>20

SELECT proNum, COUNT(empSSN) AS Number_Of_Employees,


FROM tblWorksOn
GROUP BY proNum
HAVING proNum=4

 Page 257, Exercise 6.1.3


Page 267, Exercise 6.2.2
I2DB: Algebraic Query Language

 Page 279, Exercise 6.3.1


 Page 289, Exercise 6.4.6
I2DB: Algebraic Query Language

 Three types of statements that allow us to


I2DB: Algebraic Query Language

 Insert tuples into relation

 Delete certain tuples from a relation

 Update values of certain components of certain existing

tuples
 Basic form of insertion statement
INSERT INTO R(A1,…,An) VALUES (v1 ,…,vn );
I2DB: Algebraic Query Language

 A tuple is created using the values v1 for attribute Ai

 If the list of attributes does not include all attributes of the


relation R, then the tuple created has default values for all
missing attributes
 The basic form allows to insert only one tuple at once
 The extended form allows to insert a set of tuples as result of a
sub query:
INSERT INTO R(A1, ..., An)
SELECT A1, …, An FROM … WHERE …

 Example: add new a department


I2DB: Algebraic Query Language
 The form of a deletion
I2DB: Algebraic Query Language

DELETE FROM R WHERE <condition>;

 Example:
 remove a department named ‘Phòng Kế Toán’
 remove a department which depNum is 7

 If no WHERE clause, all the department will be deleted. Be carefully !

 The form of an update statement


I2DB: Algebraic Query Language

UPDATE R SET <new‐value assignments> WHERE <condition>;

 Example: Update new salary and depNum for the


employee named ‘Mai Duy An’
 Example: increase salary to 10% for all employees who
belongs to ' Phòng Phần mềm trong nước ' department
I2DB: Algebraic Query Language
I2DB: Algebraic Query Language
 DB User operates on database by querying or
modifying the database
I2DB: Algebraic Query Language

 Operations on database are executed one at a


time
 Output of one operation is input of the next
operation
 So, how the DBMS treats simultaneous
operations?

 In applications, many operations per second may


be performed on database
I2DB: Algebraic Query Language

 These may operate on the same data


 We’ll get unexpected results
 Example: Two users book the same seat of the
flight
I2DB: Algebraic Query Language

User 1 finds a
time seat empty
User2 finds a seat
empty
User1 sets seat
22A occupied
User2 sets seat
22A occupied

 Transaction is a group of operations that need to


be performed together
I2DB: Algebraic Query Language

 A certain transaction must be serializable with


respect to other transactions, that is, the
transactions run serially – one at a time, no
overlap
 A certain combinations of database operations
need to be done atomically, that is, either they are
I2DB: Algebraic Query Language

all done or neither is done

 Example: Transfer $500 from the account number


3209 to account number 3208 by two steps
I2DB: Algebraic Query Language

 (1) Subtract $500 from account number 3209

 (2) Add $500 to account number 3208

 What happen if there is a failure after step (1) but


before step (2)?
$500
I2DB: Algebraic Query Language

ACC: 3209

$500

ACC: 3208

 Transaction is a collection of one or more


operations on the database that must be
I2DB: Algebraic Query Language

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

 Two ways to end a transaction


 The SQL statement COMMIT causes the transaction to

end successfully

 The SQL statement ROLLBACK causes the transaction

to abort, or terminate unsuccessfully

 Transactions should possess several properties,


often called the ACID properties; they should be
I2DB: Algebraic Query Language

enforced by the concurrency control and recovery


methods of the DBMS. The following are the ACID
properties:
 Atomicity

 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.

 Isolation: a transaction should appear as though it is


being executed in isolation from other transactions, even
I2DB: Algebraic Query Language

though many transactions are executing concurrently.


That is the execution of a transaction should not be
interfered with by any other transactions executing
concurrently.

 Durability : the changes applied to the database by a


committed transaction must persist in the database.
These changes must not be lost because of any failure..
 A transaction can read or write some data into the
database
I2DB: Algebraic Query Language

 When a transaction only reads data and does not


write data, the transaction may execute in parallel
with other transactions
 Many read-only transactions access the same data to
run in parallel, while they would not be allowed to run
in parallel with a transaction that wrote the same data

 SQL statement set read-only to the next


transaction
I2DB: Algebraic Query Language

 SET TRANSACTION READ ONLY;

 SQL statement set read/write to the next


transaction
 SET TRANSACTION READ WRITE;
 Dirty data: data written by a transaction that has not
yet committed
I2DB: Algebraic Query Language

 Dirty read: read of dirty data written by another


transaction
 Problem: the transaction that wrote it may eventually
abort, and the dirty data will be removed
 Sometimes the dirty read matters, sometimes it
doesn’t
I2DB: Algebraic Query Language

Transaction 1 Transaction 2 Logical value Uncommitted What


value transaction 2
show
START T1 3
UPDATE A=5 START T2 3 5
... SELECT @v=A 3 5 5
ROLLBACK UPDATE B=@v 3 5
 We can specify that dirty reads are acceptable for
a given transaction
I2DB: Algebraic Query Language

 SET TRANSACTION READ WRITE

ISOLATION LEVEL READ UNCOMMITTED;

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITED;


SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
I2DB: Algebraic Query Language

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;


SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

You might also like