database 如何在表中设置(组合)两个主键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11578703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set (combine) two primary keys in a table
提问by panindra
For a small sales related application, we designed database using logical data model. Come to the stage to convert in to physical model. While creating table in SQL Server Management Studio Express, according to our logical data model, we need to combine two attributes to form unique id. Is it possible to combine two primary keys and set it?
对于一个小型的销售相关应用程序,我们使用逻辑数据模型设计了数据库。上台转换为实物模型。在 SQL Server Management Studio Express 中创建表时,根据我们的逻辑数据模型,我们需要将两个属性组合起来形成唯一的 id。是否可以组合两个主键并设置它?
But while observing Northwind Sample, we found that in the ORDER DETAILStable, we can see two primary keys Order Id& Product Id. And according to rule table can't have two primary keys. So how it happened in Northwind?
但是在观察 Northwind Sample 时,我们发现在ORDER DETAILS表中,我们可以看到两个主键Order Id& Product Id。并且根据规则表不能有两个主键。那么它是如何发生在北风的呢?
In my case how should I set two columns in my table to make it as two primary keys?
在我的情况下,我应该如何在表中设置两列以使其成为两个主键?
Someone gave suggestion like
有人提出了这样的建议
To make a two primary key, open the table in design view and click on the two of the required fields and holding CTL, apply primary key
要创建两个主键,请在设计视图中打开表并单击两个必填字段并按住 CTL,应用主键
Will this work ??
这行得通吗??
Edit ;
编辑 ;
now my table have 2 PK's in the SSMS . is it valid or it is just a combinationof 2 pks
现在我的桌子在 SSMS 中有 2 个 PK。它是有效的还是只是 2 包的组合
回答by marc_s
The easiest way would be to use a T-SQL command in SSMS Express rather than trying to use the visual designers.....
最简单的方法是在 SSMS Express 中使用 T-SQL 命令,而不是尝试使用视觉设计器.....
Once you've designed and created your table, try something like this:
设计并创建好表格后,请尝试以下操作:
ALTER TABLE dbo.YourTableNameHere
ADD CONSTRAINT PK_YourTableNameHere
PRIMARY KEY(Item_Id, Purchase_Id)
回答by Joe G Joseph
you cannot create two primary keys in a table. You can combine two columns in a table and make as a single primary key
您不能在一个表中创建两个主键。您可以将表中的两列合并为一个主键
create table table1
(
col1 int,
col2 varchar(20),
....
Primary key (col1, col2)
)
回答by Diego
you cant have 2 primary keys but you can have a composite primary key, which is a key made of two columns, which is exactly what you got on the [SalesOrderDetail] table with [SalesOrderID] and [SalesOrderDetailID]
你不能有 2 个主键,但你可以有一个复合主键,它是一个由两列组成的键,这正是你在 [SalesOrderDetail] 表上得到的 [SalesOrderID] 和 [SalesOrderDetailID]
回答by jainvikram444
CREATE TABLE [dbo].[tab2](
[col1] [int] NOT NULL,
[col2] [int] NOT NULL,
[col3] [nchar](10) NOT NULL,
[col4] [nchar](10) NOT NULL,
CONSTRAINT [PK_tab2] PRIMARY KEY CLUSTERED
(
[col1] ASC,
[col2] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

