oracle简单复习

来源:互联网 发布:fifaol3数据库 编辑:程序博客网 时间:2024/06/08 16:55

1、Oracle查询中,可以通过括号来表示一组的条件,当然能使用PL/SQL语句块更好。

   格式大概如下:select * from table where (a,b) in (a,b);  

2、大型对象数据类包括BLOBCLOBNCLOBBFILE,可以存储文本、图像和视频等大型数据或结构化数据,最大长度为4GB

      命名规范。首字是字母,长度1-30,不能是保留字,下划线、数字、$#都行,不能有空格和单引号。  

3、客户端向服务器发出连接请求,listener监听到客户端的连接请求

      Listener监听到客户端的连接请求后,把客户端的连接请求交给数据库服务器处理

      客户端和服务器端建立连接,连接建立以后服务器端和客户端直接进行通信,而不再需要listener的参与

      oracleoradb11g_home1tnslistener    监听器服务

      oracleserviceorcl     数据库服务

4、管理表语句

create table <table_name>(

col_name1       <datatype>,  

col_namen       <datatype>);

alter table <table_name> 

[add <col_name datatype>]

  [modify <col_name datatype>]

 [drop column <column_name>];


rename old_name to new_name; //改变表的名称。

truncate table <table_name>;//truncate命令的作用是将表中存在的信息删除,只保留表的基本结构,并且被删除的数据不能被恢复。

create table <table_name> as select * from <待复制的表>;//会复制该表。

drop table<表名>;


下面是delete后,如何恢复数据,但是truncate之后就无法恢复了

create table test_tj_bakas select * from test_tj as of TIMESTAMP to_timestamp ('20140331 095510','yyyymmdd hh24miss');
delete from test_tj;--delete之后还是可以恢复的
truncate table test_tj;--truncate之后就无法恢复了
insert into test_tj select * from test_tj_bak;
 


下面是查询表顺序

select distinct <column-list>

from <table-name>

[where <condition>]

[group by<column-names>]

[having<condition>]

[order by<expression>]

order by子句

select empno,ename,sal from emp order by sal asc;

select deptno,empno,ename,sal from emp order by deptno,sal desc;

null在排序中可以理解为无穷大。


insert into <table_name> (col_name1,col_name2,......,col_namen) values(value1,value2, ……);

update <table_name>set col_name1=value/expression,col_name2=value/expression[where <condition>];

注意:update manager set(ename,sal)=(select ename,sal from emp where empno=80) where empno=100;

delete from <table_name> [where <condition>]要从表中删除满足条件的记录,WHERE条件一般不能省略,如果省略就会删除表的全部数据 。from可以省略。

 5、函数 、运算符

sysdate是返回当前服务器的系统时间。


运算符:算术运算符:+    -    *    /;连接运算符     ||     

条件运算符:            

比较运算符 :

=       等于

!=^=<>     不等于,有些平台不支持某些不等于运算符

>>=       大于,大于等于

<<=       小于,小于等于

anysome      比较单一值

all     比较所有值

select ename ||  'work as' ||  job  ||  '.'  from emp;

select ename,sal from emp where sal>any(2400,5000,1300);

select ename,sal from emp where sal>any(select sal from emp where deptno=10);

 

逻辑运算符not    and    or

select ename,deptno from emp where deptno!=10;

select ename,deptno from emp where not deptno=10;

 

成员运算符in  =any         not in  !=all

select ename,sal from emp where sal not in(2450,5000,1300);

select ename,sal from emp where sal !=all(2450,5000,1300);

 

范围运算符 between x and y   相当于>=x and <=y            not between x and y  相当于<=x or >=y

select ename,sal from emp where sal between 2000 and 3000;

select ename,sal from emp where sal>=2000 and sal<=3000;

 

null运算符 is null   null                  is not null  不是null 注意:用户可以根据需要通过函数nvl()null转换成需要的值:

select ename , nvl(comm,0) from emp;

select ename,sal+comm from emp where comm is null;

 

exist运算符 

like运算符       "_"表示一个字符;"%"表示0个或者多个字符。like   not like

select ename from emp where ename like 'A%';

如果用来匹配"s_t"  like  '%s\_t%' escape'\'        

 

