Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a 'serial' column automatically a primary key in PostgreSQL?

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?

like image 771
some1 Avatar asked May 21 '16 22:05

some1


People also ask

Is serial a primary key?

PostgreSQL has a special kind of database object generator called SERIAL. It is used to generate a sequence of integers which are often used as the Primary key of a table.

Does Postgres automatically create primary key?

By simply setting our id column as SERIAL with PRIMARY KEY attached, Postgres will handle all the complicated behind-the-scenes work and automatically increment our id column with a unique, primary key value for every INSERT .

Is a primary key automatically set?

Each table can only have one primary key. Access can automatically create a primary key field for you when you create a table, or you can specify the fields that you want to use as the primary key.

How does Postgres serial work?

PostgreSQL serial data type is used to define the auto increment number of column in a table; PostgreSQL serial will generate a serial sequence of integer numbers. We can also restart the serial number after creating a table using alter command in PostgreSQL; the serial data type's storage size is 4 bytes.


1 Answers

  1. No, use id serial primary key for that.
  2. Well, a table doesn't actually "need" a primary key. It can live without PK, it can live without an autoincrementing field, it can contain duplicate rows all right. But in relational theory (on top of which SQL is built) each relation (i. e. table) is a set (in mathematical sense) of rows. So duplicate rows are not allowed, they are simply not possible in sets. And hence each relation has a field (or several fields) which has unique values for all the relation. Such field can be used to uniquely identify a row, and one of possible unique keys is called a primary key. Such a key is usually very useful for identifying the rows, that's why tables are supposed to have them. But technically they are not required.
  3. Yes, it will.
like image 80
Egor Rogov Avatar answered Oct 16 '22 12:10

Egor Rogov