06 SQL
06 SQL
06 SQL
SQL introduction
6.2
6.3
studioName=DisneyAND year=1990(Movies))
title
SELECT
FROM
WHERE
AND
*
Movies
studioName = Disney
year = 1990;
Pretty
1990 119
Women
true
studioName procucerC#
Disney
6.4
999
Projection in SQL
Find the title and length of all movies produced by Disney Studios
in 1990.
FROM
WHERE
AND
Movies
studioName = Disney
year = 1990;
title
length
Pretty Women
119
6.5
Projection in SQL
we can modify the name of attributes. We can change title to name and
length to duration in the previous example.
SELECT title AS name, length AS duration
FROM
WHERE
AND
Movies
studioName = Disney
year = 1990;
FROM
WHERE
AND
length/60 AS Length_In_Hours
Movies
studioName = Disney
year = 1990;
6.6
Projection in SQL
SELECT title,
FROM
WHERE
AND
length/60 AS Length
hrs. AS inHours
Movies
studioName = Disney
year = 1990;
title
length
inHours
Pretty Women
1.98334
hrs.
6.7
Selection in SQL
We may build the WHERE part using six common comparison
FROM
WHERE
MGM
Movies
( year > 1970 or length <90) AND studioName =
Dictionary rules.
6.8
FROM
WHERE
Movies
title LIKE Star _ _ _ _;
6.9
quoted string.
For example: DATE 1961-08-24
Note the strict format of the YYYY-mm-dd
6.10
clause:
ORDER BY <list of attributes>
The order is by default ascending (ASC), but we can get the output highest-
6.11
Tuple Variables
6.12
SELECT *
FROM
Movies, MovieExec
WHERE
AND
producerC# = cert#;
6.13
MovieExec)
Basic Selects
Basics on Selects examples
6.14
Disambiguating Attributes
Sometimes we ask a query involving several relations, with two or
FROM
MovieStar, MovieExec
WHERE MovieStar.address =
MovieExec.address;
6.15
Tuple Variables
Two stars that share an address
SELECT Star1.name, Star2.name
6.16
WHERE
gender = F)
INTERSECT
(SELECT name, address FROM MovieExec
WHERE
6.17
6.18
UNION
(SELECT movieTitle AS title, movieYear AS year
FROM StarsIn)
6.19
6.20
Value unknown
I know there is some value here but I dont know what it is?
1.
2.
Value inapplicable
There is no value that make sense here.
1.
3.
Value withheld
We are not entitled to know this value.
1.
6.21
And is like min: true and unknown is unknown, false and unknown
is false.
6.22
Null Values
Null Values examples
6.23
SUBQUERIES
Subqueries that Produce Scalar Values
Conditions Involving Relations
6.25
SELECT name
FROM MovieExec
WHERE cert# = (SELECT producerC#
FROM
Movies
WHERE
);
What would happen if the subquery retrieve zero or more than one
tuple?
Runtime error
SELECT name
FROM MovieExec
WHERE cert# = 12345
6.26
6.27
entire expression.
NOT EXISTS R, NOT s > ALL R, NOT s > ANY R
s NOT IN R is the negation of IN operator.
Some situations of these operators are equal to other operators.
For example:
6.28
6.29
SELECT list.
6.30
movieYear, starName)
MovieStar(name, address, gender, birthdate)
MovieExec(name, address, cert#, netWorth)
Studio(name, address, cert#, netWorth)
MovieExec;
cert# IN
(SELECT producerC#
FROM
Movies
WHERE
(title, year) IN
StarsIN
WHERE
subqueries.
For example, the previous query can be written as follows:
SELECT name
FROM
WHERE
cert# = producerC#
AND
title = movieTitle
AND
year
And
= movieYear
6.32
Correlated Subqueries
The simplest subquery is evaluated once and the result is used in a
higher-level query.
Some times a subquery is required to be evaluated several times, once
for each assignment of a value that comes from a tuple variable outside
the subquery.
A subquery of this type is called correlated subquery.
6.33
Movies old
WHERE
title = old.title);
Nested loop.
For each value of old title we run the the nested subquery
6.34
Subqueries
Subqueries by Dr. Widom
6.35
SELECT A1, An
FROM R1, . Rm
WHERE condition up to now we have used sub-query
6.36
MovieExec,
(SELECT producerC#
FROM
Movies, StarsIN
WHERE
title = movieTitle
AND
year
AND
= movieYear
) Prod
WHERE
cert# = Prod.producerC#;
6.37
Subqueries
Subqueries in From Clauses examples
6.38
subquery.
Cross join is the simplest form of a join.
Actually, this is synonym for Cartesian product.
For example:
6.39
Then the result of the CROSS JOIN would be a relation with the
following attributes:
Note that if there is a common name in the two relations, then the
6.40
6.41
title and movieTitle, and year and movieYear, then we encounter the
redundancy of information.
One way to remove the unnecessary attributes is projection. You can
6.42
Natural Joins
Natural join and theta-join differs in:
1.
2.
Example
MovieStar NATURAL JOIN MovieExec
6.43
Natural Joins
Query those stars who are executive as well.
The relations are:
MovieStar(name, address, gender, birthdate)
MovieExec(name, address, cert#, netWorth)
SELECT MovieStar.name
FROM MovieStar NATURAL JOIN MovieExec
6.44
Outer Joins
Outer join is a way to augment the result of a join by dangling tuples,
2.
3.
6.45
join.
NATURAL LEFT OUTER JOIN
the third.
NATURAL RIGHT OUTER JOIN
6.46
FULL-RELATION OPERATIONS
Eliminating Duplicates
Duplicates in Unions, Intersections, and Differences
Grouping and Aggregation in SQL
Aggregation Operators
Grouping
Grouping, Aggregation, and Nulls
Having Clauses
47
6.47
Eliminating Duplicates
Query all the producers of movies in which LEONARDO DICAPRIO stars.
SELECT DISTINCT name
FROM
WHERE
cer# = producerC#
AND
title = movieTitle
AND
year
And
= movieYear
6.48
6.49
operators.
For example, we can sum up the salary of the employees of each
6.50
Aggregation Operators
SQL uses the five aggregation operators:
column name.
One exception is COUNT(*) which counts all the tuples of a query
output.
We can eliminate the duplicate values before applying aggregation
FROM
MovieExec;
6.51
Aggregation Operators
Count the number of tuples in the StarsIn relation.
SELECT COUNT(*)
FROM
StarsIn;
SELECT COUNT(starName)
FROM
StarsIn;
These two statements do the same but you will see the difference in later
slides.
6.52
Grouping
We can group the tuples by using GROUP BY clause following the
WHERE clause.
The keywords GROUP BY are followed by a list of grouping attributes.
studioName,
SUM(length) AS Total_Length
FROM
Movies
GROUP BY studioName;
6.53
Grouping
In a SELECT clause that has aggregation, only those attributes that are
SELECT list, then, you must mention it in the GROUP BY list as well.
SELECT
studioName, genre,
SUM(length) AS Total_Length
FROM
Movies
6.54
Grouping
It is possible to use GROUP BY in a more complex queries about
several relations.
2.
3.
Create a list of each producer name and the total length of film produced.
SELECT name, SUM(length)
FROM
MovieExec, Movies
WHERE
producerC# = cert#
GROUP BY name;
6.55
values?
There are a few rules to remember
1.
2.
3.
4.
6.56
GROUP BY A;
GROUP BY A;
GROUP BY A;
The result is (NULL, NULL) because SUM(B) address one NULL value
which is NULL.
6.58
HAVING Clauses
So far, we have learned how to restrict tuples from contributing in the
output of a query.
How about if we don't want to list all groups?
HAVING clause is used to restrict groups.
HAVING clause followed by one or more conditions about the group.
Query the total film length for only those producers who made at least one
film prior to 1930.
SELECT name, SUM(length)
FROM
MovieExec, Movies
WHERE
producerC# = cert#
GROUP BY name
HAVING MIN(year) < 1930;
6.59
HAVING Clauses
The rules we should remember about HAVING:
1.
2.
6.60
HAVING Clauses
The order of clauses in SQL queries would be:
SELECT
FROM
WHERE
GROUP BY
HAVING
6.61
DATABASE MODIFICATIONS
Insertion
Deletion
Updates
6.62
Insertion
The syntax of INSERT statement:
6.63
Insertion
If we are sure about the order of the attributes, then we can write
the statement as follows:
6.64
Insertion
The simple insert can insert only one tuple, however, if you want to
insert multiple tuples , then you can use the following syntax:
INSERT INTO R(A1, ..., AN)
SELECT v1, ..., vn
FROM
R1, R2, ..., RN
WHERE <condition>;
Suppose that we want to insert all studio names that are mentioned in
the Movies relation but they are not in the Studio yet.
Deletion
The syntax of DELETE statement:
DELETE FROM R
WHERE <condition>;
Every tuples satisfying the condition will be deleted from the relation R.
Updates
The syntax of UPDATE statement:
UPDATE R
SET
<value-assignment>
WHERE <condition>;
Every tuples satisfying the condition will be updated from the relation R.
If there are more than one value-assignment, we should separate them
with comma.
Attach the title 'Pres.' in front of the name of every movie executive who is
the president of a studio.
UPDATE MovieExec
SET name = 'Pres.' || name
WHERE
6.67
TRANSACTIONS IN SQL
Serializability
Atomicity
Transactions
Read-Only Transactions
Dirty Reads
Other Isolation Levels
Exercises for Section 6.6
6.68
There are millions of users using the same database and it is possible
6.69
6.6.1 Serializability
In applications like web services, banking, or airline reservations,
6.70
71
6.71
Flights
WHERE
UPDATE Flights
SET
seatStatus = 'occupied'
WHERE
is called 'transaction'.
6.73
grouped in a transaction.
SQL allows the programmer to state that a certain transaction must be
6.74
6.6.2 Atomicity
What would happen if a transaction consisting of two operations is in
progress and after the first operation is done, the database and/or
network crashes?
Let's take an example.
6.75
2.
6.76
6.77
6.78
6.6.3 Transactions
The solution to the problems of serialization and atomicity is to group
START TRANSACTION
6.79
2.
6.80
6.81
6.82
6.83
84
6.84
commit it.
Sometimes dirty read doesnt matter much and is not worth
6.85
2.
b.
Imagine, there are 3 accounts A1, A2, and A3 with $100, $200, and $300.
86
6.86
87
6.87
88
6.88
Serializable (default)
Read-uncommitted
Read-committed
Syntax:
SET TRANSACTION
ISOLATION LEVEL READ COMMITTED;
89
6.89
90
6.90
same tuples.
That's because there may be some new committed tuples by other
transactions.
The query may show more tuples because of the phantom tuples.
A phantom tuple is a tuple that is inserted by other transactions.
91
6.91
92
6.92
Isolation
Level
Dirty Read
Phantom
Read
Uncommitted
Read
Committed
Serializable
95
6.95