集合运算符

select empno,job,sal from emp where job='MANAGER'

union all/union  前者结合两个select 语句结果为一个结果集,后者则会消除任何相同的行。

select empno,job,sal from emp where sal>2000;

 

select empno,job,sal from emp where job='MANAGER'

intersect(交操作)

select empno,job,sal from emp where sal>2000;

 

select empno,job,sal from emp where job='MANAGER'

minus(差操作)

select empno,job,sal from emp where sal>2000;

 

转换函数

select to_char(sysdate,'yyyy') from dual;

select to_date('09-10-1972','DD-MM-YYYY') from dual;

select to_number('100')from dual;

 6、约束、连接

增加约束

alter table cm add constraint cm_primary primary key(Postal_Name_First);

alter table cm disable constraint cm_primary

alter table cm enable constraint cm_primary

alter table cm drop constraint cm_primary


外连接

select e.ename,d.dname from emp e,dept d where e.deptno(+)=d.deptno;这是右外连接(就是d表显示全部。)

select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno(+);

自连接

select worker.ename Wroker,manager.ename Manager

from emp worker,emp manager

where worker.mgr=manager.empno;

子查询中不能有order by 分组语句。子查询通常在where子句中和from 后面。


 7、视图

create [or replace] [force|noforce] view <view_name>

[col_name1 alias name,col_name2 alias name…]

as sub query

[with check option constraint [constraint_name]]

[with read only]

 

create or replace view emp_dept (ID,NAME,JOB,DEPT)

as

select e.empno,e.ename,e.job,d.dname

from emp e,dept d

where e.deptno=d.deptno;

注意:虚拟表emp_dept的列名和基表中的列名是相同的,所以从安全的角度考虑,用户可以定义虚拟表的列名。

可更新视图是指可以通过视图进行insertdeleteupdate操作而更改基表。

链接视图是基于链接的视图,当用户在链接视图上使用insertupdatedelete命令的时候要特别注意。一个链接视图上的DML语句只能修改视图中的一个键保留的表(key-preserved table)     

如果加上 with check optionwith read only 那么情况:

create or replace view emp_v   这样的话就只能做delete操作,delete emp_v where ename="FORD"

as

select empno,ename,job

from emp

where job="asdfas"

with check option

 

create or replace view emp_v   这样的话什么操作都不行;

as

select empno,ename,job

from emp

where job="asdfas"

with read only

 

drop view<view_name>;

 

 8、索引

create index命令

创建索引的代价:

                      基础表维护时,系统要同时维护索引,不合理的索引将严重影响系统资源,主要表现在CPUI/O上;
  插入、更新、删除数据产生大量db file sequential read锁等待;

创建索引的目的:

加快查询速度

减少I/O操作

消除磁盘排序

查看索引select index_name from user_indexes;

 

create [unique] index <index_name> on <table_name> (col_name1, ……)

eg:

create index emp_ename_index on emp(empno);在表emp中,为员工姓名建立索引。

create index emp_sal_comm_index on emp(sal,comm);emp表中,为员工工资和奖金建立索引。

唯一索引是不允许其中任何两行具有相同索引值的索引。可以通过unigue选项创建。

eg:

create unique index emp_empno_index on emp(empno);

drop index<index_name>

 

 9、序列管理sequence

create sequence <sequence_name> keyword

eg:在表cm中,增加订单号,并且使得订单号从1开始自动增加,达到10000后自动从1开始。

create sequence cm_seq

increment by 1

start with 1

maxvalue 10000

cycle 

select cm_seq.nextval from dual;  1

select cm_seq.currval from dual;  1一定要经过Nextval初始化之后才能使用currval,否则会出错。

select cm_seq.nextval from dual;  2 

为该序列增加nocache选项。

alter sequence cm_seq nocache;

drop sequence cm_seq;

注意:cache值必须小于cycle值。不能等于。 

 

10、同义词管理

创建同义词的好处:引用对象不需要指出对象的持有者。

引用对象不需要指出它所位于的数据库。

为对象提供另一个名字。

eg:

create synonym employee for emp;

drop synonym employee; 

 

11、用户管理

create user<username> identified by<pwd>

