一、只读表空间
只读的表空间只能进行select操作,设定只读表空间的命令为:
ALTER TABLESPACE userdata READ ONLY;
设定只读表空间会触发checkpoint,将缓存中的数据写入data file;但是尽管设定了只读属性,依然可以对这个表空间进行drop操作(比如drop table),因为drop操作并没有改变表空间的结构,只是更改了数据字典里的值,而数据字典是存储在SYSTEM TABLESPACE里的,SYSTEM TABLESPACE是不可以设定为READ ONLY的。
========创建一个表空间========
SQL> create tablespace wiley
2 datafile‘/oracle/oradata/orcl/wiley.dbf‘ size 20M
3 extent management local uniformsize 128k;
Tablespace created.
==========创建一个用户,指定其缺省的表空间为新建的表空间===========
SQL> create user wiley identified by******
2 default tablespace wiley;
-- 创建用户的相关信息可以查阅联机文档SQL REFERENCE,这里创建了用户wiley,并将其 -- 缺省的表空间指定为新建的表空间wiley。 User created.
SQL> grant connect, resource to wiley;
-- 为新用户赋相应的权限 Grant succeeded.
===========新用户登录数据库==============
[oracle@localhost ~]$ sqlplus wiley
SQL*Plus: Release 11.2.0.1.0 Production on Sat Aug 3014:44:12 2014 Copyright (c) 1982, 2009, Oracle. All rights reserved. Enter password: Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 -Production With the Partitioning, OLAP, Data Mining and RealApplication Testing options
==========创建表,并插入一条数据==========
SQL> create table t (id integer, namechar(10));
Table created.
SQL> insert into t values(0, ‘wiley‘);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from t;
ID NAME ---------- ---------- 0 wiley
========再插入一条数据,但是不commit===========
SQL> insert into t values(1, ‘2014‘);
1 row created.
SQL> select * from t;
ID NAME ---------- ---------- 0 wiley 1 2014
============切换成SYS用户==============
SQL> show user
USER is "SYS"
SQL> alter tablespace wiley read only;
-- 执行alter命令时,数据库被hold住了因为用户没有commit
======切换为wiley用户,commit刚才插入的值========
SQL> show user
USER is "WILEY"
SQL> commit;
Commit complete.
========再次切换为sys用户========
SQL> alter tablespace wiley read only;
Tablespace altered. -- tablespace已经被altered了
===========再次切换为wiley用户============
SQL> insert into t values (2, ‘mickey‘);
insert into t values (2, ‘mickey‘) * ERROR at line 1: ORA-00372: file 6 cannot be modified at this time ORA-01110: data file 6: ‘/oracle/oradata/orcl/wiley.dbf‘ -- 不能插入数据了
SQL> delete from t where id = 1;
delete from t where id = 1 * ERROR at line 1: ORA-00372: file 6 cannot be modified at this time ORA-01110: data file 6: ‘/oracle/oradata/orcl/wiley.dbf‘ -- 也不能删除数据
SQL> truncate table t;
truncate table t * ERROR at line 1: ORA-00372: file 6 cannot be modified at this time ORA-01110: data file 6: ‘/oracle/oradata/orcl/wiley.dbf‘ -- 也不能truncate表,因为truncate只删除数据,而不删除表结构,它和delete是等效的
SQL> select * from t;
ID NAME ---------- ---------- 0 wiley 1 2014 -- 可以查询
SQL> drop table t;
Table dropped. -- 可以drop table。
========切换为sys用户,将wiley设为可读写的表空间==========
SQL> show user
USER is "SYS"
SQL> alter tablespace wiley read write;
Tablespace altered.
只读表空间可以用来存放不经常修改的静态数据,因为只读表空间不需要恢复(recovery),从而可以提高数据库的性能。
二、将表空间设为离线
数据库在进行备份恢复时,将表空间设为离线状态会更加可靠;当表空间设为离线后,用户将不能访问数据了。但是有些情况是不能讲表空间设为离线的:
1. SYSTEM 表空间不能设为离线;
2. 如果表空间里面正在进行一些特定的事物(transaction),数据库就会在undotablespace中增加一个undosegment,这个undosegment就出于active状态,那么表空间也不能设为离线;
3. 缺省的全局临时表空间,如果这个临时表空间不是default的,是可以离线的;
=========以wiley用户创建一个表=========
SQL> create table tt (id integer, namechar(10));
Table created.
SQL> insert into tt values(0, ‘aaa‘);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from tt;
ID NAME ---------- ---------- 0 aaa
SQL> insert into tt values (1, ‘bbb‘);
1 row created.
SQL> select * from tt;
ID NAME ---------- ---------- 0 aaa 1 bbb
=======切换成SYS用户,将表空间wiley设为离线=========
SQL> alter tablespace wiley offline;
Tablespace altered. -- 即使用户有没有commit,也可以将表空间设为离线
========以wiley用户重新登录===========
[oracle@localhost~]$ sqlplus wiley
SQL*Plus: Release 11.2.0.1.0 Production on Sat Aug 3015:18:30 2014 Copyright (c) 1982, 2009, Oracle. All rights reserved. Enter password: Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 -Production With the Partitioning, OLAP, Data Mining and RealApplication Testing options # 离线的tablespace也可以登录
SQL> select * from tt;
select * from tt * ERROR at line 1: ORA-00376: file 6 cannot be read at this time ORA-01110: data file 6: ‘/oracle/oradata/orcl/wiley.dbf‘
========切回SYS用户,然后将wiley这个表空间设为在线========
SQL> alter tablespace wiley online;
Tablespace altered.
===========切回wiley用户==========
SQL> select * from tt;
-- 可以执行查询语句了 ID NAME ---------- ---------- 0 aaa 1 bbb
在设定离线时,可以附加三种选项:NORMAL, TEMPORARY和IMMEDIATE,分别对应不同的强制级别,可以查询联机文档SQL REFERENCE 获取相关信息。
本文出自 “重剑无锋 大巧不工” 博客,请务必保留此出处http://wuyelan.blog.51cto.com/6118147/1560213