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

Add Constraint to Table in PostgreSQL

Use ALTER TABLE ADD CONSTRAINT statement to add constraint to a table.

Syntax:
ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name> <constraint_definition>;

Using ALTER TABLE ADD CONSTRAINT, any of bellow constraints can be added to existing table

  • NOT NULL
  • CHECK
  • UNIQUE
  • PRIMARY KEY
  • FOREIGN KEY

Consider that you already have the following employee table.

Add UNIQUE Constraint

The UNIQUE constraint ensures that the values of a column must be unique and not a duplicate value. The following will set the unique constraint on the email column of the employee table.

Example: Add Unique Constraint
ALTER TABLE employee
ADD CONSTRAINT employee_unq UNIQUE(email);

The following is the result of the above statement.

Add NOT NULL Constraint

The NOT NULL constraint ensures ensure that a given column of a table is never assigned the null value. To add or remove NOT NULL constraint to a column of table you need to use ALTER COLUMN clause with ALTER TABLE.

The following will set the NOT NULL constraint on the gender column of the employee table.

Example: Add NOT NULL Constraint
ALTER TABLE employee
ALTER COLUMN gender SET NOT NULL;

To remove the NOT NULL constraint, use the DROP clause, as shown below.

Example: Remove NOT NULL Constraint
ALTER TABLE employee
ALTER COLUMN gender DROP NOT NULL;

Add Constraints using pgAdmin

You can add constraints using pgAdmin by right clicking on the table and select 'Properties' in the context menu. This will open a popup where you can add or edit multiple columns definition.

In the popup, go to 'Constraints' tab where you can add or edit Primary key, Foreign Key, Check, and unique constraints, as shown below.

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.