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

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
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值