Create Database book
Use book
Create Table personlike
(
Id int primary key,
Likecontent varchar (100) not null
)
Insert into personlike values (1, 'Sports ')
Insert into personlike values (2, 'travel ')
Insert into personlike values (3, 'game ')
Insert into personlike values (4, 'Surfing net ')
Insert into personlike values (5, 'dating ')
Insert into personlike values (6, 'cine ')
Insert into personlike values (7, 'shopping ')
Insert into personlike values (8, 'lobby ')
Insert into personlike values (9 ,'Program')
Insert into personlike values (10, 'assemblies ')
Create Table vote
(
Voteid int primary key,
Voteitem varchar (100) not null,
Votenum int default 0
)
Insert into vote values (1, 'wei Gaofeng ', 0)
Insert into vote values (2, 'taobao', 0)
Insert into vote values (3, 'alizhan fei', 0)
Use vote
Delete table votemaster
Use book
Create Table votemaster
(
Voteid int primary key,
Votetitle varchar (150) not null,
Votesum int default 0
)
Insert into votemaster values (1, 'election of the meeting Chairman! ', 0)
Insert into votemaster values (2, 'comments on website construction! ', 0)
Select * From votemaster
Create Table votedetails
(
-- Id int identity (1, 1) primary key, -- auto-Increment
Voteid int foreign key references votemaster (voteid ),
Votedetailsid int not null,
Voteitem varchar (20) not null,
Votenum int default 0
Primary Key (voteid, votedetailsid)
)
Insert into votedetails values (1, 1, 'wei shout', 0)
Insert into votedetails values (1, 2, 'alizhan fei', 0)
Insert into votedetails values (1, 3, 'valley source', 0)
Insert into votedetails values (, 'Very good', 0)
Insert into votedetails values (2, 2, 'hao', 0)
Insert into votedetails values (2, 3, 'General ', 0)
Insert into votedetails values (2, 4, 'to be improved', 0)
Select * From votedetails
-- Create a trigger
Create trigger updatemaster
On votedetails
For update
As
Begin
Update votemaster set votesum = votesum + 1 where voteid = (select top 1 voteid from inserted)
End
-- Use a subquery to make voteid = the ID of the newly inserted record