mrb*_*lah 160 sql t-sql sql-server ssms key
如何使用SQL Server Management Studio创建复合键?
我想要两个INT列来形成表的标识(唯一)
Cor*_*ory 348

Roa*_*rth 63
这是一些代码:
-- 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
Run Code Online (Sandbox Code Playgroud)
我将约束添加为单独的语句,因为我假设您的表已经创建.
yfe*_*lum 35
create table my_table (
id_part1 int not null,
id_part2 int not null,
primary key (id_part1, id_part2)
)
Run Code Online (Sandbox Code Playgroud)
小智 8
在SQL Server Management Studio中打开表设计器(右键单击表并选择"设计")
按住Ctrl键可突出显示左侧表边距中的两列或更多列
点击顶部标准菜单栏上的小'Key'
你完成了..
:-)
小智 6
create table myTable
(
Column1 int not null,
Column2 int not null
)
GO
ALTER TABLE myTable
ADD PRIMARY KEY (Column1,Column2)
GO
Run Code Online (Sandbox Code Playgroud)