Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Chapter 6 - Simple Queries in SQL

DBI202
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Chapter 6 - Simple Queries in SQL

DBI202
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 164

ĐẠI HỌC FPT CẦN THƠ

Chapter 6
Simple Queries in SQL
Objectives

1 Understand why must use Bags concept

2 Understand how to write simple queries

3 Understand about NULL values

4 Understand about the string data type, date-time data type

5 Know how to order the output


Contents

1 Bag concept

2 Projection in SQL

3 Selection in SQL

4 Comparison of Strings and Pattern matching in SQL

5 Date time data type

6 NULL values

7 Ordering the output


6.1 Relational operations on Bags

A bag or multiset is like a set, but an element may


appear more than once.
Exp: {1,2,1,3} is a bag.
Order in a bag is unimportant.
Some operations, like projection or union, are
much more efficient on bags than sets. Why?
6.1 Relational operations on Bags

Some operations, like projection or union, are


much more efficient on bags than sets because
duplicates are not eliminated.
Selection applies to each tuple, product and join
are done on each pair of tuples, so duplicates in
bags have no effect.
6.1 Relational operations on Bags

Some operations, like projection or union, are


much more efficient on bags than sets because
duplicates are not eliminated.
Selection applies to each tuple, product and join
are done on each pair of tuples, so duplicates in
bags have no effect.
6.1 Relational operations on Bags

Some operations, like projection or union, are


much more efficient on bags than sets because
duplicates are not eliminated.
Selection applies to each tuple, product and join
are done on each pair of tuples, so duplicates in
bags have no effect.

Bag projection yields


always the same
number of tuples as
the original relation
6.1 Relational operations on Bags
6.1 Relational operations on Bags
6.1 Relational operations on Bags
6.1 Relational operations on Bags
6.1 Relational operations on Bags
The Extended Algebra
Outerjoin
6.2 Relational algebra and
Datalog
6.2 Relational algebra and
Datalog
6.2 Relational algebra and
Datalog
6.2 Relational algebra and
Datalog
6.2 Relational algebra and
Datalog
Operations
Operations Relational algebra Datalog

Projection

Selection

Product

Joins
6.3 SQL Overview

The most commonly used relational DBMS’s query and


modify the database through a language called SQL.

SQL (stands for Structured Query Language) is the set


of statements with which all programs and users access
data in an database.

The language, Structured English Query Language


("SEQUEL") was developed by IBM Corporation. SEQUEL
later became SQL (still pronounced "sequel").

Today, SQL is accepted as the standard RDBMS language.


6.3 SQL Revisions
Year Name Alias Comments

1986 SQL-86 SQL-87 First published by ANSI. Ratified by ISO in 1987.

1989 SQL-89 FIPS 127-1 Minor revision, adopted as FIPS 127-1.

1992 SQL-92 SQL2, FIPS 127-2 Major revision (ISO 9075), Entry Level SQL-92 adopted as FIPS 127-2.

1999 SQL:1999 SQL3 Added regular expression matching, recursive queries, triggers, support for procedural
and control-of-flow statements, non-scalar types, and some object-oriented features.

2003 SQL:2003 Introduced XML-related features, window functions, standardized sequences, and
columns with auto-generated values (including identity-columns).

2006 SQL:2006 ISO/IEC 9075-14:2006 defines ways in which SQL can be used in conjunction with
XML. It defines ways of importing and storing XML data in an SQL database,
manipulating it within the database and publishing both XML and conventional SQL-
data in XML form. In addition, it provides facilities that permit applications to integrate
into their SQL code the use of XQuery, the XML Query Language published by the
World Wide Web Consortium (W3C), to concurrently access ordinary SQL-data and
XML documents.

2008 SQL:2008 Defines more flexible windowing functions, clarifies SQL 2003 items that were still
unclear [1]
6.3 Transact SQL (T-SQL)

Transact-SQL (T-SQL) is Microsoft's and


Sybase's proprietary extension to SQL

T-SQL

SQL
6.3 Sub-languages of T-SQL

T - SQL

DDL
(Data Definition Language)

DML
(Data Manipulation Language)

