第4章结构化查询语言—SQL.
- 格式:doc
- 大小:82.50 KB
- 文档页数:13
《SQL必知必会》总结⽬录第1章了解SQL第2章检索数据第3章排序检索数据第4章过滤数据第5章⾼级数据过滤第6章⽤通配符进⾏过滤第7章创建计算字段第8章使⽤数据处理函数第9章汇总数据第10章分组数据第11章使⽤⼦查询第12章联结表第13章创建⾼级联结第14章组合查询第15章插⼊数据第16章更新和删除数据第17章创建和操作表第18章使⽤视图第19章使⽤存储过程第20章管理实务处理第21章使⽤游标第22章了解⾼级SQL特性--------------------------------------------------------------------------------------------------------------------------------------------第1章:连接数据库数据库(databases):保存有组织的数据的容器表(table):某种特定类型数据的结构化清单模式(schema):关于数据库和表的布局及特性的信息列(colimn):表中的⼀个字段,所有表都是由⼀个或多个列组成的数据类型(datatype):所容许的数据的类型⾏(row):表中的记录主键(primary key):⼀列(或⼀组列),其值能够唯⼀标识表中每个⾏ ①任意两⾏都不具有相同的主键值; ②每个⾏都必须具有⼀个主键值(主键列不允许NULL值); ③主键列中的值不允许修改或更新; ④主键值不能重⽤(如果某⾏从表中删除,它主键不能赋给以后的新⾏)SQL(Structured Query Language,结构化查询语⾔):与数据库通信的语⾔第2章检索数据SELECT语句:从⼀个或多个表中检索数据关键词(keyword):SQL组成部分的保留字,不能⽤作表或列的名字//从Products表中检索⼀个名为prob_name的列SELECT prob_name FROM Products;//从Products表中检索多列SELECT prob_name, prob_id, prob_priceFROM Products;//从Products表中检索所有的列SELECT*FROM Products;第三章排序检索数据⼦句(clause): SQL语句由⼦句构成,通常由⼀个关键字加上所提供的数据组成可⽤ORDER BY⼦句明确地排序⽤SELECT语句检索出的数据。
结构化查询语言(SQL)高级应用测试(答案见尾页)一、选择题1. SQL中用于数据查询的语句是:A. SELECTB. INSERTC. UPDATED. DELETE2. 在SQL中,用于修改表结构的语句是:A. ALTER TABLEB. CREATE TABLEC. DROP TABLED. Renames the table3. SQL中的聚合函数不包括:A. COUNT()B. SUM()C. AVG()D. MAX()4. 在SQL中,用于数据分组的语句是:A. GROUP BYB. ORDER BYC. HAVINGD. DISTINCT5. 在SQL中,用于从指定表中检索特定列的语句是:A. SELECT * FROM table_name;B. SELECT column1, column2 FROM table_name;C. INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);D. DELETE FROM table_name WHERE condition;6. 在SQL中,用于插入新数据到指定表中的语句是:A. INSERT INTOB. CREATE TABLEC. UPDATED. DELETE7. 在SQL中,用于删除表中所有数据的语句是:A. DELETE FROM table_name;B. DROP TABLE table_name;C. TRUNCATE table_name;D. DELETE table_name;8. 在SQL中,用于排序查询结果的语句是:A. ORDER BYB. GROUP BYC. HAVINGD. DISTINCT9. 在SQL中,用于连接两个或多个表的語句是:A. JOINB. UNIONC. SUBQUERYD. DELETE10. 在SQL中,用于返回查询结果的最大值和最小值的语句是:A. SELECT MAX(column_name) FROM table_name;B. SELECT MIN(column_name) FROM table_name;C. SELECT MAX(column_name), MIN(column_name) FROM table_name;D. None of the above11. SQL中用于数据查询的命令是?A. SELECTB. INSERTC. UPDATED. DELETE12. 在SQL中,哪种数据类型允许存储文本信息?A. INTB. VARCHARC. DATED. TIME13. SQL中的子查询是一种什么类型的查询?A. 选择特定的行B. 选择特定的列C. 嵌套在另一个查询中D. 对结果进行计算14. 在SQL中,用于排序查询结果的命令是什么?A. ORDER BYB. GROUP BYC. HAVINGD. DISTINCT15. SQL中的聚合函数有哪些?(多选)A. COUNTB. SUMC. AVGD. MAXE. MIN16. 在SQL中,如何修改表结构?A. 使用INSERT语句B. 使用UPDATE语句C. 使用ALTER TABLE语句D. 使用CREATE TABLE语句17. SQL中的事务是什么?A. 一段程序代码B. 一组SQL语句的集合C. 一个工作单元,确保数据的完整性和一致性D. 一种数据库管理机制18. 在SQL中,如何创建一个包含特定约束的表?A. 使用CREATE TABLE语句B. 使用ALTER TABLE语句C. 使用CREATE INDEX语句D. 使用INSERT语句19. SQL中的触发器是一种什么类型的对象?A. 存储过程B. 函数C. 对象D. 规则20. 在SQL中,如何执行复杂的查询?A. 使用简单的SELECT语句B. 使用多个SELECT语句和连接操作C. 使用存储过程D. 使用函数21. SQL中的事务隔离级别中,哪个级别可以防止脏读(Dirty Read)?A. 读未提交(Read Uncommitted)B. 读已提交(Read Committed)C. 可重复读(Repeatable Read)D.串行化(Serializable)22. 在SQL中,用于查询所有用户的权限的语句是:A. SELECT USERB. SELECT ALL PRIVILEGESC. SELECT PERMISSIOND. SHOW GRANTS23. SQL中,用于创建存储过程的语句是:A. CREATE PROCEDUREB. CREATE FUNCTIONC. CREATE TRIGGERD. CREATE VIEW24. 在SQL中,用于查看当前数据库名的命令是:A. SELECT DATABASE()B. SELECT USER()C. SELECT CURRENT_DATABASE()D. SHOW DATABASES25. SQL中,用于修改表数据的语句是:A. INSERTB. UPDATEC. DELETED. ALTER26. 在SQL中,用于备份数据库的语句是:A. BACKUP DATABASEB. CREATE BACKUPC. SAVEBACK DATABASED. TAKE BACKUP27. SQL中,用于从某个表的指定列中返回所有值的唯一约束是:A. UNIQUEB. PRIMARY KEYC. NOT NULLD. FOREIGN KEY28. 在SQL中,用于强制实行数据库一致性控制的命令是:A. ROLLBACKB. COMMITC. SAVEPOINTD. SET TRANSACTION29. 在SQL中,如果需要对某个表中的所有数据进行备份,应该使用哪个命令?A. SELECT * FROM table_name;B. CREATE TABLE table_name LIKE old_table_name;C. INSERT INTO table_name SELECT * FROM table_name;D. DROP TABLE table_name;30. SQL语言中的“结构化查询语言”缩写是什么?A. SASB. SQLC. DDLD. DML31. 在SQL中,用于修改表结构的命令是?A. ALTER TABLEB. CREATE TABLEC. DROP TABLED. TRUNCATE TABLE32. SQL中的“结构化”是指数据的组织方式是?A. 数据和数据库管理系统是分开的B. 数据和数据库管理系统是相互独立的C. 数据和数据库管理系统是相互关联的D. 数据和数据库管理系统是自包含的33. 在SQL中,如果需要对某个列的数据进行统计分析,应该使用哪个函数?A. COUNT()B. SUM()C. AVG()D. MAX()34. SQL中用于数据分组的命令是?A. GROUP BYB. ORDER BYC. HAVINGD. DISTINCT35. 在SQL中,如果需要对某个表进行复杂的查询,应该使用哪个命令?A. SELECT * FROM table_name WHERE condition;B. CREATE TABLE table_name AS SELECT * FROM another_table WHERE condition;C. INSERT INTO table_name (column1, column2, ...) SELECT column1, column2, ... FROM another_table WHERE condition;D. DELETE FROM table_name WHERE condition;36. SQL中的“结构化”是指数据的组织方式是有组织的,易于理解和处理,这是指什么?A. 数据和数据库管理系统是分开的B. 数据和数据库管理系统是相互独立的C. 数据和数据库管理系统是相互关联的D. 数据和数据库管理系统是自包含的37. 在SQL中,如果需要对某个表进行排序,应该使用哪个命令?A. ORDER BYB. GROUP BYC. DISTINCTD. LIMIT38. 在SQL中,哪种类型的约束可以确保字段值的唯一性?A. 主键约束B. 外键约束C. 唯一约束D. 非空约束39. SQL中的`CASE`语句用于执行什么操作?A. 条件判断B. 数据转换C. 控制流程D. 数据汇总40. 在SQL中,如何更新表中的数据?A. 使用UPDATE语句B. 使用DELETE语句C. 使用INSERT语句D. 使用CREATE语句41. SQL中的`GROUP BY`子句用于将查询结果按照哪个或多个列进行分组?A. 对查询结果进行排序B. 对查询结果进行分组C. 对查询结果进行过滤D. 对查询结果进行投影42. 如何在SQL中使用`JOIN`操作来结合两个或多个表的列?A. 使用INNER JOINB. 使用LEFT JOINC. 使用RIGHT JOIND. 使用FULL OUTER JOIN43. 在SQL中,如何删除表中的所有数据?A. 使用DELETE语句B. 使用TRUNCATE语句C. 使用DROP TABLE语句D. 使用DELETE FROM语句44. SQL中的`ORDER BY`子句用于对查询结果进行排序,如果指定为升序,那么关键字是什么?A. ASCB. DESCC. ASCENDD. DESCEND45. 如何在SQL中使用子查询来从一个表中筛选出满足某个条件的行?A. 将子查询作为条件直接放在SELECT语句中B. 将子查询作为另一个查询的结果集C. 将子查询作为表名放在SELECT语句中D. 将子查询作为表达式放在SELECT语句中46. 在SQL中,如何修改表的结构?A. 使用ALTER TABLE语句B. 使用CREATE TABLE语句C. 使用DELETE TABLE语句D. 使用TRUNCATE TABLE语句二、问答题1. 什么是SQL?请简述SQL的特点。
结构化查询语言(Structured Query Language)简称SQL,结构化查询语言是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统;同时也是数据库脚本文件的扩展名。
结构化查询语言是高级的非过程化编程语言,允许用户在高层数据结构上工作。
它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以具有完全不同底层结构的不同数据库系统, 可以使用相同的结构化查询语言作为数据输入与管理的接口。
结构化查询语言语句可以嵌套,这使它具有极大的灵活性和强大的功能。
结构化查询语言包含六个部分:一、数据查询语言其语句,也称为“数据检索语句”,用以从表中获得数据,确定数据怎样在应用程序给出。
保留字SELECT是DQL(也是所有SQL)用得最多的动词,其他DQL常用的保留字有WHERE,ORDER BY,GROUP BY和HAVING。
这些DQL保留字常与其他类型的SQL语句一起使用。
二、数据操作语言其语句包括动词INSERT,UPDATE和DELETE。
它们分别用于添加,修改和删除表中的行。
也称为动作查询语言。
三、事务处理语言它的语句能确保被DML语句影响的表的所有行及时得以更新。
TPL语句包括BEGIN TRANSACTION,COMMIT和ROLLBACK。
四、数据控制语言它的语句通过GRANT或REVOKE获得许可,确定单个用户和用户组对数据库对象的访问。
某些RDBMS可用GRANT或REVOKE控制对表单个列的访问。
五、数据定义语言其语句包括动词CREATE和DROP。
在数据库中创建新表或删除表(CREAT TABLE 或DROP TABLE);为表加入索引等。
DDL包括许多与人数据库目录中获得数据有关的保留字。
它也是动作查询的一部分。
六、指针控制语言它的语句,像DECLARE CURSOR,FETCH INTO和UPDATE WHERE CURRENT用于对一个或多个表单独行的操作。
全国计算机等级考试四级教程——数据库原理(2013版)2015年4月9日录入目录第1章数据库原理概述............................................................................................................ - 7 -1.1 数据库技术基本概念................................................................................................... - 7 -1.1.1 信息、数据与数据处理.................................................................................... - 7 -1.1.2 数据库、数据字典、数据库管理系统、数据库系统 .................................... - 7 -1.1.3 数据库系统中的人员........................................................................................ - 8 -1.2 数据管理技术的产生与发展....................................................................................... - 8 -1.3 数据库方法与文件系统方法....................................................................................... - 9 -1.3.1 数据库库方法的优势........................................................................................ - 9 -1.3.2 数据库与文件系统............................................................................................ - 9 -1.4 数据库应用简史........................................................................................................... - 9 -1.5 数据库技术的研究领域............................................................................................. - 10 -第2章数据模型和数据库系统的模式结构.......................................................................... - 10 -2.1 数据模型和数据模型的组成要素............................................................................. - 10 -2.1.1 数据模型的概念.............................................................................................. - 10 -2.1.2 数据模型组成的要素...................................................................................... - 10 -2.2 数据模型的分类......................................................................................................... - 10 -2.3 概念数据库模型——E-R模型 ................................................................................. - 11 -2.3.1 概念数据模型的产生和基本概念.................................................................. - 11 -2.3.2 概念模型的一种表示方法——E-R图........................................................... - 11 -2.4 常用的逻辑数据模型................................................................................................. - 11 -2.4.1 层次和网状模型.............................................................................................. - 11 -2.4.2 关系模型.......................................................................................................... - 11 -2.4.3 面向对象模型.................................................................................................. - 12 -2.4.4 对象-关系数据模型 ........................................................................................ - 12 -2.5 数据库系统的模式..................................................................................................... - 12 -2.5.1 数据库系统中模式、实例和数据状态的概念.............................................. - 12 -2.5.2 数据库系统的三级模式结构.......................................................................... - 12 -2.5.3 数据库的两层映像与数据独立性.................................................................. - 13 -第3章关系数据模型和关系数据库系统.............................................................................. - 13 -3.1 关系数据库系统概述................................................................................................. - 13 -3.1.2 关系数据模型.................................................................................................. - 13 -3.2 关系模型的数据结构................................................................................................. - 14 -3.2.1 关系模型的数据结构和基本术语.................................................................. - 14 -3.2.2 关系的形式定义和关系数据库对关系的限定.............................................. - 14 -3.2.3 关系数据库中常用的表示方法...................................................................... - 15 -3.3 关系模型的完整性约束............................................................................................. - 15 -3.3.1 关系模型完整性约束的分类.......................................................................... - 15 -3.3.2 实体完整性约束.............................................................................................. - 15 -3.3.3 参照完整性约束.............................................................................................. - 15 -3.3.4 用户定义完整性约束...................................................................................... - 16 -3.3.5 关系模型完整性约束的检查.......................................................................... - 16 -3.4 关系操作语言——关系代数..................................................................................... - 16 -3.4.2 基于传统集合论的关系操作.......................................................................... - 17 -3.4.3 一元专门关系操作.......................................................................................... - 17 -3.4.4 二元的专门关系操作...................................................................................... - 17 -3.4.5 扩展关系操作.................................................................................................. - 17 -第4章关系数据库标准语言SQL(一)............................................................................... - 18 -4.1 SQL概述..................................................................................................................... - 18 -4.1.1 结构化查询语言SQL..................................................................................... - 18 -4.1.2 SQL语言的特点............................................................................................. - 18 -4.1.3 SQL的数据类型............................................................................................. - 19 -4.1.4 SQL对关系数据库模..................................................................................... - 19 -4.1.5 SQL语言的组成和语句类型......................................................................... - 20 -4.2 SQL的数据定义......................................................................................................... - 21 -4.2.1 模式的定义和删除........................................................................................ - 21 -4.2.2 基本表的定义、删除和修改........................................................................ - 21 -4.2.3 索引的建立和删除........................................................................................ - 22 -4.2.4 域的建立和删除............................................................................................ - 22 -4.3 SQL的数据查询......................................................................................................... - 23 -4.3.1 简单查询........................................................................................................ - 23 -4.3.2 连接查询........................................................................................................ - 24 -4.3.3 嵌套查询........................................................................................................ - 24 -4.3.4 集合查询........................................................................................................ - 25 -4.3.5 SQL中的连接表和外连接............................................................................. - 25 -4.4 SQL的数据修改......................................................................................................... - 26 -4.4.1 插入数据........................................................................................................ - 26 -4.4.2 删除数据........................................................................................................ - 26 -4.4.3 更新数据........................................................................................................ - 26 -4.5 SQL的数据控制......................................................................................................... - 26 -4.5.1 权限授予........................................................................................................ - 26 -4.5.2 权限收回........................................................................................................ - 27 -第5章关系数据库标准语言SQL(二)............................................................................... - 27 -5.1 SQL视图..................................................................................................................... - 27 -5.1.1 视图的概念和定义........................................................................................ - 27 -5.1.2 视图的查询.................................................................................................... - 28 -5.1.3 视图的修改.................................................................................................... - 28 -5.1.4 视图的作用.................................................................................................... - 28 -5.2 数据库程序设计........................................................................................................ - 28 -5.2.1 数据库程序设计概述.................................................................................... - 28 -5.2.2 数据库程序设计方法.................................................................................... - 29 -5.2.3 数据库程序设计中的交互序列.................................................................... - 29 -5.3 SQL的存储过程与SQL/PSM...................................................................................... - 29 -5.3.1 SQL存储过程与函数概述............................................................................. - 29 -5.3.2 SQL/PSM.......................................................................................................... - 29 -5.3.3 创建、执行和删除存储过程和函数............................................................ - 30 -5.4 SQL触发器................................................................................................................. - 32 -5.4.1 触发器概述.................................................................................................... - 32 -5.4.2 创建触发器.................................................................................................... - 32 -5.4.3 触发器的激活................................................................................................ - 33 -5.4.4 删除触发器.................................................................................................... - 33 -5.4.5 触发器示例.................................................................................................... - 33 -5.5 嵌入式SQL................................................................................................................. - 33 -5.5.1 嵌入式SQL概述............................................................................................ - 33 -5.5.2 使用嵌入式SQL时应解决三个问题............................................................ - 34 -5.5.3 嵌入式SQL程序的组成................................................................................ - 34 -5.5.4 在嵌入式SQL中使用游标检索多个元组.................................................... - 35 -5.6 动态SQL..................................................................................................................... - 35 -5.6.1 动态SQL的概念和作用................................................................................ - 35 -5.6.2 动态SQL的语句类型和执行方式................................................................ - 35 -5.7 SQL的其他功能......................................................................................................... - 36 -第6章关系数据库的规范化理论与数据库设计.................................................................. - 36 -6.1 “不好”的关系模式中存在问题............................................................................ - 36 -6.2 函数依赖.................................................................................................................... - 37 -6.2.1 函数依赖的定义............................................................................................ - 37 -6.2.2 函数依赖的逻辑蕴涵.................................................................................... - 37 -6.2.3 码.................................................................................................................... - 37 -6.2.4 函数依赖的公理系统.................................................................................... - 37 -6.3 1NF、2NF、3NF和BCNF............................................................................................ - 38 -6.3.1 1NF及进一步规范化..................................................................................... - 38 -6.3.2 2NF.................................................................................................................. - 38 -6.3.3 3NF.................................................................................................................. - 38 -6.3.4 BCNF................................................................................................................ - 38 -6.4 多值依赖和4NF......................................................................................................... - 39 -6.5 关系模式的分解........................................................................................................ - 39 -6.5.1 模式分解的等价标准.................................................................................... - 40 -6.5.2 关于模式分解的几个事实............................................................................ - 40 -6.6 数据库设计................................................................................................................ - 40 -6.6.1 设计过程概览................................................................................................ - 40 -6.6.2 概念结构设计................................................................................................ - 40 -6.6.3 逻辑结构设计................................................................................................ - 41 -6.6.4 物理结构设计................................................................................................ - 41 -6.7 规范化理论在数据库设计中的应用........................................................................ - 41 -第7章数据库系统实现技术.................................................................................................. - 42 -7.1 数据库管理系统概述................................................................................................ - 42 -7.1.1 数据库管理系统的基本功能................................................................................ - 42 -7.1.2 数据库管理系统的主要成分和工作流程............................................................ - 42 -7.2 存储管理.................................................................................................................... - 42 -7.2.1 物理存储介质简介........................................................................................ - 42 -7.2.3 缓冲区管理.................................................................................................... - 43 -7.2.4 数据字典........................................................................................................ - 43 -7.2.5 索引结构........................................................................................................ - 43 -7.3 查询处理.................................................................................................................... - 44 -7.3.1 查询处理概述................................................................................................ - 44 -7.3.2 查询执行........................................................................................................ - 44 -7.3.3 查询优化........................................................................................................ - 44 -7.4 事务管理.................................................................................................................... - 45 -7.4.2 故障恢复........................................................................................................ - 45 -7.4.3 并发控制........................................................................................................ - 46 -第8章数据库系统的体系结构和安全性.............................................................................. - 46 -8.1 概述............................................................................................................................ - 46 -8.2 分布式数据库系统体系结构.................................................................................... - 46 -8.2.1 分布式数据库系统的主要特点.................................................................... - 46 -8.2.2 分布式数据库的设计技术............................................................................ - 47 -8.2.3 分布式数据库中的查询处理........................................................................ - 47 -8.2.4 分布式数据库系统的并发控制.................................................................... - 47 -8.2.5 分布式恢复.................................................................................................... - 48 -8.3 客户机/服务器数据库体系结构...................................................................... - 48 -8.3.1 客户机/服务器计算模式.............................................................................. - 48 -8.3.2 面向WEB应用的数据库体系结构................................................................ - 48 -8.3.3 面向WEB应用的软件开发............................................................................ - 48 -8.4 安全数据库以及数据库的安全性............................................................................ - 49 -8.4.1 数据安全性问题概述.................................................................................... - 49 -8.4.2 基于授予收回权限的自主访问控制............................................................ - 50 -8.4.3 多级安全性的强制访问控制........................................................................ - 50 -8.4.4 基于角色访问控制........................................................................................ - 50 -8.4.5 统计数据库的安全性.................................................................................... - 50 -8.4.6 加密................................................................................................................ - 51 -8.4.7 可信计算机系统评估标准............................................................................ - 51 -第9章数据库技术的发展...................................................................................................... - 51 -9.1 对象及对象-关系数据库.......................................................................................... - 51 -9.1.1 面向对象数据库基本概念............................................................................ - 51 -9.1.2 对象数据库标准、语言和设计.................................................................... - 52 -9.1.3 对象-关系数据库模型.................................................................................. - 52 -9.2 几种应用数据库系统................................................................................................ - 52 -9.2.1 主动数据库.................................................................................................... - 52 -9.2.2 移动数据库.................................................................................................... - 52 -9.2.3 多媒体数据库................................................................................................ - 52 -9.3 数据仓库.................................................................................................................... - 52 -9.3.1 数据仓库的基本概念.................................................................................... - 53 -9.3.2 数据仓库的数据模型.................................................................................... - 53 -9.3.3 数据仓库体系结构........................................................................................ - 53 -9.4 数据挖掘.................................................................................................................... - 53 -9.4.1 知识发现与数据挖掘.................................................................................... - 54 -9.4.2 关联分析........................................................................................................ - 54 -9.4.3 分类和聚类.................................................................................................... - 54 -第1章数据库原理概述1.1 数据库技术基本概念1.1.1 信息、数据与数据处理1.信息现实世界事物的存在方式或运动状态的反映。
结构化查询语言(Structured query language)The third chapter is structured query language --SQLSQL (Structured Query Language) - Structured Query Language3.1 definition of the basic table and insert dataThe system structure of 3.1.1 SQL database1. basic table (Base Table): the independent existence of the table itself, which is actually stored in the database table rather than from the other table out of the guide.2. Views: (View) derived from one or several basic table or viewa few tables. The view itself is not independent of stored data, save only the view definition system.Database architecture -- storage mode supported by SQL (internal model), model, model.From the user's perspective, the basic tables and views are, like the SQL to access them. The basic table corresponds to "model", "view corresponding mode".3.1.2 defines the basic tableThe definition of the basic form is to create a basic table, table name (name) and it includes all the attribute name and data type specific provisions.Command format:CREATE TABLE (table name field name 1 types (width, decimal), 2 types of field name (width, decimal), all of all)Command function: for the establishment of a basic table.For example: CARTE TABLE books (the total number of C (6), C (8), the classification of the title C (16), C (6), at N (10,2))3.1.3 modify and delete the basic tableALTER TABLE ADD (author of C book publishing unit (8), C (20))In the "book" database structure to add "author" and "publishing unit" two fields.The basic DROP TABLE < table name > delete the basic table.3.1.4 insert dataCommand format:INSERT INTO (field name table name [1, 2] of all field names) of all VALUES (expression 1, expression of the 2 MVP MVP)Command function: according to the given field value added a new record in the database at the end.For example: INSERT INTO VALUES ("446943 books", "TP31/138", "database", "Yang Hua", 17.8)INSTER INTO book (title, author, unit) VALUES ("FoxPro encyclopedia", "Zhou Hong", 28.6)3.2 SQL --SQL-SELECT querySQL query can easily retrieve data from one or more tables in the query is highly non procedural, users only need to explicitly put forward the "what to do", and does not need to be pointed out that "how to do".The basic structure of SQL query module:SELECT < < 1> expression, 2> expression of all expressions < n>; query target (projection operation on the required properties)FROM < 1>, < < 2>, all m>; query source (name all relations)< > - WHERE conditional expression query target must meet the conditions (selection operation)The choice of operation (conditional expression) need to use the operator:1. comparison operators: P492. logical operators: AND, OR logic and NOT logic, logic or non.3.: all ALL, ANY arbitrary predicates, BETWEEN... AND... Between IN, NOT, contains the IN does not contain EXISTS, NOT, EXISTS does not exist.FourSet operations: and, INTERSECT set, MINUS set UNION set difference.Three basic tables including library management relational data model (database):The book (the total number, classification number, title, author, publisher, price)The reader (card number, name, unit, gender, title, address)Borrow (card number, serial number, date Library)(P50 page contains)3.2.1 simple queryExample: find the reader name Lee and his unit.SELECT name, unit;FROM reader;WHERE name = "li"For example: SELECT *;FROM readerThe 1.DISTINCT and ALL clausesThe DISTINCT clause: remove duplicate tuples from the query results.The ALL clause: do not remove duplicate tuples (default value)For example: SELECT DIST title, publisher;FROM book2. AS with the specified query result of custom columnFor example: SELECT AS Book AS Author title, author, publisher AS Publisher;FROM book;WHERE = "Science Press Publishing unit"The 3.ORDER BY clauseThe ORDER BY clause can be pointed out to sort the query results. The specified sort key field name with the column number or the query results. DESC said ASC said the descending, ascending. The system default is ascending. Allow multiple sorting.For example: SELECT title, publishing price;FROM book;WHERE = "higher education press publishing unit";ORDER BY at DESC4.BETWEEN... AND... NOT and BETWEEN... AND... (the use of predicate in the WHERE clause)For example: SELECT DIST title, author, publisher, price;FROM book;WHERE at BETWEEN 10 AND 20 (>=10 AND; unit price <=20)ORDER BY publishing unit, DESC unit5. predicate INIn the WHERE clause, the condition can be expressed in IN contained in the collection specified in parentheses after the. The elements in the brackets can be directly listed, can also be a sub query query result.For example: SELECT DIST title, author, publisher;FROM book;WHERE IN publishing unit ("higher education press", "Science Press")Equivalent statement: WHERE publishing unit = "highereducation press" OR = "Science Press Publishing unit"6.LIKE and wildcard "-" and "%"Underline represents an arbitrary character,% represent any number (including zero) any character.For example: SELECT DIST title, author;FROM book;The title "WHERE LIKE computer%" (computer based computer network)For example: SELECT DIST title, author;FROM book;WHERE Title LIKE "% basis%" (computer based database based tutorials)7. for the specified temporary aliasSome queries related to the same database file retrieval two times or more database query, it is necessary to introduce aliases.The user can customize the temporary alias given directly in the FROM clause, and in the SELECT and WHERE clauses with the alias restrictions on the field.At the same time, the total lending cases: the query number is "112266" and "449901" two book library card number.SELECT library card number;FROM library;The total number of WHERE = "112266" AND total number = 449901"The above statement of the query result is empty, because there can be a total number is "449901" and "112266" borrowing records. So here need to use temporary alias table.SELECT X. library card number, the total number of X. AS First Y. AS Second, the total number of;FROM borrow X borrow Y;WHERE X. library card number = Y. card number;The total number of AND X. = 112266";The total number of AND Y. = 449901"Here the "borrowing" cited two times, an alias for X, another is Y, which is equivalent to two from the database query.3.2.2 join queryWhen the query relates to two or more, to join operation. As in the FROM clause that names of the various relations, pointedout that the join conditions can be correct in WHERE clauses.If you have the same property names in different relations, to avoid confusion must be preceded by the alias dots separately, if not an alias is available.Example: find all borrowed name and unit book readers. SELECT DIST name, unit;FROM readers borrow;WHERE reader. Library card number = lending library card number.The output columns in the SELECT clause in the SELECT clause if adding string constants, in each query will output the string output tuple.For example: to find Lee borrowed the names of all the books and borrowing date.The SELECT name, "the book", the title of the book, borrow date; FROM book X, Y Z library, reader;WHERE Y. library card number =Z. library card number;AND X. total number =Y. total number AND name = "li"For example: search has lent 22 yuan more than the price of books,the price according to the ascending order.SELECT *; &&* represents books and borrow all the properties of two relations after connection.FROM borrow books;The total number of borrowing books. WHERE = total number AND at >=22;ORDER BY priceNote: in the query output, the system of the same field in two databases (the total number) automatically with -A and -B.3.2.3 nested queryNested query refers to query block embedding another query block in SELECT-FROM-WHERE (subquery). Note that ORDER can't have a BY clause in a subquery.1. a sub query module query results as a list contains IN.As the example above the following statement: (already available instead of lending to find the price of 22 yuan or more books)* SELECT;FROM library;The total number of IN WHERE;SELECT (total number;FROM book;WHERE at >=22)Example: no library readers of the library card number, name and unit inquiry after July 1997.SELECT library card number, name, unit;FROM reader;WHERE library card number NOT IN;(SELECT library card number;FROM library;WHERE library >={07/01/97} date)2.ALL and ANY and SOMEIn the WHERE clause, ALL said the corresponding values of all records and sub query results compared to meet the requirements to meet the conditions, while ANY or SOME compared with the sub query results, any record of the conditions are satisfied.For example: to find the books in book price than all the highereducation press more books.SELEC and *;FROM book;WHERE at >ALL;(SELECT price;FROM book;WHERE publisher = "higher education press")Example: find all the books and "database introduction" or "database" published in the same book publishing unit.SELECT DIST's title, author, publisher;FROM book;WHERE publishing unit =ANY;(SELECT publishing unit;FROM book;WHERE IN (the title "database introduction", "database"))The use of 3.2.4 library function (statistical query function)Counting function COUNT (< fieldname >) statistical field name column where the number of rows.COUNT general use (*) indicates that the calculated results for the number of tuples, i.e..The sum function (SUM < fieldname >) to a column value (and must be numeric fields)Calculate the average value of AVG (< fieldname >) for a list of values (must be numeric fields)The maximum value of MAX (< field >) to find the maximum value in a columnFor the minimum MIN (< fieldname >) to find the minimum value in a columnNote: in the use of library function query, select AS specify the columns is particularly useful.Example: the total number of books for the library of all the books.SELECT COUNT (*) AS collection volumes;FROM bookThe highest price, for example: Science Press published the book the lowest price, average price.SELECT publishing unit, MAX (price) AS high, MIX (price) the lowest price AS;AVG (price) AS average price;FROM book;WHERE = "Science Press Publishing unit"For example: people readers borrow books from the current information system.SELECT "information system", COUNT (library card number) AS number of borrowers;FROM library;WHERE library card number IN;(SELECT library card number;FROM reader;WHERE = "information system")Results: EXP-1 number of borrowersThe Information Department of the 3* GROUP BY clause grouping togetherThe GROUP BY clause is the role of grouping on record according to the specified items, then each group respectively using the library function.The project is usually grouped field, this field should appear in the query results, or unclear statistics belongs to which group.For example: for the highest price, the lowest price of the publishing of books and books.SELECT publishing unit, MAX (price) AS high, MIX (price) the lowest price AS;COUNT (*) AS volumes;FROM book;GROUP BY publishing unitNote: in this case, if there is no GROUP BY clause, then the statistical results is the entire book table, with "GROUP BY publishing unit" clause, we can calculate the publishing unit of the data.For example: each unit of current people to borrow books.SELECT COUNT (*) AS borrowing people;FROM readers borrow;WHERE reader. Library card number = lending library card number;GROUP BY reader unit.* HAVING clauseThe HAVING clause with GROUP after the BY clause, its role is to define packet retrieval conditions, conditions generally include library functions. (in the WHERE clause cannot be used directly in the library as a function of the conditional expression)Example: find units and the number of passengers over 1 books. (adding a HAVING clause than the example)SELECT unit, COUNT (*) AS more than 1 people;FROM library, reader;WHERE reader. Library card number = lending library card number;GROUP BY reader unit;HAVING COUNT (*) >=2EXISTS and NOT ESISTS * existential quantifierIn the nested query, the WHERE clause of the main query conditions can be used to indicate the presence of EXISTS. Ifthe query result is not empty, then meet the conditions of NOT EXISTS; on the contrary, that does not exist, if the query result is empty, then meet the conditions.For example: the economic system is to pay off all the library query. If the pay off, display all readers name, unit and title.SELECT unit, name, title;FROM reader;WHERE = economic system AND NOT EXISTS;(SELECT *;FROM library, reader;WHERE reader. Library card number = library. Library card number AND = "economic system")Note: if the subquery is not empty, indicating that the Department has not repaid all the books, then the condition is not satisfied, do not show the readers the name, unit and title. If the subquery is empty, the book has paid off, then show the readers the name, unit and title.The 3.3 part of the SQL data manipulation commandsThe updated data command - modify dataCommand format: UPDATE < table name >;SET < > update expressions;[WHERE < >] conditionsThe total number of 554433 cases: modify the name of the author and book publishing units.UPDATE book;SET = "wangweimin" = "electronic industry press publishing unit";The total number of WHERE = 554433"Delete the data commandCommand format: DELETE;FROM < table name >;< condition > WHEREFor example: the library card number "112" the total number is "446988" the book has been returned, delete the borrowing records.DELETE;FROM library;WHERE library card number = "112" AND total number = 446988""The basis and application of the third chapter" database structured query language --SQL first page 6。
第4章结构化查询语言—SQLSQL语言之所以能够为用户和业界所接受,并成为国际标准,是因为它是一个综合的、功能极强同时又简捷易学的语言。
SQL语言集数据查询、数据操纵、数据定义和数据控制功能于一体,主要特点包括:1.综合统一2.高度非过程化3.面向集合的操作方式4.以同一种语法结构提供两种使用方式SQL语言既是自含式语言,又是嵌入式语言。
自含和嵌入的含义。
4.1数据库模式的建立和删除4.1.1建立数据库模式建立一个新数据库create database 数据库名4.1.2删除数据库模式撤消一个数据库drop database 数据库名4.2 表结构的建立、修改和删除4.1建立表结构SQL语言使用CREATE TABLE语句定义基本表,其一般格式如下:CREATE TABLE <表名> (<列名><数据类型>[列级完整性约束] [,<列名><数据类型>[列级完整性约束]]…[,<表级完整性约束>]);如果完整性约束条件涉及到该表的多个属性列,则必须定义在表级上,否则既可以定义在列级也可以定义在表级。
注意:定义表时每一个定义语句之间用逗号分隔,最后一条语句不用逗号。
每个SQL语句以分号结束。
例:CREATE TABLE C(cno char(4) not null,cname char(10) not null,PRIMARY KEY(CNO) );CREATE TABLE SC(SNO CHAR(4) NOT NULL,CNO CHAR(4) NOT NULL,GRADE SMALLINT,PRIMARY KEY(SNO,CNO) ,FOREIGN KEY(SNO) REFERENCES S(SNO) ,FOREIGN KEY(CNO) REFERENCES C(CNO) ) ;4.2.2修改表结构修改基本表SQL语言用ALTER TABLE语句修改基本表,其一般格式为:ALTER TABLE <表名>[ADD <新列名><数据类型>[完整性约束]][DROP <完整性约束名>][MODIFY <列名><数据类型>];ADD子句用于增加新列和新的完整性约束条件,DROP子句用于删除制定的完整性约束条件,MODIFY子句用于修改原有的列定义,包括修改列名和数据类型。
简单介绍教材上的例子。
4.2.3删除表结构当某个基本表不再需要时,可以用DROP TABLE删除,其格式为:DROP TABLE <表名>基本表一旦删除,表中的数据、此表上建立的索引和视图都将自动被删除掉。
因此执行删除基本表的操作一定要格外小心。
4.3 表内容的插入、修改和删除4.3.1插入记录命令insert into 表名[(列名[,列名]…]values (值[,值]…)插入一条指定好值的元组insert into 表名[(列名[,列名]…](子查询)插入子查询结果中的若干条元组•示例–insert into PROFvalues ( P123, “王明”, 35, D08, 498 )–insert into PROF (PNO, PNAME, DNO)values ( P123, “王明”, D08 )4.3.2修改操作•命令update 表名set 列名= 表达式| 子查询列名= [,表达式| 子查询]…[where 条件表达式]指定对哪些列进行更新,以及更新后的值是什么•示例–老师工资上调5%update PROFset SAL = SAL * 1.054.3.3删除记录命令delete from表名[where条件表达式]从表中删除符合条件的元组,如果没有where语句,则删除所有元组•示例–清除所有选课记录delete from SC–删除王明老师所有的任课记录delete from PCwhere PNO in(select PNOfrom PROFwhere PNAME = “王明”)4.4 视图的建立、修改和删除4.4.1建立视图•定义视图create view view_name[(列名[,列名] …)]as (查询表达式)[with check option]视图的属性名缺省为子查询结果中的属性名,也可以显式指明with check option指明当对视图进行insert,update时,要检查是否满足视图定义中的条件4.4.2修改视图内容UPDATE [<数据库名>.<所有者名>.]<视图名>SET<列名>=<表达式>,...[FROM<源表名>,...][WHERE<逻辑表达式>]4.4.2修改视图定义ALTER VIEW <视图名>(<列名>,...)AS <SELECT子句>4.4.4删除视图•撤消视图drop view view_name4.5 SQL查询4.5.1 SELECT语句格式SELECT [ALL|DISTINCT] <目标列表达式>[,<目标列表达式>]…FROM<表名或视图名>[,<表名或视图名>]…[WHERE <条件表达式>][GROUP BY <列名1> [HAVING <条件表达式>]][ORDER BY <列名2> [ASC|DESC]];整个SELECT语句的含义是,根据WHERE子句的条件表达式,从FROM子句指定的基本表或视图中找出满足条件的元组,再按SELECT 子句中的目标列表达式,选出元组中的属性值形成结果表。
如果有GROUP子句,则将结果按<列名1>的值进行分组,该属性列值相等的元组为一组。
通常会在每组中作用集函数。
如果GROUP子句带HAVING 短语,则只有满足指定条件的组才予输出。
如果有ORDER BY子句,则结果表还要按<列名2>的值的升序或降序排序。
4.5.2 SELECT选项•目标列形式可以为列名,* ,算术表达式,聚集函数–“*”:表示“所有的属性”给出所有老师的信息SELECT*FROM P ROF–带+,-,*,/的算术表达式给出所有老师的姓名及税后工资额SELECT PNAME,SAL 0.95FROM PROF•语法约束缺省为保留重复元组,也可用关键字ALL显式指明。
若要去掉重复元组,可用关键字DISTINCT或UNIQUE指明•示例找出所有选修课程的学生SELECT DISTINCT SNOFROM SC4.5.3 FROM 选项•说明–FROM子句列出查询的对象表–当目标列取自多个表时,在不混淆的情况下可以不用显式指明来自哪个关系•示例–例:找出工资低于500的职工的姓名、工资、系别SELECT PNAME , SAL , DNAMEFROM P ROF , DEPTWHERE SAL < 500AND PROF.DNO = DEPT.DNO–例:列出教授“哲学”课程的老师的教工号及姓名SELECT PROF. PNO , PNAMEFROM PROF , PC , COURSEWHERE PROF.PNO = PC.PNOAND O = OAND AME = “哲学”4.5.4 WHERE选项语法成分-比较运算符<、<=、>、>=、=、<>-逻辑运算符AND,OR,NOT-BETWEEN条件判断表达式的值是否在某范围内•示例–列出工资在500~800之间的老师姓名SELECT PNAMEFROM PROFWHERE SAL BETWEEN 500 AND 8004.5.5BROUP BY 选项•命令ORDER BY 列名[ASC | DESC]•示例–按系名升序列出老师姓名,所在系名,同一系中老师按姓名降序排列SELECT DNAME,PNAMEFROM PROF,DEPTWHERE PROF.DNO = DEPT.DNOORDER BY DNAME ASC,PNAME DESC4.5.6HAVING 选项分组命令GROUP BY 列名[HAVING 条件表达式]GROUP BY将表中的元组按指定列上值相等的原则分组,然后在每一分组上使用聚集函数,得到单一值HAVING则对分组进行选择,只将聚集函数作用到满足条件的分组上4.5.7 ORDER BY 选项[ORDER BY <列名> [ASC|DESC]];4.5.8 INTO 选项SELECT语句中使用INTO选项可以将查询结果写进新表,新表结构与SELECT语句选择列表中的字段相同。
例如:SELECT FIELDS AS KEYSTRING,COUNT(FIELDS) AS HITCOUNTINTO #TEMP/*INTO*/FROM ENGLISHBOOKS..KEYS WHERE( KEYS LIKE '%鲁迅%' )ANDFRM LIKE '24_*A'AND (( MARC_ID >= '0000000001' AND MARC_ID <= '9999999999' ))GROUP BY FIELDS通过子句INTO #TEMP SQL SERVER将SELECT查询结果和COUNT的统计结果写入临时表TEMP中,临时表TEMP结构包含KEYSTRING和HITCOUNT两个字段。
在以后用到这些信息时,就可以从TEMP表中读取。