EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL Constraints
 

PostgreSQL Constraints

Priya Pedamkar
Article byPriya Pedamkar

Updated May 3, 2023

postresql constaints

 

 

Definition of PostgreSQL Constraints

PostgreSQL constraints enforce the rule on the table’s data columns; this is mainly useful to prevent invalid data from entering the table. PostgreSQL constraints are beneficial to find duplicate values; they will not accept the duplicate value or invalid data into the table. PostgreSQL constraints ensure the accuracy and reliability of data in the table. We have to define constraints on the table level as well as the column level. Table level constraints are applied to the whole table, whereas column-level constraints are applied to only one column.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

PostgreSQL Constraints with Examples

The following are commonly used constraints available in PostgreSQL are as follows.

1. Not Null Constraints

  • In PostgreSQL, by default, the column accepts null values; using no null constraints on the column will not accept any null values in a column.
  • No null Constraint in PostgreSQL are always written as column constraints.

Syntax:

Create table table_name (
Column_name1 data type Not Null,
Column_nameN data type Not Null);

Below is the description of the above syntax.

  • Create: We have created not null Constraint on a column at the time of table creation.
  • Table name: It is the name of the table.
  • Column_name1 to column_nameN: Name of column.
  • Data type: A Data type that we have defined as a column.
  • Not Null: Constraint name, which does not accept null value in the column.

Example

Below is an example of not null constraints at the time of table creation.

CREATE TABLE Employee ( emp_id INT NOT NULL, emp_name character(10) NOT NULL, emp_address character(20) NOT NULL, emp_phone character(14), emp_salary INT NOT NULL, date_of_joining date NOT NULL);

Not Null Constraints-1.1

INSERT INTO Employee (emp_id, emp_name, emp_address, emp_phone, emp_salary, date_of_joining) VALUES (1, 'ABC', 'Pune', '1234567890', 20000, '01-01-2020');
select * from Employee;

PostgreSQL Constraints-1.2

2. Unique Constraints

  • Unique constraints are the same as its name; it will prevent adding two identical values in the table.
  • Unique constraints ensure that all values in the column are identical.

Syntax:

Create table table_name (
Column_name1 data type Not Null Unique,
Column_nameN data type Not Null Unique);

Below is the description of the above syntax.

  • Create: We have created a unique constraint on the column at the time of table creation.
  • Table name: It is the name of the table.
  • Column_name1 to column_nameN: Name of column.
  • Data type: Data type of column.
  • Unique: Constraint name.

Example

Below is an example of unique constraints at the time of table creation.

CREATE TABLE department ( dept_name character(10) NOT NULL UNIQUE, dept_id int NOT NULL UNIQUE, dept_code varchar(10));

Unique Constraints -2.1

INSERT INTO department (dept_name, dept_id, dept_code) VALUES ('IT', 101, 'IT101');

Unique Constraints-2.2

INSERT INTO department (dept_name, dept_id, dept_code) VALUES ('IT', 101, 'IT101');

Unique Constraints-2.3

  • In the second example, the same value insertion is not allowed in the table because we have used a unique constraint on the dept_id column.

3. Primary Key Constraints

  • Primary Constraint, which uniquely identifies each record in the database table. We can define multiple primary key constraints on a single table. It will be allowed only one primary key Constraint on a single table.
  • Below are the example and syntax of primary key constraints in PostgreSQL.

Syntax:

Create table table_name (
Column_name1 data type primary key Not Null,
Column_nameN data type Not Null);

Below is the description of the above syntax.

  • Create: We have created a primary constraint on a column at the time of table creation.
  • Table name: Name of the table.
  • Column_name1 to column_nameN: Name of column.
  • Data type: Data type of column.
  • Primary Key: Constraint name.

Example

Below is an example of a primary key constraint in PostgreSQL at the time of table creation.

CREATE TABLE Employee_test ( emp_id INT PRIMARY KEY NOT NULL, emp_name character(10) NOT NULL, emp_address character(20) NOT NULL, emp_phone character(14), emp_salary INT NOT NULL);

PostgreSQL Constraints-3.1

INSERT INTO Employee_test (emp_id, emp_name, emp_address, emp_phone, emp_salary) VALUES (1, 'ABC', 'Pune', '1234567890', 20000);

PostgreSQL Constraints - 3.2

INSERT INTO Employee_test (emp_id, emp_name, emp_address, emp_phone, emp_salary) VALUES (1, 'PQR', 'MUMBAI', '1234567880', 25000);

PostgreSQL Constraints - 3.3

  • In the second example, the same emp_id insertion is not allowed in the table because we have used the primary key Constraint on the emp_id column.

4. Foreign Key Constraints

  • Foreign key constraints in PostgreSQL states that values in the first table column must appear with values in a second table column.
  • Below are the syntax and examples of foreign key constraints in PostgreSQL.

Syntax:

Create table table_name (
Column_name1 data type primary key Not Null,
Column_nameN data type references table_name (column_name));

Below is the description of the above syntax.

  • Create: Create table statement.
  • Table name: It is the name of the table.
  • Column_name1 to column_nameN: Name of the column.
  • Data type: Data type of column.

Example

Below is an example of foreign key constraints in PostgreSQL at the time of table creation.

CREATE TABLE Employee_test1 ( emp_id INT PRIMARY KEY NOT NULL, emp_name character(10) NOT NULL, emp_address character(20) NOT NULL, emp_phone character(14), emp_salary INT NOT NULL);

Foreign Key-4.1

CREATE TABLE Employee_test2 ( emp_id INT PRIMARY KEY NOT NULL, emp_name character(10) NOT NULL, emp_salary INT NOT NULL, id int references Employee_test1(emp_id));

Foreign Key-4.2

\d+ Employee_test1;

Foreign Key-4.3

\d+ Employee_test2;

Foreign Key-4.4

5. Check Constraints

  • Check condition in PostgreSQL enables checking the condition that values are being entered into the record.
  • Below is the syntax, and examples of check constraints in PostgreSQL are as follows.

Syntax:

Create table table_name (
Column_name1 data type primary key Not Null
Column_nameN data type Not Null check condition);

Below is the description of the above syntax.

  • Create: Create table statement.
  • Table name: Name of the table.
  • Column_name1 to column_nameN: Name of column.
  • Data type: Data type of column.
  • Check condition: check Constraint with the condition.

Example

CREATE TABLE EMP_TEST (ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, SALARY REAL CHECK(SALARY > 1000));

PostgreSQL Constraints-5.1

INSERT into EMP_TEST (ID, NAME, SALARY) VALUES (1, 'ABC', 5000);

PostgreSQL Constraints-5.2

INSERT into EMP_TEST (ID, NAME, SALARY) VALUES (1, 'ABC', 500);

PostgreSQL Constraints-5.3

  • In the second example, a salary less than 1000 insertion is not allowed in a table because we have used check constraints on the salary column.

Conclusion

PostgreSQL constraints are beneficial to validate data with duplicate and unwanted data from the table. We have mainly used not null, primary key, foreign key, check, and unique key constraints in PostgreSQL. Constraints are essential and useful in PostgreSQL.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL Constraints” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. What are the Features of PostgreSQL?
  2. Introduction to PostgreSQL Architecture
  3. Different Versions & Features of PostgreSQL
  4. Guide to PostgreSQL Views

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW