Introduction to SQL.ppt
《Introduction to SQL.ppt》由会员分享,可在线阅读,更多相关《Introduction to SQL.ppt(69页珍藏版)》请在麦多课文档分享上搜索。
1、1,Introduction to SQL,Select-From-Where Statements Subqueries Grouping and Aggregation,2,Why SQL?,SQL is a very-high-level language. Say “what to do” rather than “how to do it.” Avoid a lot of data-manipulation details needed in procedural languages like C+ or Java. Database management system figure
2、s out “best” way to execute query. Called “query optimization.”,3,Select-From-Where Statements,SELECT desired attributesFROM one or more tablesWHERE condition about tuples ofthe tables,4,Our Running Example,All our SQL queries will be based on the following database schema. Underline indicates key a
3、ttributes.Beers(name, manf)Bars(name, addr, license)Drinkers(name, addr, phone)Likes(drinker, beer)Sells(bar, beer, price)Frequents(drinker, bar),5,Example,Using Beers(name, manf), what beers are made by Anheuser-Busch?SELECT nameFROM BeersWHERE manf = Anheuser-Busch;,6,Result of Query,nameBudBud Li
4、teMichelob. . .,The answer is a relation with a single attribute, name, and tuples with the name of each beer by Anheuser-Busch, such as Bud.,7,Meaning of Single-Relation Query,Begin with the relation in the FROM clause. Apply the selection indicated by the WHERE clause. Apply the extended projectio
5、n indicated by the SELECT clause.,8,Operational Semantics,name,manf,Bud,Anheuser-Busch,9,Operational Semantics,To implement this algorithm think of a tuple variable ranging over each tuple of the relation mentioned in FROM. Check if the “current” tuple satisfies the WHERE clause. If so, compute the
6、attributes or expressions of the SELECT clause using the components of this tuple.,10,* In SELECT clauses,When there is one relation in the FROM clause, * in the SELECT clause stands for “all attributes of this relation.” Example using Beers(name, manf):SELECT *FROM BeersWHERE manf = Anheuser-Busch;
7、,11,Result of Query:,name manfBud Anheuser-BuschBud Lite Anheuser-BuschMichelob Anheuser-Busch. . . . . .,Now, the result has each of the attributes of Beers.,12,Renaming Attributes,If you want the result to have different attribute names, use “AS ” to rename an attribute. Example based on Beers(nam
8、e, manf):SELECT name AS beer, manfFROM BeersWHERE manf = Anheuser-Busch,13,Result of Query:,beer manfBud Anheuser-BuschBud Lite Anheuser-BuschMichelob Anheuser-Busch. . . . . .,14,Expressions in SELECT Clauses,Any expression that makes sense can appear as an element of a SELECT clause. Example: from
9、 Sells(bar, beer, price):SELECT bar, beer,price * 114 AS priceInYenFROM Sells;,15,Result of Query,bar beer priceInYenJoes Bud 285Sues Miller 342 ,16,Another Example: Constant Expressions,From Likes(drinker, beer) :SELECT drinker,likes Bud AS whoLikesBudFROM LikesWHERE beer = Bud;,17,Result of Query,
10、drinker whoLikesBudSally likes BudFred likes Bud ,18,Complex Conditions in WHERE Clause,From Sells(bar, beer, price), find the price Joes Bar charges for Bud:SELECT priceFROM SellsWHERE bar = Joes Bar ANDbeer = Bud;,19,Patterns,WHERE clauses can have conditions in which a string is compared with a p
11、attern, to see if it matches. General form: LIKE or NOT LIKE Pattern is a quoted string with % = “any string”; _ = “any character.”,20,Example,From Drinkers(name, addr, phone) find the drinkers with exchange 555:SELECT name FROM Drinkers WHERE phone LIKE %555-_ _ _ _;,21,NULL Values,Tuples in SQL re
12、lations can have NULL as a value for one or more components. Meaning depends on context. Two common cases: Missing value : e.g., we know Joes Bar has some address, but we dont know what it is. Inapplicable : e.g., the value of attribute spouse for an unmarried person.,22,Comparing NULLs to Values,Th
13、e logic of conditions in SQL is really 3-valued logic: TRUE, FALSE, UNKNOWN. When any value is compared with NULL, the truth value is UNKNOWN. But a query only produces a tuple in the answer if its truth value for the WHERE clause is TRUE (not FALSE or UNKNOWN).,23,Three-Valued Logic,To understand h
14、ow AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = . AND = MIN; OR = MAX, NOT(x) = 1-x. Example: TRUE AND (FALSE OR NOT(UNKNOWN) = MIN(1, MAX(0, (1 - ) =MIN(1, MAX(0, ) = MIN(1, ) = .,24,Surprising Example,From the following Sells relation:bar beer priceJoes Bar
15、Bud NULLSELECT barFROM SellsWHERE price = 2.00;,25,Reason: 2-Valued Laws != 3-Valued Laws,Some common laws, like commutativity of AND, hold in 3-valued logic. But not others, e.g., the “law of the excluded middle”: p OR NOT p = TRUE. When p = UNKNOWN, the left side is MAX( , (1 ) = != 1.,26,Multirel
16、ation Queries,Interesting queries often combine data from more than one relation. We can address several relations in one query by listing them all in the FROM clause. Distinguish attributes of the same name by “.”,27,Example,Using relations Likes(drinker, beer) and Frequents(drinker, bar), find the
17、 beers liked by at least one person who frequents Joes Bar.SELECT beerFROM Likes, FrequentsWHERE bar = Joes Bar ANDFrequents.drinker = Likes.drinker;,28,Formal Semantics,Almost the same as for single-relation queries: Start with the product of all the relations in the FROM clause. Apply the selectio
18、n condition from the WHERE clause. Project onto the list of attributes and expressions in the SELECT clause.,29,Operational Semantics,Imagine one tuple-variable for each relation in the FROM clause. These tuple-variables visit each combination of tuples, one from each relation. If the tuple-variable
19、s are pointing to tuples that satisfy the WHERE clause, send these tuples to the SELECT clause.,30,Example,drinker bar drinker beertv1 tv2Sally BudSally JoesLikesFrequents,31,Explicit Tuple-Variables,Sometimes, a query needs to use two copies of the same relation. Distinguish copies by following the
20、 relation name by the name of a tuple-variable, in the FROM clause. Its always an option to rename relations this way, even when not essential.,32,Example,From Beers(name, manf), find all pairs of beers by the same manufacturer. Do not produce pairs like (Bud, Bud). Produce pairs in alphabetic order
21、, e.g. (Bud, Miller), not (Miller, Bud).SELECT b1.name, b2.nameFROM Beers b1, Beers b2WHERE b1.manf = b2.manf ANDb1.name b2.name;,33,Subqueries,A parenthesized SELECT-FROM-WHERE statement (subquery ) can be used as a value in a number of places, including FROM and WHERE clauses. Example: in place of
22、 a relation in the FROM clause, we can place another query, and then query its result. Better use a tuple-variable to name tuples of the result.,34,Subqueries That Return One Tuple,If a subquery is guaranteed to produce one tuple, then the subquery can be used as a value. Usually, the tuple has one
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- INTRODUCTIONTOSQLPPT
