use master
go
create database xscj
go
use xscj
go
create table xs
(学号 char(6) not null primary key,
姓名 char(8),
专业名 char(10),
性别 bit default 1,
出生日期 smalldatetime,
总学分 tinyint,
备注 text)
go
create table kc
(课程号 char(3) not null primary key,
课程名 char(16),
开课学期 tinyint default 1,
check(开课学期>=1 and 开课学期<=8),
学时 tinyint,
学分 tinyint)
go
create table xs_kc
(学号 char(6) not null,
课程号 char(3) not null,
primary key(学号,课程号),
成绩 tinyint,
学分 tinyint)
go
insert into xs values('001101','王林','计算机',1,'1980-2-10',50,null)
insert into xs values('001102','程明','计算机',1,'1981-2-1',50,null)
insert into xs values('001103','王燕','计算机',0,'1979-10-6',50,null)
insert into xs values('001104','伟严严','计算机',1,'1980-8-26',50,null)
insert into xs values('001106','李芳芳','计算机',1,'1980-11-20',50,null)
insert into xs values('001107','李明','计算机',1,'1980-5-1',54,'提前修完《数据结构》,并获学分')
insert into xs values('001108','林一帆','计算机',1,'1979-8-5',52,'已提前修完一门课')
insert into xs values('001109','张强明','计算机',1,'1978-8-11',50,null)
insert into xs values('001110','张蔚','计算机',0,'1981-7-22',50,'三好学生')
insert into xs values('001111','赵琳','计算机',0,'1980-3-18',50,null)
insert into xs values('001113','严红','计算机',0,'1979-7-13',48,'有一门功课不及格,待补考')
go
create database xscj
go
use xscj
go
create table xs
(学号 char(6) not null primary key,
姓名 char(8),
专业名 char(10),
性别 bit default 1,
出生日期 smalldatetime,
总学分 tinyint,
备注 text)
go
create table kc
(课程号 char(3) not null primary key,
课程名 char(16),
开课学期 tinyint default 1,
check(开课学期>=1 and 开课学期<=8),
学时 tinyint,
学分 tinyint)
go
create table xs_kc
(学号 char(6) not null,
课程号 char(3) not null,
primary key(学号,课程号),
成绩 tinyint,
学分 tinyint)
go
insert into xs values('001101','王林','计算机',1,'1980-2-10',50,null)
insert into xs values('001102','程明','计算机',1,'1981-2-1',50,null)
insert into xs values('001103','王燕','计算机',0,'1979-10-6',50,null)
insert into xs values('001104','伟严严','计算机',1,'1980-8-26',50,null)
insert into xs values('001106','李芳芳','计算机',1,'1980-11-20',50,null)
insert into xs values('001107','李明','计算机',1,'1980-5-1',54,'提前修完《数据结构》,并获学分')
insert into xs values('001108','林一帆','计算机',1,'1979-8-5',52,'已提前修完一门课')
insert into xs values('001109','张强明','计算机',1,'1978-8-11',50,null)
insert into xs values('001110','张蔚','计算机',0,'1981-7-22',50,'三好学生')
insert into xs values('001111','赵琳','计算机',0,'1980-3-18',50,null)
insert into xs values('001113','严红','计算机',0,'1979-7-13',48,'有一门功课不及格,待补考')
insert into xs values('001201','李红庆','通信工程',1,'1979-5-11',40,'有一门不及格,待补考')
insert into xs values('001202','孙详欣','通信工程',1,'1979-6-11',42,null)
insert into xs values('001203','孙研','通信工程',1,'1978-6-10',42,null)
insert into xs values('001204','吴薇华','通信工程',0,'1978-6-1',42,null)
insert into xs values('001206','刘燕敏','通信工程',0,'1979-9-20',42,null)
insert into xs values('001210','罗林琳','通信工程',0,'1979-5-1',44,'已提前修完一门,并获得学分')
insert into xs values('001216','李计','通信工程',1,'1978-3-9',42,null)
insert into xs values('001218','马琳琳','通信工程',0,'1980-10-9',42,null)
insert into xs values('001220','王玉民','通信工程',1,'1980-3-18',42,null)
insert into xs values('001221','王林','通信工程',0,'1979-11-13',42,null)
insert into xs values('001241','王敏','通信工程',0,'1980-1-30',50,'转专业学习')
insert into kc values('101','计算机基础',1,80,5)
insert into kc values('102','程序设计与语言',2,68,4)
insert into kc values('206','离散数学',4,68,4)
insert into kc values('208','数据结构',5,68,4)
insert into kc values('209','操作系统',6,68,4)
insert into kc values('210','计算机原理',5,85,5)
insert into kc values('212','数据库原理',7,68,4)
insert into kc values('301','计算机网络',7,51,3)
insert into kc values('302','软件工程',7,51,3)
insert into kc values('304','算法',7,51,3)
insert into kc values('305','软件设计',7,51,3)
insert into kc values('102','程序设计与语言',2,68,4)
insert into kc values('206','离散数学',4,68,4)
insert into kc values('208','数据结构',5,68,4)
insert into kc values('209','操作系统',6,68,4)
insert into kc values('210','计算机原理',5,85,5)
insert into kc values('212','数据库原理',7,68,4)
insert into kc values('301','计算机网络',7,51,3)
insert into kc values('302','软件工程',7,51,3)
insert into kc values('304','算法',7,51,3)
insert into kc values('305','软件设计',7,51,3)
insert into xs_kc values('001101','101',80,null)
insert into xs_kc values('001101','102',78,null)
insert into xs_kc values('001101','206',76,null)
insert into xs_kc values('001103','101',76,null)
insert into xs_kc values('001103','102',72,null)
insert into xs_kc values('001103','206',70,null)
insert into xs_kc values('001101','102',78,null)
insert into xs_kc values('001101','206',76,null)
insert into xs_kc values('001103','101',76,null)
insert into xs_kc values('001103','102',72,null)
insert into xs_kc values('001103','206',70,null)
insert into xs_kc values('001106','101',65,null)
insert into xs_kc values('001106','102',71,null)
insert into xs_kc values('001106','206',85,null)
insert into xs_kc values('001106','102',71,null)
insert into xs_kc values('001106','206',85,null)
insert into xs_kc values('001104','101',90,null)
insert into xs_kc values('001104','102',84,null)
insert into xs_kc values('001104','206',65,null)
insert into xs_kc values('001104','102',84,null)
insert into xs_kc values('001104','206',65,null)
insert into xs_kc values('001102','102',78,null)
insert into xs_kc values('001102','206',78,null)
insert into xs_kc values('001102','206',78,null)
insert into xs_kc values('001107','101',78,null)
insert into xs_kc values('001107','102',80,null)
insert into xs_kc values('001107','206',68,null)
insert into xs_kc values('001107','102',80,null)
insert into xs_kc values('001107','206',68,null)
insert into xs_kc values('001108','101',85,null)
insert into xs_kc values('001108','102',64,null)
insert into xs_kc values('001108','206',87,null)
insert into xs_kc values('001108','102',64,null)
insert into xs_kc values('001108','206',87,null)
insert into xs_kc values('001109','101',66,null)
insert into xs_kc values('001109','102',83,null)
insert into xs_kc values('001109','206',70,null)
insert into xs_kc values('001109','102',83,null)
insert into xs_kc values('001109','206',70,null)
insert into xs_kc values('001110','101',95,null)
insert into xs_kc values('001110','102',90,null)
insert into xs_kc values('001110','206',89,null)
insert into xs_kc values('001110','102',90,null)
insert into xs_kc values('001110','206',89,null)
insert into xs_kc values('001111','101',91,null)
insert into xs_kc values('001111','102',70,null)
insert into xs_kc values('001111','102',70,null)
select * from xs
select * from kc
select * from xs_kc
--以下是一个符合SQL-92的游标声明
declare xs_cur1 cursor
for select 学号,姓名,性别,出生时间,总学分 from xs where 专业名='计算机'
for read only
declare xs_cur1 cursor
for select 学号,姓名,性别,出生时间,总学分 from xs where 专业名='计算机'
for read only
--以下是一个T-SQL扩展游标声明
declare xs_cur2 cursor dynamic
for select 学号,姓名,总学分 from xs where 专业名='计算机'
for update of 总学分
declare xs_cur2 cursor dynamic
for select 学号,姓名,总学分 from xs where 专业名='计算机'
for update of 总学分
--定义游标xs_cur3 然后打开游标,输出其行数
declare xs_cur3 cursor local scroll scroll_locks
for select 学号,姓名,总学分 from xs
for update of 总学分
--打开游标
open xs_cur3
--游标相应的操作
select '游标xs_cur3数据行数 '=@@cursor_rows
--关闭游标
close xs_cur3
declare xs_cur3 cursor local scroll scroll_locks
for select 学号,姓名,总学分 from xs
for update of 总学分
--打开游标
open xs_cur3
--游标相应的操作
select '游标xs_cur3数据行数 '=@@cursor_rows
--关闭游标
close xs_cur3
--从游标xs_cur1中提取数据
declare xs_cur1 cursor
for select 学号,姓名,性别,出生日期,总学分 from xs where 专业名='计算机'
for read only
--打开游标
open xs_cur1
--提取数据(由于xs_cur1)是只进游标,所以只能使用next提取数据
fetch next from xs_cur1
--关闭游标
close xs_cur1
declare xs_cur1 cursor
for select 学号,姓名,性别,出生日期,总学分 from xs where 专业名='计算机'
for read only
--打开游标
open xs_cur1
--提取数据(由于xs_cur1)是只进游标,所以只能使用next提取数据
fetch next from xs_cur1
--关闭游标
close xs_cur1
--从游标xs_cur2中提取数据(xs_cur2是动态游标,可以前滚,后滚,除absolute以外都可以提取)
declare xs_cur2 cursor dynamic
for select 学号,姓名,总学分 from xs where 专业名='计算机'
for update of 总学分
--打开游标
open xs_cur2
--读取游标第一行
fetch first from xs_cur2
--读取下一行
fetch next from xs_cur2
--读取上一行
fetch prior from xs_cur2
--读取最后一行
fetch last from xs_cur2
--读取当前行的上两行
fetch relative -2 from xs_cur2
--读取当前行的下三行
fetch relative 3 from xs_cur2
select 'fetch执行情况 '=@@fetch_status
--关闭游标
close xs_cur2
declare xs_cur2 cursor dynamic
for select 学号,姓名,总学分 from xs where 专业名='计算机'
for update of 总学分
--打开游标
open xs_cur2
--读取游标第一行
fetch first from xs_cur2
--读取下一行
fetch next from xs_cur2
--读取上一行
fetch prior from xs_cur2
--读取最后一行
fetch last from xs_cur2
--读取当前行的上两行
fetch relative -2 from xs_cur2
--读取当前行的下三行
fetch relative 3 from xs_cur2
select 'fetch执行情况 '=@@fetch_status
--关闭游标
close xs_cur2
--------------------------------------------------T-SQL语言----------------------------------------
--用户自定义数据类型
--添加自定义数据类型
use xscj
--调用系统存储过程
exec sp_addtype 'student_num','char(6)','not null'
--将当前的T-SQL批处理语句发送给SQL Server
go
--删除自定义数据类型
use xscj
exec sp_droptype 'student_num'
go
--用户自定义数据类型
--添加自定义数据类型
use xscj
--调用系统存储过程
exec sp_addtype 'student_num','char(6)','not null'
--将当前的T-SQL批处理语句发送给SQL Server
go
--删除自定义数据类型
use xscj
exec sp_droptype 'student_num'
go
--创建局部变量 @var1,@var2并赋值,然后输出变量
--定义变量(变量名及类型)
declare @var1 varchar(20),@var2 varchar(25)
--给变量赋值(一个set语句只能给一个变量赋值 而 select 可以初始化多个变量)
set @var1='中国'
set @var2=@var1+'是一个伟大的国家'
select @var1,@var2
go
--定义变量(变量名及类型)
declare @var1 varchar(20),@var2 varchar(25)
--给变量赋值(一个set语句只能给一个变量赋值 而 select 可以初始化多个变量)
set @var1='中国'
set @var2=@var1+'是一个伟大的国家'
select @var1,@var2
go
--创建一个名为sex的局部变量,并在select语句中使用该局部变量查找表xs中的所有女同学的学号,姓名
use xscj
declare @sex bit
set @sex=0
select 学号,姓名 from xs where 性别 =@sex
go
use xscj
declare @sex bit
set @sex=0
select 学号,姓名 from xs where 性别 =@sex
go
--使用查询给变量赋值
use xscj
declare @student char(8)
set @student=(select 姓名 from xs where 学号='001102')
select @student
go
use xscj
declare @student char(8)
set @student=(select 姓名 from xs where 学号='001102')
select @student
go
--使用游标变量
use xscj
--定义游标变量
declare @cursorVar cursor
--给游标变量赋值
set @cursorVar=cursor scroll dynamic
for select 学号,姓名 from xs where 姓名 like '王%'
--打开游标
open @cursorVar
--对游标的操作
fetch next from @cursorVar
while @@fetch_status=0
begin
fetch next from @cursorVar
end
--关闭游标
close @cursorVar
--删除游标
deallocate @cursorVar
use xscj
--定义游标变量
declare @cursorVar cursor
--给游标变量赋值
set @cursorVar=cursor scroll dynamic
for select 学号,姓名 from xs where 姓名 like '王%'
--打开游标
open @cursorVar
--对游标的操作
fetch next from @cursorVar
while @@fetch_status=0
begin
fetch next from @cursorVar
end
--关闭游标
close @cursorVar
--删除游标
deallocate @cursorVar
--算术运算符
use xscj
set nocount on
declare @startdate datetime
set @startdate=getdate()
select 学号,姓名,@startdate-出生日期 as 年龄 from xs
use xscj
set nocount on
declare @startdate datetime
set @startdate=getdate()
select 学号,姓名,@startdate-出生日期 as 年龄 from xs
--位运算符
use xscj
create table bittop
(
a int not null,
b int not null
)
insert bittop values(1,7)
go
select a&b,a|b,a^b from bittop
go
drop table bittop
use xscj
create table bittop
(
a int not null,
b int not null
)
insert bittop values(1,7)
go
select a&b,a|b,a^b from bittop
go
drop table bittop
--比较运算
use xscj
declare @student student_num
set @student='001111'
if(@student<>0)
select *from xs where 学号 =@student
use xscj
declare @student student_num
set @student='001111'
if(@student<>0)
select *from xs where 学号 =@student
--查询成绩高于王林最高成绩的学生姓名 课程名及成绩
use xscj
select 姓名,课程名,成绩
from xs,xs_kc,kc
where 成绩>all
( select b.成绩
from xs a,xs_kc b,kc c
where a.学号=b.学号 and b.课程号=c.课程号 and a.姓名='王林'
)and 姓名<>'王林'
go
use xscj
select 姓名,课程名,成绩
from xs,xs_kc,kc
where 成绩>all
( select b.成绩
from xs a,xs_kc b,kc c
where a.学号=b.学号 and b.课程号=c.课程号 and a.姓名='王林'
)and 姓名<>'王林'
go
--查询成绩高于王林最低成绩的学生姓名,课程名,成绩
use xscj
select 姓名,课程名,成绩
from xs,xs_kc,kc
where 成绩>any
(
select b.成绩
from xs a,xs_kc b,kc c
where a.学号=b.学号 and b.课程号=c.课程号 and a.姓名='王林'
)and 姓名!='王林'
go
use xscj
select 姓名,课程名,成绩
from xs,xs_kc,kc
where 成绩>any
(
select b.成绩
from xs a,xs_kc b,kc c
where a.学号=b.学号 and b.课程号=c.课程号 and a.姓名='王林'
)and 姓名!='王林'
go
--逻辑运算符
--查询总学分在40~50分的学生学号和姓名
use xscj
select 学号,姓名,总学分
from xs
where 总学分 between 40 and 50
go
--查询总学分在40~50分的学生学号和姓名
use xscj
select 学号,姓名,总学分
from xs
where 总学分 between 40 and 50
go
--查询课程名以‘计 ‘算’‘软’或开头的情况
use xscj
select *
from kc
where 课程名 like '[计软算]%'
use xscj
select *
from kc
where 课程名 like '[计软算]%'
--存储过程中,学生学号为入口参数,然后使用模式匹配查找某个学生选的所有的课程
use xscj
create procedure find_course1 @num char(20)
as
select @num=rtrim(@num)+'%'
select a.姓名,t.课程名
from xs a,xs_kc b,kc t
where a.学号=b.学号 and b.课程号=t.课程号 and a.学号 like @num
use xscj
create procedure find_course1 @num char(20)
as
select @num=rtrim(@num)+'%'
select a.姓名,t.课程名
from xs a,xs_kc b,kc t
where a.学号=b.学号 and b.课程号=t.课程号 and a.学号 like @num
--查询所有选课学生的姓名
use xscj
select distinct 姓名
from xs
where exists
(
select * from xs_kc where xs.学号=xs_kc.学号
)
go
use xscj
select distinct 姓名
from xs
where exists
(
select * from xs_kc where xs.学号=xs_kc.学号
)
go
--使用IN子句实现上述子查询
select distinct 姓名
from xs
where xs.学号 in
(
select xs_kc.学号 from xs_kc
)
--字符串连接运算符
use xscj
select(学号+space(1)+','+space(1)+姓名) as 学号及姓名
from xs
where substring(学号,5,2)='13'
select distinct 姓名
from xs
where xs.学号 in
(
select xs_kc.学号 from xs_kc
)
--字符串连接运算符
use xscj
select(学号+space(1)+','+space(1)+姓名) as 学号及姓名
from xs
where substring(学号,5,2)='13'
--流程控制语句
--如下程序用于控制总学分>40分的学生人数
declare @num int
select @num=(select count(姓名) from xs where 总学分>40)
if @num>0
select @num as '总学分>90的人数'
--如下程序用于控制总学分>40分的学生人数
declare @num int
select @num=(select count(姓名) from xs where 总学分>40)
if @num>0
select @num as '总学分>90的人数'
--如果“计算机”的平均成绩高于90分,则显示"平均成绩高于75分"
use xscj
declare @text1 char(20)
set @text1='平均成绩高于90.'
if(select avg(成绩)
from xs,xs_kc,kc
where xs.学号=xs_kc.学号 and xs_kc.课程号=kc.课程号 and kc.课程名='计算机基础'
)<90
select @text1='平均成绩低于90.'
else
select @text1
go
use xscj
declare @text1 char(20)
set @text1='平均成绩高于90.'
if(select avg(成绩)
from xs,xs_kc,kc
where xs.学号=xs_kc.学号 and xs_kc.课程号=kc.课程号 and kc.课程名='计算机基础'
)<90
select @text1='平均成绩低于90.'
else
select @text1
go
--IF...ELSE语句的嵌套使用
use xscj
if(select avg(成绩)
from xs,xs_kc,kc
where xs.学号=xs_kc.学号 and xs_kc.课程号=kc.课程号 and kc.课程名='计算机基础'
)<85
select '平均成绩低于85'
else
if(select avg(成绩)
from xs,xs_kc,kc
where xs.学号=xs_kc.学号 and xs_kc.课程号=kc.课程号 and kc.课程名='计算机基础'
)>85
select '平均成绩高于85'
go
use xscj
if(select avg(成绩)
from xs,xs_kc,kc
where xs.学号=xs_kc.学号 and xs_kc.课程号=kc.课程号 and kc.课程名='计算机基础'
)<85
select '平均成绩低于85'
else
if(select avg(成绩)
from xs,xs_kc,kc
where xs.学号=xs_kc.学号 and xs_kc.课程号=kc.课程号 and kc.课程名='计算机基础'
)>85
select '平均成绩高于85'
go
--显示字符串"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)),char(ASCII(substring(@string,@position,1)))
set @position=@position+1
end
go
declare @position int,@string char(8)
set @position=1
set @string='China'
while @position<=datalength(@string)
begin
select ASCII(substring(@string,@position,1)),char(ASCII(substring(@string,@position,1)))
set @position=@position+1
end
go
--检查学生的平均成绩,若>75 将返回状态代码1,否则 将返回状态代码2
use xscj
create procedure checkavg @param varchar(10)
as
if(select avg(成绩)
from xs_kc
where xs_kc.学号 =@param
group by 学号
)>75
return 1
else
return 2
go
use xscj
create procedure checkavg @param varchar(10)
as
if(select avg(成绩)
from xs_kc
where xs_kc.学号 =@param
group by 学号
)>75
return 1
else
return 2
go
--waitfor语句
waitfor time '00:44'
select *from xs
waitfor time '00:44'
select *from xs
--显示ABS函数对三个不同数字的效果
--返回给定数字表达式的绝对值
select ABS(-5.0),ABS(0.0),ABS(0)
--返回给定数字表达式的绝对值
select ABS(-5.0),ABS(0.0),ABS(0)
--ACOS 反余弦函数 求一个余弦值对应的角度
declare @angle real
set @angle=0
select 'The ACOS='+CONVERT(varchar,ACOS(@angle))
declare @angle real
set @angle=0
select 'The ACOS='+CONVERT(varchar,ACOS(@angle))
--RAND([seed])
--返回0~1之间的一个随机值.参数seed为整型表达式,返回值类型为float
declare @count int
set @count=5
select RAND(@count)*10 as Rand_Num
go
--返回0~1之间的一个随机值.参数seed为整型表达式,返回值类型为float
declare @count int
set @count=5
select RAND(@count)*10 as Rand_Num
go
--ASCII函数 CHAR函数
--显示字符串"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)),char(ASCII(substring(@string,@position,1)))
set @position=@position+1
end
go
--显示字符串"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)),char(ASCII(substring(@string,@position,1)))
set @position=@position+1
end
go
--LEFT(character_expression,integer_expression) 返回值为varchar型
--返回课程名最左边的8个字符
use xscj
select LEFT(课程名,4)
from kc
order by 课程号
go
--LTRIM(character_expression)删除character_expression字符串中的前导空格,返回值类型为varchar
declare @str varchar(40)
set @str=' 中国,是一个古老而伟大的国家'
select @str
select LTRIM(@str)
go
declare @str varchar(40)
set @str=' 中国,是一个古老而伟大的国家'
select @str
select LTRIM(@str)
go
--replace('string_expression1','string_expression2','string_expression3')
--用string_expression3替换string_expression1中包含string_expression2中的部分
select replace('我爱你祖国','祖国','母亲')
go
--用string_expression3替换string_expression1中包含string_expression2中的部分
select replace('我爱你祖国','祖国','母亲')
go
--SUBSTRING(expression,start,length) 函数
--分解学生的姓名为 姓 名
use xscj
select substring(姓名,1,1) as 姓,substring(姓名,2,LEN(姓名)-1) as 名
from xs
order by 姓名
--分解学生的姓名为 姓 名
use xscj
select substring(姓名,1,1) as 姓,substring(姓名,2,LEN(姓名)-1) as 名
from xs
order by 姓名
--STR(float_expression[,length[,decimal]])函数 返回值类型为 char
select STR(123.45, 6, 1)
go
select STR(123.45, 6, 1)
go
--使用CASE函数对学生按性别分类
use xscj
select 学号,sex=
CASE 性别
when 1 then '男生'
when 0 then '女生'
end
from xs
go
use xscj
select 学号,sex=
CASE 性别
when 1 then '男生'
when 0 then '女生'
end
from xs
go
--根据学生的年龄范围显示相应信息
use xscj
select 学号,姓名,年龄=
CASE
when year(getdate())-year(出生日期)<=27 then '年龄较小'
when year(getdate())-year(出生日期)<30 and year(getdate())-year(出生日期)>27 then '年龄适中'
else '年龄偏大'
end
from xs
go
use xscj
select 学号,姓名,年龄=
CASE
when year(getdate())-year(出生日期)<=27 then '年龄较小'
when year(getdate())-year(出生日期)<30 and year(getdate())-year(出生日期)>27 then '年龄适中'
else '年龄偏大'
end
from xs
go
--使用CAST 检索总学分为 40~49分的学生姓名,并将总学分转换为char(20)
use xscj
select 姓名,总学分
from xs
where CAST(总学分 as char(20)) like '4_' and 总学分>=40
go
use xscj
select 姓名,总学分
from xs
where CAST(总学分 as char(20)) like '4_' and 总学分>=40
go
--使用CONVERT 检索总学分为 40~49分的学生姓名,并将总学分转换为char(20)
use xscj
select 姓名,总学分
from xs
where CONVERT(char(20),总学分) like '4_' and 总学分>=40
go
use xscj
select 姓名,总学分
from xs
where CONVERT(char(20),总学分) like '4_' and 总学分>=40
go
--游标函数--用于返回有关游标的信息
--CURSOR_ROWS函数 返回最后打开的游标中当前存在的满足条件的行数 返回类型为 integer
--CURSOR_STATUS函数 返回游标的状态是打开还是关闭 返回值类型:smallint
use xscj
select @@CURSOR_ROWS
--select CURSOR_STATUS('local','student_cursor') as 游标状态1
declare student_cursor cursor
for select 姓名 from xs
--select CURSOR_STATUS('local','student_cursor') as 游标状态2
open student_cursor
fetch next from student_cursor
select CURSOR_STATUS('local','student_cursor') as 游标状态3
select @@CURSOR_ROWS
close student_cursor
--select CURSOR_STATUS('local','student_cursor') as 游标状态4
deallocate student_cursor
--select CURSOR_STATUS('local','student_cursor') as 游标状态5
--CURSOR_ROWS函数 返回最后打开的游标中当前存在的满足条件的行数 返回类型为 integer
--CURSOR_STATUS函数 返回游标的状态是打开还是关闭 返回值类型:smallint
use xscj
select @@CURSOR_ROWS
--select CURSOR_STATUS('local','student_cursor') as 游标状态1
declare student_cursor cursor
for select 姓名 from xs
--select CURSOR_STATUS('local','student_cursor') as 游标状态2
open student_cursor
fetch next from student_cursor
select CURSOR_STATUS('local','student_cursor') as 游标状态3
select @@CURSOR_ROWS
close student_cursor
--select CURSOR_STATUS('local','student_cursor') as 游标状态4
deallocate student_cursor
--select CURSOR_STATUS('local','student_cursor') as 游标状态5
--@@FETCH_STATUS 返回FETCH语句执行后的游标状态 返回值类型:integer
--@@FETCH_STATUS 控制while循环中的游标的活动
use xscj
declare @name varchar(20),@st_id varchar(20)
declare student_cursor cursor
for select 姓名,学号 from xs
open student_cursor
fetch next from student_cursor into @name,@st_id
select @name ,@st_id
while @@FETCH_STATUS=0
begin
fetch next from student_cursor
end
close student_cursor
deallocate student_cursor
--@@FETCH_STATUS 控制while循环中的游标的活动
use xscj
declare @name varchar(20),@st_id varchar(20)
declare student_cursor cursor
for select 姓名,学号 from xs
open student_cursor
fetch next from student_cursor into @name,@st_id
select @name ,@st_id
while @@FETCH_STATUS=0
begin
fetch next from student_cursor
end
close student_cursor
deallocate student_cursor
--DB_ID(['database_name']) 返回其数据库标识ID号 返回类型:smallint
--DB_NAME(database_id) 根据参数database_id 返回数据库名 如果参数不指定返回当前的数据库名 返回类型 nvarchar(128)
select DB_ID('wkdl')
select DB_NAME(8)
--DB_NAME(database_id) 根据参数database_id 返回数据库名 如果参数不指定返回当前的数据库名 返回类型 nvarchar(128)
select DB_ID('wkdl')
select DB_NAME(8)
--用户自定义函数
--(1)标量函数
--计算全体学生某门功课的平均成绩
use xscj
create function averageMark(@kcno char(20)) returns int
as
begin
declare @aver int
select @aver=
(
select avg(成绩)
from xs_kc
where 课程号 =@kcno
group by 课程号
)
return @aver
end
go
--(1)标量函数
--计算全体学生某门功课的平均成绩
use xscj
create function averageMark(@kcno char(20)) returns int
as
begin
declare @aver int
select @aver=
(
select avg(成绩)
from xs_kc
where 课程号 =@kcno
group by 课程号
)
return @aver
end
go
--在select语句中调用 用户自定义函数
--如下程序对上例的函数的调用
use xscj
declare @course varchar(20)
declare @aver1 int
select @course='101'
--调用用户函数,并将返回值赋值给局部变量
select @aver1=dbo.averageMark(@course)
--显示局部变量的值
select @aver1 as '101课程的平均成绩'
go
--如下程序对上例的函数的调用
use xscj
declare @course varchar(20)
declare @aver1 int
select @course='101'
--调用用户函数,并将返回值赋值给局部变量
select @aver1=dbo.averageMark(@course)
--显示局部变量的值
select @aver1 as '101课程的平均成绩'
go
--利用EXEC执行调用 用户自定义函数
use xscj
declare @aver1 int
EXEC @aver1=dbo.averageMark @kcno= '101'
select @aver1 as '101课程平均成绩'
go
use xscj
declare @aver1 int
EXEC @aver1=dbo.averageMark @kcno= '101'
select @aver1 as '101课程平均成绩'
go
--在xscj中建立一个course表,并将一个字段定义为计算列
use xscj
create table course
(
cno int,
canme nchar(20),
credit int,
--将此列定义为计算列
aver as
(
dbo.averageMark(cno)
)
)
insert into course(cno,canme,credit) values('101','计算机基础',5)
insert into course(cno,canme,credit) values('101','计算机基础',5)
insert into course(cno,canme,credit) values('102','程序设计与语言',4)
insert into course(cno,canme,credit) values('206','离散数学',4)
insert into course(cno,canme,credit) values('208','数据结构',4)
insert into course(cno,canme,credit) values('209','操作系统',4)
insert into course(cno,canme,credit) values('210','计算机原理',5)
insert into course(cno,canme,credit) values('212','数据库原理',4)
insert into course(cno,canme,credit) values('301','计算机网络',3)
insert into course(cno,canme,credit) values('302','软件工程',3)
insert into course(cno,canme,credit) values('304','算法',3)
insert into course(cno,canme,credit) values('305','软件设计',3)
select *from course
use xscj
create table course
(
cno int,
canme nchar(20),
credit int,
--将此列定义为计算列
aver as
(
dbo.averageMark(cno)
)
)
insert into course(cno,canme,credit) values('101','计算机基础',5)
insert into course(cno,canme,credit) values('101','计算机基础',5)
insert into course(cno,canme,credit) values('102','程序设计与语言',4)
insert into course(cno,canme,credit) values('206','离散数学',4)
insert into course(cno,canme,credit) values('208','数据结构',4)
insert into course(cno,canme,credit) values('209','操作系统',4)
insert into course(cno,canme,credit) values('210','计算机原理',5)
insert into course(cno,canme,credit) values('212','数据库原理',4)
insert into course(cno,canme,credit) values('301','计算机网络',3)
insert into course(cno,canme,credit) values('302','软件工程',3)
insert into course(cno,canme,credit) values('304','算法',3)
insert into course(cno,canme,credit) values('305','软件设计',3)
select *from course
--(2)内嵌表值函数 用于实现参数化视图(由于视图不支持在where子句中指定搜索条件参数)
create function fn_View1(@para nvarchar(30)) returns table
as return
(
select 学号,姓名
from xscj.dbo.xs
where 专业名 =@para
)
go
create function fn_View1(@para nvarchar(30)) returns table
as return
(
select 学号,姓名
from xscj.dbo.xs
where 专业名 =@para
)
go
--内嵌表值函数的调用
select *from fn_View1('计算机')
go
select *from fn_View1('计算机')
go
--多语句表值函数
create function score_table(@studentID char(6)) returns @score table
(
xs_ID char(6),
xs_Name char(8),
kc_Name char(16),
cj tinyint,
xf tinyint
)
as
begin
insert into @score
select s.学号,s.姓名,p.课程名,o.成绩,p.学分
from xs as s
inner join xs_kc as o on(s.学号=o.学号)
inner join kc as p on(o.课程号=p.课程号)
where s.学号 =@studentID
return
end
create function score_table(@studentID char(6)) returns @score table
(
xs_ID char(6),
xs_Name char(8),
kc_Name char(16),
cj tinyint,
xf tinyint
)
as
begin
insert into @score
select s.学号,s.姓名,p.课程名,o.成绩,p.学分
from xs as s
inner join xs_kc as o on(s.学号=o.学号)
inner join kc as p on(o.课程号=p.课程号)
where s.学号 =@studentID
return
end
--多语句表值的调用
--查询学号为 '001101'学生的各科成绩和学分
select *from score_table('001101')
------------------------------------存储过程和触发器-------------------------------
--不带参数的存储过程 返回学生学号,姓名,课程号,成绩,学分
--检查是否已存在同名的存储过程,若有,删除
use xscj
if EXISTS(select name from sysobjects where name='student_info' and type='p')
drop procedure student_info
go
--不带参数的存储过程 返回学生学号,姓名,课程号,成绩,学分
--检查是否已存在同名的存储过程,若有,删除
use xscj
if EXISTS(select name from sysobjects where name='student_info' and type='p')
drop procedure student_info
go
--创建存储过程
create procedure student_info
as
select a.学号,姓名,课程名,成绩,t.学分
from xs a inner join xs_kc b
on a.学号=b.学号 inner join kc t
on b.课程号=t.课程号
go
--student_info存储过过程的执行
EXECUTE student_info
go
create procedure student_info
as
select a.学号,姓名,课程名,成绩,t.学分
from xs a inner join xs_kc b
on a.学号=b.学号 inner join kc t
on b.课程号=t.课程号
go
--student_info存储过过程的执行
EXECUTE student_info
go
--使用带参数的存储过程
use xscj
if EXISTS(select name from sysobjects where name='student_info1' and type='p')
drop procedure student_info1
go
create procedure student_info1(@name char(8),@cname char(16))
as
select a.学号,姓名,课程名,成绩,t.学分
from xs a inner join xs_kc b
on a.学号=b.学号 inner join kc t
on b.课程号=t.课程号
where a.姓名 =@name and t.课程名 =@cname
go
--调用存储过程
EXECUTE student_info1 '王林','计算机基础'
go
--另一种方法调用存储过程
EXECUTE student_info1 @name='王林',@cname='计算机基础'
go
as
select a.学号,姓名,课程名,成绩,t.学分
from xs a inner join xs_kc b
on a.学号=b.学号 inner join kc t
on b.课程号=t.课程号
where a.姓名 =@name and t.课程名 =@cname
go
--调用存储过程
EXECUTE student_info1 '王林','计算机基础'
go
--另一种方法调用存储过程
EXECUTE student_info1 @name='王林',@cname='计算机基础'
go
--使用带有通配符参数的存储过程
use xscj
if EXISTS(select name from sysobjects where name='st_info' and type='p')
drop procedure st_info
go
--建立存储过程
create procedure st_info (@name varchar(30))
as
select a.学号,姓名,课程名,成绩,t.学分
from xs a inner join xs_kc b
on a.学号=b.学号 inner join kc t
on b.课程号=t.课程号
where 姓名 like @name
go
--调用存储过程
EXECUTE st_info '[王张]%'
use xscj
if EXISTS(select name from sysobjects where name='st_info' and type='p')
drop procedure st_info
go
--建立存储过程
create procedure st_info (@name varchar(30))
as
select a.学号,姓名,课程名,成绩,t.学分
from xs a inner join xs_kc b
on a.学号=b.学号 inner join kc t
on b.课程号=t.课程号
where 姓名 like @name
go
--调用存储过程
EXECUTE st_info '[王张]%'
--计算指定学生的总成绩
--使用带output参数的存储过程
use xscj
if EXISTS(select name from sysobjects where name='totalcredit' and type='p')
drop procedure totalcredit
go
--建立存储过程
create procedure totalcredit(@name varchar(40),@total int output)
as
select @total=SUM(成绩)
from xs,xs_kc
where xs.学号=xs_kc.学号 and 姓名 =@name
group by xs.学号
go
--调用存储过程
use xscj
declare @total int
EXECUTE totalcredit '王林',@total output
select '王林',@total
go
--使用带output参数的存储过程
use xscj
if EXISTS(select name from sysobjects where name='totalcredit' and type='p')
drop procedure totalcredit
go
--建立存储过程
create procedure totalcredit(@name varchar(40),@total int output)
as
select @total=SUM(成绩)
from xs,xs_kc
where xs.学号=xs_kc.学号 and 姓名 =@name
group by xs.学号
go
--调用存储过程
use xscj
declare @total int
EXECUTE totalcredit '王林',@total output
select '王林',@total
go
--使用output游标参数的存储过程,output游标参数用于返回存储过程的局部游标
--在XS表上声明并打开一个游标
--with encryption 对用户隐藏存储过程的文不,且调用后不显示结果
use xscj
if EXISTS(select name from sysobjects where name='st_cursor' and type='p')
drop procedure st_cursor
go
--创建存储过程
create procedure st_cursor (@st_cursor CURSOR varying output) with encryption
as
set @st_cursor=CURSOR forward_only static
for
select *from xs
open @st_cursor
go
--调用存储过程
use xscj
go
declare @MyCursor CURSOR
EXECUTE st_cursor @st_cursor=@MyCursor output
while (@@FETCH_STATUS=0)
begin
fetch next from @MyCursor
end
close @MyCursor
deallocate @MyCursor
--sp_helptext 可显示规则,默认值,未加密的存储过程,用户定义函数,触发器或视图的文本
--若加密则显示 对象备注已加密
EXEC sp_helptext st_cursor
EXEC sp_helptext totalcredit
--在XS表上声明并打开一个游标
--with encryption 对用户隐藏存储过程的文不,且调用后不显示结果
use xscj
if EXISTS(select name from sysobjects where name='st_cursor' and type='p')
drop procedure st_cursor
go
--创建存储过程
create procedure st_cursor (@st_cursor CURSOR varying output) with encryption
as
set @st_cursor=CURSOR forward_only static
for
select *from xs
open @st_cursor
go
--调用存储过程
use xscj
go
declare @MyCursor CURSOR
EXECUTE st_cursor @st_cursor=@MyCursor output
while (@@FETCH_STATUS=0)
begin
fetch next from @MyCursor
end
close @MyCursor
deallocate @MyCursor
--sp_helptext 可显示规则,默认值,未加密的存储过程,用户定义函数,触发器或视图的文本
--若加密则显示 对象备注已加密
EXEC sp_helptext st_cursor
EXEC sp_helptext totalcredit
-创建用户定义的系统的存储过程
--创建一个过程,显示表名以xs开头的所有及其对应的索引.如果没有指定参数,该过程返回表名以kc开头的所有表对应的索引
if EXISTS(select name from sysobjects where name='sp_showtable' and type='p')
drop procedure sp_showtable
go
use master
go
--创建存储过程
create procedure sp_showtable (@TABLE varchar(30)='kc%')
as
select tab.name as table_name,
inx.name as index_name,
indid as index_id
from sysindexes inx inner join sysobjects tab on tab.id=inx.id
where tab.name like @table
go
--调用存储过程
use xscj
EXEC sp_showtable 'xs%'
go
--创建一个过程,显示表名以xs开头的所有及其对应的索引.如果没有指定参数,该过程返回表名以kc开头的所有表对应的索引
if EXISTS(select name from sysobjects where name='sp_showtable' and type='p')
drop procedure sp_showtable
go
use master
go
--创建存储过程
create procedure sp_showtable (@TABLE varchar(30)='kc%')
as
select tab.name as table_name,
inx.name as index_name,
indid as index_id
from sysindexes inx inner join sysobjects tab on tab.id=inx.id
where tab.name like @table
go
--调用存储过程
use xscj
EXEC sp_showtable 'xs%'
go
--当向XS_KC表插入一个记录时,检查该记录的学号在XS表是否存在,检查课程号在KC表中是否存在
--若有一项为否,则不允许插入
use xscj
if EXISTS(select name from sysobjects where name='check_trig' and type='tr')
drop trigger check_trig
go
--创建触发器
create trigger check_trig on xs_kc for insert
as
select *from inserted a
where a.学号 not in (select b.学号 from xs b) or a.课程号 not in(select c.课程号 from kc c)
begin
raiserror('违背数据的一致性.',16,1)
rollback transaction
end
go
--数据测试
insert into xs_kc values('001101','112',80,null)
--若有一项为否,则不允许插入
use xscj
if EXISTS(select name from sysobjects where name='check_trig' and type='tr')
drop trigger check_trig
go
--创建触发器
create trigger check_trig on xs_kc for insert
as
select *from inserted a
where a.学号 not in (select b.学号 from xs b) or a.课程号 not in(select c.课程号 from kc c)
begin
raiserror('违背数据的一致性.',16,1)
rollback transaction
end
go
--数据测试
insert into xs_kc values('001101','112',80,null)
--在XS_KC表上创建一个触发器,若对学号列和课程号列修改,则会提示信息,并取消修改操作
use xscj
if EXISTS(select name from sysobjects where name='update_trig' and type='tr')
drop trigger update_trig
go
--创建触发器
create trigger update_trig on xs_kc for update
as
--检查学号列(C0) 和 课程号列(C1) 是否被修改,如果有些列被修改了,则取消修改操作
if(columns_updated()&3)>0
begin
raiserror('违背数据的一致性.',16,1)
rollback transaction
end
go
use xscj
if EXISTS(select name from sysobjects where name='update_trig' and type='tr')
drop trigger update_trig
go
--创建触发器
create trigger update_trig on xs_kc for update
as
--检查学号列(C0) 和 课程号列(C1) 是否被修改,如果有些列被修改了,则取消修改操作
if(columns_updated()&3)>0
begin
raiserror('违背数据的一致性.',16,1)
rollback transaction
end
go
--在XSCJ数据库中创建表,视图和触发器,以说明instead of insert 触发器的使用
use xscj
create table books
(
BookKey int identity(1,1),
BookName nvarchar(10) not null,
Color nvarchar(10) not null,
ComputedCol AS (BookName+Color),
Pages int
)
go
--建立一个视图,包含基表的所有列
create View View2
as
select BookKey,BookName,Color,ComputedCol,Pages
from books
go
--在View2视图上创建一个INSTEAD OF INSERT 触发器
create trigger insteadTrig on View2 instead of insert
as
begin
--实际插入时,INSERT语句中不包含BookKey字段和ComputedCol字段的值
insert into books
select BookName,Color,Pages from inserted
end
go
--正确的INSERT语句
insert into books(BookName,Color,Pages) values('计算机辅助设计','红色',100)
select *from books
--不正确的INSERT语句(因为'ComputedCol',因为该列是计算列)
insert into books(BookKey,BookName,Color,ComputedCol,Pages) values(2,'计算机辅助设计','红色','绿色',100)
--但对于视图View2,正确的INSERT语句
--虽然将BookKey和ComputedCol字段值传递了InteadTrig触发器
--但触发器中的INSERT语句没有选择inserted表BookKey和ComputedCol字段的值
insert into View2(BookKey,BookName,Color,ComputedCol,Pages) values(2,'计算机辅助设计','红色','绿色',100)
select *from View2
use xscj
create table books
(
BookKey int identity(1,1),
BookName nvarchar(10) not null,
Color nvarchar(10) not null,
ComputedCol AS (BookName+Color),
Pages int
)
go
--建立一个视图,包含基表的所有列
create View View2
as
select BookKey,BookName,Color,ComputedCol,Pages
from books
go
--在View2视图上创建一个INSTEAD OF INSERT 触发器
create trigger insteadTrig on View2 instead of insert
as
begin
--实际插入时,INSERT语句中不包含BookKey字段和ComputedCol字段的值
insert into books
select BookName,Color,Pages from inserted
end
go
--正确的INSERT语句
insert into books(BookName,Color,Pages) values('计算机辅助设计','红色',100)
select *from books
--不正确的INSERT语句(因为'ComputedCol',因为该列是计算列)
insert into books(BookKey,BookName,Color,ComputedCol,Pages) values(2,'计算机辅助设计','红色','绿色',100)
--但对于视图View2,正确的INSERT语句
--虽然将BookKey和ComputedCol字段值传递了InteadTrig触发器
--但触发器中的INSERT语句没有选择inserted表BookKey和ComputedCol字段的值
insert into View2(BookKey,BookName,Color,ComputedCol,Pages) values(2,'计算机辅助设计','红色','绿色',100)
select *from View2
--如果在XS表中添加或更改数据,则将向客户端显示一条信息
--使用带有提示消息的触发器
use xscj
if EXISTS(select name from sysobjects where name='reminder' and type='tr')
drop trigger reminder
go
--创建存储过程
create trigger reminder on xs for insert,update
as
--失败的提示信息
raiserror(4008,16,10)
--失败就回滚!该操作不进行(如果不要此句的话,虽然有提示操作失败,但操作还是要进行)
rollback transaction
go
--插入一条记录,弹出提示信息
insert into xs values('111111','王林','计算机',1,'1980-2-10',50,null)
--修改触发器reminder
use xscj
go
alter trigger reminder on xs for update
as
raiserror('执行的操作是修改',16,10)
go
--插入一条记录
insert into xs values('011101','王林','计算机',1,'1980-2-10',50,null)
--删除触发器reminder
use xscj
if EXISTS(select name from sysobjects where name='remindr' and type='tr')
drop trigger reminder
go
--使用带有提示消息的触发器
use xscj
if EXISTS(select name from sysobjects where name='reminder' and type='tr')
drop trigger reminder
go
--创建存储过程
create trigger reminder on xs for insert,update
as
--失败的提示信息
raiserror(4008,16,10)
--失败就回滚!该操作不进行(如果不要此句的话,虽然有提示操作失败,但操作还是要进行)
rollback transaction
go
--插入一条记录,弹出提示信息
insert into xs values('111111','王林','计算机',1,'1980-2-10',50,null)
--修改触发器reminder
use xscj
go
alter trigger reminder on xs for update
as
raiserror('执行的操作是修改',16,10)
go
--插入一条记录
insert into xs values('011101','王林','计算机',1,'1980-2-10',50,null)
--删除触发器reminder
use xscj
if EXISTS(select name from sysobjects where name='remindr' and type='tr')
drop trigger reminder
go
--------------------------------------------索引与数据完整性----------------------------------------
--为kc表的课程名列创建索引
--使用简单索引
use xscj
if EXISTS(select name from sysindexes where name='kc_name_ind')
drop index kc.kc_name_ind
go
--创建索引
create index kc_name_ind on kc(课程名)
go
--为kc表的课程名列创建索引
--使用简单索引
use xscj
if EXISTS(select name from sysindexes where name='kc_name_ind')
drop index kc.kc_name_ind
go
--创建索引
create index kc_name_ind on kc(课程名)
go
--根据kc表的课程号列创建唯一的聚集索引,因为指定了clustered子句,所以该索引对磁盘上的数据进行物理排序
--使用唯一聚集索引
use xscj
if EXISTS(select name from sysindexes where name='kc_id_ind')
drop index kc.kc_id_ind
go
create unique clustered index kc_id_ind on kc(课程号)
go
--删除索引
drop index kc.PK__kc__79A81403
--使用唯一聚集索引
use xscj
if EXISTS(select name from sysindexes where name='kc_id_ind')
drop index kc.kc_id_ind
go
create unique clustered index kc_id_ind on kc(课程号)
go
--删除索引
drop index kc.PK__kc__79A81403
--根据xs_kc表的学号和课程号列创建符合索引
use xscj
if EXISTS(select name
from sysindexes where name='xs_kc_ind')
drop index xs_kc.xs_kc_ind
go
create index xs_kc_ind on xs_kc(学号,课程号)
go
use xscj
if EXISTS(select name
from sysindexes where name='xs_kc_ind')
drop index xs_kc.xs_kc_ind
go
create index xs_kc_ind on xs_kc(学号,课程号)
go
--根据xs表中的总学分列创建索引,列中使用了FILLFACTOR子句
use xscj
if EXISTS(select name from sysindexes where name='score_ind')
drop index score_ind
go
create nonclustered index score_ind on xs(总学分)
with fillfactor=60
go
use xscj
if EXISTS(select name from sysindexes where name='score_ind')
drop index score_ind
go
create nonclustered index score_ind on xs(总学分)
with fillfactor=60
go
--根据xs表中学号列创建唯一聚集索引,如果输入重复的键,将忽略该INSERT或UPDATE语句
--使用 IGNORE_DUP_KEY
use xscj
if EXISTS(select name from sysindexes where name='xs_ind')
drop index xs.xs_ind
create unique clustered index xs_ind on xs(学号)
--使用 IGNORE_DUP_KEY
use xscj
if EXISTS(select name from sysindexes where name='xs_ind')
drop index xs.xs_ind
create unique clustered index xs_ind on xs(学号)
--通过SQL语句定义和绑定DEFAULT默认值对象
--将创建的默认值对象绑定到xs表的总学分字段
--定义默认规则
create default zjc_default as 0
go
--绑定规则
use xscj
EXEC sp_bindefault 'zjc_default','xs.总学分'
go
--将创建的默认值对象绑定到xs表的总学分字段
--定义默认规则
create default zjc_default as 0
go
--绑定规则
use xscj
EXEC sp_bindefault 'zjc_default','xs.总学分'
go
--首先在xscj数据库中定义表book及名为today的默认值,将其绑定到book表的hire date 列
--定义表
create table book
(
book_id char(6),
name varchar(20) not null,
hire_date datetime not null
)
go
--创建默认值对象
create default today as getdate()
go
--绑定默认值对象
EXEC sp_bindefault 'today','book.[hire_date]'
--定义表
create table book
(
book_id char(6),
name varchar(20) not null,
hire_date datetime not null
)
go
--创建默认值对象
create default today as getdate()
go
--绑定默认值对象
EXEC sp_bindefault 'today','book.[hire_date]'
--定义名为birthday_date 的数据类型,然后定义默认值对象day并将其绑定到用户定义的数据类型birthday_date中
use xscj
EXEC sp_addtype birthday_date,'datetime','NULL'
go
--定义day默认值对象
create default day as '1960,00,00'
go
--将默认值对象day绑定到birthday_date数据类型
use xscj
go
EXEC sp_bindefault 'day','birthday_date'
--解除默认值对象day与xscj库中的用户定义类型birthday_date的绑定关系,然后解除名为day的默认值对象
use xscj
go
if EXISTS(select name from sysobjects where name='day' and type='D')
begin
EXEC sp_unbindefault 'birthday_date'
drop default day
end
go
use xscj
EXEC sp_addtype birthday_date,'datetime','NULL'
go
--定义day默认值对象
create default day as '1960,00,00'
go
--将默认值对象day绑定到birthday_date数据类型
use xscj
go
EXEC sp_bindefault 'day','birthday_date'
--解除默认值对象day与xscj库中的用户定义类型birthday_date的绑定关系,然后解除名为day的默认值对象
use xscj
go
if EXISTS(select name from sysobjects where name='day' and type='D')
begin
EXEC sp_unbindefault 'birthday_date'
drop default day
end
go
--在修改表时修改创建CHECK约束
--xs_kc表中,增加成绩字段的CHECK约束
use xscj
alter table xs_kc
add constraint cj_constraint check(成绩>0 and 成绩<=100)
--删除xs_kc表中成绩字段的check约束
use xscj
alter table xs_kc
drop constraint cj_constraint
go
--xs_kc表中,增加成绩字段的CHECK约束
use xscj
alter table xs_kc
add constraint cj_constraint check(成绩>0 and 成绩<=100)
--删除xs_kc表中成绩字段的check约束
use xscj
alter table xs_kc
drop constraint cj_constraint
go
--创建一个规则,并绑定到表KC的课程号列,用于限制课程号的输入范围
use xscj
go
create rule kc_rule
as @range like '[1-5][0-9][0-9]'
go
use xscj
EXEC sp_bindrule 'kc_rule','kc.课程号'
go
use xscj
go
create rule kc_rule
as @range like '[1-5][0-9][0-9]'
go
use xscj
EXEC sp_bindrule 'kc_rule','kc.课程号'
go
--创建一个规则,用以限制输入到该规则所绑定的列中的值只能是该规则中的列出的值
use xscj
go
--创建规则
create rule list_rule
as @list IN ('C语言','离散数学','微机原理')
go
use xscj
--将规则绑定到对应列
EXEC sp_bindrule 'list_rule','KC.课程名'
go
--测试数据
--不可以插入,违反规则
insert into kc values('401','计算机基础',1,80,5)
--可以插入
insert into kc values('402','C语言',1,80,5)
use xscj
go
--创建规则
create rule list_rule
as @list IN ('C语言','离散数学','微机原理')
go
use xscj
--将规则绑定到对应列
EXEC sp_bindrule 'list_rule','KC.课程名'
go
--测试数据
--不可以插入,违反规则
insert into kc values('401','计算机基础',1,80,5)
--可以插入
insert into kc values('402','C语言',1,80,5)
--定义哟个用户数据类型course_num,然后将前面定义的规则'kc_rule'
--绑定到用户数据类型course_num上,然后定义表kc,其课程号的数据类型为course_num
use xscj
go
--用户自定义数据类型
EXEC sp_addtype 'course_num','char(3)','not null'
--将前面的用户自定义规则kc_rule绑定到用户自定义数据类型course_num上
EXEC sp_bindrule 'kc_rule','course_num'
go
--定义表KC1
create table KC1
(
课程号 course_num,
课程名 char(16) not null,
开课学期 tinyint,
学时 tinyint,
学分 tinyint
)
go
--绑定到用户数据类型course_num上,然后定义表kc,其课程号的数据类型为course_num
use xscj
go
--用户自定义数据类型
EXEC sp_addtype 'course_num','char(3)','not null'
--将前面的用户自定义规则kc_rule绑定到用户自定义数据类型course_num上
EXEC sp_bindrule 'kc_rule','course_num'
go
--定义表KC1
create table KC1
(
课程号 course_num,
课程名 char(16) not null,
开课学期 tinyint,
学时 tinyint,
学分 tinyint
)
go
--解除课程号列与kc_rule之间的绑定关系,并删除规则对象kc_rule
use xscj
go
if EXISTS(select name from sysobjects where name='kc_rule' and type='r')
begin
EXEC sp_unbindrule 'kc.课程号'
drop rule kc_rule
end
go
use xscj
go
if EXISTS(select name from sysobjects where name='kc_rule' and type='r')
begin
EXEC sp_unbindrule 'kc.课程号'
drop rule kc_rule
end
go
--解除自定义类型course_num与kc_rule 之间的绑定关系,并删除规则对象kc_rule
use xscj
go
if EXISTS(select name from sysobjects where name='kc_rule' and type='r')
begin
EXEC sp_unbindrule 'course_num'
drop rule kc_rule
end
go
use xscj
go
if EXISTS(select name from sysobjects where name='kc_rule' and type='r')
begin
EXEC sp_unbindrule 'course_num'
drop rule kc_rule
end
go
--在本地磁盘上创建一个命名备份设备
use master
EXEC sp_addumpdevice 'disk','mybackupfile','c:\mybackupfile'
--删除命名备份文件
use master
EXEC sp_dropdevice 'mybackupfile',delfile
use master
EXEC sp_addumpdevice 'disk','mybackupfile','c:\mybackupfile'
--删除命名备份文件
use master
EXEC sp_dropdevice 'mybackupfile',delfile
--使用backup语句进行完全数据库备份列子
--使用逻辑名创建一个命名的备份设备,并将数据库XSCJ完全备份到该设备
use master
EXEC sp_addumpdevice 'disk','test1','c:\db_temp\test1.bak'
go
BACKUP database xscj to test1
--使用逻辑名创建一个命名的备份设备,并将数据库XSCJ完全备份到该设备
use master
EXEC sp_addumpdevice 'disk','test1','c:\db_temp\test1.bak'
go
BACKUP database xscj to test1
--先利用事务变量命名一个事务,提交事务后,将为所有的计算机系的学生增加10个学分
use xscj
go
declare @tran_name varchar(20)
set @tran_name='MyTran1'
begin transaction @tran_name
go
use xscj
update xs
set 总学分=总学分+10
where 专业名='计算机'
go
--提交事务
commit transaction @tran_name
go
use xscj
go
declare @tran_name varchar(20)
set @tran_name='MyTran1'
begin transaction @tran_name
go
use xscj
update xs
set 总学分=总学分+10
where 专业名='计算机'
go
--提交事务
commit transaction @tran_name
go
--向XS表中插入1行数据,然后删除该行,但执行后,新插入的数据行并没有被删除
--因为事务中使用rollback语句将操作回滚到保存点My_sav即删除前的状态
begin transaction
use xscj
insert into xs
values('001115','朱一虹','计算机',1,'3/20/1980',38,NULL)
save transaction My_sav
delete from xs
where 姓名='朱一虹'
rollback transaction My_sav
commit transaction
go
--因为事务中使用rollback语句将操作回滚到保存点My_sav即删除前的状态
begin transaction
use xscj
insert into xs
values('001115','朱一虹','计算机',1,'3/20/1980',38,NULL)
save transaction My_sav
delete from xs
where 姓名='朱一虹'
rollback transaction My_sav
commit transaction
go
select *from xs