Oracle查找数据库中没有主键的表

这篇博客分享了如何使用SQL查询在Oracle数据库中找出没有主键的表,区分是否以'SYS'开头,并进一步查询所有空表和非空表。通过LEFT JOIN操作结合所有非'SYS'开头的空表,提供了一种全面了解数据库表状态的方法。
摘要由CSDN通过智能技术生成

Oracle查找数据库中没有主键的表

--没有主键的表
select table_name
  from user_tables a
 where not exists (select *
          from user_constraints b
         where b.constraint_type = 'P'
           and a.table_name = b.table_name)
           
           
--SYS开头的       

select table_name
  from user_tables a
 where not exists (select *
          from user_constraints b
         where b.constraint_type = 'P'
           and a.table_name = b.table_name)
and table_name  LIKE 'SYS%' 


--不是SYS开头的       

select table_name
  from user_tables a
 where not exists (select *
          from user_constraints b
         where b.constraint_type = 'P'
           and a.table_name = b.table_name)
and table_name NOT LIKE 'SYS%' 

--所有空表
select table_name from all_all_tables where owner='MDM' AND num_rows ='0'
--所有非空表且不是SYS开头
SELECT x.* FROM (select table_name
  from user_tables a
 where not exists (select *
          from user_constraints b
         where b.constraint_type = 'P'
           and a.table_name = b.table_name)
and table_name NOT LIKE 'SYS%')x LEFT JOIN (select table_name from all_all_tables where owner='MDM' AND num_rows ='0')y ON x.table_name = y.table_name
WHERE y.table_name IS NULL
--所有不是SYS开头的空表
SELECT x.* FROM (select table_name
  from user_tables a
 where not exists (select *
          from user_constraints b
         where b.constraint_type = 'P'
           and a.table_name = b.table_name)
and table_name NOT LIKE 'SYS%')x LEFT JOIN (select table_name from all_all_tables where owner='MDM' AND num_rows ='0')y ON x.table_name = y.table_name
WHERE y.table_name IS not null
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值