Mega Code Archive

 
Categories / MSSQL Tutorial / System Tables Views
 

Constraint Names and System Catalog Entries

5>  CREATE TABLE customer 6> ( 7> cust_id      int         IDENTITY  NOT NULL  PRIMARY KEY, 8> cust_name    varchar(30) NOT NULL 9> ) 10> GO 1> 2> SELECT 3>     OBJECT_NAME(constid) 'Constraint Name', 4>     constid 'Constraint ID', 5>     CASE (status & 0xF) 6>         WHEN 1 THEN 'Primary Key' 7>         WHEN 2 THEN 'Unique' 8>         WHEN 3 THEN 'Foreign Key' 9>         WHEN 4 THEN 'Check' 10>         WHEN 5 THEN 'Default' 11>         ELSE 'Undefined' 12>     END 'Constraint Type', 13>     CASE (status & 0x30) 14>         WHEN 0x10 THEN 'Column' 15>         WHEN 0x20 THEN 'Table' 16>         ELSE 'NA' 17>     END 'Level' 18> FROM sysconstraints 19> WHERE id=OBJECT_ID('customer') 20> 21> drop table customer; 22> GO Constraint Name                                                                                                                  Constraint ID Constraint Type Level -------------------------------------------------------------------------------------------------------------------------------- ------------- --------------- ------ PK__customer__43A1D464                                                                                                              1134679140 Primary Key     Table (1 rows affected) 1>