Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the unique constraint columns list (in TSQL)?

I can get a list of unique constraints fairly easily with the following query:

select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_TYPE='UNIQUE' 

But how do I get a list of the columns that each unique constraint applies to?

like image 960
anakic Avatar asked Apr 20 '10 12:04

anakic


People also ask

How do I get unique columns in SQL?

The SQL SELECT DISTINCT Statement The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.

How do you get unique constraints on multiple columns?

Introduction to SQLite UNIQUE constraint To define a UNIQUE constraint, you use the UNIQUE keyword followed by one or more columns. You can define a UNIQUE constraint at the column or the table level. Only at the table level, you can define a UNIQUE constraint across multiple columns.

How do you find unique constraints in a database?

To check for a unique constraint use the already provided method: select count(*) cnt from user_constraints uc where uc. table_name='YOUR_TABLE_NAME' and uc.


2 Answers

Ed is correct, the columns are exposed on the constraint column usage view, here is the SQL for it.

select TC.Constraint_Name, CC.Column_Name from information_schema.table_constraints TC inner join information_schema.constraint_column_usage CC on TC.Constraint_Name = CC.Constraint_Name where TC.constraint_type = 'Unique' order by TC.Constraint_Name 
like image 186
Andrew Avatar answered Sep 20 '22 07:09

Andrew


See INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE

like image 40
Ed Harper Avatar answered Sep 22 '22 07:09

Ed Harper