1 / 125

AnQing Teachers College Department of Computer & Information

AnQing Teachers College Department of Computer & Information. 数据库原理与应用 Principle and Application of Database system. 安庆师范学院计算机与信息学院. 9.3 T-SQL 语言程序设计基础. (1) 数据定义语言( DDL )。. (2) 数据操纵语言( DML )。. (3) 数据控制语言( DCL )。. 9.3.1 常量、变量与数据类型. 9.3.1.1 常 量. 1. 字符串常量. 2. 二进制常量.

alissa
Download Presentation

AnQing Teachers College Department of Computer & Information

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. AnQing Teachers College Department of Computer & Information 数据库原理与应用 Principle and Application of Database system 安庆师范学院计算机与信息学院 Principle and Application of Database System

  2. 9.3 T-SQL语言程序设计基础 (1) 数据定义语言(DDL)。 Principle and Application of Database System

  3. (2) 数据操纵语言(DML)。 Principle and Application of Database System

  4. (3) 数据控制语言(DCL)。 Principle and Application of Database System

  5. 9.3.1 常量、变量与数据类型 9.3.1.1 常 量 1.字符串常量 Principle and Application of Database System

  6. 2.二进制常量 二进制常量具有前辍 0x 并且是十六进制数字字符串。这些常量不使用引号。二进制常量的示例为: 0xAE 0x12Ef 0x69048AEFDD010E 0x (empty binary string) Principle and Application of Database System

  7. 3.bit 常量 • bit 常量使用数字 0 或 1 表示,并且不使用引号。如果使用一个大于 1 的数字,它将被转换为 1。 Principle and Application of Database System

  8. 4. integer 常量 • integer常量由没有用引号括起来且不含小数点的一串数字表示。integer常量必须是整数,不能包含小数点。下面是一些 integer 常量的示例:1894 2 Principle and Application of Database System

  9. 9.3. 实型常量 Principle and Application of Database System

  10. 6. 日期时间常量 时间格式: '14:30:24' '04:24 PM' 日期时间型:April 20,2000 14:30:24 Principle and Application of Database System

  11. Principle and Application of Database System

  12. 9.3.1.2 数据类型 1. 系统数据类型 2. 用户自定义数据类型 Principle and Application of Database System

  13. Principle and Application of Database System

  14. 1) 利用企业管理器定义 Principle and Application of Database System

  15. Principle and Application of Database System

  16. 2) 利用命令定义数据类型 语法格式 Principle and Application of Database System

  17. sp_addtype 'student_no','char(5)','not null' Principle and Application of Database System

  18. 9.3.1.2 数 据 类 型 3. 自定义数据类型的删除 1)用企业管理器删除自定义数据类型 Principle and Application of Database System

  19. 9.3.1.2 数 据 类 型 2) 利用命令删除自定据类型 删除student_no类型的语句为: sp_droptype 'student_no' Principle and Application of Database System

  20. 9.3.1.2 数 据 类 型 4. 利用自定义类型定义字段 Principle and Application of Database System

  21. 9.3.1.2 数 据 类 型 CREATE TABLE student ( sno student_no PRIMARY KEY, sname char(8), ssex char(2), sbirthday smalldatetime, class char(5) ) Principle and Application of Database System

  22. 9.3.1.3 变 量 1. 变 量 1) 标识符 (1) 常规标识符 由若干个中文、字母、数字、_、@、$、#构成,#不能开头,最多128字符。 (2) 分隔标识符:用" " 或 [ ] 2) 变量的分类 (1) 全局变量 (2) 局部变量 Principle and Application of Database System

  23. 9.3.1.3 变 量 2. 局部变量的使用 1) 局部变量的定义与赋值 (1) 局部变量的定义 (2) 局部变量的赋值 Principle and Application of Database System

  24. 9.3.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

  25. 用SELECT语句赋值 • SELECT {@local_variable=expression}[,…n] • 如果SELECT语句没有返回值,变量将保留当前值 • 如果expression是不返回值的标量子查询,则将变量设为NULL Principle and Application of Database System

  26. DECLARE @var1 nvarchar(30) SELECT @var1='刘丰' SELECT @var1=sname FROM student WHERE sno='110' SELECT @var1 AS NAME Principle and Application of Database System

  27. 9.3.1.3 变 量 例如:查询用于给 @var1 赋值。在 student表中sno不存在,因此子查询不返回值,并将变量@var1设为 NULL。 DECLARE @var1 nvarchar(30) SELECT @var1='刘丰' SELECT @var1= (SELECT sname FROM student WHERE sno= '110' ) SELECT @var1 AS NAME GO Principle and Application of Database System

  28. 9.3.1.3 变 量 2)局部游标变量的定义与赋值 (1) 局部游标变量的定义 Principle and Application of Database System

  29. 9.3.1.3 变 量 Principle and Application of Database System

  30. 9.3.1.3 变 量 Principle and Application of Database System

  31. 9.3.1.3 变 量 (3) 游标变量的使用步骤 例如:使用游标变量 DECLARE @st_CURSOR CURSOR SET @st_CURSOR= CURSOR SCROLL DYNAMIC FOR SELECT sno,sname,class FROM student Principle and Application of Database System

  32. 9.3.1.3 变 量 OPEN @st_CURSOR FETCH NEXT FROM @st_CURSOR WHILE @@FETCH_STATUS=0 FETCH NEXT FROM @st_CURSOR CLOSE @st_CURSOR DEALLOCATE @st_CURSOR Principle and Application of Database System

  33. 9.3.2 运算符与表达式 1.算术运算符 + 、-、*、/、% 例: DECLARE @a int,@b int SET @a=11 SET @b=3 SELECT @a+@b AS 'a+b',@a-@b AS 'a-b',@a*@b AS 'a*b',@a/@b AS 'a/b',@a%@b AS 'a%b' Principle and Application of Database System

  34. 9.3.2 运算符与表达式 2.位运算符 Principle and Application of Database System

  35. 9.3.2 运算符与表达式 例如: 在maste数据库中,建立表bitop,并插入一行,然后将a字段和 b字段上的值进行位运算。 Principle and Application of Database System

  36. 9.3.2 运算符与表达式 Principle and Application of Database System

  37. 9.3.2 运算符与表达式 Principle and Application of Database System

  38. 9.3.2 运算符与表达式 3. 比较运算符 Principle and Application of Database System

  39. 9.3.2 运算符与表达式 4. 逻辑运算符 Principle and Application of Database System

  40. 9.3.2 运算符与表达式 5. 字符串联接运算符 例如:多个字符串的联接。 SELECT (sno+space(2)+sname) AS '学号 姓名' FROM student Principle and Application of Database System

  41. 9.3.2 运算符与表达式 6. 一元运算 7. 赋值运算符 指给局部变量赋值的SET和SELECT语句中使用的“=”。 Principle and Application of Database System

  42. 9.3.2 运算符与表达式 8. 运算符的优先顺序 Principle and Application of Database System

  43. 9.3.3 流程控制语句 Principle and Application of Database System

  44. 9.3.3.1 IF...ELSE语句 Principle and Application of Database System

  45. 9.3.3.1 IF...ELSE语句 Principle and Application of Database System

  46. 9.3.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

  47. 9.3.3.2 WHILE、BREAK和CONTINUE语句 1. WHILE循环语句 Principle and Application of Database System

  48. Principle and Application of Database System

  49. DECLARE @s int,@i int SET @s=0 SET @i=1 WHILE @i<=100 BEGIN SET @s=@s+@i SET @i=@i+1 END SELECT @i,@s Principle and Application of Database System

  50. 例如:显示字符串"China"中每个字符的 ASCII 值和字符。 DECLARE @position int,@string char(8) SET @position=1 SET @string='China' WHILE @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

More Related