dr. t. y. lin | sjsu | cs 157a | fall 2011 chapter 6 the database language sql 1

157
Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 Chapter 6 THE DATABASE THE DATABASE LANGUAGE SQL LANGUAGE SQL 1

Upload: gervais-blair

Post on 26-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

Chapter 6Chapter 6

THE DATABASE THE DATABASE LANGUAGE SQLLANGUAGE SQL

11

Page 2: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6. The Database Language SQL 6. The Database Language SQL

6.1 . 46.1 . 4

6.1.7.6.1.7.

22

Page 3: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6. The Database Language SQL 6. The Database Language SQL

6.1 Simple Queries in SQL6.1 Simple Queries in SQL

6.2 Queries Involving More Than One 6.2 Queries Involving More Than One Relation Relation

6.3 Subqueries6.3 Subqueries

6.4 Full-Relation Operation6.4 Full-Relation Operation

6.5 Database Modification6.5 Database Modification

6.6 Transactions in SQL6.6 Transactions in SQL

6.7 Summary of Chapter 66.7 Summary of Chapter 6

6.8 References for chapter 66.8 References for chapter 6

33

Page 4: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

IntroductionIntroduction

SQL (pronounce “sequel”) stands for: SQL (pronounce “sequel”) stands for: Structured Query LanguageStructured Query Language

SQL consists of:SQL consists of: DML (Data Manipulation Language) DML (Data Manipulation Language) DDL (Data Definition Language)DDL (Data Definition Language)

Dialects of SQL:Dialects of SQL: ANSI SQLANSI SQL SQL-92 or SQL2SQL-92 or SQL2 SQL-99 or SQL3SQL-99 or SQL3 SQL:2003SQL:2003

44

Page 5: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

Introduction (cont’d)Introduction (cont’d)

There are versions of SQL produced by There are versions of SQL produced by the principal DBMS vendors.the principal DBMS vendors. They support ANSI standardsThey support ANSI standards Conform to a large extent to the more recent Conform to a large extent to the more recent

SQL2SQL2 Each has its variations and extensions beyond Each has its variations and extensions beyond

SQL2 including some of the features in the SQL2 including some of the features in the SQL-99 and SQL:2003SQL-99 and SQL:2003

55

Page 6: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

SIMPLE QUERIES IN SQLSIMPLE QUERIES IN SQLSection 6.1Section 6.1

66

Page 7: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1 Simple Queries in SQL6.1 Simple Queries in SQL

6.1.1 Projecting in SQL6.1.1 Projecting in SQL

6.1.2 Selecting in SQL 6.1.2 Selecting in SQL

6.1.3 Comparison of Strings6.1.3 Comparison of Strings

6.1.4 Pattern Matching in SQL6.1.4 Pattern Matching in SQL

6.1.5 Dates and Times6.1.5 Dates and Times

6.1.6 Null Values and Comparisons 6.1.6 Null Values and Comparisons Involving NULLInvolving NULL

6.1.7 The Truth-Value UNKNOWN6.1.7 The Truth-Value UNKNOWN

6.1.8 Ordering the Output6.1.8 Ordering the Output

6.1.9 Exercises for Section 6.16.1.9 Exercises for Section 6.177

Page 8: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1 Simple Queries in SQL6.1 Simple Queries in SQL

A simple query uses the following A simple query uses the following keywords:keywords:

SELECT , FROM, WHERE (BNF)SELECT , FROM, WHERE (BNF) We call these queries: We call these queries: select-from-whereselect-from-where

formform

88

Page 9: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1 Simple Queries in SQL 6.1 Simple Queries in SQL (cont’d)(cont’d)

Example 6.1 (selection)Example 6.1 (selection)Given the relation Given the relation MoviesMovies with the with the following schema:following schema:Movies(title, year, length, genre, Movies(title, year, length, genre, studioName, producerC#)studioName, producerC#)Query all movies produced by Disney Query all movies produced by Disney Studios in 1990Studios in 1990

SELECT * SELECT * FROM MoviesFROM MoviesWHERE studioName = ‘Disney’ WHERE studioName = ‘Disney’ AND year = 1990;AND year = 1990;

99

Page 10: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.1 Projection in SQL6.1.1 Projection in SQL

We can project the relation produced by We can project the relation produced by a SQL onto some of its attributes.a SQL onto some of its attributes.

Example 6.2Example 6.2Modify the example 6.1 and bring just Modify the example 6.1 and bring just titletitle and and lengthlength of the movies with the of the movies with the same condition.same condition.SELECT title, length SELECT title, length FROM MoviesFROM MoviesWHERE studioName = 'DISNEY' WHERE studioName = 'DISNEY' AND year = 1990;AND year = 1990;

1010

Page 11: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.1 Projection in SQL 6.1.1 Projection in SQL (cont’d)(cont’d)

If you wish to have different names for the If you wish to have different names for the attributes, then just type what you wish attributes, then just type what you wish as follows:as follows:

Example 6.3Example 6.3

SELECT title AS name, length AS durationSELECT title AS name, length AS durationFROM MoviesFROM MoviesWHERE studioName = 'DISNEY' WHERE studioName = 'DISNEY' AND year = 1990;AND year = 1990;

Note that you can eliminate “AS”Note that you can eliminate “AS”

1111

Page 12: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.1 Projection in SQL 6.1.1 Projection in SQL (cont’d)(cont’d)

Another option is to use an expression in Another option is to use an expression in place of an attribute.place of an attribute.

Example 6.4Example 6.4compute the length in hourscompute the length in hours

SELECT title AS name, SELECT title AS name, length/60 AS Length_In_Hours length/60 AS Length_In_HoursFROM MoviesFROM MoviesWHERE studioName = 'DISNEY' WHERE studioName = 'DISNEY' AND year = 1990;AND year = 1990;

1212

Page 13: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.1 Projection in SQL 6.1.1 Projection in SQL (cont’d)(cont’d)

Another option is to use a constant in Another option is to use a constant in place of an attribute.place of an attribute.

Example 6.5Example 6.5

SELECT title, SELECT title, length/60 AS Length length/60 AS Length ‘hrs.’ AS Hours ‘hrs.’ AS HoursFROM MoviesFROM MoviesWHERE studioName = 'DISNEY' WHERE studioName = 'DISNEY' AND year = 1990;AND year = 1990;

Note that SQL is Note that SQL is case insensitivecase insensitive..

1313

Page 14: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.2 Selection in SQL 6.1.2 Selection in SQL

We can select desired tuples through We can select desired tuples through “WHERE” clause.“WHERE” clause.

Comparison operators:Comparison operators: = (like == in Java)= (like == in Java) <> (like != in Java)<> (like != in Java) << >> <=<= >=>=

Concatenation operator for strings: Concatenation operator for strings: ||||

1414

Page 15: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.2 Selection in SQL (cont’d) 6.1.2 Selection in SQL (cont’d)

String constants String constants are surrounded by single are surrounded by single quotes. Like quotes. Like ‘Disney’‘Disney’ in the previous in the previous examples.examples.

Numeric constants can be Integer or Real.Numeric constants can be Integer or Real. The result of a comparison is TRUE or The result of a comparison is TRUE or

FALSE.FALSE. Logical operators are: AND, OR, NOTLogical operators are: AND, OR, NOT The The precedence of logical operators precedence of logical operators is:is:

NOT, AND, ORNOT, AND, OR Use parenthesis to break this precedence.Use parenthesis to break this precedence.

1515

Page 16: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.2 Selection in SQL (cont’d) 6.1.2 Selection in SQL (cont’d)

Example 6.6Example 6.6Query for the movies that made by ‘MGM’ Query for the movies that made by ‘MGM’ studios and either were made after 1970 studios and either were made after 1970 or were less than 90 minutes long.or were less than 90 minutes long.

SELECT title SELECT title FROM MoviesFROM MoviesWHERE studioName = ‘MGM’ WHERE studioName = ‘MGM’ AND (year > 1970 OR length < 90);AND (year > 1970 OR length < 90);

1616

Page 17: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.2 Selection in SQL (cont’d) 6.1.2 Selection in SQL (cont’d)

Example 6.6Example 6.6Query for the movies that made by ‘MGM’ Query for the movies that made by ‘MGM’ studios and either were made after 1970 studios and either were made after 1970 or were less than 90 minutes long.or were less than 90 minutes long.

SELECT title SELECT title FROM MoviesFROM MoviesWHERE studioName = ‘MGM’ WHERE studioName = ‘MGM’ AND (year > 1970 OR length < 90);AND (year > 1970 OR length < 90);

1717

Page 18: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.3 Comparison of Strings6.1.3 Comparison of Strings

Strings can be stored as either fixed-Strings can be stored as either fixed-length using length using CHARCHAR data type or variable- data type or variable-length using length using VARCHARVARCHAR..

When comparing strings, only real When comparing strings, only real characters are considered and padding characters are considered and padding characters are ignored regardless of the characters are ignored regardless of the data type declaration.data type declaration.

When comparing strings using <, >, <=, When comparing strings using <, >, <=, or >=, lexicographic order of characters or >=, lexicographic order of characters are considered. (like dictionary)are considered. (like dictionary)

1818

Page 19: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.3 Comparison of Strings 6.1.3 Comparison of Strings (cont’d)(cont’d)

Example:Example:‘fodder’ < ‘foo’‘fodder’ < ‘foo’Because, ‘fo’ = ‘fo’ but ‘d’ < ‘o’Because, ‘fo’ = ‘fo’ but ‘d’ < ‘o’

Example:Example:‘bar’ < ‘bargain’‘bar’ < ‘bargain’Because, ‘bar’ = ‘bar’ but ‘’ < ‘gain’Because, ‘bar’ = ‘bar’ but ‘’ < ‘gain’

Note that ‘A’ < ‘a’Note that ‘A’ < ‘a’

1919

Page 20: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.4 Pattern Matching in SQL6.1.4 Pattern Matching in SQL

Two alternative forms of comparison are:Two alternative forms of comparison are:s s LIKELIKE p ps s NOT LIKE NOT LIKE ppWhere s is a string and p is a Where s is a string and p is a patternpattern..

p can contain wildcards or ordinary p can contain wildcards or ordinary chars.chars.

%% matches zero or more chars matches zero or more chars __ (underscore) matches one char (underscore) matches one char Note that Note that strings are case sensitivestrings are case sensitive..

2020

Page 21: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.4 Pattern Matching in SQL 6.1.4 Pattern Matching in SQL (cont’d)(cont’d)

Example 6.7Example 6.7

SELECT title SELECT title FROM MoviesFROM MoviesWHERE title LIKE 'STAR_ _ _ _ _';WHERE title LIKE 'STAR_ _ _ _ _';

SELECT title FROM Movies WHERE title LIKE 'STAR ____';SELECT title FROM Movies WHERE title LIKE 'STAR ____'; Retrieves the titles that starts with ‘STAR’, Retrieves the titles that starts with ‘STAR’,

then one blank and the 4 last chars can be then one blank and the 4 last chars can be anything.anything.

So, possible matches can be:So, possible matches can be:‘Star Wars’, ‘Star Trek’‘Star Wars’, ‘Star Trek’

2121

Page 22: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.4 Pattern Matching in SQL 6.1.4 Pattern Matching in SQL (cont’d)(cont’d)

Example 6.8Example 6.8SELECT title SELECT title FROM MoviesFROM MoviesWHERE title LIKE ‘%’’s%’;WHERE title LIKE ‘%’’s%’;

Note that Note that if your string contains single if your string contains single quotequote, put another single quote to , put another single quote to distinguish between surrounding single distinguish between surrounding single quotes and the single quote itself.quotes and the single quote itself.

Retrieve all movies that contain the ‘s in Retrieve all movies that contain the ‘s in their name like: Logan’s Run, Alice’s their name like: Logan’s Run, Alice’s RestaurantRestaurant

2222

Page 23: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.5 Dates and Times6.1.5 Dates and Times

A A date constant date constant is represented by the is represented by the keywordkeyword DATEDATE followed by a quoted followed by a quoted string. string.

For example: For example: DATE ‘1961-08-24’DATE ‘1961-08-24’ Note the strict format of the Note the strict format of the ‘YYYY-mm-dd’‘YYYY-mm-dd’

2323

Page 24: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.5 Dates and Times (cont’d)6.1.5 Dates and Times (cont’d)

A A time constant time constant is represented by the is represented by the keyword TIMEkeyword TIME followed by a quoted followed by a quoted string.string.

For example: For example: TIME ’15:05:03’TIME ’15:05:03’ Another example: Another example: TIME ’15:05:03.15’TIME ’15:05:03.15’ Note the strict format of Note the strict format of ‘HH:mm:ss’ ‘HH:mm:ss’

andand ‘HH:mm:ss.nnn’‘HH:mm:ss.nnn’ Note that HH is a Note that HH is a military format military format (24-(24-

hour)hour) FractionsFractions of seconds can be as many as of seconds can be as many as

significant digit you likesignificant digit you like2424

Page 25: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.5 Dates and Times (cont’d)6.1.5 Dates and Times (cont’d)

An alternative for the time is to An alternative for the time is to represent it with respect to GMT represent it with respect to GMT (Greenwich Mean Time)(Greenwich Mean Time)

TIME ‘HH:mm:ss – HH:mm’ TIME ‘HH:mm:ss – HH:mm’ to represent the to represent the times behind GMT.times behind GMT.

For example: TIME ‘12:00:00 For example: TIME ‘12:00:00 –– 8:00) 8:00) represent the noon in Pacific standard represent the noon in Pacific standard time (PST)time (PST)

