Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Group Members: Shijun Shen Xia Tang Sixin Qiang.

Similar presentations


Presentation on theme: "SQL Group Members: Shijun Shen Xia Tang Sixin Qiang."— Presentation transcript:

1 SQL Group Members: Shijun Shen Xia Tang Sixin Qiang

2 Relational Model Very simple model. It is the Abstract model that underlies SQL. By using the relational approach, the logic representation(the program) is separated from the physical implementation of the database. This makes the development of programs more effective and less dependent on changes in the physical representation of data.

3 Introduction to SQL SQL stands for Structured Query Language. It was developed in the 1970s at IBM (Big Blue) as a way to provide users with a standard method of selecting data from many different database formats. SQL is used to gain access to the relations and the desired set of data and the programmer does not have to write algorithms for navigation because the physical implementation of the database is hidden by using the relational approach.

4 Some Concepts Table = relation.Table = relation. Column headers = attributes.Column headers = attributes. Row = tupleRow = tuple Relation schema = name(attributes).Relation schema = name(attributes). >>Example: Beers(name, manf). >>Example: Beers(name, manf). Relation instance is current set of rows for a relation schema.Relation instance is current set of rows for a relation schema. Database schema = collection of relation schemas.Database schema = collection of relation schemas.

5 SQL Queries Principle Form:Principle Form: SELECT desired attributes FROM tuple variables WHERE condition

6 Demonstrate SQL with an Example Relation Schema: Beers(name, manf) Bars(name,addr,license)Drinkers(name,addr,phone)Likes(drinker,beer)Sells(bar,beer,price)Frequents(drinker,bar)

7 Simplest Case  What beers are made by Anheuser-Busch?  Beers(name, manf)  SELECT name FROM Beers WHERE manf = 'Anheuser-Busch';  Note single quotes for strings.  name Bud Bud Lite Michelob

8 Operational Semantics of SQL For each tupleFor each tuple Check if it satisfies the WHERE clause; If So, Print the attributes in SELECT.

9 Star as list of All Attributes   Beers(name, manf)   SELECT * FROM Beers WHERE manf = 'Anheuser-Busch';   namemanf BudAnheuser-Busch Bud LiteAnheuser-Busch MichelobAnheuser-Busch

10 Renaming Columns   Beers(name, manf)   SELECT name AS beer FROM Beers WHERE manf = 'Anheuser-Busch';   beer Bud Bud Lite Michelob

11 Trick   If you want an answer with a particular string in each row, use that constant as an expression.   Example: Using relation Likes(drinker, beer) SELECT drinker, ‘likes Bud’ AS whoLikesBud FROM Likes Where beer = ‘Bud’;   whoLikesBud Sally likes Bud Fred likes Bud

12 More SQL Queries Find the price Joe’s Bar charge for BudFind the price Joe’s Bar charge for Bud Using relation Schema Sells(bars,beer,price)Using relation Schema Sells(bars,beer,price) SELECT price FROM Sells WHERE bar=‘Joe”s Bar’ AND beer =‘Bud’ Note: two single-quotes in a character string represents one single quote.Note: two single-quotes in a character string represents one single quote. Conditions in WHERE clause can use logical AND,OR, NOT and parentheses in the usual way.Conditions in WHERE clause can use logical AND,OR, NOT and parentheses in the usual way. SQL is case insensitive. Keywords like SELECT or AND can be written upper/lower case as you like.SQL is case insensitive. Keywords like SELECT or AND can be written upper/lower case as you like.

13 Patterns Patterns:Patterns: –% stands for any string. –_ stands for any one character. –“Attributes LIKE pattern” is a condition that is true if the string value of the attribute matches the pattern. Example: Find drinkers whose phone has extension 555 using relation Drinkers(name,addr,phone).Example: Find drinkers whose phone has extension 555 using relation Drinkers(name,addr,phone). SELECT name FROM Drinkers WHERE phone LIKE ‘%555-’; Note patterns must be quoted, like strings.Note patterns must be quoted, like strings.

