数据库技术(SQL语言)课件.ppt
,数据库技术,第 5 讲 SQL 语言,数据,DBMS,Client,请求,回答,SQL 语言,Oracle 8i/9iSQL Server 2000,PowerBuilderVBDelphiC+Builder,SQL Server 2000简介 2000年2月 Microsoft 公司发布了Windows 2000 操作系统家族。Windows 2000 Professional Windows 2000 Server Windows 2000 Advanced Server Windows 2000 Datacenter Server SQL Server 2000 客户端软件 SQL Server 2000 个人版 SQL Server 2000 标准版 SQL Server 2000 企业版,如何使用SQL Server 2000?,1、利用企业管理器(Enterprise Manager)建立数据库、用户,并且对用户授权;2、利用查询分析器(Query Analyzer)对SQLServer进行查询、管理。,1、SQL语言概述 SQL(Structured Query language)语言是1974年由Boyce和Chamberlin提出的。1975年1979年IBM公司San Jose Research Laboratory 研制了著名的关系数据库管理系统原型System R,并实现了这种语言。由于它功能丰富、语言简捷,倍受用户及计算机工业界欢迎,被众多计算机公司和软件公司所采用。经各公司的不断修改、扩充和完善,SQL语言最终发展成为关系数据库的标准语言。,1986年10月美国国家标准局(American National Standard Institute,简称ANSI)的数据库委员会X3H2批准了SQL作为关系数据库语言的美国标准。同年公布了SQL标准文本(简称SQL-86)。1987年国际标准化组织(International Organization for standardization,简称ISO)也通过了这一标准。此后ANSI不断修改和完善SQL标准,并于1989年公布了SQL-89标准。1992年又公布了SQL-92标准。1999年又公布了SQL-99标准。,SQL-99是SQL-92的一个超集。许多数据库系统支持SQL-99的新结构,但目前还没有一个数据库系统完全支持SQL-99的所有新结构。实际上甚至目前的数据库系统不能完全支持SQL-92。,自SQL成为国际标准语言以后,各个数据库厂家纷纷推出各自的SQL软件或与SQL的接口软件。这就使大多数数据库系统均用SQL作为共同的数据存取语言和标准接口,使不同数据库系统之间的互操作有了共同的基础。这个意义十分重大。因此,有人把确立SQL为关系数据库语言标准及其后的发展称为是一场革命。,SQL成为国际标准,对数据库以外的领域也产生了很大影响,有不少软件产品将SQL语言的数据查询功能与图形功能、软件工程工具、软件开发工具、人工智能程序结合起来。SQL已成为数据库领域中一个主流语言。,2、SQL语言的特点 集DDL、DML、DCL于一体;高度非过程化;面向集合的操作方式;一种语法提供两种操作方式(交互式、嵌入式)。,嵌入式SQL SQL是一种强有力的声明性查询语言,它在许多情况下比通用编程语言的编码简单许多。但是使用通用编程语言(至少具有过程化语言的特性)访问数据库还是很必要的,原因有二:1、SQL不能表达所有查询要求。2、非声明性的动作(例如,打印一份报告或把一次查询结果送到一个图形用户界面中)都不能用SQL实现。,动态SQL SQL的动态SQL组件允许程序在运行时构造、提交SQL查询。使用动态SQL,程序可以在运行时以字符串的形式生成SQL查询。,数据类型(SQL-92)char(n):固定长度的字符串。varchar(n):可变长字符串。int:整数。smallint:小整数类型。numeric(p,d):定点数,精度p位,小数点右边d位。real:浮点数。double precision:双精度浮点数。date:日期(年、月、日)。time:时间(小时、分、秒)。interval:两个date或time类型数据之间的差。,注意:目前各公司都在自己的DBMS上实现了对SQL语言的支持,但在语言的功能上都根据实际需要进行了扩充或简化。特别是,都增加了对过程化语句的支持功能。例如,Oracle所支持的是 PL/SQL SQL Server 2000支持的是 Transact-SQL,3、SQL语言的三级模式,SQL,View,Table,Table,Table,Datafile,4、SQL语言数据定义(DDL)Create table Drop table Create View Drop View Create Index Drop Index 数据操纵(DML)Select Update Insert Delete数据控制(DCL)Grant Revoke,SQL语言举例create table Teacher(Tno integer Primary Key,Tname char(6)not null,Title char(6),Dept char(10);,insert into Teachervalues(101,李华,讲师,计算机);insert into Teacher(Tno,Tname,Dept)values(104,李春生,计算机);,select*from teacherwhere dept=通信;select Tname,Dept from teacherwhere dept=通信;select*from teacherwhere dept=通信 and Title=讲师;,update teacherset dept=通信工程where dept=通信;delete from teacher where dept=计算机;,create table Teacher(Tno integer Primary Key,Tname char(6)not null,Title char(6),Dept char(10);,create Table Course(Cno integer not null,Tno integer not null,Cname char(10)not null,credit numeric(3,1)not null,Primary key(cno,tno);,insert into Coursevalues(1,101,数据库,3.5);insert into Coursevalues(1,103,数据库,3.5);insert into Coursevalues(2,102,网络,3);insert into Coursevalues(2,101,网络,3);insert into Coursevalues(3,103,操作系统,3);,select distinct cname from course;select*from course where credit 3;select*from course where credit between 2 and 3;,create Table Course(Cno integer not null,Tno integer not null,Cname char(10)not null,credit numeric(3,1)not null,Primary key(cno,tno);,select*from teacher where dept in(计算机,自动化);select*from teacher where dept not in(计算机);select*from teacher where tname like 李%;select*from teacher where title is null;select*from teacher order by tno desc;select*from teacher order by title;select count(*)from teacher;select count(distinct cname)from course;,数据库技术,第 6 讲 SQL 语言(二),数据,DBMS,Client,请求,回答,SQL 语言,Oracle 8i/9iSQL Server 2000,PowerBuilderVBDelphiC+Builder,create Table Course(Cno integer not null,Tno integer not null,Cname char(10)not null,credit numeric(3,1)not null,Primary key(cno,tno);,create table Teacher(Tno integer Primary Key,Tname char(6)not null,Title char(6),Dept char(10);,select*from teacher,course;select*from teacher,coursewhere teacher.tno=course.tno;,select cname,credit from course where Tno in(select Tno from Teacher where Tname=李华);select cname,credit from teacher,course where(teacher.tno=course.tno)and Tname=李华;,查询李华所授课程的名称,学分,select cname,credit from course where Tno in(select Tno from Teacher where Title=讲师);select cname,credit from teacher,course where(teacher.tno=course.tno)and Title=讲师;,视图(View)create view v_t_c as select Teacher.Tno,Tname,Title,Dept,Cno,Cname from Teacher,course where Teacher.Tno=course.Tno;Select*from v_t_c;Select*from v_t_c where Tno=101;,索引create Table Course(Cno integer not null,Tno integer not null,Cname char(10)not null,credit numeric(3,1)not null);create unique index course_ind on course(Tno,Cno);,create Table Course(Cno integer not null,Tno integer not null,Cname char(10)not null,credit numeric(3,1)not null,Primary key(cno,tno);,Group by 和 Having 子句,create table Teacher(Tno integer Primary Key,Tname char(6)not null,Title char(6),Dept char(10);,select*from teacher;select title,count(*)from teacher group by title;select title,count(*)from teacher group by title having count(*)1;,认识 nullinsert into Teachervalues(903,刘力伟,助教,计算机);insert into Teachervalues(904,赵莺,null,计算机);insert into Teachervalues(905,张大军,null,null);,create table Teacher(Tno integer Primary Key,Tname char(6)not null,Title char(6),Dept char(10);,外键create table father_t(Cno integer primary key,Cname char(10)not null,Credit numeric(3,1);,create table son_t(st_no integer primary key,fk_cno integer,grade integer,foreign key(fk_cno)references father_t(Cno);,insert into father_tvalues(1,数据库,2);insert into father_tvalues(2,网络,3);,insert into son_tvalues(101,2,86);insert into son_tvalues(102,5,78);,授权 Grant select on Teacher to st2;Revoke select on Teacher from st2;,开发人员,用户,数据库系统的权限,DBA,管理员,游标 将SQL语言嵌入到宿主语言中出现的一个问题是失配,因为SQL是对记录的集合进行操作,而宿主语言不完全支持记录集合的操作。解决的办法就是从本质上提供一种机制支持从关系(表)中一次检索一行。这种机制称为游标,任何SQL查询都可以声明一个游标。声明游标后,就可以打开它,取出下一行,然后移动游标。这样可以通过把游标定位在特殊的行,并读取它的内容来检索表中的行。,ODBC 和 JDBC 嵌入式SQL使得SQL与通用编程语言结合起来。ODBC(open database connectivity)和JDBC(Java database connectivity)促进了SQL与通用编程语言的集成。,应用,ODBC/JDBC,DBMS,SQLJava,数据库技术,其他关系语言,数据,DBMS,Client,请求,回答,QBEDatalog,QBE(Query by Example)是一种图形化的语言,它的查询语句看上去就像是表格,是用“例子”表达的。QBE和它的变体广泛应用在个人计算机的数据库系统里(例如,Access和Excel)。Datalog的文法以Prolog语言为模型,虽然目前没有被商业应用,但它已经应用在许多研究性的数据库系统里(特别是在知识库系统)。,