如何使用 SQL Server Management Studio 制作复合键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1545571/
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 do I make a composite key with SQL Server Management Studio?
提问by mrblah
how do I make a composite key with SQL Server Management Studio?
如何使用 SQL Server Management Studio 制作复合键?
I want two INT columns to form the identity (unique) for a table
我想要两个 INT 列来形成表的身份(唯一)
回答by Cory
- Open the design table tab
- Highlight your two INT fields (Ctrl/Shift+click on the grey blocks in the very first column)
- Right click -> Set primary key
- 打开系列零件设计表选项卡
- 突出显示您的两个 INT 字段(Ctrl/Shift+单击第一列中的灰色块)
- 右键->设置主键
回答by Roatin Marth
here is some code to do it:
这是一些代码来做到这一点:
-- Sample Table
create table myTable
(
Column1 int not null,
Column2 int not null
)
GO
-- Add Constraint
ALTER TABLE myTable
ADD CONSTRAINT pk_myConstraint PRIMARY KEY (Column1,Column2)
GO
I added the constraint as a separate statement because I presume your table has already been created.
我将约束添加为单独的语句,因为我认为您的表已经创建。
回答by yfeldblum
create table my_table (
id_part1 int not null,
id_part2 int not null,
primary key (id_part1, id_part2)
)
回答by Gratzy
In design mode (right click table select modify) highlight both columns right click and choose set primary key
在设计模式下(右键单击表选择修改)突出显示两列右键单击并选择设置主键
回答by user4878037
Open up the table designer in SQL Server Management Studio (right-click table and select 'Design')
在 SQL Server Management Studio 中打开表设计器(右键单击表并选择“设计”)
Holding down the Ctrl key highlight two or more columns in the left hand table margin
按住 Ctrl 键突出显示左侧表格边距中的两列或更多列
Hit the little 'Key' on the standard menu bar at the top
点击顶部标准菜单栏上的小“键”
You're done..
你完成了..
:-)
:-)
回答by KM.
Highlight both rows in the table design view and click on the key icon, they will now be a composite primary key.
突出显示表设计视图中的两行并单击键图标,它们现在将成为复合主键。
I'm not sure of your question, but only one column per table may be an IDENTITY column, not both.
我不确定你的问题,但每个表只有一列可能是 IDENTITY 列,而不是两者。
回答by Tejas
create table myTable
(
Column1 int not null,
Column2 int not null
)
GO
ALTER TABLE myTable
ADD PRIMARY KEY (Column1,Column2)
GO