存储过程、触发器和事务处理
2007-04-29 09:00
295 查看
存储过程、触发器和事务处理
--创建数据库 scroll dynamic
create database StudentDB
GO
--置此数据库为当前数据库
use StudentDB
GO
--创建学生表
create table student
(
SId varchar(20) primary key, --学生编号
SName varchar(20), --学生姓名
SClass varchar(20), --学生班级
SSex varchar(10), --学生性别
SScore float default(0) check(SScore>=0) --学生平均分
)
GO
--创建课程表
create table class
(
EId varchar(20) primary key, --课程编号
EName varchar(20), --课程名称
ETime int check(ETime>=0) --课程课时
)
GO
--创建分数表
create table score
(
SId varchar(20), --学生编号
EId varchar(20), --课程编号
EScore float, --课程分数
primary key(SID,EID), --定义主码
foreign key (SID) references student(SID) on delete cascade, --声明及联删除
foreign key (EID) references class(EID) on delete cascade --声明及联删除
)
GO
--创建计算平均值得触发器
create trigger trigger_avg_insert on score for insert
as
begin transaction
declare @count int
update student set SScore=(select avg(EScore) from score where SId=(select SId from inserted)) where SId=(select SId from inserted)
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
--创建计算平均值得触发器
create trigger trigger_avg_delete on score for delete
as
begin transaction
update student set SScore=(select avg(EScore) from score where SId=(select SId from deleted)) where SId=(select SId from deleted)
declare @count int
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
--创建计算平均值得触发器
create trigger trigger_avg_update on score for update
as
begin transaction
declare @count int
update student set SScore=(select avg(EScore) from score where SId=(select SId from inserted)) where SId=(select SId from deleted)
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建查询学生信息的存储过程
create proc proc_student_select
as
begin transaction
declare @count int
select * from student
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建查找平均分存储过程
CREATE PROCEDURE proc_student_avg
(
@SID varchar(20)
)
AS
begin transaction
select avg(EScore) as SAvg from score where SId=@SId
declare @count int
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建通过学号查询学生信息的存储过程
create proc proc_student_select_bySId
(
@SId varchar(20)
)
as
begin transaction
declare @count int
select * from student where SId=@SId
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建插入学生信息的存储过程
create proc proc_student_insert
(
@SId varchar(20),
@SName varchar(20),
@SClass varchar(20),
@SSex varchar(10)
)
as
begin transaction
declare @count int
insert into student(SID,SName,SClass,SSex) values(@SId,@SName,@SClass,@SSex)
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--删除学生信息的存储过程
create proc proc_student_delete
(
@SId varchar(20)
)
as
begin transaction
declare @count int
delete from student where SId=@SId
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--修改学生信息的存储过程
create proc proc_student_update
(
@SId varchar(20),
@SName varchar(20),
@SClass varchar(20),
@SSex varchar(10)
)
as
begin transaction
declare @count int
update student set SName=@SName,SClass=@SClass,SSex=@SSex where SId=@SId
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建查询课程信息的存储过程
create proc proc_class_select
as
begin transaction
declare @count int
select * from class
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建通过课程号查询课程信息的存储过程
create proc proc_class_select_byEId
(
@EId varchar(20)
)
as
begin transaction
declare @count int
select * from class where EId=@EId
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
--创建插入课程信息的存储过程
GO
create proc proc_class_insert
(
@EId varchar(20),
@EName varchar(20),
@ETime int
)
as
begin transaction
declare @count int
insert into class(EId,EName,ETime) values(@EId,@EName,@ETime)
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
--创建删除课程信息的存错过程
GO
create proc proc_class_delete
(
@EId varchar(20)
)
as
begin transaction
declare @count int
delete from class where EId=@EId
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
--创建修改课程信息的存储过程
GO
create proc proc_class_update
(
@EId varchar(20),
@EName varchar(20),
@ETime int
)
as
begin transaction
declare @count int
update class set EName=@EName,ETime=@ETime where EId=@EId
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建查询成绩信息的存储过程
create proc proc_score_select
as
begin transaction
declare @count int
select * from score
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建通过学号查询成绩信息的存储过程
create proc proc_score_select_bySId
(
@SId varchar(20)
)
as
begin transaction
declare @count int
select * from score where SId=@SId
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建通过查询成绩信息的存储过程
create proc proc_score_select_byEId
(
@EId varchar(20)
)
as
begin transaction
declare @count int
select * from score where EId=@EId
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建插入成绩信息的存储过程
create proc proc_score_insert
(
@SId varchar(20),
@EId varchar(20),
@EScore float
)
as
begin transaction
declare @count int
insert into score(SId,EId,EScore) values(@SId,@EId,@EScore)
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建删除成绩信息的存错过程
create proc proc_score_delete
(
@SId varchar(20),
@EId varchar(20)
)
as
begin transaction
declare @count int
delete from score where SId=@SId and EId=@EId
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建通过学号删除成绩信息的存错过程
create proc proc_score_delete_bySId
(
@SId varchar(20)
)
as
begin transaction
declare @count int
delete from score where SId=@SId
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建通过课程号删除成绩信息的存错过程
create proc proc_score_delete_byEId
(
@EId varchar(20)
)
as
begin transaction
declare @count int
delete from score where EId=@EId
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建修改成绩信息的存储过程
create proc proc_score_update
(
@SId varchar(20),
@EId varchar(20),
@EScore float
)
as
begin transaction
declare @count int
update score set EScore=@EScore where SId=@SId and EId=@EId
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建查询学生所有信息的存储过程
create proc proc_student_one_information
(
@SId varchar(20)
)
as
begin transaction
declare @count int
select student.SName,student.SClass,student.SSex,class.EName,class.ETime,score.EScore,student.SScore from student,class,score where student.SId=score.SId and class.EId=score.EId and student.SId=@SId
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建查询所有学生所有信息的存储过程
create proc proc_student_all_information
as
begin transaction
declare @count int
select student.SName,student.SClass,student.SSex,class.EName,class.ETime,score.EScore,student.SScore from student,class,score where student.SId=score.SId and class.EId=score.EId
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建某个学生已经有了分数的课程
create proc proc_class_in
(
@SId varchar(20)
)
as
begin transaction
declare @count int
select class.EId,class.EName from class,score where class.EId=score.EId and score.SId=@SId
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
GO
--创建某个学生没有分数的课程
create proc proc_class_notin
(
@SId varchar(20)
)
as
begin transaction
declare @count int
select EId,EName from class where EId not in (select EId from score where SId=@SId)
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction
相关文章推荐
- MySQL之21-29 重点: 视图,触发器,存储过程,游标,事务处理
- SQL2008--SQL语句-存储过程-触发器-事务处理-基本语法-函数
- 函数,视图,存储过程,触发器,sysobjects (系统对象表),事务,异常
- MySQL事务、存储过程、视图、自定义函数、触发器、事件的简单使用及理解
- mysql 存储过程、触发器、视图、游标、分布式事务--之触发器
- SQL SERVER 2005视图、索引、存储过程、触发器、游标及事务名词解释
- 走向面试之数据库基础:三、SQL进阶之变量、事务、存储过程与触发器
- Oracle数据库之视图、索引、存储过程、触发器、事务、函数
- 2013.10.13 事务特性、MYSQL 触发器、存储过程、函数
- 第18天(就业班) 预编译sql处理(防止sql注入)、存储过程、批处理、插入数据获取增长值、事务、大文本类型处理
- 存储过程、事务、触发器。
- 触发器、存储过程和事务处理使用
- 视图、索引、存储过程 、触发器、游标及事务详解
- oracle数据库安全,事务机制,触发器和存储过程
- JDBC 事务处理机制、批处理、触发器
- 关于事务,存储过程,触发器的简单例子
- MySQL 系列(三)你不知道的 视图、触发器、存储过程、函数、事务、索引、语句
- SQLServer 存储过程 带事务处理实例(四)
- MySQL 存储过程/游标/触发器/事务
- 存储过程、函数、带事务的触发器——对比