0% found this document useful (0 votes)
14 views

06 Ch6 TheDatabaseLanguageSQL

SQL is a database language designed for managing data in relational database management systems. It originated from relational algebra. There are several dialects of SQL, including ANSI SQL, Transact-SQL, and PL/SQL. SQL allows users to query data through operations like selection, projection, and pattern matching. It also supports data types like dates, times, and timestamps.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

06 Ch6 TheDatabaseLanguageSQL

SQL is a database language designed for managing data in relational database management systems. It originated from relational algebra. There are several dialects of SQL, including ANSI SQL, Transact-SQL, and PL/SQL. SQL allows users to query data through operations like selection, projection, and pattern matching. It also supports data types like dates, times, and timestamps.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 98

INTRODUCTION

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

THE DATABASE
LANGUAGE SQL
SQL Language Overview

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


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

 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

6.1 SIMPLE QUERIES IN SQL


Example Database Schema

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
SQL Queries and Relational
Algebra

 Syntax
I2DB: Algebraic Query Language

SELECT L
FROM R L(C(R))
WHERE C
T-SQL : Basic Syntax for a simple
SELECT queries
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
Common Query in SQL

 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
Projection in SQL

 Using alias name in select clause


I2DB: Algebraic Query Language

 Example 3:
 Listing full name and salary of all employees whose

income exceed 50000


Selection in SQL

 Condition expression in where clause may use


I2DB: Algebraic Query Language

 Constants and attributes

 Arithmetic operators
▪ +, -, *, /

 Comparison operators
▪ =, <>, <, >, , ≥

 Logical operators
▪ and, or, not

 The result of condition must be True or False


Selection in SQL

 Example 4
I2DB: Algebraic Query Language

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

male employees
Comparison of Strings

 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


Pattern Matching in SQL

 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


Pattern Matching in SQL

 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’
Pattern Matching in SQL

 USING ESCAPE keyword


I2DB: Algebraic Query Language

 SQL allows us to specify any one character we like as

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

 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 Values

 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


The Truth-Value 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
The Truth-Value Unknown

 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
Ordering the Output

 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
Ordering the Output

 Example 6:
I2DB: Algebraic Query Language

 Listing all employee by department number

ascreasingly, then by salary descreasingly


6.2 QUERIES INVOLVING MORE
THAN ONE RELATION
I2DB: Algebraic Query Language
Queries Involving More Than One
Relation

 SQL allows we combine two or more relations


I2DB: Algebraic Query Language

through joins, products, unions, intersections, and


differences
Products and Joins in SQL

 When data from more than one table in the


I2DB: Algebraic Query Language

database is required, a join condition is used.


 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
Products and Joins in SQL

 Example 7:
I2DB: Algebraic Query Language

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

trong nước’ department


What we do if …

 … a query involves several relations, and there


I2DB: Algebraic Query Language

are two or more attributes with the same name?


Tuple Variables

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


I2DB: Algebraic Query Language

need
 We use tuple variables to refer to each
occurrence of R
Disambiguating Attributes

 Example 8:
I2DB: Algebraic Query Language

 Find all cities in which our company is


What we do if …

 … a query involves two or more tuples from the


I2DB: Algebraic Query Language

same relation?
 Example 9:
 Find all those project numbers which have more than

two members
Union, Intersection, Difference of
Queries

 We combine relations using the set operations of


I2DB: Algebraic Query Language

relational algebra: union, intersection, and


difference
 SQL provides corresponding operators with
UNION, INTERSECT, and EXCEPT for , , and
-, respectively
Union, Intersection, Difference of
Queries

 Example 10.1
I2DB: Algebraic Query Language

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

or salary exceed 80000


Union, Intersection, Difference of
Queries

 Example 10.2
I2DB: Algebraic Query Language

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

supervise any other employees


Union, Intersection, Difference of
Queries

 Example 10.3
I2DB: Algebraic Query Language

 Find all employees who work on projectB and projectC


I2DB: Algebraic Query Language

6.3 SUB QUERIES


Sub-queries

 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
Sub-queries that Produce Scalar
Values

 An atomic value that can appear as one


I2DB: Algebraic Query Language

component of a tuple is referred to as a scalar


 Let’s compare two queries for the same request
Sub-queries that Produce Scalar
Values

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


I2DB: Algebraic Query Language

mềm trong nước department


Sub-queries that Produce Scalar
Values

 Example 11:
I2DB: Algebraic Query Language

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

department
Sub-queries that Produce Scalar
Values

 Example 11:
I2DB: Algebraic Query Language

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

department
Sub-queries that Produce Scalar
Values

 Example 11:
I2DB: Algebraic Query Language

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

department
Conditions Involving Relations

 Some SQL operators can be applied to a relation


I2DB: Algebraic Query Language

R and produce a bool result


 (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
Conditions Involving Tuples

 A tuple in SQL is represented by a list of scalar


I2DB: Algebraic Query Language

values between ()
 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
Sub-queries that Produce Scalar
Values

 Example 12:
I2DB: Algebraic Query Language

 Find the dependents of all employees of department

number 1
Correlated Sub-queries

 To now, sub-queries can be evaluated once and


I2DB: Algebraic Query Language

for all, the result used in a higher-level query


 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
Correlated Sub-queries

 Example 13:
I2DB: Algebraic Query Language

 Find all those projects have the same location with

projectA
Correlated Sub-queries

 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)


