Skip to main content

Primary Keys: Serial or Identity?

According to the official docs you should not use serial for primary keys, instead you should use identity.

Identity has two possible set ups: Generate By Default vs Generate Always: By default will allow you to enter your own key, if you want. See Identity. Basically generate by default is just more flexible, if you ever need to change some primary keys, though that should be very, very rare. Generally, we always just use Generated By Default.

Samples below.

CREATE TABLE "address" (
id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
);

Or:

CREATE TABLE "address" (
id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY
);