数据库学习笔记(十四)

--创建索引
create unique index IDX_sname ON student(sname)
insert into student values('0938211038', '梁欣', '女', '1992-6-3', '090802')




if exists (select name from sysindexes where name = 'IDX_sc')
DROp INDEX student.sc
go
create INDex IDX_sx ON student(studentno, classno)


--修改索引
alter index IDX_sx ON dbo.student
REORGANIZE


--统计信息
update statistics student




--创建一个存储过程
create procedure student_score1
@student_name nchar(8)
as
select sname, cname, final from student s, course c, score sc where s.studentno = sc.studentno and class.classno =sc.courseno
and s.sname = @student_name 




declare @ave numeric(6, 2)
EXEC student_score2 @student_name = '合影', @Aveage = @ave OUTPUT


create produre student_score2
@student_name nchar(8), @average numeric(6, 2) OUTPUT
as
select @average = AVG(final)
from student s, course c, score sc
where s.studentno = sc.studentno and c.courseno = sc.courseno and s.sname = @student_name




--修改存储过程
alter procedure student_score
with ENCRyption
as
select sname, cname, final
from student s, course c, score sc
where s.studentno = sc.studentno and c.courseno = sc.courseno




--触发器
create trigger update_s_tr
ON student
after update
as
if update(studentno)
begin
raiserror('不能修改学号', 16, 2)
rollback
end


update student set studentno = '2656865'
where studentno = '0937221508'




create trigger delete_c_tr
on course
INSTEAD OF DELETE
as
IF EXISTS (select * from course where type = '必须')
begin
raiserror('不能删除', 16, 2)
rollback
end 


delete from course where type = '必修'


create trigger update_s_tr
on student
after update
as 
if update(studentno)
begin
raiserror('不能删除',16, 2)
rollback
end


--启动事务
declare @TranName varchar(20);
select @TranName = 'Add_score';
begin tran @TranName;
update score set final = final + 5
where courseno = 'c05109'
commit TRAN @TranName
go

你可能感兴趣的:(数据库学习笔记(十四))