TIME ‘HH:mm:ss + HH:mm’ TIME ‘HH:mm:ss + HH:mm’ to represent to represent the times ahead GMT.the times ahead GMT.

2525

Page 26: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.5 Dates and Times (cont’d)6.1.5 Dates and Times (cont’d)

To combine date and time, we use a To combine date and time, we use a value of type value of type TIMESTAMPTIMESTAMP..

Use format:Use format: TIMESTAMP ‘YYYY-mm-dd HH:mm:ss’ TIMESTAMP ‘YYYY-mm-dd HH:mm:ss’ to represent a timestamp. to represent a timestamp. Note the space between the date and the Note the space between the date and the time.time.

For example: For example: TIMESTAMP ‘1961-08-24 12:00:00’ TIMESTAMP ‘1961-08-24 12:00:00’

TIMESTAMPs values can be compared by TIMESTAMPs values can be compared by the comparison operators.the comparison operators.

2626

Page 27: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.6 Null Values and 6.1.6 Null Values and Comparisons Involving NULLComparisons Involving NULL

Different interpretations for NULL values:Different interpretations for NULL values:

1.1. Value unknownValue unknownI know there is some value here but I I know there is some value here but I don’t know what it is?don’t know what it is?

2.2. Value inapplicableValue inapplicableThere is no value that make sense here.There is no value that make sense here.

3.3. Value withheldValue withheldWe are not entitled to know this value.We are not entitled to know this value.

2727

Page 28: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.6 Null Values and 6.1.6 Null Values and Comparisons Involving NULL Comparisons Involving NULL (cont’d)(cont’d)

When operating upon a NULL value, When operating upon a NULL value, remember that:remember that:

1.1. The result would be The result would be NULLNULL if any value, if any value, including NULL, is one of the operands of including NULL, is one of the operands of an arithmetic operation.an arithmetic operation.Example: price + 1 = NULL if the price is Example: price + 1 = NULL if the price is NULLNULL

2.2. The result would be The result would be UNKNOWNUNKNOWN if we if we compare a value, including NULL, with compare a value, including NULL, with NULL.NULL.Example: ‘Ali’ < NULL is UNKNOWNExample: ‘Ali’ < NULL is UNKNOWN

Note that NULL is not a constant.Note that NULL is not a constant.2828

Page 29: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.6 Null Values and 6.1.6 Null Values and Comparisons Involving NULL Comparisons Involving NULL (cont’d)(cont’d)

Example 6.9Example 6.9Let x have the value NULLLet x have the value NULLThen x + 3 is also NULLThen x + 3 is also NULLBut NULL + 3 is not a legal SQL expressionBut NULL + 3 is not a legal SQL expression

Also: x=3 is unknown in the above example Also: x=3 is unknown in the above example because we cannot say whether the x because we cannot say whether the x which currently is NULL is equal 3 or not?which currently is NULL is equal 3 or not?

Note that NULL = 3 is not correct SQL.Note that NULL = 3 is not correct SQL. We use: x We use: x IS NULL IS NULL or x or x IS NOT NULL IS NOT NULL to to

check if the x is NULL or not.check if the x is NULL or not.

2929

Page 30: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.7 The Truth-Value 6.1.7 The Truth-Value UNKNOWNUNKNOWN

UNKNOWN is the third truth value beside UNKNOWN is the third truth value beside TRUE and FALSE.TRUE and FALSE.

To memorize what the results of an To memorize what the results of an operation would be, consider TRUE = 1, operation would be, consider TRUE = 1, FALSE = 0, and UNKNOWN = ½FALSE = 0, and UNKNOWN = ½

The AND of two operand is the minimum The AND of two operand is the minimum of those valuesof those values

The OR of two operand is the maximum The OR of two operand is the maximum of those valuesof those values

The NOT of a value v is 1 - vThe NOT of a value v is 1 - v

3030

Page 31: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.7 The Truth-Value 6.1.7 The Truth-Value UNKNOWN (cont’d)UNKNOWN (cont’d)

The truth tableThe truth table

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

3131

Page 32: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.7 The Truth-Value 6.1.7 The Truth-Value UNKNOWN (cont’d)UNKNOWN (cont’d)

The relevant tuples won’t be retrieved if The relevant tuples won’t be retrieved if a condition in WHERE clause is evaluated a condition in WHERE clause is evaluated to UNKNOWN.to UNKNOWN.

Example 6.10Example 6.10SELECT * SELECT * FROM Movies_nullFROM Movies_nullWHERE length <= 120 OR length > 120;WHERE length <= 120 OR length > 120;

The query won’t retrieve the tuples that The query won’t retrieve the tuples that contain NULL values in the length column contain NULL values in the length column even though we expect it should bring all even though we expect it should bring all movies.movies.

3232

Page 33: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.7 The Truth-Value 6.1.7 The Truth-Value UNKNOWN (cont’d)UNKNOWN (cont’d)

The relevant tuples won’t be retrieved if The relevant tuples won’t be retrieved if a condition in WHERE clause is evaluated a condition in WHERE clause is evaluated to UNKNOWN.to UNKNOWN.

Example 6.10Example 6.10SELECT Studioname SELECT Studioname

FROM Movies_nullFROM Movies_null WHERE length is null; WHERE length is null;

3333

Page 34: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.8 Ordering the Output6.1.8 Ordering the Output

We may ask to represent the output in We may ask to represent the output in sorted order.sorted order.

We modify the select-from-where format We modify the select-from-where format and add the following clause:and add the following clause:ORDER BY <list of attributes>ORDER BY <list of attributes>

The order is by default ascending but you The order is by default ascending but you can ask to order in descending order by can ask to order in descending order by appending the keyword appending the keyword DESCDESC to an to an attributesattributes

Similarly, you can add keyword Similarly, you can add keyword ASCASC for for ascending order.ascending order.

3434

Page 35: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.8 Ordering the Output 6.1.8 Ordering the Output (cont’d)(cont’d)

Example 6.11Example 6.11SELECT producerC#,title SELECT producerC#,title FROM MoviesFROM MoviesWHERE studioName = 'DISNEY' WHERE studioName = 'DISNEY' AND year = 1990AND year = 1990ORDER BY length DESC, title;ORDER BY length DESC, title;

Note that we can list any attributes in the Note that we can list any attributes in the ORDER BY clause even if the attributes are ORDER BY clause even if the attributes are not listed in the SELECT clause.not listed in the SELECT clause.

The list can even have an expression. For The list can even have an expression. For example example ORDER BY year - 10ORDER BY year - 10

3535

Page 36: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.1.9 Exercises for Section 6.16.1.9 Exercises for Section 6.1

3636

Page 37: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

QUERIES INVOLVING MORE THAN QUERIES INVOLVING MORE THAN ONE RELATIONONE RELATION

Section 6.2Section 6.2

3737

Page 38: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2 Queries Involving More 6.2 Queries Involving More Than One RelationThan One Relation

6.2.1 Products and Joins in SQL6.2.1 Products and Joins in SQL

6.2.2 Disambiguating Attributes6.2.2 Disambiguating Attributes

6.2.3 Tuple Variables6.2.3 Tuple Variables

6.2.4 Interpreting Multi-Relation Queries6.2.4 Interpreting Multi-Relation Queries

6.2.5 Union, Intersection, and Difference of 6.2.5 Union, Intersection, and Difference of QueriesQueries

6.2.6 Exercises for Section 6.26.2.6 Exercises for Section 6.2

