EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL Constraints

PostgreSQL Constraints

By Priya PedamkarPriya Pedamkar

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.

PostgreSQL Constraints with Examples

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

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Data Science Bundle1500+ Hour of HD Videos | 80 Learning Paths | 360+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

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
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*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?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more