ORACLE数据库的导入与导出,以及触发器的用法

来源:互联网 发布:济宁网络问政平台育才 编辑:程序博客网 时间:2024/05/17 03:21

一、导出数据
1、实例:导出scott用户emp表和dept表中的数据到d盘

exp <user_name>/<password>@<db_name> table=(<table1>,<table2>...) file=<path>exp scott/a@yc tables=(emp,dept) file=D:\scott_back_table.dmp;

二、导入数据

imp userid=scott/a@yc tables=(emp) fileD:\scott_back_tables.dmp ignore=y;

注意:没有添加ignore 那么一旦出错则立即终止

imp userid=scott/a@yc tables=(dept) file=D:\scott_back_tables.dmp;

三、触发器
公式:

 create [or replace] trigger <tri_name> after|before|instand of [insert or update[of coiumn_name] or delete] on <table_name> [referncing OLD as old/NEW as new] [for each row] [when (condition)] pl/sql block

实例:

create table test1(  tid varchar2(20) primary key,  tname varchar2(100));create sequence seq_test1_tid start with 10001;create or replace trigger tri_test1_tidbefore insert on test1for each row--行级触发,执行语句每影响一行触发一次--默认是语句级触发  执行完语句后触发一次,不管这条语句会影响多少行,都只触发一次begin   select 'T'||substr(seq_test1_tid.nextval1,2)into :new.tid from dual;end;

四、修改主外键
实例1:修改dept表中的deptno,将20改为60

update dept set deptno=60 where deptno=20;

实例2:当用户修改dept表中的主键列时,用触发器自动修改emp表中的外键

create or replace trigger tri_updatebefore update on deptfor each rowbegin     update emp set daptno=:new.deptno where deptno=:old.deptno;end;

五、安全校验
实例:
当前用户修改emp表中的sal时,如果修改后的工资<3000,则自动设为3000,否则不做操作

create or replace trigger tri_emp_checkbefore update on empfor each rowwhen(new.sal<3000)begin  :new.sal:=3000;--PL/SQL两种给变量赋值的方式 select .. into ..end;update emp set sal=2000 where empno=7499;
0 0