14 Multirelation Queries List of relations in FROM clause.List of relations in FROM clause. Relation-dot-attribute disambiguates attributes from several relations.Relation-dot-attribute disambiguates attributes from several relations. Example: Find the beers that the frequenters of Joe’s Bar like, using relationExample: Find the beers that the frequenters of Joe’s Bar like, using relation Like(drinker,beer) and Frequentes(drinker,bar) Like(drinker,beer) and Frequentes(drinker,bar) SELECT beer FROM Frequents, Likes WHERE Frequents.drinker=Likes.drinker AND bar = ‘Joe”s Bar’;

15 Operational Semantics Consider a tuple variable for each relation in the FROMConsider a tuple variable for each relation in the FROM –Imagine these tuple variables each pointing to a tuple of their relation, in all combinations(e.g.,nested loops). –If the current assignment of tuple-variables to tuples makes the WHERE true, then output the attributes of the SELECT.

16 Explicit Tuple Variables Sometimes we need to refer to two more copies of a relationSometimes we need to refer to two more copies of a relation –Use tuple variables as aliases of the relations. Example: Find pairs of beers produced by the same manufacturer, use relation Beers(name,manf)Example: Find pairs of beers produced by the same manufacturer, use relation Beers(name,manf) SELECT b1.name, b2.name FROM Beers b1, Beer b2 WHERE b1.manf = b2.manf AND b1.name < b2.name Note that b1.name < b2.name is needed to avoid producing (Bud,Bud) and to avoid producing a pair in both orders.Note that b1.name < b2.name is needed to avoid producing (Bud,Bud) and to avoid producing a pair in both orders.

17 Subqueries Result of a select-from-where query can be used in the where-clause of another query.Result of a select-from-where query can be used in the where-clause of another query. Subquery Returns a Single, Unary Tuple.Subquery Returns a Single, Unary Tuple.

18 Subqueries Example: Find bars that serve Miller at the same price Joe charges for Bud, use relation Sells(bar, beer, price)Example: Find bars that serve Miller at the same price Joe charges for Bud, use relation Sells(bar, beer, price) SELECT bar FROM Sells WHERE beer = ‘Miller’ AND price = (SELECT price FROM Sells WHERE bar=‘Joe”s Bar’ AND beer=‘Bud’) Note the scoping rule: an attribute refers to the most closely nested relation with that attribute.Note the scoping rule: an attribute refers to the most closely nested relation with that attribute. Parentheses around subquery are essential.Parentheses around subquery are essential.

19 IN operator “Tuple IN relation” is true iff the tuple is in the relation.“Tuple IN relation” is true iff the tuple is in the relation. Example: Find the name and manufacturer of beers that Fred likes, use relations Beers(name,manf) and Likes(drinker,beer)Example: Find the name and manufacturer of beers that Fred likes, use relations Beers(name,manf) and Likes(drinker,beer) SELECT * FROM Beers WHERE name IN (SELECT beer FROM Likes WHERE drinker=‘Fred’); WHERE drinker=‘Fred’); Also: NOT IN, negation of IN.Also: NOT IN, negation of IN.

20 Quantifiers ANY and ALL behave as existential and universal Quantifiers respectively.ANY and ALL behave as existential and universal Quantifiers respectively. –Beware in common sense, “any” and “all” seem to be synonyms, e.g.,”I am fatter than any of you” vs. “ I am fatter than all of you.” However, this is not the case in SQL. Example: Find the beer(s) sold for the highest price. Use relation Sells(bar,beer,price)Example: Find the beer(s) sold for the highest price. Use relation Sells(bar,beer,price) SELECT beer FROM Sells WHERE price >=ALL (SELECT price FROM Sells);

21 XQL,OQL and SQL Relational Model underlies SQLRelational Model underlies SQL Object - relational Model and Object - Oriented Model underlies OQLObject - relational Model and Object - Oriented Model underlies OQL XML documents underlies XQLXML documents underlies XQL

22 XQL, OQL and SQL The result of a SQL query is a table containing a set of rows; this table may serve as the basis for further queries. The result of a XQL query is a list of XML document nodes, which may serve as the basis for further queries. The result of a OQL query have types (set, bag, list, array), which may serve as the basis for further queries.

23 THE END THE END


Download ppt "SQL Group Members: Shijun Shen Xia Tang Sixin Qiang."

Similar presentations


Ads by Google