暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

虚谷数据库-如何通过系统表查询约束、索引的相关信息

原创 W 2023-11-29
585

1、查看表是否存在约束

--在任何库下使用拥有dba权限的用户查询
select t.table_name,c.*
from dba_constraints c
join dba_tables t
on c.table_id=t.table_id
where REF_TABLE_ID=(select table_id from dba_tables where table_name='表名');

2、查某个模式下的对应表的索引个数

--在任何库下使用拥有dba权限的用户查询
select t.table_name,count(*) from dba_tables t,dba_indexes i,dba_schemas s where t.table_id=i.table_id and
t.schema_id=s.schema_id and t.db_id=s.db_id and t.db_id=i.db_id and s.schema_name='模式名' group by t.table_name order by t.table_name;


--在任何库下使用拥有dba权限的用户查询
select t.table_name,count(*) from dba_tables t left join dba_indexes i on (t.table_id=i.table_id and t.db_id=i.db_id ) join dba_schemas s on
(t.schema_id=s.schema_id and t.db_id=s.db_id) and s.schema_name='模式名' group by t.table_name order by t.table_name;


--在任何库下使用拥有dba权限的用户查询
select * from dba_partis p,dba_tables t,dba_schemas s where t.table_id=p.table_id and t.schema_id=s.schema_id and s.schema_name='模式名';


--在任何库下使用拥有dba权限的用户查询
select t.table_name || '|' || t.table_name || '|D_DATETIME|' || p.parti_val from dba_tables t,dba_partis p,dba_schemas s where
t.table_id=p.table_id and t.schema_id=s.schema_id and s.schema_name='USR_SOD' and t.table_name='表名' order by p.parti_val;

3、查询表索引信息
--在任何库下使用拥有dba权限的用户查询
select t.table_name,i.index_name,i.keys,case when i.is_primary is false and i.is_unique is false then 'idx' when i.is_primary is true then 'primary' when i.is_primary is false and i.is_unique is true then 'unique' end type
from dba_tables t,dba_schemas s,dba_indexes i where t.schema_id=s.schema_id and t.table_id=i.table_id and s.schema_name='模式名' and regexp_like(t.table_name,'ABC');
4、查询某个模式的自增值。
--在任何库下使用拥有dba权限的用户查询
select
'alter table '|| schema_name||'.'||table_name||' alter column '|| col_name ||' '||type_name || ' identity(' || curr_val ||',1);'
-- wm_concat(''''||table_name||'''')
from dba_columns c
inner join dba_tables t on c.table_id=t.table_id
inner join dba_schemas s on t.schema_id=s.schema_id
inner join dba_sequences sq on serial_id=seq_id
where schema_name='模式名' and is_serial=true;
5、 查询自增值的表是否有唯一约束,如果唯一约束字段为自增字段则需要删除唯一约束,才能创建自增。
--在任何库下使用拥有dba权限的用户查询
select
'alter table ' || schema_name || '.' || table_name || ' drop constraint ' || cons_name || ';'
from dba_constraints c
inner join dba_tables t on c.table_id=t.table_id
inner join dba_schemas s on t.schema_id=s.schema_id
where schema_name='模式名'
and table_name ='表名';
————————————————
版权声明:本文为CSDN博主「liang_kk」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_56058732/article/details/134198597

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论