对oracle当中子查询建表,merge操作,创建,修改,删除约束,创建使用触发器的复习练习(二)
--修改字段的长度alter table productinfo_bakmodify productName varchar2(20);--使用匿名程序块,在里面使用loop循环给表出入9条数据declarebegin for i in 1 .. 9 loop insert into productinfo (productid, productname, productprice, productaddress) values ('GD01001000'||i,'LG手机'||i,'手机价格'||i,'西安市南山区地址'||i); commit; end loop; dbms_output.put_line('总共插入了'||sql%rowcount||'条记录.');end;--使用子查询建立表productinfo_bakcreate table productinfo_bak as select * from productinfo where 1<>1;--使用merge语句给表productinfo_bak里面插入一条p.productid ='GD010010001'的记录merge into productinfo_bak p_bakusing productinfo pon (p_bak.productId = p.productId)when not matched then insert (p_bak.productid, p_bak.productname, p_bak.productprice, p_bak.productaddress) values (p.productid, p.productname, p.productprice, p.productaddress) where p.productid = 'GD010010001'; --创建触发器,行级触发器(for each row),在更新productinfo表的时候触发事件create or replace trigger tr_auto_update_productinfoafter update on productinfo for each rowbegin update productinfo_bak p_bak set p_bak.productid = :new.productid, p_bak.productname = :new.productname, p_bak.productprice = :new.productprice, p_bak.productaddress = :new.productaddress where p_bak.productid = :old.productid;/** 该where条件非常重要,意在只更新产品推荐表里有的数据**/ dbms_output.put_line('你在更新产品信息的时候,触发器自动更新了产品备份表里面的信息!');exception when others then dbms_output.put_line(sqlcode ||' ,' ||sqlerrm);end;select * from productinfo;select * from productinfo_bak;update productinfo p set p.productname = '金鹏1' where p.productid = 'GD010010001';select * from productinfo_bak;--在本例子中我犯了一个致命的错误就是在触发器的定义当中使用了事物控制语句?