[英]Is a 'serial' column automatically a primary key in PostgreSQL?
I've created the following table in Postgres... 我在Postgres中创建了下表...
create table printq (
model varchar(20),
session integer,
timestamp timestamp DEFAULT now(),
id serial);
It seems to do exactly what I need it to... it auto-increments the id column, when I clear the table using truncate "RESTART IDENTITY" it resets the sequence (which is why I rebuilt the table in the first place -- the id column used to not restart upon truncation) 它似乎正是我需要它...它自动递增id列,当我使用truncate“RESTART IDENTITY”清除表时,它重置序列(这就是我首先重建表的原因 - 用于在截断时不重新启动的id列)
Anyway, when I do a \\d on the table, I don't see anything about a primary key. 无论如何,当我在桌面上做一个\\ d时,我看不到关于主键的任何信息。
Table "public.printq"
Column | Type | Modifiers
-----------+-----------------------------+-----------------------------------------------------
model | character varying(20) |
session | integer |
timestamp | timestamp without time zone | default now()
id | integer | not null default nextval('printq_id_seq'::regclass)
Three questions: 三个问题:
Is the ID column already a primary key since it auto-increments, or not? ID列是否已成为主键,因为它自动递增?
If not, why would this table need a primary key, since it seems to be working fine? 如果没有,为什么这个表需要一个主键,因为它似乎工作正常? I know basically every table is supposed to have a primary key, but why exactly? 我知道基本上每个表都应该有一个主键,但为什么呢?
Finally, would the \\d command tell me if this table had a primary key? 最后,\\ d命令会告诉我这个表是否有主键吗? If not, what would tell me? 如果没有,会告诉我什么?
id serial primary key
for that. 不,使用id serial primary key
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.