我可以在一个表中有多个主键吗?
当前回答
一个表可以有一个组合主键,它是由两个或多个列组成的主键。例如:
CREATE TABLE userdata (
userid INT,
userdataid INT,
info char(200),
primary key (userid, userdataid)
);
更新:这里有一个关于复合主键的更详细描述的链接。
其他回答
一个表可以有一个组合主键,它是由两个或多个列组成的主键。例如:
CREATE TABLE userdata (
userid INT,
userdataid INT,
info char(200),
primary key (userid, userdataid)
);
更新:这里有一个关于复合主键的更详细描述的链接。
主键是唯一标识一条记录并在所有索引中使用的键。这就是为什么你只能有一个。它通常也是用于连接子表的键,但这不是必需的。PK的真正目的是确保某些东西允许您惟一地标识一条记录,这样数据更改就会影响正确的记录,从而可以创建索引。
However, you can put multiple fields in one primary key (a composite PK). This will make your joins slower (espcially if they are larger string type fields) and your indexes larger but it may remove the need to do joins in some of the child tables, so as far as performance and design, take it on a case by case basis. When you do this, each field itself is not unique, but the combination of them is. If one or more of the fields in a composite key should also be unique, then you need a unique index on it. It is likely though that if one field is unique, this is a better candidate for the PK.
Now at times, you have more than one candidate for the PK. In this case you choose one as the PK or use a surrogate key (I personally prefer surrogate keys for this instance). And (this is critical!) you add unique indexes to each of the candidate keys that were not chosen as the PK. If the data needs to be unique, it needs a unique index whether it is the PK or not. This is a data integrity issue. (Note this is also true anytime you use a surrogate key; people get into trouble with surrogate keys because they forget to create unique indexes on the candidate keys.)
There are occasionally times when you want more than one surrogate key (which are usually the PK if you have them). In this case what you want isn't more PK's, it is more fields with autogenerated keys. Most DBs don't allow this, but there are ways of getting around it. First consider if the second field could be calculated based on the first autogenerated key (Field1 * -1 for instance) or perhaps the need for a second autogenerated key really means you should create a related table. Related tables can be in a one-to-one relationship. You would enforce that by adding the PK from the parent table to the child table and then adding the new autogenerated field to the table and then whatever fields are appropriate for this table. Then choose one of the two keys as the PK and put a unique index on the other (the autogenerated field does not have to be a PK). And make sure to add the FK to the field that is in the parent table. In general if you have no additional fields for the child table, you need to examine why you think you need two autogenerated fields.
正如其他人所指出的,可以有多列主键。 但是需要注意的是,如果有一些函数依赖关系不是由键引入的,那么应该考虑规范化关系。
例子:
Person(id, name, email, street, zip_code, area)
id ->名称、电子邮件、街道、zip_code和区域之间可能存在功能依赖关系 但zip_code通常与区域相关联,因此zip_code ->区域之间存在内部函数依赖关系。
因此,可以考虑将它分割到另一个表中:
Person(id, name, email, street, zip_code)
Area(zip_code, name)
所以它和第三种形式是一致的。
有些人使用术语“主键”来确切地表示一个整型列,它的值是由某种自动机制生成的。例如MySQL中的AUTO_INCREMENT或Microsoft SQL Server中的IDENTITY。你是在使用主键吗?
如果是,答案取决于您使用的数据库的品牌。在MySQL中,你不能这样做,你会得到一个错误:
mysql> create table foo (
id int primary key auto_increment,
id2 int auto_increment
);
ERROR 1075 (42000): Incorrect table definition;
there can be only one auto column and it must be defined as a key
在其他一些数据库中,您可以在一个表中定义多个自动生成列。
一个表可以有多个候选键。每个候选键都是一个列或一组列,它们是UNIQUE的,加在一起也是NOT NULL。因此,为任何候选键的所有列指定值就足以确定是否有一行满足条件,或者根本没有行。
候选键是关系数据模型中的一个基本概念。
通常的做法是,如果一个表中存在多个键,则将其中一个候选键指定为主键。通常的做法是使表的任何外键引用主键,而不是任何其他候选键。
我推荐这些实践,但是关系模型中没有要求在候选键中选择主键。
推荐文章
- 数据库触发器是必要的吗?
- 为什么我应该使用基于文档的数据库而不是关系数据库?
- 哪个更快/最好?SELECT *或SELECT columnn1, colum2, column3等
- 将值从同一表中的一列复制到另一列
- 什么是数据库池?
- 关于数据库,每个开发人员应该知道些什么?
- INT和VARCHAR主键之间有真正的性能差异吗?
- "where 1=1"语句
- 是使用各有一个模式的多个数据库更好,还是使用一个数据库有多个模式更好?
- 如何从Oracle的表中获取列名?
- 如何检查表上持有哪些锁
- 在MongoDB中查找重复的记录
- 对于PostgreSQL表来说,多大才算太大?
- 模式、表和数据库之间的区别是什么?
- 我看到VARCHAR(255)如此频繁地使用(而不是其他长度),有什么好的原因吗?