/*主表*/
create table VoteMaster
(
VoteID INT PRIMARY KEY,/*主键ID*/
VoteTitle nvarchar(50) not null,/*投票主题*/
VoteSum int not null default(0),/*投票总数*/
)
insert into VoteMaster(VoteID,VoteTitle,VoteSum) values(1,'你喜欢黑马的那些老师?',0)
insert into VoteMaster(VoteID,VoteTitle,VoteSum) values(2,'wp7你觉得难学吗?',0)
select*from voteMaster
/*从表*/
create table VoteDetails
(
VoteID int foreign key references VoteMaster(VoteID),/*外键*/
VoteDetailsID int not null,/*投票流水号*/
VoteItem varchar(50) not null,
VoteNum int not null default(0),/*投票项数*/
primary key(VoteID,VoteDetailsID)/*联合主键*/
)
insert into VoteDetails values(1,1,'杨老师',0)
insert into voteDetails values(1,2,'邹老师',0)
insert into voteDetails values(1,3,'张老师',0)
insert into voteDetails(VoteID,VoteDetailsID,VoteItem,VoteNum) values(2,1,'非常难',0)
insert into voteDetails(VoteID,VoteDetailsID,VoteItem,VoteNum) values(2,2,'很难',0)
insert into voteDetails(VoteID,VoteDetailsID,VoteItem,VoteNum) values(2,3,'一般',0)
create trigger updateMaster
on VoteDetails
for update
as
begin
update VoteMaster set VoteSum=VoteSum+1 where VoteID=(select top 1 VoteID from inserted)
end
/*模拟点击投票*/
update VoteDetails set VoteNum=VoteNum+1 where VoteDetailsID=1 AND VoteID=1
update VoteDetails set VoteNum=VoteNum+1 where VoteDetailsID=2 AND VoteID=1