eg:

create user sem identified by sem108;

alter user sem identified by sem;

drop user <username>[cascade];

cascade选项的作用是如果用户的模式包含对象,则在删除用户的时候一并删除其对象。 

 

12、权限管理

授予用户sem增加、更改和删除表emp的权限。

grant insert on scott.emp to sem;

grant update on scott.emp to sem;

grant delete on scott.emp to sem; 

授予sem用户查询、增加和更改表emp中的enameJob两列的权限。

create view scott.emp_view

as

select ename,job

from emp;

grant select on scott.emp_view on sem; 

grant insert(ename,job)on scott.emp to sem;

grant update(ename,job)on scott.emp to sem;

注意:不能这样写select,形式应该如上。

grant create synonym to sem;

收回权限

revoke update,delete on scott.emp from sem;

 

13、汇总:

视图不是真实存在的基础表而是一张虚表,视图所对应的数据并不实际地以视图结构存储在数据库中,而是存储在视图所引用的表中。

为了提高检索数据的能力,数据库引入了索引机制。

序列使用属性nextval取得序列的下一个值,使用属性currval返回当前值。

当创建了一个同义词后,Oracle不会去检查该对象的合法性,不论它是否存在,同义词都会被建立。

用户管理,权限管理和角色管理是数据库安全体系的重要组成部分。

数据字典是Oracle存放有关数据库信息的地方,其用途是用来描述数据库元数据的。


14、触发器是一种特殊类型的存储过程。触发器主要是通过事件进行触发而被执行的,而存储过程可以通过存储过程名字而被直接调用。

触发器的主要作用是能够实现由主键和外键所不能保证的复杂的参照完整性和数据的一致性。

 

15、事物处理概述:

保持应用程序的完整性是事务管理的核心目的。

事务的概念:

事务是由相关操作构成的一个完整的操作单元。两次连续成功的COMMITROLLBACK之间的操作,称为一个事务。在一个事务内,数据的修改一起提交或撤销,如果发生故障或系统错误,整个事务也会自动撤销。

 

事务属性

1、原子性 2、一致性

3、隔离性 4、持久性

 

事务提交

显式提交。用commit命令直接完成的提交为显式提交。

隐式提交。在执行时自动的强制提交操作,在Oracle中,不需要提交命令的操作有以下几种:create alterdropgrantrevokeconnectexitquit

自动提交。SQL*Plus具有自动提交用户工作的功能,而不需要额外处理。这是由用户自行设置autocommit属性所控制的。

 

保存点

保存点在事物中确定一个点,他可以在之后用rollback语句回滚到保存点处。

 

日志

archivelog模式(可恢复)

noarchivelog模式(有限的恢复)

 

并发控制

数据库不一致的类型包括四种类型:不一致性、不可重复读、读脏数据和丢失更改。 

如果两个事务,分别锁定一部分数据,而都在等待对方释放锁才能完成事务操作,这种情况下就会发生死锁。

 

锁的概念

锁出现在数据共享的场合,用来保证数据的一致性。当多个会话同时修改一个表时,需要对数据进行相应的锁定。

锁有“只读锁”、“排它锁”,“共享排它锁”等多种类型,而且每种类型又有“行级锁”(一次锁住一条记录),“页级锁”(一次锁住一页),“表级锁”(锁住整个表) 

 

恢复与备份

Oracle数据库备份分为物理备份和逻辑备份。物理备份是数据库文件拷贝的备份,根据备份时数据所处状态的不同,物理备份又可分为冷备份与热备份。导出/导入(export/import)工具用于进行逻辑备份。

冷备份(也称作离线备份)是在数据库被正常关闭之后进行的数据文件的物理备份。

热备份(也称作在线备份)是在数据库运行于archivelog(归档)模式,在打开的情况下做数据的物理备份,可以恢复到任一时间点。

 

提交数据有三种类型:显式提交、隐式提交和自动提交。日志机制确保原子性,Oraclet具有重做日志机制。隔离性通过锁机制实现,加锁是实现数据库并发控制的一个非常重要的技术。

Oracle数据库有三种标准的备份:导出/导入(export/import)、冷备份和热备份。


 

 

 


0 0