您的位置:首页 > 数据库

SQL Server 复习 增删改查 视图 事务 存储过程 触发器

2013-11-13 21:29 507 查看
drop table classInfo;

create table classInfo

(

classId int not null ,

className varchar(30) not null,

classCount int

)

--插入一条数据

insert into classInfo(classId,className,classCount) values(1,'JAVA1班',32);

insert into classInfo(classId,className,classCount) values(2,'JAVA2班',22);

insert into classInfo(classId,className,classCount) values(3,'Android',40);

--删除一条数据

delete from classInfo where classId=1;

--更改一个字段

update classInfo set classCount=24 where classId = 1;

select * from classInfo

--添加一个字段

alter table classInfo add classGrade int null

--删除一个字段

alter table classInfo drop column classGrade

select * from stuInfo_tb

update stuInfo_tb set classId = 2 where stuId = 1003;

--删除一个字段的约束,再删除这个字段

alter table stuInfo_tb

drop constraint IX_stuTelNumber;

alter table stuInfo_tb

drop column stuTelNumber;

--设置classId为主键

alter table classInfo

add constraint PK_classId primary key(classId);

--设置stuInfo_tb的classId为外键,与classInfo表中的classId相对应

alter table stuInfo_tb add constraint FK_classInfo_stuInfo

foreign key(classId) references classInfo(classId)

--创建一个视图

if exists(select * from sysobjects

where name = 'view_stu')

drop view view_stu

go

create view view_stu

as

select stuNam,stuGender,className,classCount

from classInfo,stuInfo_tb where

classInfo.classId = stuInfo_tb.classId;

--创建一个视图

if exists(select * from sysobjects

where name = 'view_stu')

drop view view_stu

go

create view view_stu

as

select stuNam,stuGender,className,classCount

from classInfo inner join stuInfo_tb on

classInfo.classId = stuInfo_tb.classId;

--删除一个视图:

drop view view_stu;

--查询视图

select * from view_stu;

--事务:

-- 事务的四大特征:原子性,一致性,隔离性,永久性

-- 事务分为begin 、rollback、commit transaction三种情况

--创建一个事务

begin transaction

declare @count int

declare @count1 int

set @count = 0

set @count1 = 2

select * from classInfo where classId = @count1

set @count = @count + @@error

update classInfo set classCount = 43 where classId = @count1

set @count = @count + @@error

if(@count <> 0)

begin

rollback transaction

end

else

begin

commit transaction

end

--删除一个存储过程

drop procedure pro_stu;

--创建一个存储过程,不能给局部变量设置默认值,但可以给全局变量设置默认值

create procedure pro_stu

@classNo int = 1

as

select * from classInfo where classId = @classNo;

--创建一个存储过程,带输出参数

create procedure pro_cls

@classNo int = 1 ,

@className varchar(30) output

as

select @className = className from classInfo where classId = @classNo;

--执行存储过程

declare @ss varchar(30)

exec pro_cls 2,@ss output

print '----->'+ @ss

--触发器:

-- 是基于在一个表上,当该表发生delete、insert或者update

-- 中的一个动作,则触发对应的触发器并执行触发器;当表上建立了触发器,

-- 则在执行所对应的动作时,就会产生一个deleted表或者inserted表,通过该表可以

-- 查找到被删除或者插入这条记录的所有信息

--删除一个触发器:

drop trigger tri_Name

--创建一个触发器:

create trigger tri_Name

on stuInfo_tb

for delete

as

delete from classInfo where classId = (

select classId from deleted

)

select * from classInfo

select * from stuInfo_tb

insert into stuInfo_tb(stuNam,stuGender,stuAge,classId)

values('小花','男',23,3)

delete from stuInfo_tb where classId = 3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