如何在表中设置(组合)两个主键 [英] How to set (combine) two primary keys in a table

查看:680
本文介绍了如何在表中设置(组合)两个主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于与销售有关的小型应用程序,我们使用逻辑数据模型设计了数据库。进入转换为物理模型的阶段。在SQL Server Management Studio Express中创建表时,根据我们的逻辑数据模型,我们需要组合两个属性以形成唯一的ID。

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?

但是在观察Northwind示例时,我们发现 ORDER DETAILS 表,我们可以看到两个主键 Order ID & 产品ID 。而且根据规则表不能有两个主键。那么在罗斯文风是怎么回事呢?

But while observing Northwind Sample, we found that in the ORDER DETAILS table, 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?

在我的情况下,应该如何在表中设置两列以使其成为两个主键?

In my case how should I set two columns in my table to make it as two primary keys?

有人提出了这样的建议,例如

Someone gave suggestion like


要创建两个主键,请在设计视图中打开表,然后单击
。两个必填字段并持有CTL,应用主键

这项工作将会成功吗

编辑;

现在我的表在SSMS中有2个PK。它是有效的还是只是2个pks的组合?

now my table have 2 PK's in the SSMS . is it valid or it is just a combinationof 2 pks

推荐答案

最简单的方法是在SSMS Express中使用T-SQL命令,而不是尝试使用可视设计器.....

The easiest way would be to use a T-SQL command in SSMS Express rather than trying to use the visual designers.....

设计并创建表格后,尝试执行以下操作:

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)

这篇关于如何在表中设置(组合)两个主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