2010. 6. 3. 23:55

T_SQL::CONSTRAINT조사

CONSTRAINT 조사

 

  1. select * from sys.default_constraints 
  2. select * from sys.key_constraints 
  3. select * from sys.check_constraints 

  4. --=================
  5. -- 2005용
  6. --=================
  7. -- FK
  8. SELECT  object_name(fk.constraint_object_id) AS fk_name, 
  9.     fk.constraint_column_id AS fk_clolum,
  10.     object_name(fk.parent_object_id) AS parent_name,
  11.     (select name from sys.columns where object_id =  fk.parent_object_id and column_id = fk.parent_column_id) as parent_column,
  12.     object_name(fk.referenced_object_id) AS referenced_name,
  13.     (select name from sys.columns where object_id =  fk.referenced_object_id and column_id = fk.referenced_column_id) as referenced_column
  14. FROM sys.foreign_key_columns  as fk
  15. WHERE fk.parent_object_id  = object_id('<@table_name, @sysname,@table_name>') 
  16.         or  fk.referenced_object_id = object_id('<@table_name, @sysname,@table_name>')

  17. -- DEFAULT
  18. SELECT object_name(parent_object_id) as table_name,
  19.        name,
  20.        (select name from sys.columns where object_id =  df.parent_object_id and column_id = df.parent_column_id) as column_name, 
  21.        definition
  22. FROM sys.default_constraints   as df
  23. WHERE object_name(parent_object_id) = object_id('<@table_name, @sysname,@table_name>')

  24. -- DEFAULT
  25. SELECT
  26.     object_name(parent_object_id) as table_name,
  27.        name,
  28.        (select name from sys.columns where object_id =  ck.parent_object_id and column_id = ck.parent_column_id) as column_name, 
  29.        definition
  30. FROM sys.check_constraints  as ck
  31. WHERE object_name(parent_object_id) = object_id('<@table_name, @sysname,@table_name>')



  32. --=================
  33. -- 2000용
  34. --=================
  35. --FK
  36. select object_name(fkeyid) , object_name(constid),object_name(rkeyid)  
  37. from sys.sysforeignkeys where fkeyid = object_id ('<@table_name, @sysname,@table_name>')


  38. select object_name(id), object_name(constid) ,status from sys.sysconstraints  
  39. where status&5 = 1 --FK 
  40. where constid  = object_id('<@table_name, @sysname,@table_name>')
  41. order by object_name(constid)



  42. --=========================== 
  43. -- DROP
  44. --===========================
  45. -- FK 2005용
  46. SELECT 'ALTER TABLE ' + bject_name(fk.parent_object_id) + ' DROP CONSTRAINT ' +  object_name(fk.constraint_object_id)
  47. FROM sys.foreign_key_columns
  48. WHERE fk.parent_object_id  = object_id('<@table_name, @sysname,@table_name>') 

  49. -- DEFULT 2005용
  50. SELECT 'ALTER TBLE ' + object_name(parent_object_id)  + ' DROP CONSTRAINT ' + name
  51. FROM sys.default_constraints 
  52. WHERE object_name(parent_object_id) = object_id('<@table_name, @sysname,@table_name>')

  53. -- CK 2005용
  54. -- DEFULT 2005용
  55. SELECT 'ALTER TBLE ' + object_name(parent_object_id)  + ' DROP CONSTRAINT ' + name
  56. FROM sys.default_constraints  
  57. WHERE object_name(parent_object_id) = object_id('<@table_name, @sysname,@table_name>')


  58. -- FK 2000용
  59. SELECT 'ALTER TABLE ' + object_name(fkeyid) + ' DROP CONSTRAINT ' +  object_name(constid) 
  60. FROM sys.sysforeignkeys
  61. WHERE fkeyid = object_id ('<@table_name, @sysname,@table_name>')

  62. -- INDEX 2000용
  63. select  'ALTER TABLE ' + object_name(id) + ' DROP INDEX '  + name 
  64. from sys.sysindexes where  object_name(id) in ('GOODSDAQ_BANNER_POSITION')
  65. order by id, name




    

        


이 글은 스프링노트에서 작성되었습니다.

'T-SQL' 카테고리의 다른 글

T-SQL::DB_Restore_move_to  (0) 2010.06.03
T-SQL::Convert hex value to String 32bit  (0) 2010.06.03
SQL Server 2005 and 2008 Ranking Functions  (0) 2010.04.05
DMV::Index  (0) 2010.04.05