DCL
(Data Control Language)
6.3 Sub-languages of T-SQL
6.4 SELECT commands
A SELECT statement retrieves information from the database. Using a SELECT
statement, you can do the following:
• Projection: You can use the projection capability in SQL to choose the
columns in a table that you want returned by your query.
• Selection: You can use the selection capability in SQL to choose the rows
in a table that you want returned by a query (with WHERE clause)
• Joining: You can use the join capability in SQL to bring together data that
is stored in different tables by creating a link between them.
6.4 SELECT commands

A keyword refers to an individual SQL element.


For example, SELECT and FROM are keywords.
A clause is a part of a SQL statement.
For example, SELECT employee_id, last_name, ... is
a clause.
A statement is a combination of two or more
clauses.
For example, SELECT * FROM employees is a SQL
statement.
6.4 Basic Syntax
for a simple SELECT queries
SELECT [ ALL | DISTINCT ]
[ TOP n [ PERCENT ] ]
* | {column_name | expression [alias],…}
[FROM table]

❖ 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
6.4 A trick for
reading & writing queries

It’s generally easiest to examine a SELECT-FROM-


WHERE query by:
First looking at the FROM clause to learn which relations are
involved in the query
Then, move to the WHERE clause to learn what it is about
tuples that is important to the query
Finally, look at the SELECT clause to see what the output
format is
The same order: FROM, then WHERE, then SELECT is
often useful when writing queries of your own as well
6.4 Projection in SQL

We can, if we wish, eliminate some of the components


of the chosen tuples; that is, we can project the
relation produced by a SQL query onto some of its
attributes
In place of the * of the SELECT clause, we may list
some of the attributes of the relation mentioned in the
FROM clause. The result will be projected onto the
attributes listed
Example: SELECT all columns
Example: Projection in SQL
Example: Extended projection using
Arithmetic Operators

❖ Note that the resultant calculated column SALARY+300 is not


a new column in the EMPLOYEES table; it is for display only.
❖ By default, the name of a new column comes from the
calculation that generated it— in this case, salary+300.
6.4 Renaming or Defining a
Column Alias
A column alias:
Renames a column heading
Is useful with calculations
Immediately follows the column name - there can also be
the optional AS keyword between the column name and
alias
Example: ALIAS
SELECT last_name AS ‘Name’,

salary*12 AS ‘Annual Salary’

FROM employees

The example displays the last names and annual salaries of


all the employees.
Because Annual Salary contain a space, it has been enclosed
in double quotation marks.
Notice that the column heading in the output is exactly the
same as the column alias.
6.4 Duplication Eliminating
with SELECT distinct
6.5 Selection in SQL or Restricting data

While retrieving data from the database, you may


need to restrict the rows of data that are displayed
In that case, the solution is to use the WHERE clause
The WHERE clause is equal to the selection operator
of relational algebra
The expression that may follow WHERE include
conditional expressions like those found in C or Java
6.5 Selection in SQL or Restricting data

SELECT [ ALL | DISTINCT ]


[ TOP n [ PERCENT ] ]
* | {column_name | expression [alias],…}
[FROM table]
[WHERE conditions]

WHERE: restricts the query to rows that meet a


condition
The WHERE clause follows the FROM clause.
Condition: is composed of column names,
expressions, constants, and a comparison operator
Example: Restricting data
Example: Restricting data
6.5 Comparison of Strings

Two strings are equal if they are the same sequence


of characters. Recall from the section 2.3.2 that
strings can be stored as fixed-length strings (using
CHAR) or variable-length strings (using VCHAR)
When comparing strings with different declarations,
only the actual strings are compared (SQL ignores any
“pad” characters that must be presenet in the
database in order to give a string its required length)
We can use “<“, “>”, “=“ and “<>” operators to
compare two strings
6.5 Pattern matching in SQL

SQL also provides the capability to compare strings on


the basis of a simple pattern match. An alternative
form of comparision expression is:
s LIKE p
where:
S: is a string
P: is a pattern (with the optional use of some special
characters: “%”, “_” ..)
Similarly, “s NOT LIKE p” is true if and only if string s
does not match pattern p
6.5 Dates and Times

SQL generally support dates and times as special data


types. These values are often representable in a
variety of formats such as:
‘05/14/1948’ or
’14 May 1948’
We can compare dates or times using the same
comparison operators we use for numbers or strings
6.5 NULL values

Null means 'nothing' or without value or consequence


