1、查找表的所有索引(包括索引名,類型,構成列):
select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name = 要查詢的表
2、查找表的主鍵(包括名稱,構成列):
select cu.* from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'P' and au.table_name = 要查詢的表
3、查找表的唯一性約束(包括名稱,構成列):
select column_name from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'U' and au.table_name = 要查詢的表
4、查找表的外鍵(包括名稱,引用表的表名和對應的鍵名,下面是分成多步查詢):
select * from user_constraints c where c.constraint_type = 'R' and c.table_name = 要查詢的表
5、查詢外鍵約束的列名:
select * from user_cons_columns cl where cl.constraint_name = 外鍵名稱
6、查詢引用表的鍵的列名:
實例:
7、查詢沒有建立主鍵的表
select * from user_cons_columns cl where cl.constraint_name = 外鍵引用表的鍵名
實例:
7、查詢沒有建立主鍵的表
select u.table_name, u.num_rows
from user_tables u
where not exists (select cu.table_name
from user_cons_columns cu, user_constraints au
where cu.constraint_name = au.constraint_name
and au.constraint_type = 'P'
and au.table_name = u.table_name)
and u.num_rows is not null
order by u.num_rows desc;
8、查詢表記錄中有空值的索引字段from user_tables u
where not exists (select cu.table_name
from user_cons_columns cu, user_constraints au
where cu.constraint_name = au.constraint_name
and au.constraint_type = 'P'
and au.table_name = u.table_name)
and u.num_rows is not null
order by u.num_rows desc;
-- Create table
create table TEMP_INDEX
(
ID VARCHAR2(32),
TABLE_NAME VARCHAR2(100),
COLUMN_NAME VARCHAR2(100),
INDEX_NAME VARCHAR2(100),
SCSJ DATE
)
tablespace JG_ZFGFH
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 16
minextents 1
maxextents unlimited
);
-- Add comments to the table
comment on table TEMP_INDEX
is '放入索引值有空的表和列';
-- Add comments to the columns
comment on column TEMP_INDEX.ID
is '自動生成';
comment on column TEMP_INDEX.TABLE_NAME
is '表名';
comment on column TEMP_INDEX.COLUMN_NAME
is '字段名稱';
comment on column TEMP_INDEX.INDEX_NAME
is '索引名稱';
comment on column TEMP_INDEX.SCSJ
is '生成時間';
create table TEMP_INDEX
(
ID VARCHAR2(32),
TABLE_NAME VARCHAR2(100),
COLUMN_NAME VARCHAR2(100),
INDEX_NAME VARCHAR2(100),
SCSJ DATE
)
tablespace JG_ZFGFH
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 16
minextents 1
maxextents unlimited
);
-- Add comments to the table
comment on table TEMP_INDEX
is '放入索引值有空的表和列';
-- Add comments to the columns
comment on column TEMP_INDEX.ID
is '自動生成';
comment on column TEMP_INDEX.TABLE_NAME
is '表名';
comment on column TEMP_INDEX.COLUMN_NAME
is '字段名稱';
comment on column TEMP_INDEX.INDEX_NAME
is '索引名稱';
comment on column TEMP_INDEX.SCSJ
is '生成時間';
create or replace procedure P_PROCESS_INDEX
/*********************************************************************************
-----------功能:得到表索引字段中有空值字段
-----------作者: Xcp
-----------創建日期:2013-02-20
-----------版本 v1.0
*******************************************************************************/
is
cursor T_INDEX_CURSOR is
select i.table_name, t.column_name, t.index_name
from user_ind_columns t, user_indexes i, user_tab_cols c
where t.index_name = i.index_name
and t.column_name = c.column_name
and t.table_name = i.table_name
and c.table_name = i.table_name
order by c.column_id;
T_COUNT number:=0;
T_SQL varchar2(1000);
T_PRE_TABLE_NAME varchar2(100);
begin
--清空記錄保存表
delete from TEMP_INDEX;
commit;
--重新清理
for T_INDEX in T_INDEX_CURSOR loop
--事務控制,每個表提交一次
if T_PRE_TABLE_NAME is null then
T_PRE_TABLE_NAME:=T_INDEX.Table_Name;
elsif T_PRE_TABLE_NAME<>T_INDEX.Table_Name then
commit;
end if;
--求是該索引字段是否有空
begin
T_SQL:='select count(1) from '||T_INDEX.TABLE_NAME||' where '||T_INDEX.Column_Name||' is null ' ;
--dbms_output.put_line(T_SQL);
execute immediate T_SQL into T_COUNT;
--dbms_output.put_line(T_COUNT) ;
if T_COUNT>0 then
insert into TEMP_INDEX values(sys_guid(),T_INDEX.Table_Name,T_INDEX.COLUMN_NAME,T_INDEX.Index_Name,sysdate);
end if;
exception
when others then dbms_output.put_line('NO DATA FOUND!');
end;
end loop;
--事務控制,最后一個表的事務
if T_INDEX_CURSOR%NOTFOUND then
commit;
end if;
end P_PROCESS_INDEX;
/*********************************************************************************
-----------功能:得到表索引字段中有空值字段
-----------作者: Xcp
-----------創建日期:2013-02-20
-----------版本 v1.0
*******************************************************************************/
is
cursor T_INDEX_CURSOR is
select i.table_name, t.column_name, t.index_name
from user_ind_columns t, user_indexes i, user_tab_cols c
where t.index_name = i.index_name
and t.column_name = c.column_name
and t.table_name = i.table_name
and c.table_name = i.table_name
order by c.column_id;
T_COUNT number:=0;
T_SQL varchar2(1000);
T_PRE_TABLE_NAME varchar2(100);
begin
--清空記錄保存表
delete from TEMP_INDEX;
commit;
--重新清理
for T_INDEX in T_INDEX_CURSOR loop
--事務控制,每個表提交一次
if T_PRE_TABLE_NAME is null then
T_PRE_TABLE_NAME:=T_INDEX.Table_Name;
elsif T_PRE_TABLE_NAME<>T_INDEX.Table_Name then
commit;
end if;
--求是該索引字段是否有空
begin
T_SQL:='select count(1) from '||T_INDEX.TABLE_NAME||' where '||T_INDEX.Column_Name||' is null ' ;
--dbms_output.put_line(T_SQL);
execute immediate T_SQL into T_COUNT;
--dbms_output.put_line(T_COUNT) ;
if T_COUNT>0 then
insert into TEMP_INDEX values(sys_guid(),T_INDEX.Table_Name,T_INDEX.COLUMN_NAME,T_INDEX.Index_Name,sysdate);
end if;
exception
when others then dbms_output.put_line('NO DATA FOUND!');
end;
end loop;
--事務控制,最后一個表的事務
if T_INDEX_CURSOR%NOTFOUND then
commit;
end if;
end P_PROCESS_INDEX;
名稱: ?4C.ESL | .↗Evon
口號: 遇到新問題?先要尋找一個方案乄而不是創造一個方案こ
mail: 聯系我