如何在 SQL Server 2008 中创建复合主键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3922337/
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 create composite primary key in SQL Server 2008
提问by Ganesamoorthy
I want to create tables in SQL Server 2008, but I don't know how to create composite primary key. How can I achieve this?
我想在 SQL Server 2008 中创建表,但我不知道如何创建复合主键。我怎样才能做到这一点?
回答by Thilo
create table my_table (
column_a integer not null,
column_b integer not null,
column_c varchar(50),
primary key (column_a, column_b)
);
回答by Matthew Abbott
CREATE TABLE UserGroup
(
[User_Id] INT NOT NULL,
[Group_Id] INT NOT NULL
CONSTRAINT PK_UserGroup PRIMARY KEY NONCLUSTERED ([User_Id], [Group_Id])
)
回答by kevchadders
Via Enterprise Manager (SSMS)...
通过企业管理器 (SSMS)...
- Right Click on the Table you wish to create the composite key on and select Design.
- Highlight the columns you wish to form as a composite key
- Right Click over those columns and Set Primary Key
- 右键单击要在其上创建组合键的表并选择Design。
- 突出显示您希望作为组合键形成的列
- 右键单击这些列并设置主键
To see the SQL you can then right click on the Table
> Script Table As
> Create To
要查看SQL然后你可以右键点击Table
> Script Table As
>Create To
回答by Ray K.
I know I'm late to this party, but for an existing table, try:
我知道我参加这个聚会迟到了,但是对于现有的桌子,请尝试:
ALTER table TABLE_NAME
ADD CONSTRAINT [name of your PK, e.g. PK_TableName] PRIMARY KEY CLUSTERED (column1, column2, etc.)
回答by Fatih Mert Do?ancan
For MSSQL Server 2012
对于 MSSQL Server 2012
CREATE TABLE usrgroup(
usr_id int FOREIGN KEY REFERENCES users(id),
grp_id int FOREIGN KEY REFERENCES groups(id),
PRIMARY KEY (usr_id, grp_id)
)
UPDATE
更新
I should add !
我应该补充!
If you want to add foreign / primary keys altering, firstly you should create the keys with constraints or you can not make changes. Like this below:
如果要添加外键/主键更改,首先应创建带有约束的键,否则无法进行更改。像下面这样:
CREATE TABLE usrgroup(
usr_id int,
grp_id int,
CONSTRAINT FK_usrgroup_usrid FOREIGN KEY (usr_id) REFERENCES users(id),
CONSTRAINT FK_usrgroup_groupid FOREIGN KEY (grp_id) REFERENCES groups(id),
CONSTRAINT PK_usrgroup PRIMARY KEY (usr_id,grp_id)
)
Actually last way is healthier and serial. You can look the FK/PK Constraint names (dbo.dbname > Keys > ..)but if you do not use a constraint, MSSQL auto-creates random FK/PK names. You will need to look at every change (alter table) you need.
实际上,最后一种方式更健康,更连续。您可以查看 FK/PK 约束名称(dbo.dbname > Keys > ..),但如果您不使用约束,MSSQL 会自动创建随机 FK/PK 名称。您将需要查看您需要的每个更改(更改表)。
I recommend that you set a standard for yourself; the constraint should be defined according to the your standard. You will not have to memorize and you will not have to think too long. In short, you work faster.
我建议你为自己设定一个标准;应根据您的标准定义约束。您不必记住,也不必考虑太久。简而言之,你工作得更快。
回答by Sakthi Thanuskodi
First create the database and table, manually adding the columns. In which column to be primary key. You should right click this column and set primary key and set the seed value of the primary key.
首先创建数据库和表,手动添加列。在哪一列作为主键。您应该右键单击该列并设置主键并设置主键的种子值。
回答by Abhishek Jaiswal
To create a composite unique key on table
在表上创建复合唯一键
ALTER TABLE [TableName] ADD UNIQUE ([Column1], [Column2], [column3]);
回答by FahadHussain
CREATE TABLE UserGroup
(
[User_Id] INT Foreign Key,
[Group_Id] INT foreign key,
PRIMARY KEY ([User_Id], [Group_Id])
)