[英]Add multiple primary keys to a table
I have a table with a primary key available;我有一个可用主键的表; I want to add another one in primary but can't add it.我想在主要中添加另一个但不能添加它。
SRNO SRNO | TRNDATE TRNDATE | RATE速度 | AMT AMT |
---|---|---|---|
1 1个 | 08-02-2022 08-02-2022 | 120 120 | 120 120 |
2 2个 | 09-02-2022 09-02-2022 | 170 170 | 170 170 |
3 3个 | 10-02-2022 10-02-2022 | 180 180 | 180 180 |
I want to add column Trndate
to the primary key.我想将列Trndate
添加到主键。
When I was try to add in primary following code当我尝试添加主要的以下代码时
ALTER TABLE dbo.avgEnt
ADD CONSTRAINT PK_avgent PRIMARY KEY NONCLUSTERED (srno, trndate);
I get an error:我得到一个错误:
Msg 1779, Level 16, State 0, Line 1消息 1779,16 级,State 0,第 1 行
Table 'avgEnt' already has a primary key defined on it.表“avgEnt”已经定义了一个主键。Msg 1750, Level 16, State 0, Line 1消息 1750,16 级,State 0,第 1 行
Could not create constraint or index.无法创建约束或索引。 See previous errors.查看以前的错误。
When I open the design of table and add primary key (SRNO, TRNDATE)
then it works fine.当我打开表的设计并添加主键(SRNO, TRNDATE)
时,它工作正常。
When I was trying to add by using the query, it does not work.当我尝试使用查询添加时,它不起作用。
What happened when design windows save that time script i want.设计 windows 保存我想要的时间脚本时发生了什么。
My database version is SQL Server 2014.我的数据库版本是 SQL Server 2014。
A table cannot have multiple PRIMARY KEY
s, this is simply not allowed, there is not work around.一个表不能有多个PRIMARY KEY
s,这是不允许的,没有解决方法。 It is by design.这是设计使然。
A table can, however, have multiple UNIQUE INDEX
es or UNIQUE CONSTRAINT
s.但是,一个表可以有多个UNIQUE INDEX
es 或UNIQUE CONSTRAINT
s。 These are not the same as a PRIMARY KEY
but do offer similar functionality, and may well provide the solution you need.这些与PRIMARY KEY
不同,但提供类似的功能,并且很可能提供您需要的解决方案。 A column (or set of columns) that are part of a UNIQUE INDEX
can also be the target of a FOREIGN KEY
, which if your "need" for a second PRIMARY KEY
is so you can use it as a FOREIGN KEY
then this would also fulfil that requirement.作为UNIQUE INDEX
一部分的一列(或一组列)也可以是FOREIGN KEY
的目标,如果您“需要”第二个PRIMARY KEY
这样您就可以将它用作FOREIGN KEY
那么这也满足那个要求。
As an example, you might have something like this:例如,您可能有这样的事情:
CREATE TABLE dbo.PrimaryTable (ID int IDENTITY,
SomeValue varchar(10) NULL,
AnotherIdentifier uniqueidentifier NOT NULL
CONSTRAINT PK_PrimaryTable PRIMARY KEY (ID));
CREATE UNIQUE INDEX UQ_PrimaryTable_AnotherIdentifier ON dbo.PrimaryTable (AnotherIdentifier);
GO
CREATE TABLE dbo.SecondaryTable (ID int IDENTITY,
PrimaryID int NOT NULL,
AnotherValue decimal(12,2) NOT NULL,
CONSTRAINT PK_SecondaryTable PRIMARY KEY (ID),
CONSTRAINT FK_SecondaryTable_PrimaryTable FOREIGN KEY (PrimaryID) REFERENCES dbo.PrimaryTable (ID));
GO
CREATE TABLE dbo.AnotherTable (ID int IDENTITY,
AnotherID uniqueidentifier NOT NULL,
OtherValue int NOT NULL,
CONSTRAINT PK_AnotherTable PRIMARY KEY (ID),
CONSTRAINT FK_SecondaryTable_PrimaryTable_AnotherIdentifier FOREIGN KEY (AnotherID) REFERENCES dbo.PrimaryTable (AnotherIdentifier));
GO
So here you can see that dbo.SecondaryTable
uses the ID
column of dbo.PrimaryTable
as it's FOREIGN KEY
, however, dbo.AnotherTable
uses the column AnotherIdentifier
even though it isn't the PRIMARY KEY
.所以在这里你可以看到dbo.SecondaryTable
使用dbo.PrimaryTable
的ID
列,因为它是FOREIGN KEY
,然而, dbo.AnotherTable
使用AnotherIdentifier
列,即使它不是PRIMARY KEY
。 This is because the column has a UNIQUE INDEX
on it, meaning that data integrity can be maintained.这是因为该列上有一个UNIQUE INDEX
,这意味着可以保持数据的完整性。
Otherwise, as I mentioned in the comments, you could split the table into 2 tables, and then have a 3rd table with a composite key to manage the relationship.否则,正如我在评论中提到的,您可以将表拆分为 2 个表,然后使用带有复合键的第 3 个表来管理关系。 So using the dbo.PrimaryTable
you might have something like this:所以使用dbo.PrimaryTable
你可能有这样的东西:
CREATE TABLE dbo.PrimaryTable (ID int IDENTITY,
SomeValue varchar(10) NULL,
CONSTRAINT PK_PrimaryTable PRIMARY KEY (ID));
CREATE TABLE dbo.AnotherPrimaryTable (AnotherIdentifier uniqueidentifier NOT NULL,
CONSTRAINT PK_AnotherPrimaryTable PRIMARY KEY (AnotherIdentifier));
GO
CREATE TABLE dbo.PrimaryTableLink (ID int NOT NULL,
AnotherIdentifier uniqueidentifier NOT NULL,
CONSTRAINT PK_PrimaryTableLink PRIMARY KEY (ID,AnotherIdentifier),
CONSTRAINT FK_PrimaryTableLink_PrimaryTable FOREIGN KEY (ID) REFERENCES dbo.PrimaryTable (ID),
CONSTRAINT FK_PrimaryTableLink_AnotherPrimaryTable FOREIGN KEY (AnotherIdentifier) REFERENCES dbo.AnotherPrimaryTable (AnotherIdentifier));
Which you use is up to you.你使用哪个取决于你。 It more depends on your design and requirements.这更多取决于您的设计和要求。 I would likely only use the latter if you needed a many to many relationship, as with a one to one the UNIQUE INDEX
serves the goal fine.如果您需要多对多关系,我可能只会使用后者,因为对于一对一, UNIQUE INDEX
可以很好地实现目标。 If you wanted to use the multiple table solution for a one to one relationship, you'd likely need to also add UNIQUE INDEX
es to the ID
and AnotherIdentifier
columns on dbo.PrimaryTableLink
, which poses the question of why didn't you create said INDEX
on the original column in dbo.PrimaryTable
first?如果您想将多表解决方案用于一对一关系,您可能还需要将UNIQUE INDEX
es 添加到dbo.PrimaryTableLink
上的ID
和AnotherIdentifier
列,这提出了为什么不创建 said 的问题在dbo.PrimaryTable
中的原始列上的INDEX
首先?
I find one solution.我找到了一个解决方案。
select OBJECT_NAME(OBJECT_ID) AS NameofConstraint
FROM sys.objects
where OBJECT_NAME(parent_object_id)='avgEnt'
and type_desc LIKE '%CONSTRAINT'
Find constraint of table PK_avgent
查找表PK_avgent
的约束
After drop primary on table在表上删除主要之后
-- drop current primary key constraint
ALTER TABLE dbo.avgEnt
DROP CONSTRAINT PK_avgent;
GO
After add two column添加两列后
-- create new primary key constraint
ALTER TABLE dbo.avgEnt
ADD CONSTRAINT PK_avgent PRIMARY KEY NONCLUSTERED (SrNo, TrnDate);
GO
it is work form me.这是我的工作。
You can not alter the primary key.您不能更改主键。 The only way is dropping it and then recreating it.唯一的方法是删除它然后重新创建它。
ALTER TABLE avgEnt DROP PRIMARY KEY, ADD PRIMARY KEY(srno,trndate);
https://www.db-fiddle.com/f/BQuEjw2pthMDb1z8NTdHv/0 check aswell https://www.db-fiddle.com/f/BQuEjw2pthMDb1z8NTdHv/0也检查一下
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.