SQL Decode sysconstraints

Decode sysconstraints

/* This script decodes information from the sysconstraints
** table to display information about the constraints on
** a table.
*/

USE pubs
GO

SELECT
    OBJECT_NAME(constid) 'Constraint Name',
    constid 'Constraint ID',
    CASE (status & 0xF)
        WHEN 1 THEN 'Primary Key'
        WHEN 2 THEN 'Unique'
        WHEN 3 THEN 'Foreign Key'
        WHEN 4 THEN 'Check'
        WHEN 5 THEN 'Default'
        ELSE 'Undefined'
    END 'Constraint Type',
    CASE (status & 0x30)
        WHEN 0x10 THEN 'Column'
        WHEN 0x20 THEN 'Table'
        ELSE 'NA'
    END 'Level'
FROM sysconstraints
WHERE id=OBJECT_ID('employees')