Null is a special marker used in SQL to indicate that a data
value does not exist in the database. Introduced by the creator
of the relational database model
Since Null is not a member of any data domain, it is not
considered a "value", but rather a marker (or placeholder)
indicating the absence of value. Because of this, comparisons
with Null can never result in either True or False, but always in
a third logical result, Unknown
However, certain operations on Null can return values if the
value of Null is not relevant to the outcome of the operation
6.5 The Truth-value ‘Unknown’

Null is untyped in SQL, meaning that it is not


designated as an integer, character, or any other
specific data type
6.5 Ordering the Output

While retrieving data from the database, you may


need to specify the order in which the rows are
displayed.
In that case, the solution is to use the ORDER BY
clause
6.5 Ordering the Output

SELECT [ ALL | DISTINCT ]


[ TOP n [ PERCENT ] ]
* | {column_name | expression [alias],…}
FROM table
[WHERE conditions]
[ORDER BY {expression [ASC | DESC] ,…} ]

If you use the ORDER BY clause, it must be the last clause of the SQL
statement.
Expression: Specifies a column on which to sort.
A sort column can be specified as a name or column alias (which can be
qualified by the table or view name), an expression, or a nonnegative
integer representing the position of the name, alias, or expression in
select list.
Multiple sort columns can be specified. The sequence of the sort
columns in the ORDER BY clause defines the organization of the sorted
result set.
Example: Ordering output
Exercise 1

Write SQL queries to create the following tables:


• STUDIOS (name, address)
• STARS (name, address, phone)
• MOVIES (title, year, length, genre)

After creating, write SQL queries to drop them


Exercise 2

Write SQL queries to do following tasks:


• Add a column named DESCRIPTION into MOVIES
table (you must determine the data type for it)

• Add a column named HOBBIES into STARS table


(you must determine the data type for it)

• Add a column named BIRTHDAY into STARS table


(you must determine the data type for it)
Exercise 3

Write SQL queries to do following tasks:


• Remove the column named DESCRIPTION from
MOVIES table

• Remove the column named HOBBIES from STARS


table

• Remove the column named BIRTHDAY from


STARS table
Exercise 4

Create relation EMPLOYEES with the columns


LAST_NAME, DEPARTMENT_ID, SALARY
Insert the tuples (see figure above) into the
relation EMPLOYEES
Write a SQL query to show all tuples in table
EMPLYEES
Exercise 5

Write a SQL query to show all SALARY (but


eliminating duplications) in table EMPLYEES

Write a SQL query to show all DEPARTMENT_ID


(but eliminating duplications) in table EMPLYEES
Exercise 6

Write a SQL query to delete all tuples in


EMPLOYEES table

Write a SQL query to delete all tuples with NULL


value in DEPARTMENT_ID
Exercise 7

Write a SQL query to set DEPARTMENT_ID to the


value 10

Write a SQL query to set DEPARTMENT_ID to the


value 10 if DEPARTMENT_ID is NULL
Exercise 8

Write a SQL query to insert some new tuples into


EMPLOYEES
ĐẠI HỌC FPT CẦN THƠ
ĐẠI HỌC FPT CẦN THƠ

Chapter 6
Queries involving more than one relation
Objectives

1 Know how to query data from more than one relation

2 Know how to Dis-ambiguate Attributes

3 Understand the role of column Aliases and table Aliases

4 Know how to use set-operators (union, different, intersect) in SQL


Contents

1 Product and Join in SQL

2 Dis-ambiguating Attributes

3 Tuple Variables

4 Union

5 Intersection

6 Difference
Review: Capabilities of
SQL SELECT Statements

Projection: You can use the projection capability in SQL to choose the
columns in a table that you want returned by your query.
Selection: You can use the selection capability in SQL to choose the rows
in a table that you want returned by a query (with WHERE clause)
Joining: You can use the join capability in SQL to bring together data that
is stored in different tables by creating a link between them.
Review: A Cartesian Products example
1 – Products & Joins in SQL

When data from more than one table in the


database is required, a join condition is used.
Rows in one table can be joined to rows in
another table according to common values existing
in corresponding columns, that is, usually primary
and foreign key columns.

To display data from two or more related tables,


write a simple join condition in the WHERE clause.

To join n tables together, you need a minimum of


