候选键和复合键有什么区别?
我正在阅读有关候选键和复合键的内容。我开始知道
- 候选键可以作为主键,它可以是单个列或列的组合
- ,复合键也是列的组合。
对于复合密钥,我引用了此链接
如何使用 SQL Server Management Studio 创建组合键?
因此,当候选键和组合键都是列的组合时,它们可以作为主键。那么具体的区别是什么呢?您能举例说明吗?
I am reading about candidate keys and composite keys. I came to know that
- a candidate key can qualify as a primary key and it can be a single column or combination of columns
- and a composite key is also a combination of columns.
For composite key I have referred this link
how do I make a composite key with SQL Server Management Studio?
Thus when both the candidate key and composite key are a combination of columns, they can qualify as a primary key. Then what is the exact difference ? Can you please explain with examples ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
据我所知,候选键是可以用作主键的唯一键。但不一定用作一个。
复合键是唯一标识行的两个或多个属性的键。
As I know candidate key is a unique key that can be used as a primary key. but not necessarily used as one.
Composite key is a key of two or more attributes that uniquely identifies the row.
键是一组列,可用于唯一标识表中的每一行。
每张桌子至少有一把钥匙。假设我们已经确定了表中每个可能的键。这些键中的每一个都是候选键。
当我们检查这些键中的每一个时,键可能不包含列(!)、一列或多于一列,当一起考虑时,它们唯一地标识每一行。术语复合键特指由多于一列组成的键。
在 SQL 中,决定应选择一个键并将其视为比表中的其他键“更平等”。这个键称为主键。还可以在表上声明其他键,这些键通常称为唯一约束。
(!) 在 SQL 中,不允许声明没有列的键 - 尽管它偶尔会很有用(想象一个应该只包含一行的表,其中每列代表配置信息)
作为示例具有多个键的表,所有键都是复合键。想象一个预约系统,客户和顾问在特定时间在一个房间会面:
该表的候选键是 {ClientID,AppointmentTime}、{CounsellorID,AppointmentTime} 和 {RoomID,AppointmentTime}。任何这些列的组合都可以用于唯一标识表中的行,并且它们都是复合键。
我们选择将哪一个声明为主键将取决于(可能)我们自己对系统主要“焦点”的解释。我们主要关心的是房间使用情况、客户还是辅导员?无论如何,我们都会选择一个并将其声明为主键。我们还希望将其他键声明为唯一约束。
或者,我们可以决定使用代理,并使用数据库中可用的任何自动编号功能来声明一个
AppointmentID
列。这可能是(非复合)主键。但我们仍然应该声明表的其他键。A key is a set of columns that can be used to uniquely identify each row within a table.
Every table has at least one key. Let's say we've identified each possible key for the table. Each of these keys is a candidate key.
As we examine each of these keys, the key may consist of no columns (!), one column, or more than one column, which when considered together uniquely identify each row. The term composite key specifically refers to a key which consists of more than one column.
In SQL, it was decided that one key should be selected and treated "more equal" than the other keys of the table. This key is called the primary key. Other keys can also be declared on the table, these are usually referred to as Unique Contsraints.
(!) In SQL, you aren't allowed to declare a key with no columns - although it would occasionally be useful (think of a table that should only ever have a single row, where each column is representing configuration information)
As an example of a table which has multiple keys, all of which are composite. Imagine an appointment system, where a client and a counsellor meet in a room at a particular time:
The candidate keys for this table are {ClientID,AppointmentTime}, {CounsellorID,AppointmentTime} and {RoomID,AppointmentTime}. Any of those combinations of columns could be used to uniquely identify a row in the table, and all of them are composite keys.
Which one we choose to declare as primary key will depend (probably) on our own interpretation of the main "focus" of the system. Are we mainly concerned with Room usage, or Clients, or Counsellors? In any case, we'll select one and declare it the primary key. We'll also hopefully declare the other keys as unique constraints.
Or, we could decide to use a surrogate, and declare an
AppointmentID
column, using whatever auto-numbering facilities are available in the database. That could then be the (non-composite) primary key. But we should still declare the other keys for the table.候选键:主键字段的被提名者称为候选键。
复合键:创建多个主键统称为复合键。
更新:
候选键是可以用作主键的唯一键。
复合键是唯一标识行的两个或多个属性的键。
键是一组列,可用于唯一标识表中的每一行。
Candidate Key: A nominee for primary key field is known as candidate key.
Composite Key: Creating more than one primary key is jointly known as composite key.
Update :
A candidate key is a unique key that can be used as a primary key.
Composite key is a key of two or more attributes that uniquely identifies the row.
A key is a set of columns that can be used to uniquely identify each row within a table.
候选键:- 候选键是唯一键,并且是主键的“候选”。
复合键:- 两列或更多列的“组合”作为主键,被视为复合键。
CANDIDATE KEY :- Candidate key is a unique key and is a "Candidate" for being a primary key.
COMPOSITE KEY :- "Composition" of two or more columns as primary key, is consider as Composite key.
候选键就是:可以用作主键的列或列的组合,即候选或潜在的主键,复合键根据定义是可以用作主键的两个或多个列用于标识一行。在谈论组合键时,最常见的问题是是否将其作为主键来代替代理键。有时,您可能会听到人们引用不是主键的复合键(即并非所有行都唯一),在这种情况下,它(希望)只是引用非主键上的多列联接的一种方式字段(而不是进行交叉连接并过滤 where 子句中的行)。
A candidate key is just that: a column or combination of columns that could be used as the primary key, ie a candidate or potential primary key, A composite key is by definition two or more columns that could be used to identify a row. Most commonly when talking about composite keys the question is whether to have it as the primary key to have a surrogate key instead. On occassion you may hear people refer to a composite key that is not a primary key (ie not unique for all rows), in which case it is (hopefully) just a way to refer to a multi-colmn join on non-primary key fields (instead of doing cross join and filtering the rows in the where clause).
候选键:候选键是单列/多列(组合),它可以帮助我们唯一标识表中的行。
<----- 现在在所有这些候选键中,其中一个成为主键(这取决于开发人员哪个适合它们)----- ->
复合键:复合键是由 2 列或更多列组成的集合(不能是单列),它可以帮助我们识别表中的行。
-希望这有帮助
CANDIDATE KEY : Candidate key is the single column/multiple column(combined) which helps us to uniquely identify rows in a table.
<----- Now among all these candidate keys, one of them is made primary key(which depends upon the developer which one is suited for them) ------>
COMPOSITE KEY : Composite key is set of 2 or more columns (it can't be single column) which helps us to identify rows in a table.
-Hope this helps
让我们保持简单。
主键:使用一列唯一标识每一行
复合键:使用多于一列唯一标识每一行
候选键:可以是主键或复合键,但不能作为一个使用。顾名思义,它只是一个“候选者”。
Lets keep it simple.
Primary Key: Uniquely identifies each row using one column
Composite Key: Uniquely identifies each row using more than one columns
Candidate Key: Can be a Primary key or a Composite key, but is not being used as one. As the name suggests it is only a 'candidate'.