使用教材数据库(1)中创建的数据库,建立学生的insert触发器,若向学生表中插入一条记录,则自动向订购表中插入一行,令该学生订购被订购数量最多的教材
创建视图存储书号和订购总数的信息
use jiaocai
go
create view linshi
as
select b_id,count(*) as zongshu from orders
group by b_id
go
在学生表上创建触发器
create trigger order_insert on student
for insert
as
begin
declare @book varchar(20)
declare @sno varchar(30)
select @sno = id from inserted
select @book = b_id from linshi
where zongshu = (
select max(zongshu) from linshi
)
insert into orders(s_id,b_id,number)
values
(@sno,@book,null)
end
测试触发器:
insert into student values ('2211341','王丹',19,'男','数学')
select *from orders
where s_id = '2211341'