仅当两列在一行中重复时的唯一约束
Unique constraint only when two columns are duplicated in a row
我有一个投票table,其中包含被投票的用户的公司名称和被投票的用户的唯一客户编号。
用户对其他公司投票(赞成票或反对票),并且必须提供他们唯一的客户编号才能投票。
我想创建一个唯一约束,允许用户只能投票一次,但不能对同一家公司投票两次,但我希望同一个客户号码可以对多个查询投票。我只知道唯一约束,但这不起作用,因为它只允许用户在激活唯一约束时对任何公司投票一次。
例如(这一切都很好):
CompanyID Voted(customer number)
Maccas 12345
BMW 12345
Maccas 66666
并且我想约束以下内容(通过在公司名称和客户编号相同时激活约束):
CompanyID Voted
Maccas 12345
Maccas 12345 (should prevent duplicate of both columns co-existing)
我的代码:
string voteTable = "INSERT INTO dbo.votes (CompanyID,Voted) Values (@CompanyID,@Voted)";
canUserVote(); //checks if customer number exists
this.companyName = Convert.ToString(e.CommandArgument);
SqlCommand insertVoterDetails = new SqlCommand (voteTable,voteDb); //inserts voter details to votes table
insertVoterDetails.Parameters.AddWithValue("@CompanyID", this.companyName);
insertVoterDetails.Parameters.AddWithValue("@Voted", custVoteTextBox.Text);
感谢您的帮助!
当您创建 table 然后按以下方式添加复合主键..
CREATE TABLE Voting
(
[CompanyID] INT NOT NULL,
[Voted] INT NOT NULL
CONSTRAINT PK_CompanyVote PRIMARY KEY NONCLUSTERED ([CompanyID], [Voted])
)
或如下
create table Voting (
CompanyID integer not null,
Voted integer not null,
primary key (CompanyID, Voted)
);
这是 SQLFiddle,下面是完整的代码。这里我创建了基本的 company, user and company user
table,并插入了一些虚拟记录。
注意:我也施加了外键约束以确保您插入正确的值。但是,如果您愿意,可以离开这一部分。
创建tables
create table C
(
CompanyID int primary key,
CompanyName varchar(50)
);
Create table U
(
UserID int primary key,
UserName varchar(50)
);
Create table CU
(
CompanyID int,
UserID int,
FOREIGN KEY (CompanyID) REFERENCES C(CompanyID),
FOREIGN KEY (UserID) REFERENCES U(UserID),
primary key (CompanyID, UserID)
);
插入语句
Insert into c values (101,'Google')
Insert into c values (102,'Yahoo')
Insert into c values (103,'Microsoft')
Insert into U values (1001,'Mike')
Insert into U values (1002,'John')
Insert into U values (1003,'Hanis')
Insert into CU values (101,1001)
Insert into CU values (101,1002)
Insert into CU values (102,1002)
当我触发以下查询时,我也收到了一个错误,这在您的情况下是预料之中的。
Insert into CU values (102,1002)
Msg 2627, Level 14, State 1, Line 1 Violation of PRIMARY KEY
constraint 'PK__CU__FCEF90863A56FFA3'. Cannot insert duplicate key in
object 'dbo.CU'. The duplicate key value is (102, 1002). The statement
has been terminated.
我有一个投票table,其中包含被投票的用户的公司名称和被投票的用户的唯一客户编号。
用户对其他公司投票(赞成票或反对票),并且必须提供他们唯一的客户编号才能投票。
我想创建一个唯一约束,允许用户只能投票一次,但不能对同一家公司投票两次,但我希望同一个客户号码可以对多个查询投票。我只知道唯一约束,但这不起作用,因为它只允许用户在激活唯一约束时对任何公司投票一次。
例如(这一切都很好):
CompanyID Voted(customer number)
Maccas 12345
BMW 12345
Maccas 66666
并且我想约束以下内容(通过在公司名称和客户编号相同时激活约束):
CompanyID Voted
Maccas 12345
Maccas 12345 (should prevent duplicate of both columns co-existing)
我的代码:
string voteTable = "INSERT INTO dbo.votes (CompanyID,Voted) Values (@CompanyID,@Voted)";
canUserVote(); //checks if customer number exists
this.companyName = Convert.ToString(e.CommandArgument);
SqlCommand insertVoterDetails = new SqlCommand (voteTable,voteDb); //inserts voter details to votes table
insertVoterDetails.Parameters.AddWithValue("@CompanyID", this.companyName);
insertVoterDetails.Parameters.AddWithValue("@Voted", custVoteTextBox.Text);
感谢您的帮助!
当您创建 table 然后按以下方式添加复合主键..
CREATE TABLE Voting
(
[CompanyID] INT NOT NULL,
[Voted] INT NOT NULL
CONSTRAINT PK_CompanyVote PRIMARY KEY NONCLUSTERED ([CompanyID], [Voted])
)
或如下
create table Voting (
CompanyID integer not null,
Voted integer not null,
primary key (CompanyID, Voted)
);
这是 SQLFiddle,下面是完整的代码。这里我创建了基本的 company, user and company user
table,并插入了一些虚拟记录。
注意:我也施加了外键约束以确保您插入正确的值。但是,如果您愿意,可以离开这一部分。
创建tables
create table C
(
CompanyID int primary key,
CompanyName varchar(50)
);
Create table U
(
UserID int primary key,
UserName varchar(50)
);
Create table CU
(
CompanyID int,
UserID int,
FOREIGN KEY (CompanyID) REFERENCES C(CompanyID),
FOREIGN KEY (UserID) REFERENCES U(UserID),
primary key (CompanyID, UserID)
);
插入语句
Insert into c values (101,'Google')
Insert into c values (102,'Yahoo')
Insert into c values (103,'Microsoft')
Insert into U values (1001,'Mike')
Insert into U values (1002,'John')
Insert into U values (1003,'Hanis')
Insert into CU values (101,1001)
Insert into CU values (101,1002)
Insert into CU values (102,1002)
当我触发以下查询时,我也收到了一个错误,这在您的情况下是预料之中的。
Insert into CU values (102,1002)
Msg 2627, Level 14, State 1, Line 1 Violation of PRIMARY KEY constraint 'PK__CU__FCEF90863A56FFA3'. Cannot insert duplicate key in object 'dbo.CU'. The duplicate key value is (102, 1002). The statement has been terminated.