n-1 join conditions.
Types of JOINs
What is an inner join?

To determine a student’s class name, you compare the value in the


CLASS_ID column in the CLASS table with the CLASS_ID values in the
STUDENT table.
The relationship between the STUDENT and CLASS tables is values in the
CLASS_ID column on both tables must be equal.
How to express
inner Join with Relational Algebra?

δA.class_id = B.class_id(A x B)

SELECT *
FROM student A, class B
WHERE A.class_id = B.class_id
Inner join with non-equal condition

A join condition containing something other


than an equality operator.

δA.mark >= B.min AND A.mark < B.max(AxB)

SELECT A.student_id, B.grade


FROM student A, ranking B
WHERE A.marks >= B.MIN
AND A.mark < B.MAX
Outer Joins

If a row does not satisfy a join condition, the row will not appear in the
query result. These rows are called dangling rows (or dangling tuples).

The missing rows can be returned if an outer join operator is used in


the join condition.

CLASS STUDENT
CLASS_ID CLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1A
107 Lop 107 106 2B
201 Lop 201
107 3C
202 Lop 202
107 4D
5E
6F
7G
8H
Left Outer Join

List CLASS_NAME of all the class, regardless of


whether or not they have any student or not when
joining CLASS and STUDENT

CLASS STUDENT
CLASS_ID CLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1A
107 Lop 107 106 2B
201 Lop 201 107 3C
202 SELECT A.class_name
Lop 202 107 4D
5E
FROM class A 6F
7G
LEFT OUTER JOIN student B
8H
ON A.class_id = B.class_id
Output of LEFT OUTER JOIN

CLASS STUDENT
CLASS_ID CLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1A
106 Lop 106 106 2B
107 Lop 107 107 3C
107 Lop 107 107 4D
201 Lop 201
202 Lop 202

The red box is on the Left


Right Outer Join

List NAME of all the student, regardless of whether


or not they have any class or not when joining
CLASS and STUDENT
CLASS STUDENT
CLASS_ID CLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1A
107 Lop 107 106 2B
201 Lop 201 107 3C
202 Lop 202 107 4D
SELECT B.name 5E
FROM class A 6F
7G
RIGHT OUTER JOIN student B 8H

ON A.class_id = B.class_id
Output of RIGHT OUTER JOIN

CLASS STUDENT
CLASS_IDCLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1 A
106 Lop 106 106 2 B
107 Lop 107 107 3 C
107 Lop 107 107 4 D
5 E
6 F
7 G
8 H

The red box is on the Right


Full Outer Join

List NAME of all the student and ALL classes,


regardless of whether or not they have any class
or not when joining CLASS and STUDENT
CLASS STUDENT
CLASS_ID CLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1A
107 Lop 107 106 2B
201 Lop 201 107 3C
202 SELECT B.name
Lop 202 107 4D
5E
FROM class A 6F
7G
FULL OUTER JOIN student B 8H
ON A.class_id = B.class_id
Output of FULL OUTER JOIN

CLASS STUDENT
CLASS_IDCLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1 A
106 Lop 106 106 2 B
107 Lop 107 107 3 C
107 Lop 107 107 4 D
5 E
6 F
7 G
8 H
201 Lop 201
202 Lop 202

LEFT OUTER JOIN RIGHT OUTER JOIN


Self Joins

Sometimes you need to


join a table to itself.

SELECT A.employee_id + ‘ MANAGED BY ‘ +


B.employee_id
FROM employee A, employee B
WHERE A.Manager_id = B.Employee_ID
SQL joins

19
Cartesian Products

A Cartesian product is formed when:


A join condition is omitted
A join condition is invalid
All rows in the first table are joined to all rows in
the second table

To avoid a Cartesian product, always include


a valid join condition in a WHERE clause.
2. Disambiguating attributes

Sometimes we ask a query involving several relations,


and among these relations are two or more attributes
with the same name. If so, we need a way to indicate
which of these attributes is meant by a use of their
shared name.
SQL solves this problem by allowing us to place a
relation name and a dot in front of an attribute, thus
R.A refers to the attribute A of relation R
Disambiguating attributes example

