Select to pick up 2-column Constraint

Asked

Viewed 295 times

0

I have a PERSON table in it that has the field A and B that form a UNIQUE.

I rotate this select to return the name of the constraint:

SELECT DISTINCT COL.CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab, INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
WHERE Col.Constraint_Name = Tab.Constraint_Name
AND Col.Table_Name = Tab.Table_Name
AND Col.Table_Name = 'PESSOA'
AND COL.COLUMN_NAME IN ('A','B')
AND Tab.CONSTRAINT_TYPE = 'UNIQUE'

The problem is that if there is a UNIQUE individual for both fields is returned as well. How do pro select return only the constraint associated with columns A and B?

1 answer

1


SELECT COL.CONSTRAINT_NAME 
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab, INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
WHERE Col.Constraint_Name = Tab.Constraint_Name
AND Col.Table_Name = Tab.Table_Name
AND Col.Table_Name = 'PESSOA'
AND COL.COLUMN_NAME IN ('A','B')
AND Tab.CONSTRAINT_TYPE = 'UNIQUE
GROUP BY COL.CONSTRAINT_NAME
HAVING COUNT(*) > 1

It will bring the cases of more than one column.

  • Thanks, show, I need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.