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

一、导出数据
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_tid
before insert on test1
for 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_update
before update on dept
for each row
begin 
    update emp set daptno=:new.deptno where deptno=:old.deptno;
end;

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

create or replace trigger tri_emp_check
before update on emp
for each row
when(new.sal<3000)
begin
  :new.sal:=3000;--PL/SQL两种给变量赋值的方式 select .. into ..
end;
update emp set sal=2000 where empno=7499;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值