Oracle如何创建表空间和备份数据
1.创建数据表空间
create tablespace "archives"
logging
datafile 'D:\oracle\product\10.2.0\oradata\a' size 500m extent
management local segment space management auto ;
alter database
datafile 'D:\oracle\product\10.2.0\oradata\a' autoextend
on next 1m;
2.创建临时表空间(日志文件)
create
temporary tablespace "chenxm_temp" tempfile
'e:\spaces_eu\a' size 100m autoextend
on next 1m maxsize unlimited extent management local uniform
size 1m;
3.创建用户和授权
create user "eucrm" profile "default"
identified by "1" default tablespace "chenxm"
temporary tablespace "chenxm_temp"
account unlock;
grant unlimited tablespace to "eucrm";
grant create database link to "eucrm";
grant create any table to "eucrm";
grant create any trigger to "eucrm";
grant create any view to "eucrm";
grant create database link to "eucrm";
grant create procedure to "eucrm";
grant create table to "eucrm";
grant create tablespace to "eucrm";
grant create trigger to "eucrm";
grant drop any table to "eucrm";
grant insert any table to "eucrm";
grant update any table to "eucrm";
grant "connect" to "eucrm";
alter user "eucrm" default role all;
4.删除表空间与用户
drop user eucrm cascade;
drop tablespace chenxm including contents;
drop tablespace chenxm_temp including contents;
5.创建表
建表时最好依照第三范式去建表(三个范式),这样可以最大程度避免出现数据冗余,
而且表结构合理的情况下,可以加快查询速度
第一范式:表中每一个字段的数据都不可分割。
在目前的数据库软件及其设计中,基本只有char类字段要考虑这一范式
(而不是像有些人说的现有数据库完全不需要考虑,实际上很多垃圾软件公司设计数据库为了方便以后变更,很多都用varchar型的字段)
,但适当合并一些char类字段,可简化数据库结构,如将电话1、电话2、电话3(定长字符串)类的设计改为电话(varchar型变长字符串)
采用适当的正则表达式确保输入数据的正确性.
第二范式:非主键列完全依赖主键列。
与完全依赖所对应的部分依赖,是指某些非主键列仅依赖复合主键中的部分列,而不是整个复合主键。因此所有单主键的表都符合第二范式。
(学号,课程名称)→ (姓名,年龄,成绩,学分)
违反第二范式:
课程名称学分,学分实际上只依赖于课程名(与学号无任何关系)
学号姓名,年龄,同理,姓名、年龄与课程名无任何关系。
第三范式:非主键列不可传递依赖主键,即非主键列互不依赖。
(学号)→ (姓名,年龄,所在学院,学院地点,学院电话)
违反第三范式:(学号)→ (所在学院)→ (学院地点,学院电话)
6.建立索引
索引:在经常查询的字段上面建立索引,
利用index对查询进行优化,(index可以避免对表的一个全面扫描)
原理:当以某个字段建立一个索引的时候,数据库就会生成一个索引页,
索引页不单单保存索引的数据,还保存了索引在数据库的具体的物理地址[rowid],
单我们查询数据时,oracle会先查索引页,这样就能够很快的定位查到要的记录)
注意:
如果表的列很少,不适合建索引.
当执行过多次的insert,delete,update,会出现索引碎片,
影响查询速度,我们应该对索引进行重组.
7. sql语句的优化
尽量使你的sql语句能够使用索引。
怎样使sql语句能够使用到索引呢:
sql语句中包含drop table if exists adminnot in,<>,is null,is not null,like '%%'的时候不会用索引。
IN: in会拆成一堆or,可以使用表的索引。
NOT IN:强列推荐不使用,因为它不能应用表的索引。优化方案:用NOT EXISTS或(外连接+判断为空)方案代替
<>操作符(不等于):不等于操作符是永远不会用到索引的,因此对它的处理只会产生全表
扫描。
优化方案:用其它相同功能的操作运算代替,如a<>0改为 a>0 or a<0;a<>’’ 改为 a>’’.
IS NULLIS NOT NULL操作(判断字段是否为空):
判断字段是否为空一般是不会应用索引的,因为B树索引(oracle大多是使用B树索引)是不索引空值的。
优化方案:用其它相同功能的操作运算代替,如 a is not null改为 a>0a>’’等。
is null时,用一个缺省值代替空值,例如业扩申请中状态字段不允许为空,缺省为申请。
LIKELIKE操作符可以应用通配符查询,里面的通配符组合可能达到几乎是任意的查询,但是如果用得不好则会产生性能上的问题,
优化方案:LIKE‘%001%’ 这种查询不会引用索引,会产生全表扫描,
LIKE‘001%’则会引用范围索引。进行范围的查询,性能肯定大大提高。
8.备份数据
导出:
exp用户名/密码@服务名 file=d:\file name.dmp
数据导出:
1将数据库test完全导出,用户名system密码manager导出到d:\daochu.dmp
exp system/manager@test file=d:\daochu.dmp full=y
2将数据库中system用户与sys用户的表导出
exp system/manager@test file=d:\daochu.dmp owner=(system,sys)
3将数据库中的表table1table2导出
exp system/manager@test file=d:\daochu.dmp tables=(table1,table2)
4将数据库中的表table1中的字段filed1"00"打头的数据导出
exp system/manager@test file=d:\daochu.dmp tables=(table1) query=\" where filed1 like '00%'\"
导入
imp system/admin@服务名 file=d:\daochu.dmp
数据的导入
1d:\daochu.dmp中的数据导入 test数据库中。
imp userid=system/manager@test fromuser= touser= file=d:\daochu.dmp
上面可能有点问题,因为有的表已经存在,然后它就报错,对该表就不进行导入。
在后面加上 ignore=y就可以了。
2d:\daochu.dmp中的表table1导入
imp system/manager@test file=d:\daochu.dmp tables=(table1)
例如:
imp username/password@servicename file=e:\filename.dmp full=y ignore=y
exp username/password@servicename file=e:\filename.dmp full=y

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。