Oracle安装后必须干的几件事
1. 改默认密码有效期为无限期
详见CSDN博客:Oracle11g密码过期(默认180天)及设置密码无限期的方法
2. 给空表分表下segment
ORACLE 11G中有个新特性,当表无数据时,不分配segment,以节省空间。
影响:当使用exp导出xxxx.dmp文件时,空表不会导入,回导致表缺失的问题
1. 临时性解决Oracle11g空表导出问题
此法仅限本次对已经存在的表进行分析与分配空间,后面加的表就不起作用了,还是得每次分析一次
- 首先使用下面的sql语句查询一下当前用户下的所有空表
select table_name from user_tables where NUM_ROWS=0 or num_rows is null;
- 根据空表拼接
对空表分配空间
的语句
select 'alter table '||table_name||' allocate extent;' from user_tables where num_rows=0 or num_rows is null;
- 执行上面对空表分配空间的alter语句,类似语句如下
alter table TB_TEST1 allocate extent;
alter table TB_TEST2 allocate extent;
alter table TB_TEST3 allocate extent;
alter table TB_TEST4 allocate extent;
alter table TB_TEST5 allocate extent;
- 再次执行exp命令时就可以导出空表了
2. 永久性解决Oracle11g空表导出问题
通过修改Oracle11g的配置,自动为空表分配segment表空间,永久性解决空表导出问题
- 进入sqlplus
sqlplus / as sysdba
- 先查询下
deferred_segment_creation
的值
通过show parameter deferred_segment_creation;
语句查询,如下:
C:\Users\yuanjinsheng>sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on 星期三 12月 14 09:34:30 2022
Copyright (c) 1982, 2010, Oracle. All rights reserved.
连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
# 查询
SQL> show parameter deferred_segment_creation;
NAME TYPE
------------------------------------ ----------------------
VALUE
------------------------------
deferred_segment_creation boolean
TRUE
SQL>
具体如下图:
- 修改Oracle的deferred_segment_creation参数值,默认为true,改为false即可
执行alter system set deferred_segment_creation=false;
更改参数值
C:\Users\yuanjinsheng>sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on 星期三 12月 14 09:34:30 2022
Copyright (c) 1982, 2010, Oracle. All rights reserved.
连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
# 修改
SQL> alter system set deferred_segment_creation=false;
系统已更改。
#再次查看值为FALSE
SQL> show parameter deferred_segment_creation;
NAME TYPE
------------------------------------ ----------------------
VALUE
------------------------------
deferred_segment_creation boolean
FALSE
SQL>
具体过程如下图:
-
改完配置后重启Oracle数据库服务让设置的参数生效
-
注意:该参数设置后只对上面参数改动后新增的空表分配segment空间,对之前建立的空表(已经存在的空表)不分配segment空间,仍不能导出,需要通过下面语句进行分析处理
-
给原有的空表分配segment空间
# 1.适用数据库用户登录,并查询当前用户下的空表
select 'alter table '||table_name||' allocate extent;' from user_tables where num_rows=0 or num_rows is null;
# 2. 将上面语句执行的结果拷贝出来并一一执行一下,如下:
alter table TB_TEST1 allocate extent;
alter table TB_TEST2 allocate extent;
alter table TB_TEST3 allocate extent;
alter table TB_TEST4 allocate extent;
alter table TB_TEST5 allocate extent;
- 再次执行exp命令时就可以导出空表了
3. 表空间
备份与恢复数据更方便
以下脚本以Oracle12c为例,Oracle11g同样适用
1. 查看表空间
--查看用户
select * from dba_users t where t.username like 'C##%'
---查看表空间名称及表空间文件位置
select t1.name,t2.name from v$tablespace t1,v$datafile t2 where t1.ts# = t2.ts#;
---1.表空间信息查看
select * from dba_tablespaces; ---查看表空间
select * from dba_data_files; --查看数据库文件位置
select * from dba_free_space; --查看空闲空间
---2.临时表空间信息查看
select * from dba_temp_files;
select * from dba_temp_free_space;
2. 创建表空间及用户
--创建表空间
create tablespace TESTDB_DATA
datafile 'E:\APP\ORADATA\ORCL\TESTDB_DATA.dbf' --dbf文件位置
size 500M
autoextend on next 100M
maxsize unlimited;
--创建临时表空间
create temporary tablespace temporary_TESTDB_DATA
tempfile 'E:\APP\ORADATA\ORCL\TESTDB_DATA_temp.dbf'
size 500M
autoextend on
next 100M maxsize unlimited
extent management local;
---创建用户并指定默认表空间
create user c##testdb identified by testdb
profile default
default tablespace TESTDB_DATA
temporary tablespace temporary_TESTDB_DATA
account unlock;
3. 修改表空间(需要时)
---修改表的表空间
select 'alter table ' || TABLE_NAME || ' move tablespace docmanagement;'
from USER_TABLES UT
where UT.TABLESPACE_NAME = 'USERS'
--查询当前用户下在 USERS 表空间中的所有索引 ,并修改索引的表空间为 docmanagement
select 'alter index '|| index_name ||' rebuild tablespace docmanagement;' from user_indexes i
where i.tablespace_name = 'USERS'
4. 删除表空间(需要时)
--删除表空间及数据库文件
drop tablespace DOCMANAGEMENT including contents and datafiles;
--删除临时表空间及数据库文件
drop tablespace TEMPORARY_DOCMANAGEMENT including contents and datafiles;
4. 用户相关
以testdb/testdb作为用户名与密码举例
--创建用户
create user c##testdb identified by testdb
--用户授权,将connect,resource,dba权限授予testdb
grant connect,resource,dba to c##testdb
--修改用户密码
alter user testdb identified by test123
--删除用户
--drop user c##testdb cascade