您的位置:首页 > 数据库 > Oracle

对oracle当中子查询建表,merge操作,创建,修改,删除约束,创建使用触发器的复习练习

2012-04-29 19:51 861 查看
Sql代码

/** 对oracle当中子查询建表,merge操作,创建,修改,删除约束,创建使用触发器的复习练习**/  
/**    
本例子的作用是熟悉使用oracle当中的触发器:有两张表  productinfo:产品表,productinfo_info:产品推荐表。  
其中产品推荐表里面只有部分产品表里面的信息(当然不是所有的产品都推荐),这两张表的字段结构完全一样。  
要实现:当管理者修改产品表里面的某个纪录的时候,如果该记录在产品推荐表里面也存在(根据productIdid判断),  
则触发器自动修改产品推荐表里面的这条记录以达到跟产品表里面的记录保持一致的效果。  
**/  
  
--新建了一个产品信息表  
create table productinfo(  
productId varchar2(20) unique,  
productName varchar2(10) not null,  
productPrice varchar2(10) primary key,  
productAddress varchar2(10) );  
  
--练习使用sql修改约束和字段大小  
alter table productinfo modify   
productAddress varchar2(20) not null;  
  
--练习使用sql删除,修改,新增表里面的约束  
alter table productinfo  
--drop constraint sys_c009964;(删除的是productPrice为主键的约束)  
--modify productPrice  constraint product_price_not  not null;(给productPrice增加非空的约束)  
--add constraint productin_address_check check(length(productAddress)>5);(给地址增加check约束)  
add constraint productinfo_pk  primary key(productId);--增加productid为主键的约束  
  
--查看所有的约束名字,约束的状态(是否启用) ,约束的类型,约束是建立在哪个列上面的  
select c.constraint_name, c.status, c.constraint_type,n.COLUMN_NAME  
from user_constr  select * from productinfo_bak ;  
aints c, user_cons_columns n  
where c.CONSTRAINT_NAME = n.CONSTRAINT_NAME and c.TABLE_NAME= 'PRODUCTINFO';  

 

Sql代码

--修改字段的长度  
alter table productinfo_bak  
modify productName varchar2(20);  
  
--使用匿名程序块,在里面使用loop循环给表出入9条数据  
declare  
begin  
  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_bak  
create table productinfo_bak as  select * from productinfo  where 1<>1;  
  
--使用merge语句给表productinfo_bak里面插入一条p.productid ='GD010010001'的记录  
merge into productinfo_bak p_bak  
using productinfo p  
on (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_productinfo  
after update on productinfo for each row  
begin  
  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.productaddr
a624
ess  
  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;  
--在本例子中我犯了一个致命的错误就是在触发器的定义当中使用了事物控制语句  

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