Two relations:
MovieStar (name, address, gender, birthdate)
MovieExec (name, address, cert#, netWorth)
Both relations have attributes “name” and “address”. Look
at the following query to see the way of dis-ambiguating:

SELECT MovieStar.Name, MovieExec.Name


FROM MovieStar, MovieExec
WHERE MovieStar.Address = MovieExec.Address
3. Tuple Variables

Dis-ambiguating attributes by prefixing the relation


name works as long as the query involves combining
several different relations
How-ever, sometimes we need to ask a query that
involves two ore more tuples from the same relation.
How to do that?
Solution: we may list a relation R as many times as
we need to in the FROM clause but we need a way to
refer to each occurrence of R. SQL allows us to define,
for each occurrence of R in the FROM clause, an
“ALIAS” which we shall refer to as a tuple variables
Tuple variables example

Sometimes you need to


join a table to itself.

SELECT A.employee_id + ‘ MANAGED BY ‘ +


B.employee_id
FROM employee A, employee B
WHERE A.Manager_id = B.Employee_ID
SET operators
(Union, Intersection, Difference)
UNION & UNION ALL

The UNION operator eliminates any duplicated rows.

But UNION ALL still returns duplicated rows


Example: UNION
INTERSECT

{A ∩ B} = {a | a is in A and B}
Difference

SELECT employee_id, job_id FROM employees


EXCEPT
SELECT employee_id, job_id FROM job_history

SQL Server

Oracle
ĐẠI HỌC FPT CẦN THƠ

Chapter 6
Subqueries
Objectives

1 Know what are subqueries and when to use them

2 Know what are correlated subqueries

3 Understand why we should not over-use the correlated subqueries


Contents

1 Subqueries that produce scalar values

2 Conditions involving relations

3 Conditions involving tuples

4 Correlated subqueries

5 Subqueries in FROM clauses


Sub-Queries
In SQL, a query that is part of another is called a
subquery
You can write sub-queries in the WHERE clause of
another SQL statement to obtain values based on an
unknown conditional value.
Example: Sub-queries
Sub-Queries Syntax

SELECT [ ALL | DISTINCT ]


[ TOP n [ PERCENT ] ]
* | {column_name | expression [alias],…}
FROM table
WHERE expression OPERATOR
(SELECT select_list
FROM table)

The subquery (inner query) executes once before


the main query.
The result of the subquery is used by the main
query (outer query).
1 – Subqueries
that produce scalar values

An atomic value that can appear as one component of


a tuple is referred to as a scalar.
A SELECT query can produce a relation with any
number of attributes in its schema, and there can be
any number of tuples in the relation
However, often we are only interested in values of a
single attribute. Furthermore, sometimes we can
deduce from information about keys or from other
information, that there will be only a single value
produced for that attribute. If so, we can use this
SELECT query, surrounded by parentheses, as if it
were a constant
Example: Sub-queries that
produce scalar values
2 – Conditions involving relations

There are a number of SQL operators that we can


apply to a relation R and produce a boolean result:
EXISTS R: is a condition that is true if and only if R is not
empty
s IN R: is true if and only if s is equal to one of the values
in R
s > ALL R: is true if and only if s is greater than every value
in unary relation R
s > ANY R: is true if and only if s is greater than at least
one value in unary relation R
All the above operators can be negated by putting
NOT in front of the entire expression
3 – Conditions involving tuples
A tuple in SQL is represented by a parenthesized list of
scalar values
Exp:
(123, ‘Foo’)
(456,’Dump’)
(name, address, networth)
If a tuple t has the same number of components as a
relation R, then it makes sense to compare t and R in
expressions involving IN, ALL, ANY operators
Conditions involving tuples example

SELECT producerC#
FROM movies
WHERE (title, year) IN
(SELECT movieTile, movieYear
FROM starsIn
WHERE starName = ‘Harrison Ford’)

Note: You can not run the above code in SQL Server
but in Oracle, DB2, MySQL
4 - Correlated Sub-Queries

A correlated subquery is one way of reading every row in a


table and comparing values in each row against related data.
It is used whenever a sub-query must return a different
result or set of results for each candidate row considered by
the main query.
Example: Correlated Sub-queries
The differences between
simple subqueries and correlated subqueries
The simple subqueries can be evaluated once and for
all, and the result used in a higher-level query
A correlated subqueries require the subqueries to be
evaluated many times, once for each assignment of a
value to some term in the subqueries that come from
a tuple variable outside the subqueries
So, you should avoid using correlated subqueries
whenever possible
Example: Correlated Sub-queries
5 – Subqueries in FROM clauses

Another use for subqueris is as relations in a FROM


clause:

SELECT name
FROM movieExec,
(SELECT producerC#
FROM movies, starsIn
WHERE title = movieTitle
AND year = movieYear
AND starName = ‘Harrison Ford’
) as Prod
WHERE cert# = Prod.producerC#
ĐẠI HỌC FPT CẦN THƠ

Chapter 6
Full relation operations
Objectives

1 Understand what are grouping and aggregate functions

2 Understand HAVING clauses

3 Know about the NULL when grouping


Contents

1 Aggregation operators

2 Grouping

3 Grouping, Aggregation and NULLs

4 HAVING clauses
1 - Aggregate Functions
OR Aggregate Operators

STUDENT
CLASS_ID ID NAME
106 1A
106 2B
107 3C
107 4D
5E
6F
7G
8H

Group functions (or Aggregate Functions) operate on sets of


rows to give one result per group. These sets may be the
whole table or the table split into groups.
Exp: If we want all rows with the same CLASS_ID value will
be groupped: GROUP BY class_id
1 - Aggregate Functions
OR Aggregate Operators
AVG
COUNT
MAX
MIN
SUM
STDDEV
VARIANCE

❖ DISTINCT makes the function consider only nonduplicate values;


❖ ALL makes it consider every value including duplicates.
❖ The default is ALL and therefore does not need to be specified.
❖ All group functions ignore NULL values.
Example: AVG, SUM, MIN, MAX, COUNT
Example: Aggregate Functions
and DISTINCT
2 - Grouping

Until now, all group functions (demonstrated in above


examples) have treated the table as one large group
of information.
At times, you need to divide the table of information
into smaller groups. This can be done by using the
GROUP BY clause
The keyword GROUP BY is followed by a list of
grouping attributes.
2 - Grouping

Syntax: Divide rows in a table into smaller


groups by using the GROUP BY clause.

SELECT column, group_function (column)


FROM table
[WHERE conditions]
[GROUP BY group_by_expression]
[ORDER BY {column [ASC | DESC] ,…} ]

group_by_expression: specifies columns whose


values determine the basis for grouping rows
Example: Aggregate Functions
and GROUP BY
Example: Aggregate Functions
and GROUP BY
Example: Aggregate Functions
and GROUP BY
2 – Grouping: rules to remember

SELECT column, group_function (column)


FROM table
[WHERE conditions]
[GROUP BY group_by_expression]
[ORDER BY {column [ASC | DESC] ,…} ]

When aggregate functions are used in a select


list, the select list can contain only:
• Aggregate functions
• Grouping columns from a GROUP BY clause
• An expression that returns the same value for every
row in the result set, such as a constant
6.4.6 – Grouping, Aggregation,
and NULLs
When tuples have nulls, there are a few rules we must
remember:
The value NULL is ignored in any aggregation: it does
not contribute to a SUM, AVG or COUNT of an attribute, nor
can it be the minimum or maximum in its column.
Exp: COUNT(*) is always a count of the number of tuples in
a relation; but COUNT(A) is the number of tuples with non-
NULL values for attribute A
On the other hand, NULL is treated as an ordinary
value when forming groups: that is, we can have a
group in which one or more of the grouping attributes are
assigned the value NULL
When we perform any aggregation except COUNT over an
empty bag of values, the result is NULL (the COUNT of an
empty bag is 0)
3 - HAVING clauses

Sometimes, we want to exclude some groups from displaying


result. The solution is using HAVING clause
3 - HAVING clauses

Syntax:
SELECT column, group_function (column)
FROM table
[WHERE conditions]
[GROUP BY group_by_expression]
[HAVING conditions]
[ORDER BY {column [ASC | DESC] ,…} ]

❖The WHERE clause is used to restrict the rows that


you select
❖But the HAVING clause is used to restrict groups.
6.4.7 - HAVING clauses
(page 288)
SELECT column, group_function (column)
FROM table
[WHERE conditions]
[GROUP BY group_by_expression]
[HAVING conditions]
[ORDER BY {column [ASC | DESC] ,…} ]

Groups are formed and group functions are


calculated before the HAVING clause is applied to
the groups.
In Oracle (not SQL Server), the HAVING clause can
precede the GROUP BY clause, but it is
recommended that you place the GROUP BY clause
first because it is more logical.
3 - HAVING clauses

1
Select ROWS (with WHERE clause) first

2
ROWS are grouped (GROUP BY clause)

3
Groups matching the HAVING clause are
displayed
Example: HAVING

The example displays department numbers and


maximum salaries for those departments whose
maximum salary is greater than $10,000.
Example: HAVING

The example displays the job ID and total


monthly salary for each job with a total
payroll exceeding $13,000.
Example: HAVING

Group functions can be nested to a depth of


two. The example displays the maximum
average salary.
ĐẠI HỌC FPT CẦN THƠ

Chapter 6
Database Modifications & Transactions
Objectives

1 Understand what are DDL, DML

2 Know how to write INSERT, UPDATE, DELETE queries

3 Understand what are transactions, COMMIT, ROLLBACK

4 Understand ISOLATION LEVELS


Contents

1 Insertions

2 Deletions

3 Updates

4 Transactions, COMMIT, ROLLBACK, ISOLATION LEVELS

5 Exercises
Review: Sub-languages of T-SQL

T - SQL

DDL
(Data Definition Language)

DML
(Data Manipulation Language)

DCL
(Data Control Language)
Review: Sub-languages of T-SQL
Review:
DDL – Data Definition Language
Review:
DML – Data Manipulation Language
1 – Insertion with INSERT command
Example: Insert command
Example: Insert command
2 – Deletions with DELETE command
Example: Delete command
Example: Delete command
Differences between DELETE
and TRUNCATE

DELETE is DML statement


TRUNCATE is DDL statement

DELETE marks rows as deleted but the data file size is


not decreased
TRUNCATE really deletes rows so the data file size is
decreased

TRUNCATE TABLE [table name]


3 – Updates with UPDATE command
Example: Update command
Example: Update command
4 – Transactions in SQL

A transaction is a sequence of SQL statements


(executed by a single user) that together constitute
a logical unit of work.
A transaction begins with the user’s first executable
SQL statement.
A transaction ends when it is explicitly committed
or rolled back by that user.
4 – Transactions in SQL

Transactions allow you to group SQL commands


into atomic elements.
Essentially, they provide you with the capability of
performing a series of commands in an "all or
nothing" fashion.

The syntax for a simple transaction in SQL is rather


basic:
BEGIN TRANSACTION
<SQL statements>
COMMIT
Example: Transaction

Consider a banking database. When a bank customer


transfers money from a savings account to a checking
account, the transaction can consist of three separate
operations:
decrease the savings account
increase the checking account
Record the transaction in the transaction journal
Example: Transaction

$500

ACC: 3209

$500

ACC: 3208
Example: Transaction

DBMS must guarantee that all three SQL


statements are performed to maintain the accounts
in proper balance.
When something prevents one of the statements in
the transaction from executing (such as a hardware
failure), the other statements of the transaction
must be undone; this is called rolling back. If an
error occurs in making any of the updates, then no
updates are made.
Transaction integrity

Transaction integrity refers to the quality of a


transaction as measured by its ACID properties.
There are 3-types of problems that violate
transaction integrity (solving these problems
involves enforcing various levels of isolation):
Dirty reads
Non-Repeatable reads
Phantom rows
ACID properties of Transaction

The quality of a DBMS product is measured by its


transactions’ adherence to the ACID properties. ACID
is an acronym for 4 interdependent properties:
Atomicity
Consistency
Isolation
Durability
ACID properties of Transaction

Atomicity: The transaction must be atomic. This


means that: ALL or NOTHING.
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
ACID properties of Transaction

Consistency: The transaction must preserve


database consistency. This means that: the database
must begin in a state of consistency and return to a
state of consistency once the transaction is complete.
Consistency means that: every row and value must agree
with all constraints.
For example: if the parent row is written to the disk but the
child rows are not written to the disk, so the consistency
property is violated
ACID properties of Transaction

Isolation: Each transaction must be isolated or


separated from the effects of other transaction.
Regardless of what any other transaction is doing, a
transaction must be able to continue with the exact same
data sets it started with.
For example: Assume Joe is updating 100 rows, and
while Joe’s transaction is under way, Sue delete one of
rows Joe is working on. If the delete takes place, Joe’s
transaction is not isolated from Sue’s transaction.
This property is less critical in a single-user DB than in a
multi-user DB
ACID properties of Transaction

Durability: The durability of a transaction refers to


its permanence regardless of system failure.
Once a transaction is committed, it stays committed. The
DB product must be constructed so that even if the data
drive melts, the DB can be restored up to the last
transaction that was committed a split second before the
hard drive died.
Transactional faults

The following transaction faults can potentially affect


the integrity of the transaction:
• Dirty read (most dangerous)
• Non-Repeatable reads (medium dangerous)
• Phantom rows (least dangerous)
Transactional faults - Dirty read

The most egregious fault is a


transaction’s work being visible
to other transactions before
the transaction even commit its
changes
When a transaction can read
another transaction’s
uncommitted updates, this is
called a dirty read.
Transactional faults –
Non-Repeatable read

A non-repeatable read is similar to a


dirty read, but a non-repeatable read
occurs when a transaction can see the
committed updates from another
transaction.
True isolation means that one
transaction never affects another
transaction. If the isolation is
complete, then no data changes from
outside the transaction should be seen
by the transaction
Reading a row inside a transaction
should produce the same result every
time
If reading a row twice results in
different values, that is a non-
repeatable read fault
Transactional faults –
Phantom Rows

Like a non-repeatable read


fault, a Phantom Rows Fault
is when updates from
another transaction affect
not only the result set’s
data value but also causes
the SELECT to return a
different set of rows
Isolation levels

Database deals with the 3 transactional faults by


establishing isolation between transactions
The level of isolation, or the height of the fence
between transaction, can be adjusted to control which
transactional faults are permitted.
Isolation levels
Isolation levels
Isolation levels
Isolation levels

If you need to change the isolation model in use by


SQL Server, simply issue the command:
SET TRANSACTION ISOLATION LEVEL <level>
where <level> is replaced with any of the following
keywords:
READ COMMITTED
READ UNCOMMITTED
REPEATABLE READ
SERIALIZABLE
SNAPSHOT
COMMIT, ROLLBACK

The changes made by the SQL statements that constitute a


transaction can be either committed or rolled back.
• Committing a transaction makes permanent the changes
resulting from all SQL statements in the transaction. The
changes made by the SQL statements of a transaction
become visible to other user sessions’ transactions that start
only after the transaction is committed.
• Rolling back a transaction retracts any of the changes
resulting from the SQL statements in the transaction. After a
transaction is rolled back, the affected data is left
unchanged as if the SQL statements in the transaction were
never executed.
COMMIT, ROLLBACK

In SQL Server, every DML operation is a transaction whether


it has a BEGIN TRANSACTION or not
To wrap multiple commands within a single transaction, a
little code is needed. Two markers – one at the beginning of
the transaction and the other at its completion. If the code
detects an error, the transaction can be rolled back or
Undone
Example: Commit & Rollback
COMMIT, ROLLBACK

When the sequence of tasks is complete, the COMMIT


TRANSACTION closes the transaction
Savepoints

For long transactions that contain many SQL statements,


intermediate markers, or savepoints, can be declared.
Savepoints can be used to divide a transaction into smaller
parts.

By using savepoints, you can arbitrarily mark your work at


any point within a long transaction. This gives you the
option of later rolling back all work performed from the
current point in the transaction to a declared savepoint
within the transaction.

For example, you can use savepoints throughout a long


complex series of updates, so if you make an error, you do
not need to resubmit every statement.
SUMMARY

T-SQL includes: DDL, DML, DCL.


DML includes: select, insert, delete, update commands.
A transaction is a sequence of SQL statements that together constitute a
logical unit of work
ACID is an acronym for 4 interdependent properties: atomicity,
consistency, Isolation, durability
The following transaction faults can potentially affect the integrity of the
transaction: Dirty read, Non-Repeatable reads, Phantom rows
Database deals with the 3 transactional faults by establishing isolation
between transactions: READ COMMITTED, READ UNCOMMITTED,
REPEATABLE READ, SERIALIZABLE
The changes made by the SQL statements that constitute a transaction can
be either committed or rolled back.
ĐẠI HỌC FPT CẦN THƠ

You might also like