T-SQL语言程序设计基础.ppt
《T-SQL语言程序设计基础.ppt》由会员分享,可在线阅读,更多相关《T-SQL语言程序设计基础.ppt(126页珍藏版)》请在三一办公上搜索。
1、Principle and Application of Database System,AnQing Teachers College Department of Computer&Information,数据库原理与应用Principle and Application of Database system,安庆师范学院计算机与信息学院,Principle and Application of Database System,10 T-SQL语言程序设计基础,(1)数据定义语言(DDL)。,Principle and Application of Database System,(2)数据
2、操纵语言(DML)。,Principle and Application of Database System,(3)数据控制语言(DCL)。,Principle and Application of Database System,10.1.1 常量、变量与数据类型,10.1.1.1 常 量,1.字符串常量,Principle and Application of Database System,2.二进制常量,二进制常量具有前辍 0 x 并且是十六进制数字字符串。这些常量不使用引号。二进制常量的示例为:0 xAE 0 x12Ef 0 x69048AEFDD010E 0 x(empty bi
3、nary string),Principle and Application of Database System,3.bit 常量bit 常量使用数字 0 或 1 表示,并且不使用引号。如果使用一个大于 1 的数字,它将被转换为 1。,Principle and Application of Database System,4.integer 常量integer 常量由没有用引号括起来且不含小数点的一串数字表示。integer 常量必须是整数,不能包含小数点。下面是一些 integer 常量的示例:1894 2,Principle and Application of Database Sy
4、stem,5.实型常量,Principle and Application of Database System,6.日期时间常量,时间格式:14:30:24 04:24 PM,日期时间型:April 20,2000 14:30:24,Principle and Application of Database System,Principle and Application of Database System,10.1.1.2 数据类型,1.系统数据类型,2.用户自定义数据类型,Principle and Application of Database System,Principle an
5、d Application of Database System,1)利用企业管理器定义,Principle and Application of Database System,Principle and Application of Database System,2)利用命令定义数据类型,语法格式,Principle and Application of Database System,sp_addtype student_no,char(5),not null,Principle and Application of Database System,10.1.1.2 数 据 类 型,3
6、.自定义数据类型的删除1)用企业管理器删除自定义数据类型,Principle and Application of Database System,10.1.1.2 数 据 类 型,2)利用命令删除自定据类型,删除student_no类型的语句为:,sp_droptype student_no,Principle and Application of Database System,10.1.1.2 数 据 类 型,4.利用自定义类型定义字段,Principle and Application of Database System,10.1.1.2 数 据 类 型,CREATE TABLE s
7、tudent(sno student_no PRIMARY KEY,sname char(8),ssex char(2),sbirthday smalldatetime,class char(5),Principle and Application of Database System,10.1.1.3 变 量,1.变 量,1)标识符,(1)常规标识符,(2)分隔标识符:用 或,2)变量的分类,(1)全局变量,(2)局部变量,由若干个中文、字母、数字、_、$、#构成,#不能开头,最多128字符。,Principle and Application of Database System,10.1
8、.1.3 变 量,2.局部变量的使用1)局部变量的定义与赋值(1)局部变量的定义,(2)局部变量的赋值,Principle and Application of Database System,10.1.1.3 变 量,例如:创建局部变量var1、var2,并赋值,然后输出变量的值。,DECLARE var1 varchar(20),var2 varchar(20)SET var1=中国SET var2=var1+是一个伟大的国家!SELECT var1,var2 GO,Principle and Application of Database System,用SELECT语句赋值SELECT
9、 local_variable=expression,n 如果SELECT语句没有返回值,变量将保留当前值 如果expression是不返回值的标量子查询,则将变量设为NULL,Principle and Application of Database System,DECLARE var1 nvarchar(30)SELECT var1=刘丰SELECT var1=sname FROM student WHERE sno=110SELECT var1 AS NAME,Principle and Application of Database System,10.1.1.3 变 量,例如:查询
10、用于给 var1 赋值。在 student表中sno不存在,因此子查询不返回值,并将变量var1设为 NULL。,DECLARE var1 nvarchar(30)SELECT var1=刘丰SELECT var1=(SELECT sname FROM student WHERE sno=110)SELECT var1 AS NAMEGO,Principle and Application of Database System,10.1.1.3 变 量,2)局部游标变量的定义与赋值(1)局部游标变量的定义,Principle and Application of Database System
11、,10.1.1.3 变 量,Principle and Application of Database System,10.1.1.3 变 量,Principle and Application of Database System,10.1.1.3 变 量,(3)游标变量的使用步骤,例如:使用游标变量,DECLARE st_CURSOR CURSORSET st_CURSOR=CURSOR SCROLL DYNAMICFORSELECT sno,sname,class FROM student,Principle and Application of Database System,10.1
12、.1.3 变 量,OPEN st_CURSORFETCH NEXT FROM st_CURSORWHILE FETCH_STATUS=0 FETCH NEXT FROM st_CURSORCLOSE st_CURSORDEALLOCATE st_CURSOR,Principle and Application of Database System,10.2 运算符与表达式,1算术运算符,+、-、*、/、%,例:DECLARE a int,b intSET a=11SET b=3SELECT a+b AS a+b,a-b AS a-b,a*b AS a*b,a/b AS a/b,a%b AS a
13、%b,Principle and Application of Database System,10.2 运算符与表达式,2.位运算符,Principle and Application of Database System,10.2 运算符与表达式,例如:在maste数据库中,建立表bitop,并插入一行,然后将a字段和 b字段上的值进行位运算。,Principle and Application of Database System,10.2 运算符与表达式,Principle and Application of Database System,10.2 运算符与表达式,Principl
14、e and Application of Database System,10.2 运算符与表达式,3.比较运算符,Principle and Application of Database System,10.2 运算符与表达式,4.逻辑运算符,Principle and Application of Database System,10.2 运算符与表达式,5.字符串联接运算符,例如:多个字符串的联接。,SELECT(sno+space(2)+sname)AS 学号 姓名 FROM student,Principle and Application of Database System,1
15、0.2 运算符与表达式,6.一元运算,7.赋值运算符,指给局部变量赋值的SET和SELECT语句中使用的“=”。,Principle and Application of Database System,10.2 运算符与表达式,8.运算符的优先顺序,Principle and Application of Database System,10.3 流程控制语句,Principle and Application of Database System,10.3.1 IF.ELSE语句,Principle and Application of Database System,10.3.1 IF.
16、ELSE语句,Principle and Application of Database System,10.3.1 IF.ELSE语句,例如:如果3-105课程的平均成绩大于80分,显示“3-105课程成绩还不错”,否则显示“3-105课程成绩一般”。,IF(SELECT AVG(degree)FROM score WHERE cno=3-105)80 PRINT 3-105课程成绩还不错ELSE PRINT 3-105课程成绩一般,Principle and Application of Database System,10.3.2 WHILE、BREAK和CONTINUE语句,1.WHI
17、LE循环语句,Principle and Application of Database System,Principle and Application of Database System,DECLARE s int,i intSET s=0SET i=1WHILE i=100BEGIN SET s=s+i SET i=i+1ENDSELECT i,s,Principle and Application of Database System,例如:显示字符串China中每个字符的 ASCII 值和字符。,DECLARE position int,string char(8)SET posi
18、tion=1SET string=ChinaWHILE position=DATALENGTH(string)BEGIN SELECT ASCII(SUBSTRING(string,position,1)SELECT SUBSTRING(string,position,1)SET position=position+1 END,Principle and Application of Database System,2.BREAK语句一般用于循环语句中,用于退出本层循环。3.CONTINUE语句一般用于循环语句中,结束本次循环,进行下一次循环条件的判断。,Principle and Appli
19、cation of Database System,10.3.3 GOTO语句,Principle and Application of Database System,DECLARE s int,i intSET s=0SET i=1loop:SET s=s+i SET i=i+1IF i=100 GOTO loopSELECT i,s,Principle and Application of Database System,10.3.4 RETURN语句,用于从过程、批处理或语句块中无条件退出,不执行位于RETURN之后的语句。,Principle and Application of D
20、atabase System,DECLARE avg floatIF NOT EXISTS(SELECT*FROM score WHERE sno=108)GOTO label1BEGIN PRINT 108学生的平均成绩:SELECT avg=AVG(degree)FROM score WHERE sno=108 PRINT avg RETURNENDlabel1:PRINT 108学生无成绩,Principle and Application of Database System,CREATE PROC mypro no char(5)ASRETURN(SELECT AVG(degree)
21、FROM score WHERE sno=no)DECLARE no char(5),avg floatSET no=108EXEC avg=mypro noSELECT no,avg,Principle and Application of Database System,10.3.5 WAITFOR语句,例如:语句设定在早上八点执行存储过程,添加角色Manager。,Principle and Application of Database System,BEGIN WAITFOR DELAY 00:00:05 EXEC sp_addrole ManagerEND,Principle an
22、d Application of Database System,10.4函数,编程语言中的函数是用于封装经常执行的逻辑的子例程。任何代码若必须执行函数所包含的逻辑,都可以调用该函数,而不必重复所有的函数逻辑。SQL Server 2000支持两种函数类型:内置函数和用户定义函数。,Principle and Application of Database System,内置函数,聚合函数 行集函数,Principle and Application of Database System,1.数学函数,Principle and Application of Database System,例
23、如:下面程序返回给定角的 ACOS 值。,Principle and Application of Database System,例如:下面程序通过 RAND 函数产生随机值。,Principle and Application of Database System,2.字符串处理函数,Principle and Application of Database System,例如:返回课程名最左边的3 个字。,SELECT LEFT(cname,3)FROM course,Principle and Application of Database System,例如:使用 LTRIM 字符删
24、除字符变量中的起始空格。,DECLARE string varchar(40)SET string=中国是一个古老而又伟大的国家!SELECT LTRIM(string),Principle and Application of Database System,例如:用 REPLACE实现字符串的替换。,DECLARE str1 char(20),str2 char(20),str3 char(20)SET str1=数据库原理SET str2=原理SET str3=应用SELECT REPLACE(str1,str2,str3),Principle and Application of Da
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SQL 语言程序设计 基础
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-6521725.html