01 创建数据表空间
本地管理的表空间:
create tablespace test01 datafile '/oracle/oradata/orcl/test01a.dbf' size 2m autoextend off segment space management auto;
autoextend off —不自动扩展
segment space management auto —自动段管理,推荐
CREATE TABLESPACE test02 LOGGING DATAFILE '/oracle/oradata/orcl/test02a.dbf' SIZE 2M AUTOEXTEND OFF, '/oracle/oradata/orcl/test02b.dbf' SIZE 2M AUTOEXTEND OFF EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;
CREATE TABLESPACE test03 LOGGING DATAFILE '/oracle/oradata/orcl/test03a.dbf' SIZE 2M AUTOEXTEND ON NEXT 1M MAXSIZE 20M, '/oracle/oradata/orcl/test03b.dbf' SIZE 2M AUTOEXTEND ON NEXT 1M MAXSIZE 20M EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;
创建大表空间:
CREATE BIGFILE TABLESPACE bigtbs DATAFILE '/u02/oracle/data/bigtbs01.dbf' SIZE 50G;
02 创建临时表空间
create temporary tablespace temp1 tempfile '/oracle/oradata/orcl/temp1.dbf' size 5m autoextend off;
03 创建UNDO表空间
create undo tablespace testundo1 datafile '/oracle/oradata/orcl/testundo1.dbf' size 2m autoextend off;
04 表空间的扩展与修改大小
alter tablespace temp1 add tempfile '/oracle/oradata/orcl/temp1b.dbf' size 5m autoextend off;
alter tablespace test01 add datafile '/oracle/oradata/orcl/test01b.dbf' size 1m autoextend off;
alter database datafile '/oracle/oradata/orcl/test02b.dbf' resize 2m;
select name, bytes from v$tempfile;
alter database tempfile '/oracle/oradata/orcl/temp1.dbf' resize 5m;
alter tablespace temp add tempfile '/oracle/oradata/orcl/tempb.dbf' size 1m;
05 表空间重命名
ALTER TABLESPACE test03 RENAME TO test04;
06 删除表空间
drop tablespace testundo1; -- 直接删除表空间,而不删除对应的数据文件
drop tablespace test04 including contents and datafiles; --加上该选项 则连同数据文件 一起删除了
如果出现:ORA-02449: unique/primary keys in table referenced by foreign keys
select p.owner, p.table_name, p.constraint_name, f.table_name referencing_table, f.constraint_name foreign_key_name, f.status fk_status from dba_constraints P, dba_constraints F, dba_tables T where p.constraint_name = f.r_constraint_name and f.constraint_type = 'R' and p.table_name = t.table_name and t.tablespace_name = UPPER('&tablespace_name') order by 1,2,3,4,5;
drop tablespace test04 including contents and datafiles cascade constraints;
07 更改表空间的读写模式
alter tablespace test01 read only; alter tablespace test01 read write;
select tablespace_name,status from dba_tablespaces;
11g:
alter table test read only; alter table test read write;
08 更改表空间的在线模式
alter tablespace users offline; alter database datafile 6 offline; alter database datafile 6 offline for drop; alter database datafile 6 online; recover datafile 6;