我已经在Postgres中创建了以下表:
create table printq (
model varchar(20),
session integer,
timestamp timestamp DEFAULT now(),
id serial);
它似乎完全符合我的需求……它自动递增id列,当我使用截断“RESTART IDENTITY”清除表时,它会重置序列(这就是我首先重建表的原因——id列过去不会在截断后重新启动)
无论如何,在对表执行\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)
三个问题:
由于ID列是自动增量的,它是否已经是主键了?
如果不是,既然它看起来很好用,为什么这张表需要一个主键呢?我知道基本上每张表都应该有一个主键,但为什么?
最后,\ d命令会告诉我这个表是否有主键吗?如果不是,什么会告诉我?