Sobes.tech
Junior — Middle

Can you list the types of constraints used in PostgreSQL?

sobes.tech AI

Answer from AI

The main types of constraints used in PostgreSQL are:

  • PRIMARY KEY — a unique identifier for a record, does not allow NULL.
  • FOREIGN KEY — ensures referential integrity between tables.
  • UNIQUE — guarantees the uniqueness of values in a column or a group of columns.
  • NOT NULL — prohibits NULL values in a column.
  • CHECK — verifies that column values satisfy a specified condition.
  • EXCLUSION — provides exclusion constraints, for example, to prevent overlapping ranges.

Example of creating a table with constraints:

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    email VARCHAR(255) UNIQUE NOT NULL,
    age INT CHECK (age >= 18),
    country_id INT REFERENCES countries(id)
);
Can you list the types of constraints used in… - sobes.tech