如何使用SQL Server Management Studio创建复合键?

时间:2021-05-19 16:59:24

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列构成表的标识(惟一)

7 个解决方案

#1


315  

如何使用SQL Server Management Studio创建复合键?

  1. Open the design table tab
  2. 打开design table选项卡
  3. Highlight your two INT fields (Ctrl/Shift+click on the grey blocks in the very first column)
  4. 突出显示两个INT字段(Ctrl/Shift+单击第一列中的灰色块)
  5. Right click -> Set primary key
  6. 右击->设置主键。

#2


57  

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.

我将约束添加为单独的语句,因为我假定您的表已经创建。

#3


33  

create table my_table (
    id_part1 int not null,
    id_part2 int not null,
    primary key (id_part1, id_part2)
)

#4


13  

In design mode (right click table select modify) highlight both columns right click and choose set primary key

在设计模式(右击表选择修改)中,突出显示两列右击并选择“设置主键”

#5


7  

Open up the table designer in SQL Server Management Studio (right-click table and select 'Design')

在SQL Server Management Studio中打开表设计器(右键单击表并选择'Design')

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..

你完成. .

:-)

:-)

#6


4  

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.

我不确定您的问题,但是每个表中可能只有一列是标识列,而不是两列都是。

#7


3  

create table myTable 
(
    Column1 int not null,
    Column2 int not null
)
GO


ALTER TABLE myTable
    ADD  PRIMARY KEY (Column1,Column2)
GO

#1


315  

如何使用SQL Server Management Studio创建复合键?

  1. Open the design table tab
  2. 打开design table选项卡
  3. Highlight your two INT fields (Ctrl/Shift+click on the grey blocks in the very first column)
  4. 突出显示两个INT字段(Ctrl/Shift+单击第一列中的灰色块)
  5. Right click -> Set primary key
  6. 右击->设置主键。

#2


57  

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.

我将约束添加为单独的语句,因为我假定您的表已经创建。

#3


33  

create table my_table (
    id_part1 int not null,
    id_part2 int not null,
    primary key (id_part1, id_part2)
)

#4


13  

In design mode (right click table select modify) highlight both columns right click and choose set primary key

在设计模式(右击表选择修改)中,突出显示两列右击并选择“设置主键”

#5


7  

Open up the table designer in SQL Server Management Studio (right-click table and select 'Design')

在SQL Server Management Studio中打开表设计器(右键单击表并选择'Design')

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..

你完成. .

:-)

:-)

#6


4  

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.

我不确定您的问题,但是每个表中可能只有一列是标识列,而不是两列都是。

#7


3  

create table myTable 
(
    Column1 int not null,
    Column2 int not null
)
GO


ALTER TABLE myTable
    ADD  PRIMARY KEY (Column1,Column2)
GO