我可以在一个表中有多个主键吗?
当前回答
(我一直在研究这些,很多)
候选键——唯一标识表行所需的最小列组合。 复合键- 2个或更多列。
一个表中可以存在多个候选键。 主键-只有一个候选键是由我们选择的 备用键-所有其他候选键 主键和备用键都可以是复合键
来源: https://en.wikipedia.org/wiki/Superkey https://en.wikipedia.org/wiki/Candidate_key https://en.wikipedia.org/wiki/Primary_key https://en.wikipedia.org/wiki/Compound_key
其他回答
只能有一个主键,但可以在主键中有多个列。
你也可以在你的表上有唯一索引,这将有点像一个主键,因为它们将强制唯一的值,并将加快这些值的查询。
同时有两个主键是不可能的。但是(假设你没有把复合键搞砸),你可能需要的是让一个属性是唯一的。
CREATE t1(
c1 int NOT NULL,
c2 int NOT NULL UNIQUE,
...,
PRIMARY KEY (c1)
);
However note that in relational database a 'super key' is a subset of attributes which uniquely identify a tuple or row in a table. A 'key' is a 'super key' that has an additional property that removing any attribute from the key, makes that key no more a 'super key'(or simply a 'key' is a minimal super key). If there are more keys, all of them are candidate keys. We select one of the candidate keys as a primary key. That's why talking about multiple primary keys for a one relation or table is being a conflict.
是的,在SQL中是可能的, 但是我们不能在MsAccess中设置多个主键。 我不知道其他数据库的情况。
CREATE TABLE CHAPTER (
BOOK_ISBN VARCHAR(50) NOT NULL,
IDX INT NOT NULL,
TITLE VARCHAR(100) NOT NULL,
NUM_OF_PAGES INT,
PRIMARY KEY (BOOK_ISBN, IDX)
);
主键是唯一标识一条记录并在所有索引中使用的键。这就是为什么你只能有一个。它通常也是用于连接子表的键,但这不是必需的。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.
一个表可以有一个组合主键,它是由两个或多个列组成的主键。例如:
CREATE TABLE userdata (
userid INT,
userdataid INT,
info char(200),
primary key (userid, userdataid)
);
更新:这里有一个关于复合主键的更详细描述的链接。
推荐文章
- 模式、表和数据库之间的区别是什么?
- 我看到VARCHAR(255)如此频繁地使用(而不是其他长度),有什么好的原因吗?
- 使用pgadmin连接到heroku数据库
- Delete_all vs destroy_all
- 我如何移动一个redis数据库从一个服务器到另一个?
- 如何首次配置postgresql ?
- 数据库性能调优有哪些资源?
- 如何在PostgreSQL中自动更新时间戳
- 当使用JDBC连接到postgres时,是否可以指定模式?
- 删除MySQL中的主键
- 对象'DF__*'依赖于列'*' -将int改为double
- 将映像存储在MongoDB数据库中
- 重复Mongo ObjectId的可能性在两个不同的集合中生成?
- 用户代理字符串可以有多大?
- 字符串作为SQL数据库的主键