1 / 26

网络数据库技术

网络数据库技术. T-SQL 程序设计. 设计程序. 目标: 掌握 Transact-SQL 程序设计语言 了解 Transact-SQL 程序设计方法. [ 案例 1] 求 x^2-3x+2=0. /* 求 x^2-3x+2=0*/ declare @a decimal,@b decimal,@c decimal,@x1 decimal,@x2 decimal set @a=1 set @b=-3 set @c=2 /*go*/ set @x1=(-@b-sqrt(@b*@b-4*@a*@c))/(2*@a)

druce
Télécharger la présentation

网络数据库技术

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. 网络数据库技术 T-SQL程序设计

  2. 设计程序 目标: • 掌握Transact-SQL程序设计语言 • 了解Transact-SQL程序设计方法

  3. [案例1]求x^2-3x+2=0 • /*求x^2-3x+2=0*/ • declare @a decimal,@b decimal,@c decimal,@x1 decimal,@x2 decimal • set @a=1 • set @b=-3 • set @c=2 • /*go*/ • set @x1=(-@b-sqrt(@b*@b-4*@a*@c))/(2*@a) • set @x2=(-@b+sqrt(@b*@b-4*@a*@c))/(2*@a) • print @x1 • print @x2 • go

  4. [相关知识1] Transact-SQL语言基础 SQL:Structured Query Language ,RDBMS标准语言 数据定义(DDL): CREATE、ALTER、DROP SELECT、INSERT、DELETE、UPDATE 数据操纵(DML): T-SQL GRANY、DENY、ROVOKE 数据控制(DCL): 附加语言元素: 变量、运算符、函数、流程控制语句

  5. [相关知识2]基本语句 注释语句 : --<注释文本> 或 /* <注释文本> […] */ 定义批处理结束语句 :GO 输出语句 :PRINT <表达式>

  6. [相关知识3]数据类型 整型: bigint(8)、int(4)、smallint(2)、tinyint(1) 数值 定点: decimal或numeric 实型 浮点: float(8)、real(4) 字符: char(1~8000)、varchar(变长)、text(>8000) 数据类型 datetime(1753-9999)、smalldatetime(1900-2079) 时间日期: money(8)、smallmoney(4) 货币: 二进制: bit(0/1)、binary、varbinary(变长)、image 自定义

  7. [相关知识4.1]变量 定义:DECLARE @<局部变量名> <数据类型> 局部变量 赋值:SET @<局部变量名>=<表达式> 作用域:批 变量 全局变量:

  8. [相关知识5]运算符 1. 算术运算符:+、-、*、/、% 2. 字符串运算符:+(注意与算术加的区别) 3.关系运算符:=、>、<、>=、<=、<>、!=、!>、!< 4.逻辑运算符:not、and、or、between、like、in、 any、some、all、exists 5. 赋值运算符:=

  9. [案例2]查看学号为3101233101学生的年龄 • use student • declare @age int • declare @birthday datetime • select @birthday=sbirthday from s where sno='3101233101' • set @age=year(getdate())-year(@birthday) +1 • print @age

  10. [相关知识6]函数 1.数学函数:sin(n)(正弦)、asin(n)(反正弦)、pi()(圆周率)、abs(n)(绝对值)、exp(n)(指数)、log(n)(对数)、power(n,m)(nm)、round(n,m)(四舍五入)、sign(n)(符号)、sqrt(n)(平方根)、rand([n])(随机数) 等 2.字符串函数:str(n,n1,n2)(数值转字符串)、len(s)(串长)、left(s,n)(左子串)、right(s,n)(右子串)、substring(s,n1,n2)(子串)、lower(s)(转小写)、upper(s)(转大写)、ltrim(s)(删除左空格)、rtrim(s)(删除右空格)、space(n)(产生空格)、REVERSE(s)(反转字符串)、charindex(s1,s2)(s1在s2中的起始位置)等 3.日期时间函数:getdate()、year(d)、month(d)、day(d)、datepart(datepart,d) (datepart日期类型)、dateadd(datepart,n,d)(日期加)、datediff(datepart,d1,d2) (日期减)等 实例 计算香港回归多少年、多少天,今天以后15个月是哪一天。 4.类型转换函数:cast(expression AS data_type) convert(data_type,expression[,style])

  11. [案例3]查看SQL Server版本、服务器、服务名称等信息 • print '目前所用SQL Server的版本信息如下:' • print @@version • Print '目前所用SQL Server服务器的名称为:'+@@servername • print '目前所用服务为:'+@@servicename

  12. [拓展案例]时间格式转换 • print '系统当前日期:' • print getdate() • print '美国格式'+convert(char(10),getdate(),101) • print 'ANSI格式'+convert(char(10),getdate(),102) • print '系统当前时间:'+convert(char(10),getdate(),114) • print '美国格式'+cast(getdate() as char(10))

  13. [相关知识4.2]变量 局部变量 变量 全局变量: 系统变量,以@@开头,称无参函数

  14. [案例4] 求三个数中最大值 • declare @a int,@b int,@c int,@t int • set @a=1 • set @b=2 • set @c=3 • if @a<@b • begin • set @t=@a • set @a=@b • set @b=@t • end • if @a>@c • begin • print '最大数:'+cast(@a as char(4)) • end • else • begin • print '最大数:'+cast(@c as char(4)) • end

  15. [相关知识7]流程控制语句 顺序结构 选择结构 结构化程序 循环结构

  16. [相关知识8]定义语句块语句 语句格式: BEGIN <T-SQL语句>|<语句块> END

  17. [相关知识9]选择结构 1. 条件语句: IF <逻辑表达式> <T-SQL语句>|<语句块> [ELSE <T-SQL语句>|<语句块>] 实例根据分数输出考试等级 实例求3个整数中最大数 2.多路分支语句: CASE WHEN <逻辑表达式> THEN <表达式> […] ELSE <表达式> END 实例用CASE语句实现根据分数输出考试等级 实例 根据身份证号输出学生年龄 集合并 CASE语句作表达式

  18. [拓展案例]根据分数输出等级 • DECLARE @score smallint • SET @score=(SELECT score FROM sc WHERE sno='3101233101' and cno='12312050') • print case • when @score>=90 then '优秀' • when @score>=80 then '良好' • when @score>=70 then '中' • when @score>=60 then '及格' • ELSE '不及格' • end

  19. [案例5]求1到100之间的奇数和。 declare @i tinyint,@sum smallint • set @i=1 • set @sum=0 • while @i<=99 • begin • set @sum=@sum+@i • set @i=@i+2 • end • print 'sum='+str(@sum)

  20. [相关知识10]循环结构 1. 循环语句:WHILE <逻辑表达式> <T-SQL语句>|<语句块> 2. 中断语句 :BREAK 3. 短路语句 :CONTINUE

  21. [拓展案例]求100~200之间的全部素数。 • declare @m tinyint,@i tinyint • set @m=101 • while @m<=200 • begin • set @i=2 • while @i<=sqrt(@m) • begin • if (@m%@i=0) • break • set @i=@i+1 • end • if (@i>sqrt(@m)) • print @m • set @m=@m+2 • end

  22. [知识拓展1]等待语句 语句格式: WAITFOR DELAY '<时间间隔>'|TIME '<时间>' 实例设置等待一小时后执行查询 实例设置到十点整执行查询

  23. [知识拓展2]返回语句 语句格式: RETURN [<整数表达式>] 返回值:0 执行成功 负数 失败

  24. [知识拓展3]脚本文件 脚本:一条或多条T-SQL语句 。 脚本文件 :保存脚本的文件 ,扩展名.sql。 如库结构、表结构文件。 执行:查询分析器、osql、isql。

  25. 一、在查询分析器中使用脚本 查询分析器→文件→保存 查询分析器→文件→打开 实例 将求素数的脚本保存为脚本文件

  26. 二、在osql中使用脚本 osql:查询SQL Server 2000的交互式命令行工具。 语法格式: osql [-S<服务器名>] [-U<用户名>] [-P<密码>] [-i<脚本文件>] 实例使用osql查询数据库student表c的所有信息 实例使用osql执行脚本文件

More Related