EN
PostgreSQL - make column values unique
0
points
In this article, we would like to show you how to make column values unique in PostgreSQL.
Quick solution:
ALTER TABLE "table_name"
ADD UNIQUE ("column_name");
or
ALTER TABLE "table_name"
ADD CONSTRAINT "constraing_name" UNIQUE ("column1", "column2", "columnN");
Practical example
To show how to make column values unique, we will use the following table:
Example 1 - make single column unique
In this example, we will make name
column unique.
Query:
ALTER TABLE "users"
ADD CONSTRAINT "UC_username" UNIQUE ("name");
Result:
Using query
SHOW INDEXES FROM "users";
Using HeidiSQL
Example 2 - make multiple columns unique
In this example, we will make name
and country
columns unique.
Query:
ALTER TABLE "users"
ADD CONSTRAINT "UC_user" UNIQUE ("name", "country");
Result:
Using query
SHOW INDEXES FROM "users";
Using HeidiSQL: