Reputation: 1737
I've created the following table in 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)
Anyway, when I do a \d on the table, I don't see anything about a primary key.
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?
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? If not, what would tell me?
Upvotes: 12
Views: 4824
Reputation: 5398
id serial primary key
for that.Upvotes: 15