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:
xxxxxxxxxx
1
ALTER TABLE "table_name"
2
ADD UNIQUE ("column_name");
or
xxxxxxxxxx
1
ALTER TABLE "table_name"
2
ADD CONSTRAINT "constraing_name" UNIQUE ("column1", "column2", "columnN");
To show how to make column values unique, we will use the following table:

In this example, we will make name
column unique.
Query:
xxxxxxxxxx
1
ALTER TABLE "users"
2
ADD CONSTRAINT "UC_username" UNIQUE ("name");
Result:
Using query
xxxxxxxxxx
1
SHOW INDEXES FROM "users";

Using HeidiSQL

In this example, we will make name
and country
columns unique.
Query:
xxxxxxxxxx
1
ALTER TABLE "users"
2
ADD CONSTRAINT "UC_user" UNIQUE ("name", "country");
Result:
Using query
xxxxxxxxxx
1
SHOW INDEXES FROM "users";

Using HeidiSQL:
