数据库课件总结:Database Chapter Three Outline_第1页
数据库课件总结:Database Chapter Three Outline_第2页
数据库课件总结:Database Chapter Three Outline_第3页
数据库课件总结:Database Chapter Three Outline_第4页
数据库课件总结:Database Chapter Three Outline_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Database Chapter Three OutlineSQLSQL names are case insensitive 不区分大小写Data Definition Language Create Table ConstructAn SQL relation is defined using the create table command:create table r (A1 D1, A2 D2, ., An Dn,(integrity-constraint1),.,(integrity-constraintk)r is the name of the relationeach Ai

2、is an attribute name in the schema of relation rDi is the data type of values in the domain of attribute Ai Integrity Constraints in Create Tablenot nullprimary key (A1, ., An ) Example: Declare branch_name as the primary key for branch and ensure that the values of assets are non-negative. create t

3、able branch (branch_namechar(15), branch_citychar(30), assetsinteger, primary key (branch_name), check(assets=0)PRIMARY KEY - FOREIGN KEY - CHECKDrop and Alter Table Constructsalter table r add A D 添加属性 alter table r drop A 删除属性Drop table r 删除表Basic Query StructureA typical SQL query has the form:se

4、lect A1, A2, ., Anfrom r1, r2, ., rmwhere PAi represents an attributeRi represents a relationP is a predicate.This query is equivalent to the relational algebra expression.The result of an SQL query is a relation.The meaning of select is different fromThe select clause can contain arithmetic express

5、ions involving the operation, +, , *, and /, and operating on constants or attributes of tuples.DISTINCT - FROM - WHERE - GROUB BY - HAVING - ORDER BY. ASC DESCThe Rename Operation old-name as new-nameString OperationsThe operator “like” uses patterns that are described using two special characters:

6、percent (%). The % character matches any substring.underscore (_). The _ character matches any character. concatenation (using “|”) converting from upper to lower case (and vice versa) (upper, lower) finding string length, extracting substrings, etc.Set Operations(select customer_name from depositor

7、)union(select customer_name from borrower) Intersect、except;自动去除重复。若要全部则: UNION ALL 、 INTERSECT ALL 、 EXCEPT ALL;Aggregate Functionsavg: average value min: minimum value max: maximum value sum: sum of valuescount: number of valuesGroup ByHaving ClauseAll aggregate operations except count(*) ignore t

8、uples with null values on the aggregated attributes.Nested SubqueriesA subquery is a select-from-where expression that is nested within another query (appear in where clause or having by clause). Cant use order by in subquery set membership: in and not inSet Comparison SOME大于子查询结果中的某个值 ALL大于子查询结果中的所

9、有值 SOME 小于子查询结果中的某个值 = SOME 大于等于子查询结果中的某个值 = ALL大于等于子查询结果中的所有值= SOME 小于等于子查询结果中的某个值 = ALL小于等于子查询结果中的所有值= SOME 等于子查询结果中的某个值 =ALL等于子查询结果中的所有值(通常没有实际意义)!=(或) SOME 不等于子查询结果中的某个值!=(或)ALL不等于子查询结果中的任何一个值 (= some) in However, ( some) not in ( all) not in However, (= all) inexists r r 如果子查询不为空,则返回true;not ex

10、ists r r = Relevant Nested Subqueries NOT EXIST(B EXCEPT A) 表示关系A包含关系B。The unique construct tests whether a subquery has any duplicate tuples in its result. (专门用来处理“只有一个”、“有两个或者两个以上的”)Derived RelationsSQL allows a subquery expression to be used in the from clauseFind the average account balance of t

11、hose branches where the average account balance is greater than $1200.select branch_name, avg_balancefrom (select branch_name, avg (balance) from account group by branch_name ) as branch_avg ( branch_name, avg_balance )where avg_balance 1200Note that we do not need to use the having clause, since we

12、 compute the temporary (view) relation branch_avg in the from clause, and the attributes of branch_avg can be used directly in the where clause.With ClauseThe with clause provides a way of defining a temporary view whose definition is available only to the query in which the with clause occurs. View

13、s (只要没有更新操作,视图可以出现在关系名出现的任何地方)A view provides a mechanism to hide certain data from the view of certain users. create view v as A view definition causes the saving of an expression; the expression is substituted into queries using the view.Views Defined Using Other ViewsA view relation v1 is said to

14、 depend directly on a view relation v2 if v2 is used in the expression defining v1 A view relation v1 is said to depend on view relation v2 if either v1 depends directly to v2 or there is a path of dependencies from v1 to v2 A view relation v is said to be recursive if it depends on itself.View Expa

15、nsionView expansion of an expression repeats the following replacement step:repeatFind any view relation vi in e1Replace the view relation vi by the expression defining vi until no more view relations are present in e1 As long as the view definitions are not recursive, this loop will terminateModification of the Database Deletion Delete from r where PModification of the Database InsertionInsert into r(A1,A2,An) values(v1,v2, ,vn)Modification of the Database UpdatesUPDATE r

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论