本次知识点
1,掌握 oracle中把一张表中的数据复制到另一张表中去
2,oracle中创建索引
3, oracle中临时表的创建
4,oracle中创建视图
5,oracle中删除drop truncate delete 的区别
6,oracle中创建储存过程
7,oracle中创建游标
8,oracle中创建触发器
9,oracle 中的 恢复 表数据
10,oracle 中的导入导出操作
1,
复制表
001, 如果没有表结构创建表结构
CREATE TABLE 新表 AS SELECT * FROM 旧表 WHERE 1=2;
002,向新表中复制数据
insert into 新表 select * from 旧表;
003,同一个表中 将表中的a字段的值 赋给 b字段
update 表名set A=B;
2,
索引
CREATE [UNIQUE] INDEX index_name ON table_name(column_name[,column_name
…
]);
语法解析:
1,unique 指定索引列上的值必须是唯一的 称为 唯一索引(例如 主键)
2,index_name 索引名称
3,table_name 索引作用于的表
4, colunm_name 指定对那个列 进行索引 如果是多个列 就称为 组合索引
删除索引 drop index index_name
3,
临时表
创建临时表有两种方式:
1、会话级临时表 on commit preserve rows; 当COMMIT的时候保留数据
2、事务级临时表 on commit delete rows; 当COMMIT的时候删除数据(默认情况)
1.会话级临时表(会话级临时表是指临时表中的数据只在会话生命周期之中存在)
格式:
Create Global Temporary Table Temp_Name
(
Col1 Type1,
Col2 Type2
...
)
On Commit Preserve Rows;
eg:
create global temporary table temp_tb (col1 varchar(20)) on commit preserve rows;
insert into temp_tb values('test');
select * from temp_tb;
2.事务级临时表( 事务级临时表是指临时表中的数据只在事务生命周期中存在)
格式:
Create Global Temporary Table Temp_Name
(
Col1 Type1,
Col2 Type2
...
)
On Commit Delete Rows;
例子
create global temporary table temp_tb (col1 varchar(20)) on commit delete rows;
insert into temp_tb values('test');
select * from temp_tb;
4,
视图
create or repacle view view_name [with read only]
语法解析:with read only 加上的话 意为 创建只读视图
查询:select * from v_emp1; select * from v_emp1 where ename like '%M%';
修改:update v_emp1 set job='销售' where ename='sb';
添加:insert into v_emp2 values('2222','sb2','技术');
删除:delete from v_emp2 where empno=2222
5,
删除表drop delete truncate 区别
delete:属于DML语句,删除数据库中指定条件的数据,相应语法: DELETE table WHERE a = b;
执行该语句后,需要使用commit进行提交才能反映到数据库中
truncate:(不是trancate)属于DDL语句,快速的删除指定表的所有数据,
TRUNCATE在各种表上无论是大的还是小的都非常快,同其它DDL语句一样,都显示的有提交操作,因此,执 行之后是无法进行回滚操作的。TRUNCATE将重新设置高水平线和所有的索引。在对整个表和索引进行完全浏览时,经过TRUNCATE操作后的表比Delete操作后的表要快得多。TRUNCATE不能触发任何Delete触发器。当表被清空后表和表的 索引将重新设置成初始大小,而delete则不能。
语法:TRUNCATE TABLE 表名
drop: 属于DDL语句,作用是删除整个表、删除指定的用户、删除指定的存储空间等等
6,
储存过程
Create procedure 存储过程名称 as
Begin
...
End 存储过程名称;
In 只进不出
Out 只出不进
In out 可进可出
eg:
create
or
replace
procedure
proc1(para1
varchar2
,para2 out
varchar2
,para3
in out
varchar2
)
as
v_name
varchar2(
20
);
begin
v_name :
=
'zhangsf'
; para3 :
=
v_name;dbms_output.put_line(
'para3:'
||para3);
end;
7,
游标
create or replace cursor cursor_name as
显示游标
隐式游标的属性 返回值类型 意 义
SQL%ROWCOUNT 整型 代表 DML 语句成功执行的数据行数
SQL%FOUND 布尔型 值为 TRUE 代表插入、删除、更新或单行查询操作成功
SQL%NOTFOUND 布尔型 与 SQL%FOUND 属性返回值相反
SQL%ISOPEN 布尔型 DML 执行过程中为真,结束后为假
动态游标
强类型动态游标
弱类型动态游标
eg:
set serverout on;
declare cursor cu_emp is select empno,ename,sal from emp;
e_no number;
e_name varchar2(10);
e_sal number;
begin
open cu_emp;
fetch cu_emp into e_no,e_name,e_sal;
while cu_emp%found loop
dbms_output.put_line('编号:'||e_no||',姓名:'||e_name||',基本薪资:'||e_sal);
fetch cu_emp into e_no,e_name,e_sal;
end loop;
close cu_emp;
end;
set serverout on;
declare cursor cu_emp is select empno,ename,sal from emp;
e_no emp.empno%type;
e_name emp.ename%type;
e_sal emp.sal%type;
begin
open cu_emp;
fetch cu_emp into e_no,e_name,e_sal;
while cu_emp%found loop
dbms_output.put_line('编号:'||e_no||',姓名:'||e_name||',基本薪资:'||e_sal);
fetch cu_emp into e_no,e_name,e_sal;
end loop;
close cu_emp;
end;
set serverout on;
declare cursor cu_emp is select * from emp;
e emp%rowtype;
begin
open cu_emp;
fetch cu_emp into e;
while cu_emp%found loop
dbms_output.put_line('编号:'||e.empno||',姓名:'||e.ename||',基本薪资:'||e.sal);
fetch cu_emp into e;
end loop;
close cu_emp;
end;
set serverout on;
declare cursor cu_emp is select * from emp where sal>2000 and sal<3000;
e emp%rowtype;
begin
open cu_emp;
fetch cu_emp into e;
while cu_emp%found loop
dbms_output.put_line('编号:'||e.empno||',姓名:'||e.ename||',基本薪资:'||e.sal);
fetch cu_emp into e;
end loop;
close cu_emp;
end;
begin
if sql%isopen then
dbms_output.put_line('sql游标已打开');
else
dbms_output.put_line('sql游标未打开');
end if;
end;
declare e_count number;
begin
select count(*) into e_count from emp;
dbms_output.put_line('游标捕获的记录数:'||sql%rowcount);
end;
declare e_count number;
begin
select count(*) into e_count from emp;
dbms_output.put_line('游标捕获的记录数:'||sql%rowcount);
end;
begin
update emp set ename='sb3' where empno=111;
if sql%rowcount=1 then
dbms_output.put_line('已更新');
else
dbms_output.put_line('未更新');
end if;
end;
begin
update emp set ename='sb3' where empno=111;
if sql%found then
dbms_output.put_line('已更新');
else
dbms_output.put_line('未更新');
end if;
end;
declare type emptype is ref cursor return emp%rowtype;
cu_emp emptype;
e_count number;
e emp%rowtype;
begin
select count(*) into e_count from emp where job='PRESIDENT1';
if e_count=0 then
open cu_emp for select * from emp;
else
open cu_emp for select * from emp where job='PRESIDENT';
end if;
fetch cu_emp into e;
while cu_emp%found loop
dbms_output.put_line('编号:'||e.empno||',姓名:'||e.ename||',基本薪资:'||e.sal);
fetch cu_emp into e;
end loop;
close cu_emp;
end;
declare type customType is ref cursor;
e_count number;
e emp%rowtype;
s salgrade%rowType;
cType customType;
begin
select count(*) into e_count from emp where job='PRESIDENT1';
if e_count=0 then
open cType for select * from salgrade;
fetch cType into s;
while cType%found loop
dbms_output.put_line('等级:'||s.grade||',最低薪资:'||s.losal||',最高薪资:'||s.hisal);
fetch cType into s;
end loop;
close cType;
else
open cType for select * from emp where job='PRESIDENT';
fetch cType into e;
while cType%found loop
dbms_output.put_line('编号:'||e.empno||',姓名:'||e.ename||',基本薪资:'||e.sal);
fetch cType into e;
end loop;
close cType;
end if;
end;
8,
触发器
触发器的禁用和开启
禁用触发器: alter trigger 触发器名称 disable
启用触发器: alter trigger 触发器名称 enable
行触发器针对行记录。
语法结构:
Create trigger 触发器名称
Before/after 触发动作
For each row
On 作用对象
触发器内置变量 :old :new
语句触发器针对整个表,作用整个表操作;
语法结构:
Create trigger 触发器名称
Before/after 触发动作
On 作用对象
触发器谓词:INSERTING、UPDATING、DELETING
9,
恢复数据
分为两种方法:scn和时间戳两种方法恢复。
一、通过scn恢复删除且已提交的数据
1、获得当前数据库的scn号
select current_scn from v$database; (切换到sys用户或system用户查询)
查询到的scn号为:1499223
2、查询当前scn号之前的scn
select * from 表名 as of scn 1499220; (确定删除的数据是否存在,如果存在,则恢复数据;如果不是,则继续缩小scn号)
3、恢复删除且已提交的数据
flashback table 表名 to scn 1499220;
二、通过时间恢复删除且已提交的数据
1、查询当前系统时间
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
2、查询删除数据的时间点的数据
select * from 表名 as of timestamp to_timestamp('2013-05-29 15:29:00','yyyy-mm-dd hh24:mi:ss'); (如果不是,则继续缩小范围)
3、恢复删除且已提交的数据
flashback table 表名 to timestamp to_timestamp('2013-05-29 15:29:00','yyyy-mm-dd hh24:mi:ss');
注意:如果在执行上面的语句,出现错误。可以尝试执行 alter table 表名 enable row movement; //允许更改时间戳
10,
导入导出数据
Oracle
的备份(是
Oracle
的版本只能是相邻的,不能垮版本。)
备份方案有:逻辑备份(
IMP&EXP
命令进行备份)、物理文件备份(脱机及联机备份)、利用
RMAN(Recovery Manager)
的增量物理文件系统备份。
逻辑备份分为四种模式:表空间备份
(tablespace)
、表备份
(table)
、用户备份
(user)
和完全备份
(full)
。
exp导出,imp导出命令
-- 全量导出exp system/manager@TEST file=d:\daochu.dmp full=y-- 将数据库中system用户与sys用户的表导出exp system/manager@TEST file=d:\daochu.dmp owner=(system,sys)-- 将数据库中的表table1中的字段filed1以"00"打头的数据导出exp system/manager@TEST file=d:\daochu.dmp tables=(table1) query=\" where filed1 like '00%'\"-- 将数据库中的表table1/table2导出exp system/manager@TEST file=d:\daochu.dmp tables=(table1,table2)
导入
imp system/manager@TEST file=d:\daochu.dmp tables=(table1,table2)
4,将一个用户所属的数据导入另一个用户
imp system/manager file=tank log=tank fromuser=seapark touser=seapark_copy
imp system/manager file=tank log=tank fromuser=seapark(table1,table2) touser=seapark_copy(table1,table2);
(imp mk/mk file=d:\daochu.dmp fromuser=mk touser=cyll)(导出 导入的话 要是由执行导出的用户进行导入操作)
exp help=y 导入帮助
imp help=y 导出帮助
用户 表空间
--显示 用户下面的所有表
select * from tab;
--- 查看用户下面的永久表空间
select default_tablespace from user_users;
---查看临时表空间 和用户状态
select *from user_users
--更改用户的 永久表空间
alter user user(操作的用户) default tablespace tablespacename(将要设置的默认表空间名称)
--修改 新添加用户的默认表空间
alter database default tablespace tablespaceName
--如果默认表空间不存在 则下面有创建表空间的语句
create tablespace tablepaceName datafile datafile size 500m autoextend on;
--删除 一个用户
drop user cyll cascade;
eg:
create tablespace tjsgbb DATAFILE 'tjsgbb.ora' size 500M AUTOEXTEND ON;
create user tom identified by cat default tablespace tjsgbb;
grant connect to tom;
grant resource to tom;
grant dba to tom;
oracle 用户
Oracle 用户分两种,一种是系统用户 sys system ;另外一种是普通用户;
视图 dba_users 存储着所有用户信息;
创建用户:
Create user 用户名 identified by 密码 default tablespace 表空间
授予 session 权限:grant create session to TEST;
锁定和开启帐号:alter user TEST account lock / unlock ;
修改用户密码:alter user TEST identified by 123 ;
删除用户: drop user TEST cascade ; 删除用户,并且把用户下的对象删除,比如表,视图,触发器等。
oracle 权限
Oracle 权限分为系统权限和对象权限;
系统权限是 Oracle 内置的,与具体对象无关的权限,比如创建表的权限,连接数据库权限;
对象权限就是对具体对象,比如表,视图,触发器等的操作权限;
系统权限视图:system_privilege_map
权限分配视图:dba_sys_privs
回收系统权限 revoke 权限 from 用户
对象权限分配
用户表权限视图:dba_tab_privs
给对象授权 grant 权限 on 对象 to 用户 with grant option;
回收权限:revoke 对象权限 on 对象 from 用户;