Sub-queries in FROM Clauses

 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

 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


SQL Join Expression

 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
SQL Join Expression

 More Example
I2DB: Algebraic Query Language
Natural Joins

 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


Outer Joins

 The outer join is a way to augment the result of


I2DB: Algebraic Query Language

join by the dangling tuples, padded with null


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
Outer joins

 Example 17.1:
I2DB: Algebraic Query Language

 For each location, listing the projects that are processed

in it
Outer joins

 Example 17.2:
I2DB: Algebraic Query Language

 For each department, listing the projects that it controls


6.4 Full-Relation Operations

 Study some operations that acts on relations as


I2DB: Algebraic Query Language

whole, rather than on tuples individually


Eliminating Duplicates

 A relation, being a set, cannot have more than


I2DB: Algebraic Query Language

one copy of any given tuple


 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
Eliminating Duplicates

 Example 17.3: List all location in which the


I2DB: Algebraic Query Language

projects are processed.


 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
Duplicates in Unions,
Intersections, and Differences

 Set operations on relations will eliminate


I2DB: Algebraic Query Language

duplicates automatically
 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 and Aggregation in SQL

 Grouping operator partitions the tuples of relation


I2DB: Algebraic Query Language

into groups, based on the values of tuples in one


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
Aggregation Operators

 Five aggregation operators


I2DB: Algebraic Query Language

 SUM acts on single numeric column

 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
Aggregation Operators

 Example 18.1
I2DB: Algebraic Query Language

 Find average salary of all employees

 Example 18.2
 Find number of employees
Grouping

 To partition the tuples of relation into groups


I2DB: Algebraic Query Language

 Syntax

SELECT <list of attributes>


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

 Example 19.1:
I2DB: Algebraic Query Language

 Group employees by department number

 Example 19.2
 List number of employees for each department number
Grouping

 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
Grouping

 Example 20
I2DB: Algebraic Query Language

 Compute the number of employees for each project


Grouping, Aggregation, and Nulls

 When tuples have nulls, there are some rules:


I2DB: Algebraic Query Language

 The value NULL is ignored in any aggregation


▪ 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
Grouping, Aggregation, and Nulls

 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)
Considerations …

 If we want to apply conditions to tuples of relations,


I2DB: Algebraic Query Language

we put those conditions in WHERE 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
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>
HAVING clause

 Example 21:
I2DB: Algebraic Query Language

 Print the number of employees for each those

department, whose average salary exceeds 80000


HAVING clause

 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)
HAVING 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
Exercises

 Page 257, Exercise 6.1.3


I2DB: Algebraic Query Language

 Page 267, Exercise 6.2.2


 Page 279, Exercise 6.3.1
 Page 289, Exercise 6.4.6
I2DB: Algebraic Query Language

6.5 DATABASE MODIFICATIONS


Database Modifications

 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
Insertion

 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 …


Insertion

 Example: add new a department


I2DB: Algebraic Query Language
Deletion

 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 !


Updates

 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’
Updates

 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

6.6 TRANSACTION IN SQL


Introduction

 DB User operates on database by querying or


I2DB: Algebraic Query Language

modifying the database


 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?
Serializability

 In applications, many operations per second may


I2DB: Algebraic Query Language

be performed on database
 These may operate on the same data
 We’ll get unexpected results
Serializability

 Example: Two users book the same seat of the


I2DB: Algebraic Query Language

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

 Transaction is a group of operations that need to


I2DB: Algebraic Query Language

be performed together
 A certain transaction must be serializable with
respect to other transactions, that is, the
transactions run serially – one at a time, no
overlap
Atomicity

 A certain combinations of database operations


I2DB: Algebraic Query Language

need to be done atomically, that is, either they


are all done or neither is done
Atomicity

 Example: Transfer $500 from the account number


I2DB: Algebraic Query Language

3209 to account number 3208 by two steps


 (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)?
Atomicity

$500
I2DB: Algebraic Query Language

ACC: 3209

$500

ACC: 3208
Transactions

 Transaction is a collection of one or more operations


I2DB: Algebraic Query Language

on the database that must be 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
Transactions

 Transaction begins by SQL command START


I2DB: Algebraic Query Language

TRANSACTION
 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


ACID properties of Transaction
(review)

 Transactions should possess several properties, often


I2DB: Algebraic Query Language

called the ACID properties; they should be enforced


by the concurrency control and recovery methods of
the DBMS. The following are the ACID properties:
 Atomicity

 Consistency

 Isolation

 Durability
ACID properties of Transaction

 Atomicity: a transaction is an atomic unit of processing; it


should either be performed in its entirety or not performed at all.
I2DB: Algebraic Query Language

 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.
ACID properties of Transaction

 Isolation: a transaction should appear as though it is being


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

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..
Read-Only Transactions

 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
Transactions

 SQL statement set read-only to the next


I2DB: Algebraic Query Language

transaction
 SET TRANSACTION READ ONLY;

 SQL statement set read/write to the next


transaction
 SET TRANSACTION READ WRITE;
Dirty Reads

 Dirty data: data written by a transaction that has not


I2DB: Algebraic Query Language

yet committed
 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
Dirty Reads
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
Dirty Reads

 We can specify that dirty reads are acceptable for


I2DB: Algebraic Query Language

a given transaction
 SET TRANSACTION READ WRITE

ISOLATION LEVEL READ UNCOMMITTED;


Other Isolation Levels

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