3838

Page 39: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.1 Products and Joins in 6.2.1 Products and Joins in SQLSQL

Cartesian product Cartesian product of two relations R(a, b) of two relations R(a, b) containing n tuples and T(c, d, e) containing containing n tuples and T(c, d, e) containing m tuples, produces another relation m tuples, produces another relation U(a, b, c, d, e) with n x m tuples.U(a, b, c, d, e) with n x m tuples.

To have a clear picture about what happens To have a clear picture about what happens when we make a Cartesian product of two when we make a Cartesian product of two relations, consider a two level nested for-relations, consider a two level nested for-loop: in the outer loop, each tuples of R is loop: in the outer loop, each tuples of R is retrieved and in the inner loop, it combines retrieved and in the inner loop, it combines with each tuples of T and produce the with each tuples of T and produce the resulting relation tuples.resulting relation tuples.

3939

Page 40: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.1 Products and Joins in 6.2.1 Products and Joins in SQL (Product)SQL (Product)

Example 6.12a Example 6.12a

Movies(title, year, length, genre, Movies(title, year, length, genre, studioName, producerC#)studioName, producerC#)

MovieExec(name, address, cert#, netWorth)MovieExec(name, address, cert#, netWorth)

SELECT *SELECT *

FROM Movies, MovieExecFROM Movies, MovieExec

4040

Page 41: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.1 Products and Joins in 6.2.1 Products and Joins in SQL (cont’d)SQL (cont’d)

Cartesian product of two relations are Cartesian product of two relations are not usually used. We should eliminate not usually used. We should eliminate the unnecessary tuples by applying some the unnecessary tuples by applying some conditions in the WHERE clause.conditions in the WHERE clause.

Based on what conditions we apply in the Based on what conditions we apply in the WHERE clause, different types of joins WHERE clause, different types of joins are produced.are produced.

4141

Page 42: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.1 Products and Joins in 6.2.1 Products and Joins in SQL (Equi/Theta Join)SQL (Equi/Theta Join)

Example 6.12aExample 6.12a

Retrieve the name of the producer of ‘Star Retrieve the name of the producer of ‘Star Wars’.Wars’.

We need the following relations:We need the following relations:

Movies(title, year, length, Incolor, Movies(title, year, length, Incolor, studioName, producerC#)studioName, producerC#)

MovieExec(name, address, cert#, netWorth)MovieExec(name, address, cert#, netWorth)SELECT * SELECT *

FROM Movies, MovieExecFROM Movies, MovieExec

WHERE title = 'STAR WARS' WHERE title = 'STAR WARS'

AND producerC# = cert#;AND producerC# = cert#;4242

Page 43: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.1 Products and Joins in 6.2.1 Products and Joins in SQL (DB1)SQL (DB1)

EquiJoin EquiJoin involves the join of two or more

tables using the equality operator. The equijoin by definition produces a result containing two identical columns.

4343

Page 44: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.1 Products and Joins in 6.2.1 Products and Joins in SQL (DB1)SQL (DB1)

Example : Get all supplier and part information such

that the supplier and part in question are located in the same city.

  SELECT SUPPLIER.*, PART.* FROM SUPPLIER, PART WHERE SUPPLIER.CITY=PART.CITY;  

4444

Page 45: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.1 Products and Joins in 6.2.1 Products and Joins in SQL (Natural Join)SQL (Natural Join)

Example 6.12bExample 6.12b

SELECT SELECT

title, year, length, InColor, studioName, title, year, length, InColor, studioName,

producerC#,name,address,producerC#,name,address,cert#(del),netWorth,netWorthFROM Movies, MovieExecFROM Movies, MovieExec

WHERE title = 'STAR WARS' WHERE title = 'STAR WARS'

AND producerC# = cert#;AND producerC# = cert#;

4545

Page 46: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.1 Products and Joins in 6.2.1 Products and Joins in SQL (cont’d)SQL (cont’d)

Example 6.12Example 6.12

Retrieve the name of the producer of ‘Star Retrieve the name of the producer of ‘Star Wars’.Wars’.

We need the following relations:We need the following relations:

Movies(title, year, length, InColor, Movies(title, year, length, InColor, studioName, producerC#)studioName, producerC#)

MovieExec(name, address, cert#, netWorth)MovieExec(name, address, cert#, netWorth)SELECT name SELECT name

FROM Movies, MovieExecFROM Movies, MovieExec

WHERE title = 'STAR WARS' WHERE title = 'STAR WARS'

AND producerC# = cert#;AND producerC# = cert#;4646

Page 47: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.1 Products and Joins in 6.2.1 Products and Joins in SQL (DB1)SQL (DB1)

Natural Join In the equijoin, if one of the two identical

columns is eliminated, then what is left is called the natural join. Natural join is probably the single most useful form of join and is usually referred to as simply "Join"

4747

Page 48: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.1 Products and Joins in 6.2.1 Products and Joins in SQL (DB1)SQL (DB1)

SELECT S.SNUM, S.SNAME, S.STATUS, S.CITY, P.PNUM, P.PNAME, P.COLOR, P.WEIGHT FROM SUPPLIERS S, PARTS P Where S.CITY=P.CITY;

4848

Page 49: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.2 Disambiguating 6.2.2 Disambiguating AttributesAttributes

If the relations involving in a join contain If the relations involving in a join contain attributes with the same name, then we attributes with the same name, then we must qualify each attribute by the must qualify each attribute by the relation name and a dot as follows:relation name and a dot as follows:

Example 6.13Example 6.13

Retrieve pairs of stars and executives with Retrieve pairs of stars and executives with the same address.the same address.SELECT SELECT MovieStar.name, MovieExec.nameMovieStar.name, MovieExec.name

FROM MovieStar, MovieExecFROM MovieStar, MovieExec

WHERE MovieStar.address =WHERE MovieStar.address =

MovieExec.address;MovieExec.address;4949

Page 50: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.3 Tuple Variables6.2.3 Tuple Variables

Sometimes we need to join a relation with Sometimes we need to join a relation with itself. So, the previous method does not itself. So, the previous method does not work here.work here.

We may list a relation R as many times as We may list a relation R as many times as we need to in the FROM clause.we need to in the FROM clause.

We need a way to refer to each We need a way to refer to each occurrence of R.occurrence of R.

SQL allows us to define an SQL allows us to define an aliasalias for each for each occurrence of R.occurrence of R.

This is also called This is also called Tuple VariableTuple Variable. .

5050

Page 51: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.3 Tuple Variables (cont’d)6.2.3 Tuple Variables (cont’d)

Tuple variable syntax:Tuple variable syntax:From R AS R1, R AS R2, ...From R AS R1, R AS R2, ...

Note that Note that AS is optional AS is optional and usually we and usually we omit it.omit it.

If a relation has an alias, you are allowed If a relation has an alias, you are allowed to disambiguate the attributes with the to disambiguate the attributes with the alias as well.alias as well.

When you don’t mention tuple variable When you don’t mention tuple variable for a relation, in fact it has a tuple for a relation, in fact it has a tuple variable with the same name of the variable with the same name of the relation.relation.

5151

Page 52: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.3 Tuple Variables (cont’d)6.2.3 Tuple Variables (cont’d)

Example 6.14Example 6.14

Retrieve pairs of stars that share the same Retrieve pairs of stars that share the same address.address.SELECT SELECT Star1.name, Star2.nameStar1.name, Star2.name

FROM MovieStar Star1, MovieStar Star2FROM MovieStar Star1, MovieStar Star2

WHERE Star1.address = Star2.addressWHERE Star1.address = Star2.address

AND Star1.name < Star2.name;AND Star1.name < Star2.name;

What’s the role of the second condition?What’s the role of the second condition? What would happen if we use <>?What would happen if we use <>?

5252

Page 53: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.4 Interpreting Multi-6.2.4 Interpreting Multi-Relation QueriesRelation Queries

If there are several tuple variables, we If there are several tuple variables, we may imagine may imagine nested loopsnested loops, one for each , one for each tuple variable, in which the variables tuple variable, in which the variables each range over the tuples of their each range over the tuples of their respective relations.respective relations.

For each assignment of tuples to the For each assignment of tuples to the tuple variables, we decide whether the tuple variables, we decide whether the WHERE clause is true.WHERE clause is true.

If so, we produce a tuple consisting of If so, we produce a tuple consisting of the values of the expressions following the values of the expressions following SELECT.SELECT.

5353

Page 54: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.4 Interpreting Multi-6.2.4 Interpreting Multi-Relation Queries (cont’d)Relation Queries (cont’d)

Another approach is to relate the query Another approach is to relate the query to relational algebra.to relational algebra.

At first, create the Cartesian product of At first, create the Cartesian product of the relations.the relations.

Then, apply selection operator by Then, apply selection operator by considering WHERE clause conditions.considering WHERE clause conditions.

Then, make a projection of the attributes Then, make a projection of the attributes listed in the SELECT clause.listed in the SELECT clause.

5454

Page 55: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.4 Interpreting Multi-6.2.4 Interpreting Multi-Relation Queries (cont’d)Relation Queries (cont’d)

Example 6.15Example 6.15

Convert the query of example 6.14 to RA.Convert the query of example 6.14 to RA.

ΠΠL1 L1 (σ(σC1 And C2C1 And C2 (R X T)) (R X T))

Where:Where:

R = MovieStar Star1R = MovieStar Star1

T = MovieStar Star2T = MovieStar Star2

L1 = Star1.name, Start2.nameL1 = Star1.name, Start2.name

C1 = Star1.address = Star2.addressC1 = Star1.address = Star2.address

C2 = Star1.name < Star2.nameC2 = Star1.name < Star2.name

5555

Page 56: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.5 Union, Intersection, and 6.2.5 Union, Intersection, and Difference of QueriesDifference of Queries

There are 3 operations in set theory There are 3 operations in set theory called: Union, Intersection, and called: Union, Intersection, and DifferenceDifference

SQL uses the keywords SQL uses the keywords UNIONUNION, , INTERSECTINTERSECT, and , and EXCEPTEXCEPT for the same for the same operations on relations.operations on relations.

Next example shows how to use these Next example shows how to use these operators on relations.operators on relations.

5656

Page 57: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.5 Union, Intersection, and 6.2.5 Union, Intersection, and Difference of Queries (cont’d)Difference of Queries (cont’d)

Example 6.16Example 6.16

Query the names and addresses of all Query the names and addresses of all female movie stars who are also movie female movie stars who are also movie executives with a net worth over executives with a net worth over $10,000,000$10,000,000

(SELECT name, address FROM MovieStar(SELECT name, address FROM MovieStar

WHERE gender = 'M') WHERE gender = 'M')

INTERSECT INTERSECT

(SELECT name, address FROM MovieExec(SELECT name, address FROM MovieExec

WHERE netWorth > 10000000);WHERE netWorth > 10000000);

5757

Page 58: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.5 Union, Intersection, and 6.2.5 Union, Intersection, and Difference of Queries (cont’d)Difference of Queries (cont’d)

Example 6.17Example 6.17

Query the names and addresses of movie Query the names and addresses of movie stars who are not movie executives.stars who are not movie executives.

(SELECT name, address FROM MovieStar)(SELECT name, address FROM MovieStar)

MINUS MINUS

(SELECT name, address FROM MovieExec);(SELECT name, address FROM MovieExec);

5858

Page 59: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.5 Union, Intersection, and 6.2.5 Union, Intersection, and Difference of Queries (cont’d)Difference of Queries (cont’d)

Example 6.18Example 6.18

Query all the titles and years of movies Query all the titles and years of movies that appeared in either the Movies or that appeared in either the Movies or StarsIn relations. StarsIn relations. (SELECT title, year FROM Movies)(SELECT title, year FROM Movies)

UNION UNION

(SELECT movieTitle AS title, movieYear AS year (SELECT movieTitle AS title, movieYear AS year

FROM StarsInFROM StarsIn););

5959

Page 60: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.2.6 Exercises for Section 6.26.2.6 Exercises for Section 6.2

6060

Page 61: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

SUBQUERIESSUBQUERIESSection 6.3Section 6.3

6161

Page 62: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3 Subqueries6.3 Subqueries

6.3.1 Subqueries that Produce Scalar 6.3.1 Subqueries that Produce Scalar ValuesValues

6.3.2 Conditions Involving Relations 6.3.2 Conditions Involving Relations

6.3.3 Conditions Involving Tuples6.3.3 Conditions Involving Tuples

6.3.4 Correlated Subqueries6.3.4 Correlated Subqueries

6.3.5 Subqueries in 6.3.5 Subqueries in FromFrom Clauses Clauses

6.3.6 SQL Join Expressions6.3.6 SQL Join Expressions

6.3.7 Natural Joins6.3.7 Natural Joins

6.3.8 Outer Joins6.3.8 Outer Joins

6.3.9 Exercises for Section 6.36.3.9 Exercises for Section 6.36262

Page 63: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3 Subqueries6.3 Subqueries

A query that is part of another is called a A query that is part of another is called a subquerysubquery..

The subqueries can have subqueries as The subqueries can have subqueries as well.well.

We already saw subqueries in the We already saw subqueries in the previous examples. We created a UNION previous examples. We created a UNION query by connecting two subqueries.query by connecting two subqueries.

6363

Page 64: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.1 Subqueries that Produce 6.3.1 Subqueries that Produce Scalar ValuesScalar Values

A select-from-where expression can A select-from-where expression can produce a relation with any number of produce a relation with any number of attributes and any number of tuples.attributes and any number of tuples.

If it produces one tuple with one If it produces one tuple with one attribute, we call it a attribute, we call it a scalarscalar..

We can use a scalar as a We can use a scalar as a constantconstant. To do . To do that, we surround the query in a that, we surround the query in a parenthesis as the following example parenthesis as the following example shows.shows.

6464

Page 65: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.1 Subqueries that Produce 6.3.1 Subqueries that Produce Scalar Values (cont’d)Scalar Values (cont’d)

Example 6.19 (another version of Example Example 6.19 (another version of Example 6.12)6.12)

Query the producer of Star Wars. Query the producer of Star Wars.

SELECT nameSELECT name

FROM MovieExecFROM MovieExec

WHERE cert# = (SELECT producerC# WHERE cert# = (SELECT producerC#

FROM MoviesFROM Movies

WHERE title = 'STAR WARS' WHERE title = 'STAR WARS' and producerC# > 950); and producerC# > 950);

What would happen if the subquery retrieve What would happen if the subquery retrieve zero or more than one tuple?zero or more than one tuple?

6565

Page 66: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.1 Subqueries that Produce 6.3.1 Subqueries that Produce Scalar Values (cont’d)Scalar Values (cont’d)

Example 6.19 (another version of Example Example 6.19 (another version of Example 6.12)6.12)

Query the producer of Star Wars. Query the producer of Star Wars.

SELECT nameSELECT name

FROM MovieExecFROM MovieExec

WHERE cert# in (SELECT producerC# WHERE cert# in (SELECT producerC#

FROM MoviesFROM Movies

WHERE title = 'STAR WARS' WHERE title = 'STAR WARS' ); );

The subquery retrieve zero or more than one The subquery retrieve zero or more than one tuple.tuple.

6666

Page 67: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.2 Conditions Involving 6.3.2 Conditions Involving Relations (skip to examples)Relations (skip to examples)

These operators can be applied to These operators can be applied to relations and produce a Boolean result.relations and produce a Boolean result.

The relation must be expressed as a The relation must be expressed as a subquery.subquery.

In this sub-section, we consider the In this sub-section, we consider the operators in their simple form where a operators in their simple form where a scalar value s is involved.scalar value s is involved.

Therefore, the subquery R is required to Therefore, the subquery R is required to produce a one-column relation.produce a one-column relation.

6767

Page 68: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.2 Conditions Involving 6.3.2 Conditions Involving Relations (skip to examples)Relations (skip to examples)

EXISTS REXISTS R is true iff R is not empty. is true iff R is not empty. s IN R s IN R is true iff s is equal to one of the is true iff s is equal to one of the

values in R. values in R. s > ALL R s > ALL R is true iff s is greater than is true iff s is greater than

every value in unary relation R. Other every value in unary relation R. Other comparison operators (<, <=, >=, =, <>) comparison operators (<, <=, >=, =, <>) can be used.can be used.

s > ANY R s > ANY R is true iff s is greater than at is true iff s is greater than at least one value in unary relation R. Other least one value in unary relation R. Other comparison operators (<, <=, >=, =, <>) comparison operators (<, <=, >=, =, <>) can be used.can be used.

6868

Page 69: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.2 Conditions Involving 6.3.2 Conditions Involving Relations (skip to examples)Relations (skip to examples)

To negate EXISTS, ALL, and ANY To negate EXISTS, ALL, and ANY operators, put NOT in front of the entire operators, put NOT in front of the entire expression.expression.NOT EXISTS RNOT EXISTS R, , NOT s > ALL RNOT s > ALL R, , NOT s > NOT s > ANY RANY R

s NOT IN R s NOT IN R is the negation of IN operator.is the negation of IN operator. Some situations of these operators are Some situations of these operators are

equal to other operators.equal to other operators. For example: For example:

s <> ALL R is equal to s NOT IN Rs <> ALL R is equal to s NOT IN Rs = ANY R is equal to s IN Rs = ANY R is equal to s IN R

6969

Page 70: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.3 Conditions Involving 6.3.3 Conditions Involving TuplesTuples(skip to examples)(skip to examples)

A tuple in SQL is represented by a A tuple in SQL is represented by a parenthesized list of scalar values.parenthesized list of scalar values.

Examples: Examples: (123, ‘I am a string’, 0, NULL)(123, ‘I am a string’, 0, NULL)(name, address, salary)(name, address, salary)

The first example shows all constants and The first example shows all constants and the second shows attributes.the second shows attributes.

Mixing constants and attributes are Mixing constants and attributes are allowed.allowed.

7070

Page 71: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.1 Subqueries that Produce 6.3.1 Subqueries that Produce Scalar Values (cont’d)Scalar Values (cont’d)

Example 6.19 (another version of Example 6.12)Example 6.19 (another version of Example 6.12)

Query the producer of Star Wars. Query the producer of Star Wars.

SELECT nameSELECT name

FROM MovieExecFROM MovieExec

WHERE Exists WHERE Exists

(SELECT producerC# (SELECT producerC#

FROM MoviesFROM Movies

WHERE title = 'STAR WARS' WHERE title = 'STAR WARS' and cert# = producerC# ); and cert# = producerC# );

The subquery retrieve zero or more than one The subquery retrieve zero or more than one tuple.tuple.

7171

Page 72: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.3 Conditions Involving 6.3.3 Conditions Involving Tuples (Examples)Tuples (Examples)

If the type and the number of attributes in If the type and the number of attributes in a tuple are the same as of a relation, we a tuple are the same as of a relation, we can compare them.can compare them.

Example:Example:('Tom', 'Smith') IN ('Tom', 'Smith') IN

(SELECT firstName, LastName(SELECT firstName, LastName FROM foo); FROM foo);

Note that the order of the attributes must Note that the order of the attributes must be the same in the tuple and the SELECT be the same in the tuple and the SELECT list.list.

7272

Page 73: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.3 Conditions Involving 6.3.3 Conditions Involving Tuples (One/two Column Tuples (One/two Column Relation)Relation)

Example 6.20:Example 6.20:Query all the producers of movies in which Harrison Ford stars.Query all the producers of movies in which Harrison Ford stars.

SELECT nameSELECT name

FROM MovieExecFROM MovieExec

WHERE cer# IN WHERE cer# IN

(SELECT producerC#(SELECT producerC# FROM Movies FROM Movies

WHERE (title, year) IN WHERE (title, year) IN

(SELECT movieTitle, movieYear(SELECT movieTitle, movieYear

FROM StarsINFROM StarsIN

WHERE starName = 'Harrison Ford')WHERE starName = 'Harrison Ford')

););

7373

Page 74: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.3 Conditions Involving 6.3.3 Conditions Involving Tuples (using Join Queries)Tuples (using Join Queries)

Note that sometimes, you can get the same result Note that sometimes, you can get the same result without the expensive subqueries.without the expensive subqueries.

For example, the previous query can be written For example, the previous query can be written as follows:as follows:SELECT nameSELECT name

FROM MovieExec, Movies, StarsINFROM MovieExec, Movies, StarsIN

WHERE cer# = producerC#WHERE cer# = producerC#

AND title = movieTitleAND title = movieTitle

AND year = movieYearAND year = movieYear

And starName = 'Harrison Ford';And starName = 'Harrison Ford';

7474

Page 75: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.4 Correlated Subqueries6.3.4 Correlated Subqueries

The simplest subquery is evaluated once The simplest subquery is evaluated once and the result is used in a higher-level and the result is used in a higher-level query.query.

Some times a subquery is required to be Some times a subquery is required to be evaluated several times, once for each evaluated several times, once for each assignment of a value that comes from a assignment of a value that comes from a tuple variable outside the subquery.tuple variable outside the subquery.

A subquery of this type is called A subquery of this type is called correlated subquerycorrelated subquery..

7575

Page 76: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.4 Correlated Subqueries 6.3.4 Correlated Subqueries (cont'd)(cont'd)

Example 6.21Example 6.21

Query the titles that have been used for Query the titles that have been used for two or more movies.two or more movies.

SELECT titleSELECT title

FROM Movies oldFROM Movies old

WHERE year < ANY WHERE year < ANY

(SELECT year(SELECT year FROM Movies FROM Movies

WHERE title = old.title);WHERE title = old.title);

7676

Page 77: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.5 Subqueries in 6.3.5 Subqueries in FromFrom ClausesClauses

In a FROM list, we my use a In a FROM list, we my use a parenthesized subquery.parenthesized subquery.

The subquery must have a tuple variable The subquery must have a tuple variable or alias.or alias.

7777

Page 78: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.5 Subqueries in 6.3.5 Subqueries in FromFrom Clauses (cont'd)Clauses (cont'd)

Example 6.22Example 6.22

Query the producers of Harrison Ford's Query the producers of Harrison Ford's movies.movies.

Select nameSelect name

FROM MovieExec, FROM MovieExec,

(SELECT producerC#(SELECT producerC# FROM Movies, StarsIN FROM Movies, StarsIN

WHERE title = movieTitleWHERE title = movieTitle

AND year = movieYearAND year = movieYear

AND starName = 'Harrison Ford'AND starName = 'Harrison Ford'

) Prod) Prod

WHERE cert# = Prod.producerC#;WHERE cert# = Prod.producerC#;7878

Page 79: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.6 SQL Join Expressions6.3.6 SQL Join Expressions

Join operators construct new temp Join operators construct new temp relations from existing relations.relations from existing relations.

These relations can be used in any part These relations can be used in any part of the query that you can put a subquery.of the query that you can put a subquery.

Cross join Cross join is the simplest form of a join.is the simplest form of a join. Actually, this is Actually, this is synonymsynonym for for Cartesian Cartesian

productproduct.. For example:For example:From Movies CROSS JOIN StarsInFrom Movies CROSS JOIN StarsInis equal to:is equal to:From Movies, StarsInFrom Movies, StarsIn

7979

Page 80: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.6 SQL Join Expressions 6.3.6 SQL Join Expressions (cont'd)(cont'd)

If the relations we used are:If the relations we used are:Movies(title, year, length, genre, Movies(title, year, length, genre, studioName, producerC#)studioName, producerC#)StarsIn(movieTitle, movieYear, starName)StarsIn(movieTitle, movieYear, starName)Then the result of the CROSS JOIN would be Then the result of the CROSS JOIN would be a relation with the following attributes:a relation with the following attributes:R(title, year, length, genre, studioName, R(title, year, length, genre, studioName, producerC#, movieTitle, movieYear, producerC#, movieTitle, movieYear, starName)starName)

Note that if there is a common name in the Note that if there is a common name in the two relations, then the attributes names two relations, then the attributes names would be qualified with the relation name. would be qualified with the relation name.

8080

Page 81: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.6 SQL Join Expressions 6.3.6 SQL Join Expressions (cont'd)(cont'd)

Cross join by itself is rarely a useful Cross join by itself is rarely a useful operation.operation.

Usually, a theta-join is used as follows:Usually, a theta-join is used as follows:FROM R JOIN S ON conditionFROM R JOIN S ON condition

For example:For example:Movies JOIN StarsIn ONMovies JOIN StarsIn ON title = movieTitle AND title = movieTitle AND year = movieYear year = movieYear

The result would be the same number of The result would be the same number of attributes but the tuples would be those attributes but the tuples would be those that agree on both the title and year.that agree on both the title and year.

8181

Page 82: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.6 SQL Join Expressions 6.3.6 SQL Join Expressions (cont'd)(cont'd)

Note that in the previous example, the title Note that in the previous example, the title and year are repeated twice. Once as title and year are repeated twice. Once as title and year and once as movieTitle and and year and once as movieTitle and movieYear.movieYear.

Considering the point that the resulting Considering the point that the resulting tuples have the same value for title and tuples have the same value for title and movieTitle, and year and movieYear, then movieTitle, and year and movieYear, then we encounter the redundancy of we encounter the redundancy of information.information.

One way to remove the unnecessary One way to remove the unnecessary attributes is projection. You can mention attributes is projection. You can mention the attributes names in the SELECT list.the attributes names in the SELECT list.

8282

Page 83: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.7 Natural Joins6.3.7 Natural Joins

Natural join and theta-join differs in:Natural join and theta-join differs in:1.1. The join conditionThe join condition

All pairs of attributes from the two relations All pairs of attributes from the two relations having a common name are equated, and also having a common name are equated, and also there are no other conditions.there are no other conditions.

2.2. The attributes listThe attributes listOne of each pair of equated attributes is One of each pair of equated attributes is projected out.projected out.

ExampleExampleMovieStar NATURAL JOIN MovieExecMovieStar NATURAL JOIN MovieExec

8383

Page 84: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.7 Natural Joins (cont'd)6.3.7 Natural Joins (cont'd)

Example 6.24Example 6.24

Query those stars who are executive as well.Query those stars who are executive as well.

The relations are:The relations are:

MovieStar(name, address, gender, birthdate)MovieStar(name, address, gender, birthdate)

MovieExec(name, address, cert#, netWorth) MovieExec(name, address, cert#, netWorth)

SELECT MovieStar.nameSELECT MovieStar.name

FROM MovieStar NATURAL JOIN MovieExecFROM MovieStar NATURAL JOIN MovieExec

8484

Page 85: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.8 Outer Joins6.3.8 Outer Joins

Outer join is a way to augment the result Outer join is a way to augment the result of a join by dangling tuples, padded with of a join by dangling tuples, padded with null values.null values.

8585

Page 86: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.8 Outer Joins (cont'd)6.3.8 Outer Joins (cont'd)

Example 6.25Example 6.25

Consider the following relations:Consider the following relations:MovieStar(name, address, gender, birthdate)MovieStar(name, address, gender, birthdate)

MovieExec(name, address, cert#, netWorth)MovieExec(name, address, cert#, netWorth) Then Then

MovieStar MovieStar NATURAL FULL OUTER JOIN NATURAL FULL OUTER JOIN MovieExecMovieExec

Will produce a relation whose tuples are of 3 Will produce a relation whose tuples are of 3 kinds:kinds:

1.1. Those who are both movie stars and Those who are both movie stars and executiveexecutive

2.2. Those who are movie star but not executiveThose who are movie star but not executive

3.3. Those who are executive but not movie starThose who are executive but not movie star8686

Page 87: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.8 Outer Joins (cont'd)6.3.8 Outer Joins (cont'd)

We can replace keyword FULL with LEFT We can replace keyword FULL with LEFT or RIGHT to get two new join.or RIGHT to get two new join.

NATURAL LEFT OUTER JOIN NATURAL LEFT OUTER JOIN would yield the would yield the first two tuples but not the third.first two tuples but not the third.

NATURAL RIGHT OUTER JOIN NATURAL RIGHT OUTER JOIN would yield would yield the first and third tuples but not the the first and third tuples but not the second.second.

8787

Page 88: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.8 Outer Joins (cont'd)6.3.8 Outer Joins (cont'd)

We can have theta-outer-join as follows:We can have theta-outer-join as follows: R FULL OUTER JOIN S ON conditionR FULL OUTER JOIN S ON condition R LEFT OUTER JOIN S ON conditionR LEFT OUTER JOIN S ON condition R RIGHT OUTER JOIN S ON conditionR RIGHT OUTER JOIN S ON condition

8888

Page 89: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.3.9 Exercises for Section 6.36.3.9 Exercises for Section 6.3

8989

Page 90: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

FULL-RELATION OPERATIONSFULL-RELATION OPERATIONSSection 6.4Section 6.4

9090

Page 91: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4 Full-Relation Operations6.4 Full-Relation Operations

6.4.1 Eliminating Duplicates6.4.1 Eliminating Duplicates

6.4.2 Duplicates in Unions, Intersections, 6.4.2 Duplicates in Unions, Intersections, and Differencesand Differences

6.4.3 Grouping and Aggregation in SQL6.4.3 Grouping and Aggregation in SQL

6.4.4 Aggregation Operators6.4.4 Aggregation Operators

6.4.5 Grouping6.4.5 Grouping

6.4.6 Grouping, Aggregation, and Nulls6.4.6 Grouping, Aggregation, and Nulls

6.4.7 6.4.7 HavingHaving Clauses Clauses

6.4.8 Exercises for Section 6.46.4.8 Exercises for Section 6.4

9191

Page 92: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.1 Eliminating Duplicates6.4.1 Eliminating Duplicates

SQL does not eliminate duplicate tuples SQL does not eliminate duplicate tuples by itself. So, it does not treat the by itself. So, it does not treat the relations as a relations as a setset. It treats the relations . It treats the relations as a as a bagbag..

To eliminate duplicate tuples, use To eliminate duplicate tuples, use DISTINCT keyword after SELECT as the DISTINCT keyword after SELECT as the next example shows.next example shows.

Note that duplicate tuples elimination is Note that duplicate tuples elimination is a very expensive operation for database, a very expensive operation for database, so, use DISTINCT keyword wisely.so, use DISTINCT keyword wisely.

9292

Page 93: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.1 Eliminating Duplicates6.4.1 Eliminating Duplicates

Example 6.27Example 6.27

Query all the producers of movies in which Query all the producers of movies in which Harrison Ford stars.Harrison Ford stars.SELECT DISTINCT nameSELECT DISTINCT name

FROM MovieExec, Movies, StarsINFROM MovieExec, Movies, StarsIN

WHERE cer# = producerC#WHERE cer# = producerC#

AND title = movieTitleAND title = movieTitle

AND year = movieYearAND year = movieYear

And starName = 'Harrison Ford';And starName = 'Harrison Ford';

9393

Page 94: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.2 Duplicates in Unions, 6.4.2 Duplicates in Unions, Intersections, and DifferencesIntersections, and Differences

Duplicate tuples Duplicate tuples are eliminated are eliminated in UNION, in UNION, INTERSECT, and EXCEPT.INTERSECT, and EXCEPT.

In other words, bags are converted to sets.In other words, bags are converted to sets. If you don't want this conversion, use If you don't want this conversion, use

keyword ALL after the operators.keyword ALL after the operators. Example 6.28Example 6.28

((SELECT title, year FROM Movies)SELECT title, year FROM Movies)

UNION ALLUNION ALL

(SELECT movieTitle AS title, movieYear AS (SELECT movieTitle AS title, movieYear AS year FROM StarsIn);year FROM StarsIn);

9494

Page 95: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.3 Grouping and 6.4.3 Grouping and Aggregation in SQLAggregation in SQL

We can partition the tuples of a relation We can partition the tuples of a relation into "into "groupsgroups" based on the values of one " based on the values of one or more attributes. The relation can be or more attributes. The relation can be an output of a SELECT statement.an output of a SELECT statement.

Then, we can aggregate the other Then, we can aggregate the other attributes using aggregation operators.attributes using aggregation operators.

For example, we can sum up the salary of For example, we can sum up the salary of the employees of each department by the employees of each department by grouping the company into departments.grouping the company into departments.

9595

Page 96: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.4 Aggregation Operators6.4.4 Aggregation Operators

SQL uses the five aggregation operators:SQL uses the five aggregation operators:SUM, AVG, MIN, MAX, and COUNTSUM, AVG, MIN, MAX, and COUNT

These operators can be applied to scalar These operators can be applied to scalar expressions, typically, a column name.expressions, typically, a column name.

One exception is COUNT(*) which counts One exception is COUNT(*) which counts all the tuples of a query output.all the tuples of a query output.

We can eliminate the duplicate values We can eliminate the duplicate values before applying aggregation operators by before applying aggregation operators by using DISTINCT keyword. For example:using DISTINCT keyword. For example:COUNT(DISTINCT x)COUNT(DISTINCT x)

9696

Page 97: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.4 Aggregation Operators 6.4.4 Aggregation Operators (cont'd)(cont'd)

Example 6.29Example 6.29

Find the average net worth of all movie Find the average net worth of all movie executives.executives.

SELECT AVG(netWorth)SELECT AVG(netWorth)

FROM MovieExec;FROM MovieExec;

9797

Page 98: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.4 Aggregation Operators 6.4.4 Aggregation Operators (cont'd)(cont'd)

Example 6.30Example 6.30

Count the number of tuples in the StarsIn Count the number of tuples in the StarsIn relation.relation.

SELECT COUNT(*)SELECT COUNT(*)

FROM StarsIn;FROM StarsIn;

SELECT COUNT(starName)SELECT COUNT(starName)

FROM StarsIn;FROM StarsIn;

These two statements do the same but you These two statements do the same but you will see the difference in later slides.will see the difference in later slides.

9898

Page 99: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.5 Grouping6.4.5 Grouping

We can group the tuples by using GROUP We can group the tuples by using GROUP BY clause following the WHERE clause.BY clause following the WHERE clause.

The keywords GROUP BY are followed by The keywords GROUP BY are followed by a list of grouping attributes.a list of grouping attributes.

9999

Page 100: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.5 Grouping (cont'd)6.4.5 Grouping (cont'd)

Example 6.31Example 6.31

Find sum of the movies length each studio Find sum of the movies length each studio is produced.is produced.

SELECT studioName, SELECT studioName,

SUM(length) AS Total_LengthSUM(length) AS Total_Length

FROM MoviesFROM Movies

GROUP BY studioName;GROUP BY studioName;

100100

Page 101: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.5 Grouping (cont'd)6.4.5 Grouping (cont'd)

In a SELECT clause that has aggregation, only In a SELECT clause that has aggregation, only those attributes that are mentioned in the those attributes that are mentioned in the GROUP BY clause may appear unaggregated.GROUP BY clause may appear unaggregated.

For example, in previous example, if you For example, in previous example, if you want to add genre in the SELECT list, then, want to add genre in the SELECT list, then, you you mustmust mention it in the GROUP BY list as mention it in the GROUP BY list as well.well.

SELECT studioName, SELECT studioName, genregenre, ,

SUM(length) AS Total_LengthSUM(length) AS Total_Length

FROM MoviesFROM Movies

GROUP BY studioName, GROUP BY studioName, genregenre;;

101101

Page 102: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.5 Grouping (cont'd)6.4.5 Grouping (cont'd)

It is possible to use GROUP BY in a more It is possible to use GROUP BY in a more complex queries about several relations.complex queries about several relations.

In these cases the following steps are In these cases the following steps are applied:applied:1.1. Produce the output relation based on the Produce the output relation based on the

select-from-where parts.select-from-where parts.

2.2. Group the tuples according to the list of Group the tuples according to the list of attributes mentioned in the GROUP BY list.attributes mentioned in the GROUP BY list.

3.3. Apply the aggregation operatorsApply the aggregation operators

102102

Page 103: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.5 Grouping (cont'd)6.4.5 Grouping (cont'd)

Example 6.32Example 6.32

Create a list of each producer name and Create a list of each producer name and the total length of film produced.the total length of film produced.

SELECT name, SUM(length)SELECT name, SUM(length)

FROM MovieExec, MoviesFROM MovieExec, Movies

WHERE producerC# = cert#WHERE producerC# = cert#

GROUP BY name;GROUP BY name;

103103

Page 104: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.6 Grouping, Aggregation, 6.4.6 Grouping, Aggregation, and Nullsand Nulls

What would happen to aggregation What would happen to aggregation operators if the attributes have null operators if the attributes have null values?values?

There are a few rules to rememberThere are a few rules to remember

104104

Page 105: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.6 Grouping, Aggregation, 6.4.6 Grouping, Aggregation, and Nulls (cont'd)and Nulls (cont'd)

1.1. NULL values are ignored when the NULL values are ignored when the aggregation operator is applied on an aggregation operator is applied on an attribute.attribute.

2.2. COUNT(*) counts all tuples of a relation, COUNT(*) counts all tuples of a relation, therefore, it counts the tuples even if the therefore, it counts the tuples even if the tuple contains NULL value. tuple contains NULL value.

3.3. NULL is treated as an ordinary value when NULL is treated as an ordinary value when forming groups.forming groups.

4.4. When we perform an aggregation, except When we perform an aggregation, except COUNT, over an empty bag, the result is COUNT, over an empty bag, the result is NULL. The COUNT of an empty bag is 0NULL. The COUNT of an empty bag is 0

105105

Page 106: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.6 Grouping, Aggregation, 6.4.6 Grouping, Aggregation, and Nulls (cont'd)and Nulls (cont'd)

Example 6.33Example 6.33

Consider a relation R(A, B) with one tuple, Consider a relation R(A, B) with one tuple, both of whose components are NULL. both of whose components are NULL. What's the result of the following What's the result of the following SELECT?SELECT?

SELECT A, COUNT(B)SELECT A, COUNT(B)

FROM RFROM R

GROUP BY A;GROUP BY A;

The result is (NULL, 0) but why?The result is (NULL, 0) but why?

106106

Page 107: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.6 Grouping, Aggregation, 6.4.6 Grouping, Aggregation, and Nulls (cont'd)and Nulls (cont'd)

What's the result of the following SELECT?What's the result of the following SELECT?

SELECT A, COUNT(*)SELECT A, COUNT(*)

FROM RFROM R

GROUP BY A;GROUP BY A;

The result is (NULL, 1) because COUNT(*) The result is (NULL, 1) because COUNT(*) counts the number of tuples and this counts the number of tuples and this relation has one tuple.relation has one tuple.

107107

Page 108: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.6 Grouping, Aggregation, 6.4.6 Grouping, Aggregation, and Nulls (cont'd)and Nulls (cont'd)

What's the result of the following SELECT?What's the result of the following SELECT?

SELECT A, SUM(B)SELECT A, SUM(B)

FROM RFROM R

GROUP BY A;GROUP BY A;

The result is (NULL, NULL) because SUM(B) The result is (NULL, NULL) because SUM(B) addes one NULL value which is NULL.addes one NULL value which is NULL.

108108

Page 109: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.7 HAVING Clauses6.4.7 HAVING Clauses

So far, we have learned how to restrict So far, we have learned how to restrict tuples from contributing in the output of tuples from contributing in the output of a query.a query.

How about if we don't want to list all How about if we don't want to list all groups?groups?

HAVING clause is used to restrict groups.HAVING clause is used to restrict groups. HAVING clause followed by one or more HAVING clause followed by one or more

conditions about the group.conditions about the group.

109109

Page 110: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.7 HAVING Clauses (cont'd)6.4.7 HAVING Clauses (cont'd)

Example 6.34Example 6.34

Query the total film length for only those Query the total film length for only those producers who made at least one film producers who made at least one film prior to 1930.prior to 1930.SELECT name, SUM(length)SELECT name, SUM(length)

FROM MovieExec, MoviesFROM MovieExec, Movies

WHERE producerC# = cert#WHERE producerC# = cert#

GROUP BY nameGROUP BY name

HAVING MIN(year) < 1930;HAVING MIN(year) < 1930;

110110

Page 111: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.7 HAVING Clauses (cont'd)6.4.7 HAVING Clauses (cont'd)

The rules we should remember about The rules we should remember about HAVING:HAVING:1.1. An aggregation in a HAVING clause applies An aggregation in a HAVING clause applies

only to the tuples of the group being tested.only to the tuples of the group being tested.

2.2. Any attribute of relations in the FROM clause Any attribute of relations in the FROM clause may be aggregated in the HAVING clause, but may be aggregated in the HAVING clause, but only those attributes that are in the GROUP only those attributes that are in the GROUP BY list may appear unaggregated in the BY list may appear unaggregated in the HAVING clause (the same rule as for the HAVING clause (the same rule as for the SELECT clause).SELECT clause).

111111

Page 112: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.7 HAVING Clauses (cont'd)6.4.7 HAVING Clauses (cont'd)

The order of clauses in SQL queries would The order of clauses in SQL queries would be:be: SELECTSELECT FROMFROM WHEREWHERE GROUP BYGROUP BY HAVINGHAVING

Only SELECT and FROM are mandatory.Only SELECT and FROM are mandatory.

112112

Page 113: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.4.8 Exercises for Section 6.46.4.8 Exercises for Section 6.4

113113

Page 114: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

DATABASE MODIFICATIONSDATABASE MODIFICATIONSSection 6.5Section 6.5

114114

Page 115: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.5 Database Modifications6.5 Database Modifications

6.5.1 Insertion6.5.1 Insertion

6.5.2 Deletion 6.5.2 Deletion

6.5.3 Updates6.5.3 Updates

6.5.4 Exercises for Section 6.56.5.4 Exercises for Section 6.5

115115

Page 116: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.5.1 Insertion6.5.1 Insertion

The syntax of INSERT statement:The syntax of INSERT statement:

INSERT INTO R(AINSERT INTO R(A11, ..., A, ..., ANN))VALUES (vVALUES (v11, ..., v, ..., vnn););

If the list of attributes doesn't include all If the list of attributes doesn't include all attributes, then it put default values for attributes, then it put default values for the missing attributes.the missing attributes.

116116

Page 117: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.5.1 Insertion (cont'd)6.5.1 Insertion (cont'd)

Example 6.35Example 6.35

INSERT INTO StarsIn(MovieTitle, movieYear, INSERT INTO StarsIn(MovieTitle, movieYear, starName)starName)

VALUES ('The Maltese Falcon', 1942, 'Sydney VALUES ('The Maltese Falcon', 1942, 'Sydney Greenstreet');Greenstreet');

If we are sure about the order of the attributes, If we are sure about the order of the attributes, then we can write the statement as follows:then we can write the statement as follows:

INSERT INTO StarsInINSERT INTO StarsIn

VALUES ('The Maltese Falcon', 1942, 'Sydney VALUES ('The Maltese Falcon', 1942, 'Sydney Greenstreet');Greenstreet');

117117

Page 118: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.5.1 Insertion (cont'd)6.5.1 Insertion (cont'd)

The simple insert can insert only one The simple insert can insert only one tuple, however, if you want to insert tuple, however, if you want to insert multiple tuples , then you can use the multiple tuples , then you can use the following syntax:following syntax:

INSERT INTO R(AINSERT INTO R(A11, ..., A, ..., ANN))SELECT vSELECT v11, ..., v, ..., vnn

FROM RFROM R11, R, R22, ..., R, ..., RNN

WHERE <condition>;WHERE <condition>;

118118

Page 119: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.5.1 Insertion (cont'd)6.5.1 Insertion (cont'd)

Example 6.36Example 6.36

Suppose that we want to insert all studio Suppose that we want to insert all studio names that are mentioned in the Movies names that are mentioned in the Movies relation but they are not in the Studio relation but they are not in the Studio yet.yet.

INSERT INTO Studio(name)INSERT INTO Studio(name)SELECT DISTINCT studioNameSELECT DISTINCT studioNameFROM MoviesFROM MoviesWHERE studionName NOT INWHERE studionName NOT IN

(SELECT name(SELECT name

FROM Studio);FROM Studio);119119

Page 120: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.5.2 Deletion 6.5.2 Deletion

The syntax of DELETE statement:The syntax of DELETE statement:

DELETE FROM RDELETE FROM RWHERE <condition>;WHERE <condition>;

Every tuples satisfying the condition will Every tuples satisfying the condition will be deleted from the relation R.be deleted from the relation R.

120120

Page 121: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.5.2 Deletion (cont'd) 6.5.2 Deletion (cont'd)

Example 6.37Example 6.37

DELETE FROM StarsInDELETE FROM StarsIn

WHERE movieTitle = 'The Maltese Falcon' ANDWHERE movieTitle = 'The Maltese Falcon' AND

movieYear = 1942 ANDmovieYear = 1942 AND

starName = 'Sydney Greenstreet';starName = 'Sydney Greenstreet';

121121

Page 122: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.5.2 Deletion (cont'd) 6.5.2 Deletion (cont'd)

Example 6.38Example 6.38

Delete all movie executives whose net Delete all movie executives whose net worth is less than ten million dollars.worth is less than ten million dollars.

DELETE FROM MovieExecDELETE FROM MovieExec

WHERE netWorth < 10000000;WHERE netWorth < 10000000;

122122

Page 123: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.5.3 Updates6.5.3 Updates

The syntax of UPDATE statement:The syntax of UPDATE statement:

UPDATE RUPDATE RSET <value-assignment>SET <value-assignment>WHERE <condition>;WHERE <condition>;

Every tuples satisfying the condition will Every tuples satisfying the condition will be updated from the relation R.be updated from the relation R.

If there are more than one value-If there are more than one value-assignment, we should separate them assignment, we should separate them with comma.with comma.

123123

Page 124: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.5.3 Updates6.5.3 Updates

Example 6.39Example 6.39

Attach the title 'Pres.' in front of the name Attach the title 'Pres.' in front of the name of every movie executive who is the of every movie executive who is the president of a studio.president of a studio.

UPDATE MovieExecUPDATE MovieExec

SET name = 'Pres.' || nameSET name = 'Pres.' || name

WHERE cert# IN (SELECT presC# FROM Studio);WHERE cert# IN (SELECT presC# FROM Studio);

124124

Page 125: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.5.4 Exercises for Section 6.56.5.4 Exercises for Section 6.5

125125

Page 126: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

TRANSACTIONS IN SQLTRANSACTIONS IN SQLSection 6.6Section 6.6

126126

Page 127: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6 Transactions in SQL6.6 Transactions in SQL

6.6.1 Serializability6.6.1 Serializability

6.6.2 Atomicity 6.6.2 Atomicity

6.6.3 Transactions6.6.3 Transactions

6.6.4 Read-Only Transactions6.6.4 Read-Only Transactions

6.6.5 Dirty Reads6.6.5 Dirty Reads

6.6.6 Other Isolation Levels6.6.6 Other Isolation Levels

6.6.7 Exercises for Section 6.66.6.7 Exercises for Section 6.6

127127

Page 128: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6 Transactions in SQL6.6 Transactions in SQL

Up to this point, we assumed that:Up to this point, we assumed that: the SQL operations are done by the SQL operations are done by one user.one user. The operations are done one at a time.The operations are done one at a time. There is no hardware/software failure in There is no hardware/software failure in

middle of a database modification. Therefore, middle of a database modification. Therefore, the operations are done the operations are done atomicallyatomically..

In Real life, situations are totally In Real life, situations are totally different.different.

There are millions of users using the There are millions of users using the same database and it is possible to have same database and it is possible to have some concurrent operations on one tuple.some concurrent operations on one tuple.

128128

Page 129: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.1 Serializability6.6.1 Serializability

In applications like web services, In applications like web services, banking, or airline reservations, banking, or airline reservations, hundreds to thousands operations per hundreds to thousands operations per second are done on one database.second are done on one database.

It's quite possible to have two or more It's quite possible to have two or more operations affecting the same, let's say, operations affecting the same, let's say, one bank account.one bank account.

If these operations overlap in time, then If these operations overlap in time, then they may act in a strange way.they may act in a strange way.

Let's take an example.Let's take an example.

129129

Page 130: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.1 Serializability (cont'd)6.6.1 Serializability (cont'd)

Example 6.40Example 6.40

Consider an airline reservation web Consider an airline reservation web application. Users can book their desired application. Users can book their desired seat by themselves. seat by themselves.

The application is using the following The application is using the following schema:schema:

Flights(fltNo, fltDae, seatNo, seatStatus)Flights(fltNo, fltDae, seatNo, seatStatus)

When a user requests the available seats When a user requests the available seats for the flight no 123 on date 2011-12-15, for the flight no 123 on date 2011-12-15, the following query is issued:the following query is issued:

130130

Page 131: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.1 Serializability (cont'd)6.6.1 Serializability (cont'd)

SELECT seatNoSELECT seatNo

FROM FlightsFROM Flights

WHERE fltNo = 123 ANDWHERE fltNo = 123 AND

fltDate = DATE '2011-12-25' ANDfltDate = DATE '2011-12-25' AND

seatStatus = 'available';seatStatus = 'available';

When the customer clicks on the seat# 22A, When the customer clicks on the seat# 22A, the seat status is changed by the following the seat status is changed by the following SQL:SQL:

UPDATE FlightsUPDATE Flights

SET seatStatus = 'occupied'SET seatStatus = 'occupied'

WHERE fltNo = 123 ANDWHERE fltNo = 123 AND

fltDate = DATE '2011-12-25' ANDfltDate = DATE '2011-12-25' AND

seatNo = '22A';seatNo = '22A';131131

Page 132: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.1 Serializability (cont'd)6.6.1 Serializability (cont'd)

What would happen if two users at the What would happen if two users at the same time click on the reserve button for same time click on the reserve button for the same seat#?the same seat#?

Both see the same seats available and Both see the same seats available and both reserve the same seat. both reserve the same seat.

To prevent these happen, SQL has some To prevent these happen, SQL has some solutions.solutions.

We group a set of operations that need We group a set of operations that need to be performed together. This is called to be performed together. This is called 'transaction''transaction'..

132132

Page 133: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.1 Serializability (cont'd)6.6.1 Serializability (cont'd)

For example, the query and the update in For example, the query and the update in example 6.40 can be grouped in a example 6.40 can be grouped in a transaction.transaction.

SQL allows the programmer to stat that a SQL allows the programmer to stat that a certain transaction must be certain transaction must be serializableserializable with respect to other transactions.with respect to other transactions.

That is, these transactions must behave That is, these transactions must behave as if they were run serially, as if they were run serially, one at a time one at a time with no overlapwith no overlap..

133133

Page 134: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.2 Atomicity 6.6.2 Atomicity

What would happen if a transaction What would happen if a transaction consisting of two operations is in consisting of two operations is in progress and after the first operation is progress and after the first operation is done, the database and/or network done, the database and/or network crashes?crashes?

Let's take an example.Let's take an example.

134134

Page 135: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.2 Atomicity (cont'd) 6.6.2 Atomicity (cont'd)

Example 6.41Example 6.41

Consider a bank's account records system Consider a bank's account records system with the following relation:with the following relation:

Accounts(acctNo, balance)Accounts(acctNo, balance)

Let's suppose that $100 is going to transfer Let's suppose that $100 is going to transfer from acctNo 123 to acctNo 456.from acctNo 123 to acctNo 456.

To do this, the following two steps should To do this, the following two steps should be done:be done:

1.1. Add $100 to account# 456Add $100 to account# 456

2.2. Subtract $100 from account# 123.Subtract $100 from account# 123.

135135

Page 136: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.2 Atomicity (cont'd) 6.6.2 Atomicity (cont'd)

The needed SQL statements are as follows:The needed SQL statements are as follows:

UPDATE AccountsUPDATE Accounts

SET balance = balance + 100SET balance = balance + 100

WHERE acctNo = 456;WHERE acctNo = 456;

UPDATE AccountsUPDATE Accounts

SET balance = balance - 100SET balance = balance - 100

WHERE acctNo = 123;WHERE acctNo = 123;

What would happen if right after the first What would happen if right after the first operation, the database crashes?operation, the database crashes?

136136

Page 137: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.2 Atomicity (cont'd) 6.6.2 Atomicity (cont'd)

The problem addressed by example 6.41 The problem addressed by example 6.41 is that certain combinations of is that certain combinations of operations need to be done operations need to be done atomicallyatomically..

That is, That is, either they are both done or either they are both done or neither is doneneither is done..

137137

Page 138: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.3 Transactions6.6.3 Transactions

The solution to the problems of The solution to the problems of serialization and atomicity is serialization and atomicity is to group to group database operations into transactionsdatabase operations into transactions..

A transaction is a set of one or more A transaction is a set of one or more operations on the database that must be operations on the database that must be executed executed atomicallyatomically and in a and in a serializable serializable manner.manner.

To create a transation, we use the To create a transation, we use the following SQL command: following SQL command:

START TRANSACTIONSTART TRANSACTION

138138

Page 139: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.3 Transactions (cont'd)6.6.3 Transactions (cont'd)

There are two ways to end a transaction:There are two ways to end a transaction:1.1. The SQL receives The SQL receives COMMITCOMMIT command. command.

2.2. The SQL receives The SQL receives ROLLBACKROLLBACK command. command. COMMIT command causes all changes COMMIT command causes all changes

become become permanentpermanent in the database. in the database. ROLLBACK command causes all changes ROLLBACK command causes all changes

undoneundone..

139139

Page 140: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.4 Read-Only Transactions6.6.4 Read-Only Transactions

We saw that when a transaction read a We saw that when a transaction read a data and then want to write something, data and then want to write something, is prone to serialization problems. is prone to serialization problems.

When a transaction only reads data and When a transaction only reads data and does not write data, we have more does not write data, we have more freedom to let the transaction execute in freedom to let the transaction execute in parallel with other transactions.parallel with other transactions.

We call these transactions We call these transactions read-onlyread-only..

140140

Page 141: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.4 Read-Only Transactions 6.6.4 Read-Only Transactions (cont'd)(cont'd)

Example 6.43Example 6.43

Suppose we want to read data from the Suppose we want to read data from the Flights relation of example 6.40 to Flights relation of example 6.40 to determine whether a certain seat was determine whether a certain seat was available?available?

What's the worst thing that can happen?What's the worst thing that can happen?

When we query the availability of a certain When we query the availability of a certain seat, that seat was being booked or was seat, that seat was being booked or was being released by the execution of some being released by the execution of some other program. Then we get the wrong other program. Then we get the wrong answer.answer.

141141

Page 142: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.4 Read-Only Transactions 6.6.4 Read-Only Transactions (cont'd)(cont'd)

If we tell the SQL that our current If we tell the SQL that our current transaction is transaction is read-onlyread-only, then SQL allows , then SQL allows our transaction be executed with other our transaction be executed with other read-only transactions in parallel.read-only transactions in parallel.

The syntax of SQL command for read-only The syntax of SQL command for read-only setting:setting:

SET TRANSACTION READ ONLY;SET TRANSACTION READ ONLY; We put this statement before our read-We put this statement before our read-

only transaction.only transaction.

142142

Page 143: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.4 Read-Only Transactions 6.6.4 Read-Only Transactions (cont'd)(cont'd)

The syntax of SQL command for read-The syntax of SQL command for read-write setting:write setting:

SET TRANSACTION READ WRITE;SET TRANSACTION READ WRITE; We put this statement before our read-We put this statement before our read-

write transaction.write transaction. This option is the default.This option is the default.

143143

Page 144: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.5 Dirty Reads6.6.5 Dirty Reads

The data that is written but not The data that is written but not committed yet is called committed yet is called dirty datadirty data..

A A dirty read dirty read is a read of dirty data is a read of dirty data written by another transaction.written by another transaction.

The The riskrisk in reading dirty data is that the in reading dirty data is that the transaction that wrote it never commit it.transaction that wrote it never commit it.

144144

Page 145: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.5 Dirty Reads (cont'd)6.6.5 Dirty Reads (cont'd)

Example 6.44Example 6.44

Consider the account transfer of example Consider the account transfer of example 6.41.6.41.

Here are the steps:Here are the steps:1.1. Add money to account 2.Add money to account 2.

2.2. Test if account 1 has enough money?Test if account 1 has enough money?a.a. If there is not enough money, remove the If there is not enough money, remove the

money from account 2 and end.money from account 2 and end.

b.b. If there is, subtract the money from account 1 If there is, subtract the money from account 1 and end.and end.

Imagine, there are 3 accounts A1, A2, and Imagine, there are 3 accounts A1, A2, and A3 with $100, $200, and $300. A3 with $100, $200, and $300.

145145

Page 146: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.5 Dirty Reads (cont'd)6.6.5 Dirty Reads (cont'd)

Example 6.44 (cont'd)Example 6.44 (cont'd)

Let's suppose:Let's suppose:

Transaction T1 transfers $150 from A1 to Transaction T1 transfers $150 from A1 to A2A2

Transaction T2 transfers $250 from A2 to Transaction T2 transfers $250 from A2 to A3A3

What would happen if the dirty read is What would happen if the dirty read is allowed?allowed?

146146

Page 147: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.5 Dirty Reads (cont'd)6.6.5 Dirty Reads (cont'd)

The syntax of SQL command for dirty-The syntax of SQL command for dirty-read setting:read setting:

SET TRANSACTION READ WRITESET TRANSACTION READ WRITE

ISOLATION LEVEL READ UNCOMMITTED;ISOLATION LEVEL READ UNCOMMITTED; We put this statement before our read-We put this statement before our read-

write transaction.write transaction. This option is the default.This option is the default.

147147

Page 148: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.6 Other Isolation Levels6.6.6 Other Isolation Levels

There are four isolation level. There are four isolation level. We have seen the first two before.We have seen the first two before.

Serializable (default)Serializable (default) Read-uncommittedRead-uncommitted Read-committedRead-committed

Syntax:Syntax:SET TRANSACTIONSET TRANSACTIONISOLATION LEVEL READ COMMITTED;ISOLATION LEVEL READ COMMITTED;

Repeatable-readRepeatable-readSyntaxSyntaxSET TRANSACTIONSET TRANSACTIONISOLATION LEVEL READ COMMITTED;ISOLATION LEVEL READ COMMITTED;

148148

Page 149: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.6 Other Isolation Levels 6.6.6 Other Isolation Levels (cont'd)(cont'd)

For each the default is 'READ WRITE' (except For each the default is 'READ WRITE' (except the isolation READ UNCOMMITTED that the the isolation READ UNCOMMITTED that the default is 'READ ONLY') and if you want default is 'READ ONLY') and if you want 'READ ONLY', you should 'READ ONLY', you should mention it mention it explicitlyexplicitly..

The The defaultdefault isolation level is isolation level is 'SERIALIZABLE'SERIALIZABLE'.'. Note that if a transaction T is acting in Note that if a transaction T is acting in

'SERIALIZABLE' level and the other one is 'SERIALIZABLE' level and the other one is acting in 'READ UNCOMMITTED' level, then acting in 'READ UNCOMMITTED' level, then this transaction can see the dirty data of T. this transaction can see the dirty data of T. It means that each one acts based on their It means that each one acts based on their level.level.

149149

Page 150: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.6 Other Isolation Levels 6.6.6 Other Isolation Levels (cont'd)(cont'd)

Under Under READ COMMITTED isolationREAD COMMITTED isolation, it , it forbids reading the dirty data. forbids reading the dirty data.

But it does not guarantee that if we issue But it does not guarantee that if we issue several queries, we get the same tuples. several queries, we get the same tuples.

That's because there may be some new That's because there may be some new committed tuples by other transactions.committed tuples by other transactions.

150150

Page 151: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.6 Other Isolation Levels 6.6.6 Other Isolation Levels (cont'd)(cont'd)

Example 6.46Example 6.46

Let's consider the seat choosing problem Let's consider the seat choosing problem under 'READ COMMITTED' isolation.under 'READ COMMITTED' isolation.

Your query won't see seat as available if Your query won't see seat as available if another transaction reserved it but not another transaction reserved it but not committed yet.committed yet.

You may see different set of seats in You may see different set of seats in subsequent queries depends on if the subsequent queries depends on if the other transactions commit their other transactions commit their reservations or rollback them.reservations or rollback them.

151151

Page 152: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.6 Other Isolation Levels 6.6.6 Other Isolation Levels (cont'd)(cont'd)

Under Under REPEATABLE READ isolationREPEATABLE READ isolation, if a , if a tuple is retrieved for the first time, then tuple is retrieved for the first time, then we are sure that the same tuple will be we are sure that the same tuple will be retrieve if the query is repeated.retrieve if the query is repeated.

But the query may show more tuples But the query may show more tuples because of the phantom tuples.because of the phantom tuples.

A A phantomphantom tuple is a tuple that is tuple is a tuple that is inserted by other transactions.inserted by other transactions.

152152

Page 153: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.6 Other Isolation Levels 6.6.6 Other Isolation Levels (cont'd)(cont'd)

Example 6.47Example 6.47

Let's continue the seat choosing problem Let's continue the seat choosing problem under 'REPEATABLE READ' isolation.under 'REPEATABLE READ' isolation.

If a seat is available on the first query, then If a seat is available on the first query, then it will remain available at the subsequent it will remain available at the subsequent queries. Now suppose that some new queries. Now suppose that some new tuples are inserted into the flight relation tuples are inserted into the flight relation (phantom tuples) for that particular flight (phantom tuples) for that particular flight for any reason. Then the subsequent for any reason. Then the subsequent queries retrieve the new tuples as well.queries retrieve the new tuples as well.

153153

Page 154: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.6 Other Isolation Levels 6.6.6 Other Isolation Levels (cont'd)(cont'd)

Properties of SQL isolation levelsProperties of SQL isolation levels

154154

Isolation Level

Dirty Read

Non-repeatabl

e ReadPhantom

Read Read UncommitteUncommitte

dd

Read Read CommittedCommitted -- Repeatable Repeatable

ReadRead -- -- SerializableSerializable -- -- --

Page 155: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.6.7 Exercises for Section 6.66.6.7 Exercises for Section 6.6

155155

Page 156: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.7 Summary of Chapter 66.7 Summary of Chapter 6

156156

Page 157: Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1

Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011

6.8 References for Chapter 66.8 References for Chapter 6

157157