2010. 6. 3. 23:55
T_SQL::CONSTRAINT조사
2010. 6. 3. 23:55 in T-SQL
CONSTRAINT 조사
- select * from sys.default_constraints
- select * from sys.key_constraints
- select * from sys.check_constraints
- --=================
- -- 2005용
- --=================
- -- FK
- SELECT object_name(fk.constraint_object_id) AS fk_name,
- fk.constraint_column_id AS fk_clolum,
- object_name(fk.parent_object_id) AS parent_name,
- (select name from sys.columns where object_id = fk.parent_object_id and column_id = fk.parent_column_id) as parent_column,
- object_name(fk.referenced_object_id) AS referenced_name,
- (select name from sys.columns where object_id = fk.referenced_object_id and column_id = fk.referenced_column_id) as referenced_column
- FROM sys.foreign_key_columns as fk
- WHERE fk.parent_object_id = object_id('<@table_name, @sysname,@table_name>')
- or fk.referenced_object_id = object_id('<@table_name, @sysname,@table_name>')
- -- DEFAULT
- SELECT object_name(parent_object_id) as table_name,
- name,
- (select name from sys.columns where object_id = df.parent_object_id and column_id = df.parent_column_id) as column_name,
- definition
- FROM sys.default_constraints as df
- WHERE object_name(parent_object_id) = object_id('<@table_name, @sysname,@table_name>')
- -- DEFAULT
- SELECT
- object_name(parent_object_id) as table_name,
- name,
- (select name from sys.columns where object_id = ck.parent_object_id and column_id = ck.parent_column_id) as column_name,
- definition
- FROM sys.check_constraints as ck
- WHERE object_name(parent_object_id) = object_id('<@table_name, @sysname,@table_name>')
- --=================
- -- 2000용
- --=================
- --FK
- select object_name(fkeyid) , object_name(constid),object_name(rkeyid)
- from sys.sysforeignkeys where fkeyid = object_id ('<@table_name, @sysname,@table_name>')
- select object_name(id), object_name(constid) ,status from sys.sysconstraints
- where status&5 = 1 --FK
- where constid = object_id('<@table_name, @sysname,@table_name>')
- order by object_name(constid)
- --===========================
- -- DROP
- --===========================
- -- FK 2005용
- SELECT 'ALTER TABLE ' + bject_name(fk.parent_object_id) + ' DROP CONSTRAINT ' + object_name(fk.constraint_object_id)
- FROM sys.foreign_key_columns
- WHERE fk.parent_object_id = object_id('<@table_name, @sysname,@table_name>')
- -- DEFULT 2005용
- SELECT 'ALTER TBLE ' + object_name(parent_object_id) + ' DROP CONSTRAINT ' + name
- FROM sys.default_constraints
- WHERE object_name(parent_object_id) = object_id('<@table_name, @sysname,@table_name>')
- -- CK 2005용
- -- DEFULT 2005용
- SELECT 'ALTER TBLE ' + object_name(parent_object_id) + ' DROP CONSTRAINT ' + name
- FROM sys.default_constraints
- WHERE object_name(parent_object_id) = object_id('<@table_name, @sysname,@table_name>')
- -- FK 2000용
- SELECT 'ALTER TABLE ' + object_name(fkeyid) + ' DROP CONSTRAINT ' + object_name(constid)
- FROM sys.sysforeignkeys
- WHERE fkeyid = object_id ('<@table_name, @sysname,@table_name>')
- -- INDEX 2000용
- select 'ALTER TABLE ' + object_name(id) + ' DROP INDEX ' + name
- from sys.sysindexes where object_name(id) in ('GOODSDAQ_BANNER_POSITION')
- 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 |