脚本:查看约束信息



说明:首先要使用sys用户建立一个讲long转成字符串的函数 
之后用sys用户调用脚本 
该函数来源于 http://blog.163.com/yanenshun@126/blog/static/128388169201311318581758/

CREATE OR REPLACE FUNCTION sys.LONG_TO_CHAR(in_rowid      rowid,
                                        in_owner      varchar,
                                        in_table_name varchar,
                                        in_column     varchar2)
  RETURN varchar AS
  text_c1 varchar2(32767);
  sql_cur varchar2(2000);

begin
  DBMS_OUTPUT.ENABLE(buffer_size => null);
  sql_cur := 'select ' || in_column || ' from
' || in_owner || '.' || in_table_name || ' where rowid =
' || chr(39) || in_rowid || chr(39);
 dbms_Output.put_line(sql_Cur) ;

--  execute immediate sql_cu     into text_c1;

  text_c1 := substr(text_c1, 1, 4000);
  RETURN TEXT_C1;
END;


SELECT OWNER,TABLE_NAME,constraint_name,constraint_type,TO_CHAR(wm_concat(column_name))
FROM  
(SELECT 
T1.OWNER,
T1.table_name,
T2.constraint_name, 
CASE WHEN T1.constraint_type = 'P' then 'PRIMARY KEY' 
     WHEN T1.constraint_type = 'R' then 'FOREIGN KEY'
     WHEN T1.constraint_type = 'U' then 'UNIQUE KEY'
     WHEN T1.constraint_type = 'C' then 'CHECK'
 END   constraint_type,
 T2.column_name        
FROM DBA_CONSTRAINTS  T1  ,DBA_CONS_COLUMNS T2 
WHERE T1.owner=T2.owner
AND T1.table_name = T2.table_name
AND T1.constraint_name=T2.constraint_name
AND T1.constraint_type !='C'
AND T1.owner='BOSENRUI'
ORDER BY T1.OWNER,
T1.table_name,
T2.constraint_name,
constraint_type,
t2.position)
GROUP BY  OWNER,TABLE_NAME,constraint_name,constraint_type
UNION ALL

SELECT US.NAME OWNER ,
       CEO.NAME  TABLE_NAME,
       CON.NAME constraint_name,
       'CHECK',
       LONG_TO_CHAR(CDEF.ROWID,'SYS','CDEF$','CONDITION') check_condition
FROM SYS.CON$  CON  , 
     SYS.USER$ US,
     SYS.CDEF$ CDEF ,
     sys."_CURRENT_EDITION_OBJ"  CEO
WHERE   CON.OWNER#=US.USER#
 AND CON.CON# = CDEF.CON# 
 AND CEO.obj# = CDEF.obj#
 AND CDEF.TYPE# IN (1,7)
 and US.NAME='BOSENRUI'

 

你可能感兴趣的:(ORACLE开发)