WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

PostgreSQL · Intermediate · question 31 of 100

What are the different types of constraints in PostgreSQL, and how do they help maintain data integrity?

📕 Buy this interview preparation book: 100 PostgreSQL questions & answers — PDF + EPUB for $5

PostgreSQL supports several types of constraints for maintaining data integrity. Constraints are used to define rules or conditions that must be satisfied before data can be inserted, updated or deleted from a table. The following are the most common types of constraints in PostgreSQL:

1. NOT NULL Constraint: The NOT NULL constraint ensures that a column cannot contain a null value. If a user tries to insert a NULL value into a column that has a NOT NULL constraint on it, PostgreSQL will raise an error.

Here is an example of defining a NOT NULL constraint in a table creation statement:

CREATE TABLE students (
   id serial PRIMARY KEY,
   name varchar NOT NULL,
   age integer,
   class varchar
);

In this example, the "name" column has a NOT NULL constraint defined on it.

2. UNIQUE Constraint: The UNIQUE constraint ensures that each value in a column is unique across all rows in the table. If a user tries to insert a duplicate value into a column that has a UNIQUE constraint on it, PostgreSQL will raise an error.

Here is an example of defining a UNIQUE constraint in a table creation statement:

CREATE TABLE students (
   id serial PRIMARY KEY,
   name varchar UNIQUE,
   age integer,
   class varchar
);

In this example, the "name" column has a UNIQUE constraint defined on it.

3. PRIMARY KEY Constraint: The PRIMARY KEY constraint is used to uniquely identify each row in a table. It is a combination of the NOT NULL and UNIQUE constraints, as it ensures that each value is unique and cannot be NULL.

Here is an example of defining a PRIMARY KEY constraint in a table creation statement:

CREATE TABLE students (
   id serial PRIMARY KEY,
   name varchar,
   age integer,
   class varchar
);

In this example, the "id" column has a PRIMARY KEY constraint defined on it.

4. FOREIGN KEY Constraint: The FOREIGN KEY constraint is used to establish a link between two tables. It ensures that the values in a column of one table exist in a column of another table.

Here is an example of defining a FOREIGN KEY constraint in a table creation statement:

CREATE TABLE students (
   id serial PRIMARY KEY,
   name varchar,
   age integer,
   class_id integer REFERENCES classes(id)
);

CREATE TABLE classes (
   id serial PRIMARY KEY,
   name varchar
);

In this example, the "class_id" column in the "students" table has a FOREIGN KEY constraint that references the "id" column in the "classes" table.

5. CHECK Constraint: The CHECK constraint is used to limit the values that can be inserted into a column. It allows creating complex logical expressions to restrict the values of the column.

Here is an example of defining a CHECK constraint in a table creation statement:

CREATE TABLE students (
   id serial PRIMARY KEY,
   name varchar,
   age integer,
   class varchar,
   CONSTRAINT check_age CHECK (age > 10 AND age < 18)
);

In this example, the "check_age" constraint is defined to restrict the age value to be between 10 and 18.

Constraints are essential for maintaining data integrity in a database. They help ensure that only valid data is inserted into a table and prevent data inconsistencies that can lead to data corruption or incorrect results in queries. By enforcing constraints, the database system provides a reliable and consistent data store for your application.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PostgreSQL interview — then scores it.
📞 Practice PostgreSQL — free 15 min
📕 Buy this interview preparation book: 100 PostgreSQL questions & answers — PDF + EPUB for $5

All 100 PostgreSQL questions · All topics