表可以有多个主键吗? [英] Can a table have multiple Primary keys?

查看:165
本文介绍了表可以有多个主键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在很困惑,也许你可以帮助我更好地了解这个问题,一个表有两个主键如果是,那么如何?

I am very confused right now, maybe you can help me to understand the problem better regarding the question that can a table have two primary keys if yes then how? and it no then why?

推荐答案

您询问您是否可以拥有多个主键字段你当然可以。您只能有一个主键,但可以包含尽可能多的列,因为您需要唯一标识行。

You ask if you can have more than one primary key field and you most certainly can. You can have only one primary key, but that can consist of as many columns as you need to uniquely identify your rows.

在创建表格时使用类似的方式:

Use something like this when you are creating your table:

CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName) 

其中 P_Td LastName 是表中的列。

如果您认为需要多个主键,那么答案是不是真的。您只能有一个主键。但是,您可以拥有尽可能多的索引,对它们有唯一的约束。唯一索引与主键大致相同。

If you think you want more than one primary key, then the answer is "not really." You can have only one primary key. However, you can have as many indexes as you want that have a unique constraint on them. A unique index does pretty much the same thing as a primary key.

例如: -

CREATE TABLE Persons
(
   P_Id int NOT NULL,
   LastName varchar(255) NOT NULL,
   FirstName varchar(255),
   Address varchar(255),
   City varchar(255),
   CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)
)

注意:在上面的示例中,只有一个PRIMARY键( pk_PersonID )。然而, pk_PersonID 的值由两列( P_Id LastName )。

Note: In the example above there is only ONE PRIMARY KEY (pk_PersonID). However, the value of the pk_PersonID is made up of two columns (P_Id and LastName).

这篇关于表可以有多个主键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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