Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

PostgreSQL Unique Constraints

Unique constraint is used to enforce data integrity in PostgreSQL tables. It ensures that no duplicate values are entered in specific columns that is not primary key column.

use a UNIQUE constraint instead of a PRIMARY KEY constraint if you want to enforce the uniqueness of a column, or combination of columns, that is not the primary key.

The unique constraint can be applied to one or more columns in a table. PostgreSQL raises an error when you try to insert or update a value which is already exists in a column.

A NULL value in a column means missing data, and the NULL value of one column does not equal a NULL value in another column. Hence unique constraint allows storing one NULL value in the column.

Unique constraints can be defined at the table level or at the column level, as shown below.

Syntax: Unique Constraint
CREATE TABLE table-name (
   column1 data_type(length) UNIQUE,
   column2 data_type(length),
...
    UNIQUE(column2)
);

In the above syntax, the unique constraint is defined on column1 at column level and for column2 at table level.

The following create a unique constraint on the email column of the employee table.

Example: Create Unique Constraint
CREATE TABLE IF NOT EXISTS employee 
(emp_id INT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50),
gender CHAR(1),
birthdate DATE,
email VARCHAR(100) UNIQUE,
salary INT);

The above CREATE TABLE statement will create the following employee table with the unique constraint:

Open Table Properties

Create Unique Constraint on Multiple Columns

When a unique constraint is defined on a group of columns, then the combination of those column values needs to be unique across the table. A Unique constraint can be defined on multiple columns by specifying it at table level.

Syntax: Multi-column Unique Constraint
CREATE TABLE table_name (
   column1 data_type(length),
   column2 data_type(length),
...
UNIQUE(column_1, column_2)
);

Let's add a unique constraint on combination of first_name and last_name.

Example: Unique Constraint on Multiple Columns
CREATE TABLE IF NOT EXISTS employee 
(emp_id	INT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50),
gender CHAR(1),
birthdate	DATE,
email	VARCHAR(100),
salary		INT,
UNIQUE (first_name, last_name));

In the above example, the first_name column also has a NOT NULL constraint also. Hence, you can have multiple constraints defined on one column.

Adding UNIQUE Constraint on Existing Table:

Unique constraints can be added on one or more columns of the existing table, provided that column or set of columns have unique values across the table rows.

Syntax: ALTER TABLE ADD CONSTRAINT
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
UNIQUE(column1,column2,..);

The following will add unique constraint on the email column of the employee table.

Example: Add Unique Constraint in Existing Table
ALTER TABLE employee
ADD CONSTRAINT unique_employee_email
UNIQUE(email);

Create Unique Constraint using pgAdmin

You can create unique constraints on new or existing tables using pgAdmin. The steps would remain the same if you add unique constraints on new tables or existing tables.

Expand your databases node in the left pane and then expand Schemas -> public -> Tables nodes.

Now, right-click on the table where you want to add the unique constraints and click on 'Properties' in the menu.

Open Table Properties

In the popup, go to the 'Constraints' tab and click on the Unique tab, as shown below.

Add Unique Constraint

Now, to add a unique constraints, click on the + button to add a row in a grid. Again click on the edit icon to add unique column definition, as shown below.

Add Unique Constraint

As you can see above, add unique constraint name in the Name textbox.

Now, go to the Definition tab and select one or more columns where you want to apply the unique constraint, as shown below.

Add Unique Constraint Definition

Click on the Save button to add the unique constraint on the selected columns.

In the same way, you can add unique constraints to a new table using pgAdmin.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.