1 / 288

本讲主要内容

第10讲 SQL 语言( V) ( 重点 ) SQL Language(V) Textbook:Chapter 9 More SQL: Assertions,Views, and Programming Techniques. 本讲主要内容. 视图的基本概念 视图的优点 创建和管理视图 查询视图 更新视图中的数据 加密视图 派生表. SQL 的功能组成. Data Manipulation Language (DML)( 重点 ) 数据查询( Data Query Language (DQL))( 核心 ) 数据查询功能用于实现对数据库中数据的查询

Download Presentation

本讲主要内容

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第10讲 SQL语言(V) (重点)SQL Language(V)Textbook:Chapter 9 More SQL: Assertions,Views, and Programming Techniques

  2. 本讲主要内容 • 视图的基本概念 • 视图的优点 • 创建和管理视图 • 查询视图 • 更新视图中的数据 • 加密视图 • 派生表

  3. SQL的功能组成 • Data Manipulation Language (DML)(重点) • 数据查询(Data Query Language (DQL))(核心) • 数据查询功能用于实现对数据库中数据的查询 • 数据操纵(Data Manipulation) • 数据操纵功能用于实现对数据库数据的增加、删除和修改 • Data Definition Language (DDL) • 数据定义(Data Definition) • 数据定义功能用于定义、删除和修改数据库中的对象 • Data Control Language (DCL) • 数据控制(Data Control) • 数据控制功能用于控制用户对数据库的操作权限

  4. 包括完整性约束 条件

  5. 数据定义

  6. 视图VIEW(外模式) • 什么是视图 • 视图是虚拟的表(a virtual table),保存在视图中的数据并不是物理存储的数据,而是由基本表或其他视图派生的 • A view is a named SELECT statement that dynamically produces a result set that you can further operate on.

  7. 视图的优点 • 提供了一定程度的逻辑独立性 • 为用户集中数据,保证数据安全性 • 视图为用户提供一个受限的环境,用户只能访问允许的数据,一些不必要的、不合适的数据则不在视图中显示 StudentInfo基本表

  8. 视图的优点(续) • 简化数据库查询

  9. 创建和管理视图 • 使用SQL语句 • 使用企业管理器(参见实验1讲义)

  10. 创建(定义)视图 CREATE VIEW <视图名>[(<列名>[, <列名>]……)] AS <子查询> [WITH CHECK OPTION] 其中,子查询可以是任意复杂的SELECT语句,但通常不允许含有ORDER BY子句和DISTINCT短语。

  11. [WITH CHECK OPTION] • 对视图进行UPDATE,INSERT和DELETE操作时要保证更新、插入或删除的行满足视图定义中的谓词条件(即子查询中的条件表达式)

  12. 只是把视图的定义存入数据字典,并不执行其中的SELECT语句只是把视图的定义存入数据字典,并不执行其中的SELECT语句

  13. 定义视图时指定列名 • 某个目标列不是单纯的属性名,而是聚集函数或计算列 • 多表连接时选出了几个同名列作为视图的字段 • 需要在视图中为某个列启用新的更合适的名字

  14. CREATE VIEW EMPLOYEE_AVG_SALARY(DNO,AVG_SALARY) AS SELECT DNO,AVG(SALARY) FROM EMPLOYEE GROUP BY DNO

  15. 修改视图定义

  16. 删除视图

  17. 查询视图 • 步骤 • 有效性检查 • 安全性检查 • 视图消解 • 从数据字典中取出视图的定义,把定义中的子查询和用户针对视图的查询结合起来,转换成等价的对基本表的查询,然后再执行修正的等价查询

  18. 视图消解 SELECT * FROM STUDENTINFO WHERE STUDENTNATION='汉族' AND STUDENTGENDER='男'

  19. 更新视图中的数据 • 可以通过更新视图的方式实现对表中数据的更新。视图的更新操作包括插入、修改和删除数据 • 视图是一张虚表,所以对视图的更新,最终实际上是转换成对视图的底层表的更新

  20. 更新视图中的数据:不是所有视图中的数据都是可更新的更新视图中的数据:不是所有视图中的数据都是可更新的 • 基本判断原则 • 视图中的每一列都必须和一个基本表中的一个源列对应 • 视图中的每一行都必须和一个基本表中的一个源行对应

  21. 可更新视图的具体判断标准

  22. Views are updateable (can be the target of UPDATE, DELETE, or INSERT statements) as long as the modification affects only one of the base tables referenced by the view.

  23. 可更新视图的具体判断标准 • No aggregate functions (AVG, COUNT, SUM, MIN, MAX, GROUPING, STDEV, STDEVP, VAR, and VARP) or GROUP BY, UNION, DISTINCT, or TOP clauses are used in the select list. • No derived columns(导出列或者计算列) are used in the select list. Derived columns are result set columns formed by anything other than a simple column reference.

  24. 通过视图更新数据的注意事项 • 所有的数据修改都必须遵守在待更新基本表(甚至包括相关表)上设置的完整性约束条件

  25. Adding Data through a View

  26. Changing Data through a View

  27. Deleting Data through a View

  28. WITH CHECK OPTION的应用

  29. 为什么要通过视图更新数据 • 可以限制用户可更新的底层表中的列和行; • 使列名具有更好的描述性。

  30. 加密视图

  31. 派生表(Derived Tables)

  32. A derived table is a fancy name for the result of using another SELECT statement in the FROM clause of a SELECT statement. This works because the result of a SELECT statement is a table, which is exactly what the FROM clause requires. You can think of a view as a named derived table. A view is named, and its definition is persistent(持久的) and reusable(可重用的); a derived table is a completely dynamic(动态的), temporal(临时的) concept. • You can insert, update, and delete rows in a view but not in a derived table.

  33. 第9章 Transact-SQL编程Transact-SQL Programming Textbook:Chapter 9 More SQL: Assertions,Views, and Programming Techniques

  34. 本讲主要内容 • Transact-SQL(简称T-SQL)简介 • SQL SERVER标识符 • T-SQL语言要素 • 执行T-SQL语句的方法 • T-SQL编程规范 • 存储过程及举例 • 触发器及举例 • ADO.NET简介 • 三层体系结构 • COMMERCE案例

  35. The Transact-SQL Programming Language • SQL Server Implementation of Entry-Level ANSI ISO Standard • Can Be Run on Any Entry-Level Compliant Product • Contains Additional Unique Functionality

  36. SQL Server标识符 • 常规标识符 • 标识符的第一个字符必须是字母 • 标识符不能是T-SQL的保留字 • 标识符中不允许嵌入空格或其他特殊字符 • 以特殊字符开始的标识符的含义 • @:表示局部变量 • @@:表示全局变量 • #:表示局部临时对象 • ##:表示全局临时对象

  37. SQL Server标识符(续) • 分隔标识符 • 标识符中出现空格 • 标识符是T-SQL保留字 • 分隔用 [ ]或" "

  38. 完整的对象名格式 • server.database.owner.object • 简写格式 • server.database..object • server..owner.object • server…object • database.owner.object • database..object • owner.object • object

  39. T-SQL Language Elements • Local Variables • Global Variables • Operators • Control of Flow Language Elements • Functions • Comments

  40. Local Variables • User-defined with DECLARE Statement • Assigned Values with SET or Select Statement DECLARE @vLastNamechar(20), @vFirstName varchar(11) SET @vLastName = 'Dodsworth' SELECT @vFirstName = FirstName FROM Employees WHERE LastName = @vLastName PRINT @vFirstName + ' ' + @vLastName GO

More Related