Download presentation
Presentation is loading. Please wait.
1
Chap 5. The DB Language (SQL)
2
Contents Simple Queries in SQL
Queries Involving More Than One Relation Subqueries Full-Relation Operations Database Modifications
3
Example Schema Movie Database Schema
Movies(title, year, length, genre, studioName, producerC#) MovieStar(name, address, gender, birthdate) StarsIn(movieTitle, movieYear, starName) MovieExec(name, address, cert#, netWorth) Studio(name, address, presC#)
4
Simple Queries in SQL Basic form of query in SQL
retrieve tuples of one relation that satisfy a condition basic keywords: SELECT, FROM, WHERE FROM clause: relation or relations to which the query refers WHERE clause: condition that tuples must satisfy SELECT clause: attributes of the tuples as part of the answer (ex) Find movies produced by Disney studios in 1990. SELECT * FROM Movies WHERE studioName = ‘Disney’ AND year = 1990; the entire attributes of the tuple
5
Simple Queries in SQL (cont’d)
Projection in SQL project the relation onto some of its attributes (ex) Find the titles and lengths for all movies produced by Disney Studios in 1990. SELECT title, length FROM Movies WHERE studioName = ‘Disney’ AND year = 1990; p title, year (s studioName=‘Disney’ AND year=1990 (Movies)) title length Pretty Woman
6
Simple Queries in SQL (cont’d)
optional Rename column headers SELECT title AS name, length AS duration FROM Movie WHERE studioName = ‘Disney’ AND year = 1990; Pretty Woman name duration
7
Note: Case sensitivity
SQL is case-insensitive about keywords, names of attributes, relations, aliases e.g., in keywords FROM, from, FrOm: all OK upper and lower case letters are only distinct inside the quoted string e.g., ‘FROM’ and ‘from’ are different character strings
8
Simple Queries in SQL (cont’d)
title, length*0.0167lengthInHours (Movies) SELECT list can function like the lists in an extended projection arithmetic expression, constant, string expression, operator, etc (ex) Output with the length in hours and an additional column having constant ‘hrs.’ SELECT title, length* AS length, ‘hrs.’ AS inHours FROM Movies WHERE studioName = ‘Disney’ AND year = 1990; title length inHours Pretty Woman hrs.
9
Note: Reading and Writing SQL
Reading SELECT-FROM-WHERE query FROM clause relation(s) used WHERE clause conditions a tuple satisfy SELECT clause the output form of the query Writing SELECT-FROM-WHERE query its sequence is equal to reading FROM clause, WHERE clause, SELECT clause
10
Simple Queries in SQL (cont’d)
Selection in SQL expression for conditions in the WHERE clause the six common comparison operators: =, <>, <, >, <=, >= arithmetic operators: +, -, *, etc concatenation operator || to strings strings are denoted by single quotes numeric constants boolean values may be combined by the logical operators AND, OR, and NOT comparison expressions
11
Simple Queries in SQL (cont’d)
(Ex) Retrieve the titles of movies made by MGM Studios that either were made after 1970 or were less than 90 minutes long. SELECT title FROM Movies WHERE (year > 1970 OR length < 90) AND studioName = ‘MGM’
12
Note: SQL Queries and Relational Algebra
Relational algebra for basic SQL queries SELECT L FROM R Þ p L (s C (R)) WHERE C (Ex) Previous example SELECT title, length FROM Movies WHERE studioName = ‘Disney’ AND year = 1990; p title, year (s studioName=‘Disney’ AND year=1990 (Movies)) p title, length s studioName = ‘Disney’ AND year = 1990 Movies
13
Simple Queries in SQL (cont’d)
Comparisons of Strings string comparison is based on the lexicographic order (ex) ‘at’ < ‘bar’ , ‘fodder’ < ‘foo’ Bit string represented by B followed by a quoted string of 0’s and 1’s. (ex) B’011’ is the string of three bits, 011 hexadecimal notation: X instead of B (ex) X’7ff’
14
Simple Queries in SQL (cont’d)
Pattern Matching s LIKE p string comparison based on a simple pattern match s is a string type attribute, and p is a pattern pattern: a string with the optional use of special characters % and _ % : any sequence of 0 or more characters _ : any one character
15
Simple Queries in SQL (cont’d)
(ex) Retrieve titles of movies whose names start with “Star “ and are nine characters long. SELECT title FROM Movies WHERE title LIKE ‘Star _ _ _ _’ /* Result: {Star Wars, Star Trek, } */ (ex) Retrieve all movies with possessive (‘s) in their titles SELECT title WHERE title LIKE ‘%’’s%’ /* Result : {Rogan’s Run, } */ two consecutive apostrophes represent a single apostrophe
16
Simple Queries in SQL (cont’d)
Escape characters in LIKE expression What if does LIKE expression involve % or _ ? keyword ESCAPE and the chosen escape character, in quotes (ex) s LIKE ‘x%%x%’ ESCAPE ‘x’ /* any string that begins and ends with % */
17
Simple Queries in SQL (cont’d)
Dates and Times are generally supported as special data types date type: DATE (ex) date constant: DATE ‘ ’ time type: TIME (ex) time constant : TIME ‘15:00:02.5’ date+time type: TIMESTAMP (ex) date+time constant : TIMESTAMP ‘ :00:00’ dates or times can be compared by using the same comparison operators
18
Simple Queries in SQL (cont’d)
Null Values used when the value is not defined value unknown there is some value, but we do not know e.g., the unknown birthday of a movie star value inapplicable there is no value that makes sense here e.g., the spouse of an unmarried movie star value withheld we are not entitled to know the value e.g., an unlisted phone number Two NULL values are not the same
19
Simple Queries in SQL (cont’d)
Operations on NULL values NULL in arithmetic operators like ×, + the result is NULL (ex) if the value of x is NULL, x + 3 is NULL NULL in comparison operators like =, > a result is UNKNOWN (ex) if the value of x is NULL, x > 3 is UNKNOWN Predicates related with NULL: IS NULL, IS NOT NULL (ex) Check whether the value of x is NULL. x IS NULL Predicate: operator producing a boolean result the result is TRUE or FALSE
20
Simple Queries in SQL (cont’d)
NULL is not a constant cannot use NULL explicitly as an operand (ex) NULL + 3, NULL = 5 : not correct SQL NULL in some commercial DBMSs can be used as if it were a constant (ex) NULL + 3 : the result is NULL (ex) INSERT INTO R(A1, A2) VALUES(NULL, NULL) a tuple where the value of each attribute is null is inserted e.g., Oracle (NULL, NULL)
21
Simple Queries in SQL (cont’d)
Truth table for three-valued logic X y x AND y x OR y NOT x TRUE UNKNOWN UNKNOWN TRUE FALSE FALSE UNKNOWN FALSE UNKNOWN TRUE UNKNOWN TRUE UNKNOWN TRUE UNKNOWN UNKNOWN FALSE FALSE UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN
22
Note: Pitfalls regarding NULLs
Query’s result including tuples with NULL values include only tuples for which the condition has the value TRUE (ex) Consider the following query: SELECT * FROM Movies WHERE length <= 120 OR length > 120 a tuple with a NULL in length attribute WHERE clause evaluates to UNKNOWN such a tuple is not returned as part of the answer “Find all movies tuples with non-NULL lengths”
23
Simple Queries in SQL (cont’d)
Ordering the Output the order may be based on the value of any attribute output in sorted order ORDER BY <list of attribute> the default order: ASC (ascending) output highest first: DESC (descending)
24
Simple Queries in SQL (cont’d)
(Ex) Retrieve the movies produced by Disney in Order the output by length (shortest first), and among movies of equal length, alphabetically. SELECT * FORM Movies WHERE studioName = ‘Disney’ AND year = 1990 ORDER BY length, title;
25
Queries Involving More Than One Relation
Cartesian Products and Joins in SQL simple way to couple relations in one query list relations in the FROM clause (ex) Movies (title, year, length, genre, studioName, producerC#), MovieExec (name, address, cert#, netWorth) Find the name and address of the producer of Star Wars. SELECT name, address FROM Movies, MovieExec WHERE title = ‘Star Wars’ AND producerC# = cert# Theta Join: sC (R S) Two relations in FROM clause: Cartesian product
26
Queries Involving More Than One Relation (cont’d)
Disambiguating Attributes a query involving several relations may have two or more attributes with the same name. R.A : attribute A of relation R (ex) Find pairs of names for a star and an executive who have the same address. SELECT MovieStar.name, MovieExec.name FROM MovieStar, MovieExec WHERE MovieStar.address = MovieExec.address R.A notation can also be used in situations where no ambiguity
27
Queries Involving More Than One Relation (cont’d)
Tuple Variables a query may involve two or more tuples in the same relation use tuple variable that is an alias for the relation (ex) Retrieve names of two stars who have the same address. SELECT Star1.name, Star2.name FROM MovieStar AS Star1, MovieStar AS Star2 WHERE Star1.address = Star2.address AND Star1.name < Star2.name Star1.name < Star2.name avoid the case that each star name is paired with itself produce each pair of stars with a common address only once
28
Note: Join & Tuple Variables
Not in the textbook Star1, Star2: tuple variables MovieStar * address MovieExec * address MovieStar * address Star1 Star2 Select MovieStar.name, MovieExec.name From MovieStar, MovieExec Where MovieStar.address = MovieExec.address Select Star1.name, Star2.name From MovieStar AS Star1, MovieStar AS Star2 Where Star1.address = Star2.address
29
Queries Involving More Than One Relation (cont’d)
Conversion to Relational Algebra Cartesian product of all relations in the FROM clause Selection using the condition in the WHERE clause Projection using the attributes in the SELECT clause SELECT name, address FROM Movies, MovieExec WHERE title = ‘Star Wars’ AND producerC# = cert# p name, address (s title = ‘Star Wars’ AND producerC# = cert# (Movies × MovieExec))
30
Queries Involving More Than One Relation (cont’d)
MovieStar(name, address, gender, birthdate) MovieExec(name, address, cert#, netWorth) Union, Intersection, and Difference keywords: UNION, INTERSECT, EXCEPT (ex) Retrieve the names and addresses of all female movie stars who are also movie executives with a net worth over 10,000,000. (SELECT name, address FROM MovieStar WHERE gender = ‘F’ ) INTERSECT FROM MovieExec WHERE netWorth > ); Select name, address From MovieStar, MovieExec Where MovieStar.name = MovieExec.name AND MovieStar.address = MovieExec.address AND gender = ‘F’ AND netWorth >
31
Subqueries Subqueries a subquery returns a relation
a query that is part of the entire query (ex) two subqueries in UNION, INTERSECT, EXCEPT a subquery returns a relation can appear in FROM clauses can be used in various ways in WHERE clauses
32
Subqueries (cont’d) (Ex) Retrieve the titles and years of movies whose producers are presidents of studios located in Los Angeles. Movies (title, year, length, genre, studioName, producerC#) Studio (name, address, presC#) SELECT title, year FROM Movies WHERE producerC# IN (SELECT presC# FROM Studio WHERE address LIKE ‘%Los Angeles%’) SELECT title, year FROM Movies, Studio WHERE producerC# = presC# AND address LIKE ‘%Los Angeles’ For each tuple in Movies, check if it satisfies the condition in the WHERE clause
33
Subqueries (cont’d) Subqueries that produce scalar values
when the result of a subquery is a single value from a single attribute, this subquery can be used as if it were a constant (Ex) Movies(title, year, length, genre, studioName, producerC#) MovieExec(name, address, cert#, netWorth) Find the producer’s name of Star Wars. SELECT name FROM MovieExec WHERE cert# = (SELECT producerC# FROM Movies WHERE title = ‘Star Wars’);
34
Subqueries (cont’d) Conditions involving relations
a relation can be used in conditions, but it must be expressed as a subquery Predicates that involve one-column relation R: relation, s: scalar value EXISTS R true iff R is not empty s IN R true iff s is equal to one of the values in R Select name From MovieExec Where EXISTS (Select producerC# From Movies Where producerC# = cert# AND title = ‘Star Wars’ )
35
Subqueries (cont’d) s > ALL R s > ANY R
(Ex) Find the titles of movies made by Disney whose length is greater than the length of every movie made by MGM. s > ALL R true iff s is greater than every value in unary relation R s > ANY R true iff s is greater than at least one value in unary relation R EXIST and IN are predicates while ALL and ANY(or SOME) are quantifiers “>” could be replaced by any of comparison operators may need the difference operator and the join operator in relational algebra expressions the join operator may work in relational algebra expressions
36
Subqueries (cont’d) Conditions Involving Tuples a tuple in SQL
represented by a parenthesized list of scalar values (ex) (123, ‘foo’), (name, address, networth) if a tuple t has the same number of components as a relation R, t and R can be compared t IN R t <> ANY R /* There is some tuple in R other than t */
37
Subqueries (cont’d) (Ex) Movies(title, year, length, genre, studioName, producerC#) StarsIn(movieTitle, movieYear, starName) MovieExec(name, address, cert#, netWorth) Find the names of producers of movies where Harrison Ford stars. SELECT name FROM MovieExec WHERE cert# IN (SELECT producerC# FROM Movies WHERE (title, year) IN (SELECT movieTitle, movieYear FROM StarsIn WHERE starName = ‘Harrison Ford’)); No duplicates
38
Subqueries (cont’d) (example – cont’d)
A nested query can be written as a single Select-From-Where SELECT name FROM MovieExec, Movies, StarsIn WHERE cert# = producerC# AND title = movieTitle AND year = movieYear AND starName = ‘Harrison Ford’; FROM clause: relations mentioned in the main query or subqueries WHERE clause: IN are replaced by equi-join duplicates may occur
39
Subqueries (cont’d) (ex – cont’d) duplicates StarsIn Movies MovieExec
Not in the textbook (ex – cont’d) duplicates <starName> <title> <producer-name> Harrison Ford Star Wars Steven Spielberg Harrison Ford Indiana Jones Steven Spielberg (H.F., t1, y1) (t1, y1, c1) (c1, Steven Spielberg) (H.F., t2, y2) (t2, y2, c1) StarsIn Movies MovieExec
40
Subqueries (cont’d) Correlated Subquery
A subquery needs to be evaluated for each value of the tuple variable outside the subquery. Correlated Subquery a tuple variable declared outside the subquery is used inside the subquery (ex) Find the titles that have been used for two or more movies. SELECT title FROM Movies AS Old WHERE year < ANY (SELECT year FROM Movies WHERE title = Old.title) The cost for processing a correlated subquery can be very expensive Movies with the same title made in different years King Kong King Kong King Kong title year Select title From Movies M1, Movies M2 Where M1.title = M2.title AND M1.year < M2.year
41
Subqueries (cont’d) Subqueries in From Clauses
a parenthesized subquery can be used in the FROM clause we must give this subquery a tuple-variable alias (ex) Find the names of producers of Harrison ford’s movies. SELECT Name FROM MovieExec,(SELECT producerC# FROM Movies, StarsIn WHERE title = movieTitle AND year = movieYear AND starname = ‘Harrison Ford’) Prod WHERE cert# = Prod.producerC#;
42
Subqueries (cont’d) SQL Join Expressions
, : relations SQL Join Expressions the join expression can be used as a query by itself, or can be used as a subquery in the FROM clause CROSS JOIN /* cartesian product */ Movies CROSS JOIN StarsIn JOIN ON <condition> /* theta join */ Movies JOIN StarsIn ON title = movieTitle AND year = movieYear; NATURAL JOIN MovieStar NATURAL JOIN MovieExec SELECT * FROM Movies, StarsIn
43
Subqueries (cont’d) Join expression in the FROM clause
SELECT title, year, starName FROM Movies JOIN StarsIn ON title = movieTitle AND year = movieYear;
44
Subqueries (cont’d) Outerjoin
add to the output dangling tuples, padded with Null values dangling tuples: tuples fail to be joined Natural Outerjoin NATURAL [LEFT | RIGHT | FULL] OUTER JOIN Theta-Outerjoin : use “JOIN ON...” syntax [LEFT | RIGHT | FULL] OUTER JOIN ON ...
45
Subqueries (cont’d) (Ex) MovieStar(name, address, gender, birthdate)
MovieExec(name, address, cert#, netWorth) MovieStar NATURAL FULL OUTER JOIN MovieExec; name Mary T. Moore Tom Hanks Carrie Fisher George Lucas address Maple St. Cherry Ln. Locust Ln. Oak Rd. gender ‘F’ ‘M’ NULL birthdate 9/9/77 8/8/88 5/5/55 cert# 12345 23456 networth $100… $200… A star, but not an executive(e.g., Tom Hanks), or An executive, but not a star(e.g., George Lucas) dangling tuples, but are added to the output
46
Subqueries (cont’d) (ex) Movies NATURAL LEFT OUTER JOIN MovieExec;
the first two tuples are in the output, but the 3rd tuple is not (ex) Movies NATURAL RIGHT OUTER JOIN MovieExec; the first and 3rd tuples are in the output, but the 2nd tuple is not (ex) Movies FULL OUTER JOIN StarsIN ON title = movieTitle AND year = movieYear (ex) Movies LEFT OUTER JOIN StarsIN ON
47
Note: Predicates in Boolean Expressions
Basic comparison predicates: =, <>, <, >, <=, >= LIKE predicate (ex) title LIKE ‘%love%’ IN predicate IS NULL, IS TRUE, IS FALSE, IS UNKNOWN unary predicates (ex) studioName IS NULL, cost = 100 IS TRUE EXISTS and UNIQUE unary predicates EXISTS subquery, UNIQUE subquery ANY(or SOME) and ALL quantifiers used with comparison predicates “true” if the subquery contains no duplicates
48
Full-Relation Operations
Operations that act on relations as a whole, rather than on tuples individually Eliminating duplicates relations in SQL are bags, rather than sets the SQL system does not ordinarily eliminate duplicates keyword DISTINCT (ex) SELECT DISTINCT genre, studioName From Movies produce only one copy of any tuple The cost of duplicate elimination is expensive thus, duplicate elimination should be used judiciously
49
Full-Relation Operations (cont’d)
Duplicates in UNION, INTERSECT and EXCEPT union, intersection, and difference operations normally eliminate duplicates prevent duplicate elimination: keyword ALL R UNION ALL S (SELECT title, year FROM Movie) UNION ALL (SELECT movieTitle AS title, movieYear AS year FROM StarsIn); R INTERSECT ALL S R EXCEPT ALL S the result is a set the result is a bag
50
Full-Relation Operations (cont’d)
Aggregation operators are used to a scalar valued expression, typically a column name. COUNT(*) is an exception, which counts all the tuples Aggregation Operators five aggregation operators COUNT: the number of values SUM: the sum of the values in this column AVG: the average of the values in this column MIN: the least value in this column MAX: the greatest value in this column Duplicates elimination e.g., COUNT(DISTINCT x): eliminate duplicates from the column x before applying the aggregation operator
51
Full-Relation Operations (cont’d)
(ex) SELECT COUNT(*) FROM StarsIn; (ex) SELECT COUNT(starName) (ex) SELECT COUNT(DISTINCT starName) (ex) SELECT AVG(netWorth) FROM MovieExec; “*”: denotes an entire tuple Counts the number of values in the starName column. Almost same as COUNT(*) Do not count duplicate values more than once COUNT(starName) - does not count NULL, i.e., count rows excluding the null values
52
Full-Relation Operations (cont’d)
Grouping: GROUP BY clause grouping attributes follows GROUP BY tuples are grouped according to their values in the grouping attributes SELECT studioName, SUM(length) FROM Movies GROUP BY studioName; In SELECT clauses, only grouping attributes may appear in unaggregated form The total length of films produced by each studio aggregated value studioName, sumLength studioName, SUM(length)sumLength Movies grouping attributes SELECT studioName, genre, SUM(length) FROM Movies GROUP BY studioName make sense ???
53
Full-Relation Operations (cont’d)
Grouping, Aggregation, and Nulls there are a few rules when tuples have nulls NULL is ignored in any aggregation COUNT(A): number of tuples with non-NULL values for attribute A NULL can form a group there can be a group where the value of the grouping attribute is NULL empty bag the result is NULL for any aggregation, except COUNT COUNT(*): a number of tuples in a relation Let R be a relation with no tuple. Select COUNT(*) From R The result is 0.
54
Full-Relation Operations (cont’d)
(Ex) Suppose we have a relation R(A, B). R has one tuple (NULL, NULL) SELECT A, COUNT(B) FROM R GROUP A; SELECT A, SUM(B) Insert Into R Values (NULL, NULL) The result of COUNT(*) is 1 (NULL, 0) may or may not be displayed in the output (NULL, NULL)
55
Full-Relation Operations (cont’d)
Condition on the groups: HAVING clause express a condition on aggregated properties of the group (ex) Find the producer names and the total film lengths for only those producers who made at least one film prior to 1930. SELECT name, SUM(length) FROM MovieExec, Movie WHERE producerC# = cert# GROUP BY name HAVING MIN(year) < 1930; As in SELECT clause, only grouping attributes may appear unaggregated in the HAVING clause. (ex) name LIKE ‘John%’ : OK genre = ‘Drama’ : No condition about a group
56
Full-Relation Operations (cont’d)
Order of clauses in SQL SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY WHERE clause: specifies the condition about tuples HAVING clause: specifies the condition about groups Evaluation sequence tuples satisfying conditions in the WHERE clause are selected these tuples are grouped by grouping attributes in the GROUP BY clause groups satisfying conditions in the HAVING clause are selected
57
Database Modifications
Database modification statements INSERT, DELETE, UPDATE Insertion INSERT INTO R(A1, … , An) VALUES (v1, … , vn); the keywords INSERT INTO, the name of a relation R, a parenthesized list of attribute names the keyword VALUES, and a tuple expression (Ex) INSERT INTO StarsIn(movieTitle, movieYear, starName) VALUES(‘The Maltese Falcon’, 1942,‘Sydney Greenstreet’);
58
Database Modifications (cont’d)
may omit the list of attributes (ex) INSERT INTO StarsIn VALUES(‘The Maltese Falcon’,1942,‘Sydney Greenstreet’); (ex) Add to relation Studio all movie studios that are mentioned in Movies, but do not appear in Studio. INSERT INTO Studio(name) SELECT DISTINCT studioName FROM Movies WHERE studioName NOT IN(SELECT name FROM Studio); address and presC# attributes in the inserted Studio tuples : NULL values are used
59
Database Modifications (cont’d)
Deletion DELETE FROM R WHERE <condition>; the keywords DELETE FROM the name of a relation R, the keyword WHERE, and a condition (ex) DELETE FROM StarsIn WHERE movieTitle = ‘The Maltese Falcon’ AND movieYear = 1942 AND starName = ‘Sydney Greenstreet’;
60
Database Modifications (cont’d)
Update UPDATE R SET <new-value assignments> WHERE <condition>; the keyword UPDATE, a relation name R the keyword SET, a list of assignments the keyword WHERE, and a condition (ex) Attach the title Pres. in front of the name of every movie executive who is the president of a studio. UPDATE MovieExec SET name = ‘Pres. ‘ || name WHERE cert# IN (SELECT presC# FROM Studio); If more than one assignments, they are separated by commas the condition for identifying tuples to be updated requires information in other tables column1 = value1, column2 = value2,
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.