In this article, you will learn how to create a PostgreSQL unique index to ensure the uniqueness of values in one or more columns.
When a primary key is added to a column on a table, uniqueness is also added.
UNIQUE index is as follows.
1 |
CREATE UNIQUE INDEX indexadi ON tabloadi (kolonadi ); |
First, let’s create a two-column table as follows.
1 |
create table uniquetabled1(id int ,adi text) |
Let’s assign the index to the id column and perform the insertion operation.
1 |
create unique index uniqueindex on uniquetabled1 (id) |
1 |
insert into uniquetabled1 values(1,'Faruk') |
Let’s check the status of PostgreSQL when we insert the same record again.
As seen above, it is mentioned that there is a record of “1” and that duplicate records cannot be allowed due to the uniqueindex.