【发布时间】:2016-05-21 22:13:10
【问题描述】:
我在 Postgres 中创建了下表...
create table printq (
model varchar(20),
session integer,
timestamp timestamp DEFAULT now(),
id serial);
它似乎完全符合我的需要......它会自动增加 id 列,当我使用 truncate "RESTART IDENTITY" 清除表时,它会重置序列(这就是我在第一次重建表的原因place -- 截断时不重新启动的 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 命令会告诉我这个表是否有主键吗?如果没有,会告诉我什么?
【问题讨论】:
标签: postgresql primary-key psql