
The conname column in this view also tells you about the table containing this constraint. The contype column tells you the type of the constraint, which for the primary key is p. In the column contype, the value ‘p’ is for a primary key, ‘f’ is for a foreign key, ‘u’ is for the UNIQUE constraint, and ‘c’ is for the CHECK constraint.įor example, you can see the constraint named student_pkey for the primary key in the student table. Select columns conname and contype to see the constraint name along with the constraint type. In our example, we select the constraints from the table student ( WHERE t.relname ='student'). Join the view pg_catalog.pg_constraint with the view pg_class ( JOIN pg_class t ON t.oid = c.conrelid) and use the relname column to filter the constraints by table name. To find the name of a constraint in PostgreSQL, use the view pg_constraint in the pg_catalog schema. We want to display the names of the constraints in the table student. The below example shows that we have to insert a record in the EMP table.You want to find the names of the constraints in a table in PostgreSQL. Insert record in the table after creating a unique constraint on the column. At the same time, we are creating a unique constraint on multiple columns at the time of table creation. In the above example, we have created an index on the emp_id and emp_mail column.

Example #3Ĭreate unique constraint on multiple columns.ĬREATE TABLE Emp (emp_id INT NOT NULL, emp_name character(10) NOT NULL, emp_mail character(20) NOT NULL, emp_phone character(14), emp_salary INT NOT NULL, date_of_joining date NOT NULL, UNIQUE (emp_id, emp_mail)) We are creating this unique constraint after defining a column data type. In the above example, we have created a unique constraint on the cust_id column. Unique constraint creates after defining the data type of the column.ĬREATE TABLE dis_uni (cust_ID INT, product_name VARCHAR (100) NOT NULL, product_price varchar (10) NOT NULL, product_discount NUMERIC, UNIQUE (CUST_ID)) By default, it will create a Btree index on the emp_id column. In the above example, we are creating a unique constraint on the emp_id column after defining a unique constraint, the index will automatically create on the emp_id column. Unique constraint creates at the time of defining the data type of the column.ĬREATE TABLE Emp_UNI (emp_id INT UNIQUE, emp_name character(10) NOT NULL, emp_address character(20) NOT NULL, emp_phone character(14), emp_salary INT NOT NULL, date_of_joining date NOT NULL) We can also create a unique constraint on the column after creating a table by using alter command.They state that a column or several groups of column rows values are unique in all the tables.Insert into discount values (1, ‘ABC’, 100, 50).The below figure shows the error message after inserting the same record, which was present in a table. TablePlus is a modern, native tool with elegant UI that allows you to simultaneously manage multiple databases such as MySQL, PostgreSQL, SQLite, Microsoft SQL Server and more.If we are inserting a value that was present in a table, it will issue an error like “duplicate key violates unique constraint”.If we are updating the value on the column row and which was already present in the table, it will issue an error.While creating a unique constraint on the column every time of data insertion, it will check duplicate records from all rows of a table.On cust_id column, Btree index was automatically created while using a unique constraint on cust_id column.In the above example, we have created a unique constraint on the cust_id column in the discount table.The below example shows that the index will automatically create on the cust_id column:ĬREATE TABLE discount (cust_ID INT Unique, product_name VARCHAR (100) NOT NULL, product_price varchar (10) NOT NULL, product_discount NUMERIC)

While we are creating a unique constraint column or any group of the column, PostgreSQL will create an index automatically on that column. How UNIQUE Constraint works in PostgreSQL?


Unique constraint: PostgreSQL unique constraint is straight that all the records in the table column are unique, duplicates are not allowed in it.Table name: Table name on which column we are creating a unique constraint.The data type is most important while creating a table. Data type: Data type defines the type of data we have stored in the table.In this column, we are creating a unique constraint. Column 1 to column N: Column name used while creating a column in PostgreSQL.We can create a constraint on the table column. Create: Create a table by using a unique constraint in PostgreSQL.The above syntax shows the unique constraint was created of a group of multiple columns simultaneously.ĪLTER table table_name add column_name data_type UNIQUE īelow is a parameter description of the above syntax:
