sql中使用触发器
create database sale
go
use sale
go
create table goods
(
id int primary key identity(1,1),
name varchar(20) not null,
count int check(count>=0)
)
create table sale
(
id int primary key identity(1,1),
gid int references goods(id),
time datetime default(getdate()),
count int check(count>0)
)
insert into goods values('宝马',10)
go
select * from goods
go
create trigger salegoods
on sale for insert
as
declare @gid int,@count int
select @gid=gid,@count=COUNT from inserted
update goods set count=count-@count where id=@gid
go
select * from goods
select * from sale
insert into sale values (1,getdate(),1);