数据库原理PPT课件第三章.ppt
《数据库原理PPT课件第三章.ppt》由会员分享,可在线阅读,更多相关《数据库原理PPT课件第三章.ppt(294页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、4.2Database SystemnData DefinitionnBasic Query StructurenSet OperationsnAggregate FunctionsnNull ValuesnNested SubqueriesnComplex Queries nViewsnModification of the DatabasenJoined Relations* 4.3Database SystemnIBM Sequel language developed as part of System R project at the IBM San Jose Research La
2、boratorynRenamed Structured Query Language (SQL)nANSI and ISO standard SQL:lSQL-86lSQL-89lSQL-92 lSQL:1999 (language name became Y2K compliant!)lSQL:2003nCommercial systems offer most, if not all, SQL-92 features, plus varying feature sets from later standards and special proprietary features. lNot
3、all examples here may work on your particular system.4.4Database SystemnThe schema for each relation.nThe domain of values associated with each attribute.nIntegrity constraintsnThe set of indices to be maintained for each relations.nSecurity and authorization information for each relation.nThe physi
4、cal storage structure of each relation on disk.Allows the specification of not only a set of relations but also information about each relation, including:4.5Database Systemnchar(n). Fixed length character string, with user-specified length n.nvarchar(n). Variable length character strings, with user
5、-specified maximum length n. Nchar(n), nVarchar(n) (for unicode ,2byte code,1-4000 chars)nText. Variable length character strings,1 to 2GB.=varchar(max)nint. Integer (4B,a finite subset of the integers that is machine-dependent).nsmallint. Small integer (2B,a subset of the integer domain type).nBigi
6、nt. Big integer (8B),tinyint(1B),bit(多个位占多个位占1B)nnumeric(p,d). Fixed point number, with user-specified precision of p digits, with d digits to the right of decimal point. Decimal(p,d).4.6Database Systemnreal, double precision. Floating point and double-precision floating point numbers, with machine-
7、dependent precision.nfloat(n). Floating point number, with user-specified precision of at least n digits.nDatatime 8B:yyyy.mm.dd h:m:s,smalldatatime 4BnMoney 8BnXml (2GB)(不同表单可以存在不同表单可以存在xml类型的属性字段中类型的属性字段中)nBinary(n) 1-8000BnImage 2GBnTimestamp 8BnSql_variant 1200nComparison results can be combined
8、 using the logical connectives and, or, and not. nComparisons can be applied to results of arithmetic expressions.4.17Database SystemnSQL includes a between comparison operatornExample: Find the loan number of those loans with loan amounts between $90,000 and $100,000 (that is, $90,000 and $100,000)
9、 select loan_numberfrom loanwhere amount between 90000 and 100000 OR: where amount =90000 and amount S.assets and S.branch_city = Brooklyn nKeyword as is optional and may be omitted borrower as T borrower T select customer_name, T.loan_number, S.amount from borrower as T, loan as S where T.loan_numb
10、er = S.loan_number4.21Database SystemnSQL includes a string-matching operator for comparisons on character strings. The operator “like” uses patterns that are described using two special characters:lpercent (%). The % character matches any substring.lunderscore (_). The _ character matches any chara
11、cter.nFind the names of all customers whose street includes the substring “Main”.select customer_namefrom customerwhere customer_street like % Main% nMatch the name “Main%”like Main% escape nSQL supports a variety of string operations such aslconcatenation (using “|”)l converting from upper to lower
12、 case (and vice versa)l finding string length, extracting substrings, etc.4.22Database SystemnList in alphabetic order the names of all customers having a loan in Perryridge branchselect distinct customer_namefrom borrower, loanwhere borrower loan_number = loan.loan_number and branch_name = Perryrid
13、ge order by customer_name ascnWe may specify desc for descending order or asc for ascending order, for each attribute; ascending order is the default.lExample: order by customer_name desc4.23Database SystemnIn relations with duplicates, SQL can define how many copies of tuples appear in the result.n
14、Multiset versions of some of the relational algebra operators given multiset relations r1 and r2:1. (r1): If there are c1 copies of tuple t1 in r1, and t1 satisfies selections , then there are c1 copies of t1 in (r1).2. A (r ): For each copy of tuple t1 in r1, there is a copy of tuple A (t1) in A (r
15、1) where A (t1) denotes the projection of the single tuple t1.3. r1 x r2 : If there are c1 copies of tuple t1 in r1 and c2 copies of tuple t2 in r2, there are c1 x c2 copies of the tuple t1. t2 in r1 x r24.24Database SystemnExample: Suppose multiset relations r1 (A, B) and r2 (C) are as follows: r1
16、= (1, a) (2,a) r2 = (2), (3), (3)nThen B(r1) would be (a), (a), while B(r1) x r2 would be(a,2), (a,2), (a,3), (a,3), (a,3), (a,3)nSQL duplicate semantics: select A1, A2, ., Anfrom r1, r2, ., rmwhere Pis equivalent to the multiset version of the expression:)(21,21mPAAArrrn4.25Database SystemnThe set
17、operations union, intersect, and except operate on relations and correspond to the relational algebra operations nEach of the above operations automatically eliminates duplicates; to retain all duplicates use the corresponding multiset versions union all, intersect all and except all.Suppose a tuple
18、 occurs m times in r and n times in s, then, it occurs:lm + n times in r union all slmin(m,n) times in r intersect all slmax(0, m n) times in r except all s4.26Database SystemnFind all customers who have a loan, an account, or both:(select customer_name from depositor)except(select customer_name fro
19、m borrower)(select customer_name from depositor)intersect(select customer_name from borrower)n Find all customers who have an account but no loan.(select customer_name from depositor)union(select customer_name from borrower)n Find all customers who have both a loan and an account.4.27Database System
20、nThese functions operate on the multiset of values of a column of a relation, and return a valueavg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values4.28Database SystemnFind the average account balance at the Perryridge branch.n Find the number of depositors
21、 in the bank.n Find the number of tuples in the customer relation.select avg (balance)from accountwhere branch_name = Perryridge select count (*)from customerselect count (distinct customer_name)from depositorselect avg (distinct balance) 4.29Database SystemnFind the number of depositors for each br
22、anch.Note: Attributes in select clause outside of aggregate functions must appear in group by list(除聚集函数外,在除聚集函数外,在selectselect中出现的属性必须在中出现的属性必须在group bygroup by中出现中出现)(Group by branch_name,street -先按branch_name分组,在组内再按street分组)select branch_name, count (distinct customer_name)from depositor, accoun
23、twhere depositor.account_number = account.account_numbergroup by branch_name4.30Database SystemnFind the names of all branches where the average account balance is more than $1,200.Note: predicates in the having clause are applied after the formation of groups whereas predicates in the where clause
24、are applied before forming groups(where 条件作用于条件作用于 分组前,而分组前,而having条件作用于分组后,条件作用于分组后,即每组满足的条件)即每组满足的条件) select branch_name, avg (balance) from account group by branch_name having avg (balance) 12004.31Database System 使用使用GROUP BY子句可以按一定的条件对查询到子句可以按一定的条件对查询到的结果进行分组,再对每一组数据计算统计信息。的结果进行分组,再对每一组数据计算统计信息
25、。 SELECT column_name1, column_name2 ,.n FROM table_name WHERE search_condition GROUP BY group_by_expression HAVING search_condition GROUP BY将查询结果按将查询结果按 (group_by_expression)进行分组,该属性列相等的记录为一个组。进行分组,该属性列相等的记录为一个组。 通常,在每组中通过聚合函数来计算一个或者多通常,在每组中通过聚合函数来计算一个或者多个列。个列。 如果如果GROUP带有带有HAVING,则只有满足,则只有满足search_
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据库 原理 PPT 课件 第三
限制150内