Oracle系列之表空间

涉及到表的处理请参看原表结构与数据  Oracle建表插数据等等

创建表空间等等

select tablespace_name from dba_tablespaces;--dba权限用户查询数据库中的表空间
select * from all_tables where tablespace_name='tablespace_name';--查询表空间中的表,注意大写
select tablespace_name,table_name from user_tables where table_name='tb_Employee';--查询表处于哪个表空间

--创建用户的时候定义默认表空间,设置用户默认的表空间和临时的表空间,并设置用户建立的数据对象最大只能是3m,设置初始账户为锁定状态
create user db_user identified by password default tablespace tablespace_name temporary tablespace tablespace_name quota 3m on tablespace_name account lock;
create tablespace tablespace_name datafile 'E: ablespace_name.dbf' size 20m uniform size 128k;--建立表空间
create table table_name(deptno number(4),dname varchar2(14),loc varchar2(13)) tablespace tablespace_name;--为表指定表空间
create index index_name on table_name(column_name) tablespace tablespace_name;--为索引指定表空间
alter user db_user default tablespace tablespace_name;--修改用户默认表空间
拥有unlimited tablespace权限的用户可在任意表空间上操作(grant unlimited tablespace to db_user)。没有unlimited tablespace权限的用户要在非默认表空间上操作需要在目标表空间有一定的配额,即在目标表空间分配给用户一定的空间或者不限制空间。
alter user db_user quota 100m||unlimited on tablespace_name;

改变表空间的状态

当建立表空间时,表空间处于联机的(online)状态,此时该表空间是可以访问的,并且该表空间是可以读写的,即可以查询该表空间的数据,而且还可以在表空间执行各种语句。但是在进行系统维护或是数据维护时,可能需要改变表空间的状态。一般情况下,由特权用户或是dba来操作。
alter tablespace tablespace_name offline;--1. 使表空间脱机
alter tablespace tablespace_name online;--2. 使表空间联机 
当建立表空间时,表空间可以读写,如果不希望在该表空间上执行update,delete,insert操作,那么可以将表空间修改为只读
alter tablespace tablespace_name read only;--3. 只读表空间
(修改为可写是 alter tablespace tablespace_name read write;)
我们给大家举一个实例,说明只读特性:
select * from all_tables where tablespace_name='tablespace_name';--知道tablespace_name,显示该表空间包括的所有表,查询表空间中的表,注意大写
select tablespace_name, table_name from user_tables where table_name='tb_Employee';--知道table_name,查看该表属于那个表空间
通过2.我们可以知道system.tb_Employee是在system这个表空间上,现在我们可以将system改为只读的但是我们不会成功,因为system是系统表空间,如果是普通表空间,那么我们就可以将其设为只读的,给大家做一个演示,可以加强理解。
alter tablespace tablespace_name read only;
alter tablespace tablespace_name read write;--使表空间可读写

一般情况下,由特权用户或是dba来操作,如果是其它用户操作,那么要求用户具有drop tablespace系统权限。
drop tablespace tablespace_name including contents and datafiles;--删除表空间
说明:including contents表示删除表空间时,删除该空间的所有数据库对象,而datafiles表示将数据库文件也删除。

扩展表空间

表空间是由数据文件组成的,表空间的大小实际上就是数据文件相加后的大小。那么我们可以想象,假定表tb_Employee存放到tablespace_name表空间上,初始大小就是2m,当数据满2m空间后,如果在向tb_Employee表插入数据,这样就会显示空间不足的错误。
案例说明:
1. 建立一个表空间 fj01
2. 在该表空间上建立一个普通表 mydment 其结构和dept一样
3. 向该表中加入数据 insert into mydment select * from dept;
4. 当一定时候就会出现无法扩展的问题,怎么办?
5. 就扩展该表空间,为其增加更多的存储空间。有三种方法:
1. 增加数据文件
alter tablespace fj01 add datafile 'E:fj01.dbf' size 20m;
2. 增加数据文件的大小
alter tablespace tablespace_name 'E:fj01.dbf' resize 20m;
这里需要注意的是数据文件的大小不要超过500m。
3. 设置文件的自动增长。
alter tablespace tablespace_name 'E:fj01.dbf' autoextend on next 10m maxsize 500m extend management local;

删除表空间及其所有内容,同时删除其所对应的数据文件:
drop tablespace tablespace_name including contents and datafiles;
删除表空间及其所有内容,同时删除其所对应的数据文件,以及其他表空间中与表空间相关的参照完整性约束:
drop tablespace tablespace_name including contents and datafiles cascade constraints;

原文地址:https://www.cnblogs.com/tufujie/p/5074104.html