Oracle表操作
accttodo 8/1/2022 后端数据库Oracle
[TOC]
# Oracle查询表
--当前用户拥有的表
select table_name from user_tables;
1
2
2
--所有用户的表
select table_name from all_tables;
1
2
2
--所有表,包括系统表
select table_name from dba_tables;
1
2
2
--根据查询条件
select table_name from dba_tables where owner='用户名';
1
2
2
常用列字段:
user_tables:
table_name
,tablespace_name
,last_analyzed
all_tables、dba_tables:
ower
,table_name
,tablespace_name
,last_analyzed
all_objects:
ower
,object_name
,subobject_name
,object_id
,created
,last_ddl_time
,timestamp
,status
# Oracle查询表字段
# 查询表字段信息
--当前用户拥有的表
select * from user_tab_columns where Table_Name='用户表';
1
2
2
--所有用户的表
select * from all_tab_columns where Table_Name='用户表';
1
2
2
--所有表,包括系统表
select * from dba_tab_columns where Table_Name='用户表';
1
2
2
常用列字段:
user_tab_columns:
table_name
,column_name
,data_type
,data_length
,data_precision
,data_scale
,nullable
,column_id
all_tab_columns 、dba_tab_columns:
ower
,table_name
,column_name
,data_type
,data_length
,data_precision
,data_scale,nullable
,`column_id
# 查询表字段注释信息
--当前用户拥有的表
SELECT * FROM user_col_comments WHERE TABLE_NAME='TABLE_NAME'
1
2
2
--所有用户的表
SELECT * FROM all_col_comments WHERE TABLE_NAME='TABLE_NAME'
1
2
2
--所有表,包括系统表
SELECT * FROM dba_col_comments WHERE TABLE_NAME='TABLE_NAME'
1
2
2
常用列字段:
user_col_comments:
table_name
,column_name
,comments
all_col_comments 、dba_col_comments:
ower
,table_name
,column_name
,comments
# Oracle删除所有的表
执行删除之前一定要确保做好备份或者其他确认措施!
- 禁用数据库中所有的约束
--查询拼接所有约束禁用脚本后,执行所有约束禁用脚本。
select 'alter table ' || table_name || ' disable constraint ' || constraint_name || ';'
from user_constraints
where constraint_type = 'R';
1
2
3
4
2
3
4
- 清空所有表中的数据
--查询拼接所有的清表脚本后,执行所有的清表脚本。
select 'truncate table ' || table_name || ';'
from user_tables;
1
2
3
2
3
- 删除所有表
--查询拼接所有的删除脚本后,执行所有的删除脚本。(purge 删除表不进入回收站)
select 'drop table ' || table_name || 'purge;' as sqlscript
from user_tables;
--或
select 'drop table ' || table_name || 'purge;' as sqlscript
from cat
where table_type = 'TABLE' ;
1
2
3
4
5
6
7
2
3
4
5
6
7
- 启用数据库中所有表的约束
--查询拼接所有约束启用脚本后,执行所有约束启用脚本。
select 'alter table ' || table_name || ' enable constraint ' || constraint_name || ';'
from user_constraints
where constraint_type = 'R';
1
2
3
4
2
3
4
注意:执行删除之后,表会进入回收站,出现BIN$开头的的表名,常用操作脚本如下:
--清空回收站
PURGE recyclebin;
1
2
2
--查询回收站表
select t.object_name,t.type ,t.original_name
FROM user_recyclebin t;
1
2
3
2
3
--删除表不进入回收站
drop table tableName purge;
1
2
2