Download presentation
Presentation is loading. Please wait.
Published byHerbert Jenkins Modified over 8 years ago
1
1 SQL: Concept and Usage
2
2 SQL: an Overview SQL (Structured Query Language) –Also be pronounced as “sequel” –A relational database language –Consists of a set of facilities for defining, accessing, and managing relational databases, i.e. DDL + DML + DCL + etc. –The de facto and de jure standard in the relational database world SQL is based on the notion of relational tuple calculus SQL relation is a bag, not a set SQL is case insensitive –Only inside quotes does SQL make a distinction bet. upper- and lower- case letters
3
3 A Brief History on SQL The relational model E. F. Codd (1970) “Structured English Query Language” (SEQUEL) by D. Chamberlin et. al. at IBM (1974) SEQUEL/2 (subsequently SQL) defined and implemented in System R (1966-67) SQL-based products (early 1980s) –Oracle, IBM SQL/DS (1982), IBM DB2 (1983) nowadays, numerous SQL-based products Official standardization movement –ISO (International Standard Organization) ISO/IEC JTC1 SC21 WG3 –ANSI (American National Standards Institute) –the SQL Access Group –X/OPEN –ODMG
4
4 SQL Standardizations ANSI’s proposal for standard SQL (1986) ISO accepts ANSI SQL as standard (1987) –informally SQL/86 ISO extends SQL/86 to include integrity enhancement feature (1989) –informally SQL/89 (or SQL1) International Standard ISO/IEC 9075: 1992 Database Language SQL –ISO revises and greatly expands SQL/89 –informally SQL/92 (or SQL2) ISO is working and producing documents for SQL3 (1998-2000)
5
5 Sample Schema –Movie(title, year, length, inColor, studioName, producerC#) –StarsIn(movieTitle, movieYear, starName) –MovieStar(name, address, gender, birthdate) –MovieExec(name, address, cert#, netWorth) –Studio(name, address, presC#)
6
6 SELECT Statements To retrieve tuples with some condition, use SELECT basic format –SELECT FROM WHERE GROUP BY HAVING ORDER BY
7
7 Simple Queries (1) Retrieve all movies produced by Disney studio in 1990 –select * from Movie where studioName = ‘Disney’ and year = 1990; How to evaluate –Apply each tuple to the condition –Return tuples that make the condition true
8
8 Simple Queries (2) Retrieve titles and lengths of movies produced by Disney studio in 1990 –select title, length from Movie where studioName = ‘Disney’ and year = 1990; To rename displayed attribute –select title as name, length as duration // “as” is optional from Movie where studioName = ‘Disney’ and year = 1990;
9
9 Simple Queries (3) To add new attributes into the output –select title, length * 0.016667 AS length, ‘hrs.’ AS inHours from Movie where studioName = ‘Disney’ AND year = 1990;
10
10 Condition in Where Clause The expressions in WHERE clause –comparison operators: =, <>,, = –arithmetic operators: +, *, and so on –concatenation operator: || to strings Retrieve the titles of the movies made after 1970 that are in black-and- white –select title from Movie where year > 1970 and Not inColor; Retrieve the titles of the movies made by MGM that were either made after 1970 or less than 90 minutes long –select title from Movie where (year > 1970 or length < 90) and studioName = ‘MGM’;
11
11 Comparison of Strings Two strings are equal if they are the same sequence of characters With comparison operator, apply lexicographic order –‘fodder’ < ‘foo’ –‘bar’ < ‘bargain’ pattern matching –s LIKE p, where s is a string and p is a pattern –%: denotes an arbitrary number of characters –_(underline): denotes single arbitrary character Example –select title from Movie where title like ‘Star ____’; –select title from Movie where title like ‘%’’s%’; // to search all movies with a possessive (‘s) in their titles –select title from Movie where title like ‘x%x%’ escape ‘x’; // matches any string that begins and ends with %
12
12 Dates and Times Implementations of SQL generally support dates and times as special data types Date type is represented by the keyword DATE followed by a quoted string of a special form –DATE ‘1948-05-14’ –Note that a one-digit month or day is padded with a leading 0 Time type similarly –TIME ‘15:00:02.5’ –Use the military clock Use comparison operator to dates and times
13
13 ORDER BY Clause SQL allows user to order the tuples in the result of a query by some attribute –ORDER BY To retrieve the movies listed by length (shortest first), and among movies with equal length, alphabetically –select * from Movie where studioName = ‘Disney’ and year = 1990 order by length, title asc ; // ORDER BY 3, 1 ASC (ascending order, by default) –DESC: descending order
14
14 Products and Joins Retrieve the names of the producer of Star Wars –select name from Movie, MovieExec where title = ‘Star Wars’ and producerC# = cert#; Retrieve the movie star name and movie executive name who has the same address –select MovieStar.name, MovieExec.name from MovieStar, MovieExec where MovieStar.address = MovieExec.address; // to disambiguate
15
15 Alias table-name [AS] alias –to declare alternative relation name (alias, correlation) –separated by a blank, not by a comma –query that involves more than one tuple from the same relation Retrieve two star names who share an address –select Star1.name, Star2.name from MovieStar AS Star1, MovieStar AS Star2 where Star1.address = Star2.address AND Star1.name < Star2.name; –what if the second condition is omitted ? –what if the second condition would be expressed with <>
16
16 An Alias Example employee(SSN, name, age, gender, superSSN) For each employee, retrieve the employee’s name and his/her immediate supervisor name –select e.name, s.name from employee e s where e.superSSN = s.SSN;
17
17 Interpreting Multirelation Queries Nested loops LET the tuple variables in the from clause range over relations R1, R2, …, Rn; FOR each tuple t1 in relation R1 DO FOR each tuple t2 in relation R2 DO... FOR each tuple tn in relation Rn DO IF the where clause is satisfied when the values from t1, t2, …, tn are substituted for all attribute references THEN evaluate the attributes of the select clause according to t1, t2,..., tn and produce the tuple of values that results. Parallel Assignment Conversion to relational algebra
18
18 Set Operations SQL directly supports some set operations Set operationSQL stmts intersection ( ) INTERSECT union ( )UNION Set difference (–)EXCEPT Operand tables must union-compatible –Of the same degree –Corresponding columns are of compatible data types Duplicate tuples are eliminated from the result –i.e. returns as a set
19
19 Set Examples 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 (select name, address from MovieExec where netWorth > 10000000); Retrieve the names and addresses of movie stars who are not also movie executives –(select name, address from MovieStar) except (select name, address from MovieExec); Retrieve all the titles and years of movies that appear in either the Movie or StarsIn relation –(select title, year from Movie) union (select movieTitile as title, movieYear as year from StarsIn);
20
20 An Unintuitive Consequence Suppose R, S, and T are unary (one-component) relations, each having attribute A alone Compute R (S T) in SQL –select R.A from R, S, T where R.A = S.A or R.A = T.A; Suppose that T is empty, then R.A = T.A can never satisfied Hence, the result is empty regardless of how many elements R and S have in common
21
21 Subqueries A complete query can be nested within the WHERE-clause Retrieve the producer name of Star Wars –select name from Movie, MovieExec where title = ‘Star Wars’ AND producerC# = cert#; –alternatively –select name from MovieExec where cert# = (select producerC# from Movie where title = ‘Star Wars’); // must produce scalar values
22
22 Conditions Involving Relations SQL operators that produce a Boolean result –EXISTS R is true iff R is not empty –s IN R is true if and only if s is equal to one of the values in R –s > ALL R is true iff s is greater than every value in unary relation R s <> ALL R is the same as s NOT IN R. –s > ANY R is true iff s is greater than at least one value in unary relation R EXISTS, ALL, ANY and IN can be combined with NOT –s NOT IN R is true if and only if s is equal to no value in R –NOT s > ALL R is true iff s is not the maximum value in R –NOT s > ANY R is true iff s is the minimum value in R
23
23 Conditions Involving Tuples A tuple in SQL is represented by a parenthesized list of scalar values Retrieve the producer names of Harrison Ford’s movies –select name from MovieExec where cert# IN (select producerC# from Movie where (title, year) in (select movieTitle, movieYear from StarsIn where starName = ‘Harrison Ford’ ) );
24
24 Correlated Subqueries Sometimes, subquery needs to be evaluated many times ! Find the titles that have been used for two or more movies –select title from Movie as Old where year < any (select year from Movie where title = Old.title); –A movie made twice will be listed once Scoping rules for names
25
25 Eliminating Duplicates Use the keyword DISTINCT to eliminate duplicates Retrieve the producer names of Harrison Ford’s movies using no subqueries –select distinct name from MovieExec, Movie, StarsIn where cert# = producerC# AND title = movieTitle AND year = movieYear AND starName = ‘Harrison Ford’;
26
26 Duplicates with Set Operations To allow duplicates, use keyword ALL –union all –intersect all –except all Retrieve all the titles and years of movies, including duplicates, that appear in either the Movie or StarsIn relation –(select title, year from Movie) union all (select movieTitile as title, movieYear as year from StarsIn);
27
27 Aggregation Aggregation operators in SQL –SUM –AVG –MIN –MAX –COUNT Those are applicable to a set as well as a bag Find the average net worth of all movie executives –select AVG(netWorth) from MovieExec;
28
28 Aggregate examples Compute the number of producer name –select count (name) from MovieExec; Count the number of tuples in the MovieExec relation –select count (*)// duplicates are allowed from MovieExec;// but it does not make difference –select count (name) // the same as above from MovieExec;// presuming “name” is the key –select count (distinct name) // no duplicate at all from MovieExec;
29
29 GROUP BY Clause Apply aggregate functions to subgroups of tuples in a relation based on some attribute values –Aggregate functions are applied to each group independently –Grouping attributes should appear in the SELECT-clause Compute the sum of the lengths of all movies for each studio –select studioName, sum(length) from Movie group by studioName; Retrieve the producer name and the total film length they have made –select name, sum(length) from MovieExec, Movie where producerC# = cert# group by name;
30
30 HAVING Clause To retrieve group that satisfies certain conditions only Retrieve the producer name who made at least one film prior to 1930 and his total film length –select name, sum(length) from MovieExec, Movie where producerC# = cert# group by name having min(year) < 1930; Rule of executions –WHERE-clause is executed first to select tuples –Next, HAVING-clause is applied to select groups
31
31 HAVING Examples Employee(name, salary, dno) Department(dnumber, dname, location) For each department having more than five employees, retrieve the department name and the number of employees making more than $40,000 –select dname, count(*)// wrong query from department, employee where dnumber = dno and salary > 40000 group by dname having count(*) > 5; –select dname, count(*) from department, employee where dnumber = dno and salary > 40000 and dno in (select dno from employee group by dno having count(*) > 5) group by dname;
32
32 Database Modifications Modification operations –Insert tuples into a relation –Delete certain tuples from a relation –Updates values of certain components of certain existing tuples
33
33 Insert Statements Insert form: –INSERT INTO R(A 1,…,A n ) VALUES ( 1,…, n ); attributes not specified are set to default value (e.g. NULL) To add Sydney Greenstreet to the list of stars of The Maltes Falcon –insert into StarsIn (movieTitle, movieYear, starName) values (‘The Maltese Falcon’, 1942, ‘Sydney Greenstreet’); To add to the Studio relation all movie studios that are mentioned in the Movie relation but do not appear in Studio –insert into Studio (name) select distinct studioName from Movie where studioName not in (select name from Studio);
34
34 Delete Statements Delete form: –DELETE FROM R WHERE ; Delete from the Studio, the fact that Sydney Greenstreet was a star in The Maltese Falcon –delete from StarsIn where movieTitle = ‘The Maltese Falcon’ and movieYear = 1942 and starName = ‘Sydney Greenstreet’ ; Delete all movie executives whose net worth is less than ten millons –delete from MovieExec where netWorth < 10000000;
35
35 Update Statements Update form: –UPDATE R SET WHERE ; Modify the relation MovieExec by prepending the title Pres. in front of every movie executive who is the president of a studio –update MovieExec set name = ‘Pres. ’ || name where cert# in (select presC# from Studio);
36
36 Data Types Principle data types 1Character strings of fixed or varying length –CHAR(n) or VARCHAR(n) 2Bit strings of fixed or varying length –BIT(n) or BIT VARYING(n) 3integer –INT or INTEGER –SHORTINT 4floating-point –FLOAT or REAL –DOUBLE PRCISION: a higher precision (double in C) –DECIMAL(n,d): value consists of n decimal digits and d fraction –DECIMAL(6,2): 0123.45 5DATE and TIME
37
37 Table Creation, Deletion, and Modification Create table MovieStar ( name char(30), address varchar(255), gender char(1), birthdate date); –column name must be unique tablewise –The relation might be bulk loaded by translating existing data Drop Table MovieStar; Alter table MovieStar ADD phone char(16); // null values are populated Alter table MovieStar DROP birthdate;
38
38 Default Values Default value is placed in a component if no other value is known Usage DEFAULT ; Examples –gender char(1) default ‘?’, birthdate date default date ‘0000-00-00’, –alter table MovieStar add phone char(16) default ‘unlisted’;
39
39 Domains When we declare the attribute, we may follow its name by the domain name instead of a data type Format –CREATE DOMAIN AS ; Examples –Create domain MovieDomain as varchar(50) default ‘unknown’; –title MovieDomain,// use MovieDomain title varchar(50) default ‘unknown’,// do not use MovieDomain Alter domain MovieDomain SET DEFAULT ‘no such title’; Drop domain MovieDomain; –the effect of above statement will be to make this domain no longer available for declarations of attributes. However, already defined using this domain will continue to have the same type and default as they had before dropping the domain
40
40 Indexes Creation of index is not a part of any SQL standards including SQL2 Typical syntax is –CREATE INDEX ON –Create index YearIndex on Movie(year); –Create index KeyIndex on Movie(title, year);// multiattribute index INDEX deletion –DROP INDEX –drop index YearIndex; Trade-off –Speeds up the queries –On modification, more complex and time-consuming
41
41 Views A view does not necessarily exist in physical form i.e. virtual relation Useful when we need to reference frequently A view is always up to date; –if we modify the tuples in the base tables on which the view is defined, the view automatically reflects these changes Modification on view –very restricted –still a research problem
42
42 View Creation and Deletion Creation –Create view as ; –Create view ParamountMovie as select title, year from Movie where studioName = ‘Paramount’; –Create view MovieProd (movieTitle, prodName) as // rename attribute select title, name from Movie, MovieExec where producerC# = cert#; Deletion –Drop view view-name; –Drop view ParamountMovie; –dropping the view does not affect any tuples of the underlying relation Movie
43
43 Querying Views Retrieve the title of movies which was made by Paramount in 1979 –Select title from ParamountMovie // from the ParamountView where year = 1979; –Select title from Movie // from the relation Movie where year = 1979 and studioName = ‘Paramount’; Retrieve the name of all stars of movies made by Paramount –Select distinct starName from ParamountMovie, StarsIn where title = movieTitle and year = movieYear;
44
44 Querying Views Create the view MovieProd with movie titles and names of their producers –Create view MovieProd as select title, name from Movie, MovieExec where producerC# = cert#; Retrieve the producer name of Gone with the Wind –Select name from MovieProd where title = ‘Gone with the Wind’; –Equivalently, Select name from Movie, MovieExec where producerC#=cert# and title = ‘Gone with the Wind’;
45
45 Modifying Views Updateable views SQL2 provides a formal definition of when modifications to a view are permitted –roughly, a view that is defined by selecting (using SELECT, not SELECT DISTINCT) some attributes from one relation R –Two important technical points: the WHERE clause must not involve R in a subquery the attributes in the SELECT clause must include enough attributes
46
46 View Modification Examples Insert the movie ‘Star Trek’ and year, 1979 into the ParamountView –Create view ParamountView as select studioName, title, year from Movie where studioName = ‘Paramount’; –Insert into ParamountView values (‘Paramount’, ‘Star Trek’, 1979); Delete all movies with “Trek” in their titles –Delete from ParamountView // from the view where title like ‘%Trek%’; –Delete from Movie// from the base table where title like ‘%Trek%’ and studioName = ‘Paramount’;
47
47 Interpreting Queries Involving Views A query Q may be represented by its expression tree in relational algebra This expression tree uses as leaves some relations that are views Q V W
48
48 Examples –Create view ParamountMovie as select title, year from Movie where studioName = ‘Paramount’; –Select title from ParamountMovie where year = 1979; title year =1979 ParamountMovie Expression tree for the query title year =1979 AND studioName = ‘Paramount’ Movie Simplifying the query over base tables title, year studioName = ‘Paramount’ Movie title year =1979 title, year Expression the query in terms of base tables studioName = ‘Paramount’ Movie
49
49 Operations on Nulls Two important rules –When we operate on a NULL and any other value, including another NULL, using an arithmetic operator like + or , the result is NULL –When we compare a NULL value and any value, including another NULL, using a comparison operator like = or >, the result is UNKNOWN. Example –Let x have the value NULL x + 3 is NULL x = 3 is UNKNOWN NULL + 3 // illegal NULL = 3 // illegal
50
50 Three-valued Logic When NULL values occur, comparisons yield a third truth-value: UNKNOWN True:1, False:0, and Unknown:1/2 –AND of two truth-value: the minimum –OR of two truth-value: the maximum –Negation of a truth-value: 1 minus the truth-value
51
51 Tuples that make where-clause true will be a part of answer! Movie(title, year, length, inColor, studioName, producerC#) Select * from Movie where length 120; –The true meaning of the query is to find all movies with non-Null lengths –Alternatively, Select * from Movie where length is not null;
52
52 SQL2 Join Several kinds of join operators available in SQL2 Movie (title, year, length, inColor, studioName, producerC#) StarsIn (movieTitle, movieYear, starName) –CROSS JOIN: Cartesian product Movie cross join StarsIn; –JOIN ON: theta-join Movie join StarsIn on title = MovieTitle and year = movieYear; –NATURAL JOIN: MovieStar natural join MovieExec; // join terms are of the same name Join expression may appear in FROM clause –select title, year, length, inColor, studioName, producerC#, starName from Movie join StarsIn on title = movieTitle and year = movieYear;
53
53 Outerjoins MovieStar (name, address, gender, birthdate) MovieExec (name, address, cert#, netWorth) –MovieStar natural full outer join MovieExec; Variations of outerjoin –MovieStar natural left outer join MovieExec; –MovieStar natural right outer join MovieExec; –Movie full outer join StarsIn on title = movieTitle and year = movieYear; –Movie left outer join StarsIn on title = movieTitle and year = movieYear; –Movie right outer join StarsIn on title = movieTitle and year = movieYear;
54
54 Recursion in SQL3 Recursion query is a part of SQL3 –linear recursion only –negative predicates must be stratified use the WITH keyword to define IDB relation –WITH {R AS } ; –{} denotes one or more occurrences, separated by a comma –The definitions inside a WITH statement are only available within that statement and cannot be used anywhere
55
55 Recursive Query Example Flights(airline, frm, to, departs, arrives) Reaches(x,y) Flights(a,x,y,d,r) Reaches(x,y) Reaches(x,z) and Reaches(z,y) Externalize the Reaches predicate –With recursive Reaches(frm, to) as (select frm, to from Flights) union (select R1.frm, R2.to from Reaches R1, Reaches R2 where R1.to = R2.frm) select * from Reaches; SF NY CHI DEN DAL UA 930-1230 UA 1500-1800 AA 1900-2200 UA 1830-2130 AA 900-1430 UA 1400-1700 AA 1530-1730 AA 1500-1930
56
56 Linear Recursion A recursion is linear if the FROM clause for any defined relation has at most one occurrence of a relation that is mutually recursive with the relation being defined Flights(airline, frm, to, departs, arrives) Reaches(x,y) Flights(a,x,y,d,r) Reaches(x,y) Reaches(x,z) and Reaches(z,y) Externalize the Reaches predicate –With Pairs as select frm, to from Flights, recursive Reaches(frm, to) as Pairs union (select Pairs.frm, Reaches.to from Pairs, Reaches where Pairs.to = Reaches.frm) select * from Reaches;
57
57 Stratified Negation Defines pairs of cities such that UA flies but AA does not –With Triples as select airline, frm, to from Flights, recursive Reaches(airline, frm, to) as triples union (select Triples.airline, Triples.frm, Reaches.to from Triples, Reaches where Triples.to = Reaches.frm and Triples.airline = Reaches.airline ) (select frm, to from Reaches where airline=‘UA’) except (select frm, to from Reaches where airline=‘AA’);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.