Sign up for Hasura Newsletter

Primary Keys

A primary key is used to identify a row uniquely in a table. It could be a single column or a group of columns (composite). A primary key is one of the constraints that we define to restrict data input and validate.

Primary key is a combination of

  • NOT NULL Constraint
  • UNIQUE Constraint

Defining a primary key

As we looked at the constraints in the previous section, In the CREATE TABLE statement, you can define a primary key for a column by using the PRIMARY KEY keyword against the required unique column or a combination of columns.

CREATE TABLE author (
id integer PRIMARY KEY,
name text,
);

Here the id column is marked as the primary key.

Adding a primary key later

In case you had forgotten to mark any column as a primary key during table creation, we can also add a primary key later by altering the table using the following command:

ALTER TABLE author ADD PRIMARY KEY (id);

Removing a primary key

We can remove an existing primary key by using the DROP CONSTRAINT keyword in the alter table syntax.

ALTER TABLE author DROP CONSTRAINT author_pkey;
Did you find this page helpful?
Start with GraphQL on Hasura for Free
  • ArrowBuild apps and APIs 10x faster
  • ArrowBuilt-in authorization and caching
  • Arrow8x more performant than hand-rolled APIs
Promo
footer illustration
Brand logo
© 2021 Hasura Inc. All rights reserved
Github
Titter
Discord
Facebook
Instagram
Youtube
Linkedin