现有两个表,一个表示users表,一个表是house表。每一个表都有一个user_id字段。
我想实现的功能是:
当对users表的user_id进行更新后,house表的user_id也能自动进行更新。我知道这需要写一个触发器语句只是琢磨了好久,还是没搞懂该怎么写...请大大们好好心帮我写一个,我知道这个很简单,但我写的真是无法编译。我不知道是不是我符号缺失还是什么问题
我的代码如下:
Create or replace trigger tri_users
after update of user_id
on users
for each row
begin
update housepro
set user_id=:new.user_id
where user_id=:old.user_id
end;
提示的错误是:
错误156:在关键字‘or’ 附近有语法错误。
第7行:‘:’附近有语法错误。
请大人们给予解答十分感谢~~!!

解决方案 »

  1.   

    Create trigger tri_users 
    on users
    if update(user_id)
    begin
    update house
    set user_id=i.user_id from instered i join users b on i.user_id=b.user_id
    end
      

  2.   

    看不懂你要干什么?--触发器的操作1create table 化验室纱组(本厂编号 int,客户 int,色号 int,纱支 int)
    create table 化验室布组(本厂编号 int,客户 int,色号 int,布类 int)
    go
    create trigger my_trig on 化验室纱组 for insert ,update ,delete
    as
    if not exists(select 1 from inserted)
       delete 化验室布组 from deleted t where 化验室布组.本厂编号 = t.本厂编号 
    else if not exists(select 1 from deleted) 
       insert into 化验室布组(本厂编号 ,客户 ,色号) select 本厂编号 ,客户 ,色号 from inserted
    else
       update 化验室布组 set 客户 = t.客户 , 色号 = t.色号 from inserted t where 化验室布组.本厂编号 = t.本厂编号
    go--1、insert 对化验室纱组插入数据,然后查看化验室布组表的数据
    insert into 化验室纱组 values(1 , 2 , 3 , 4)
    insert into 化验室纱组 values(5 , 6 , 7 , 8)
    go
    select * from 化验室布组
    /*
    本厂编号        客户          色号          布类          
    ----------- ----------- ----------- ----------- 
    1           2           3           NULL
    5           6           7           NULL(所影响的行数为 2 行)
    */--2、update , 更改化验室纱组表中本厂编号=1的色号=6
    update 化验室纱组 set 色号 = 6 where 本厂编号 = 1
    go
    select * from 化验室布组
    /*
    本厂编号        客户          色号          布类          
    ----------- ----------- ----------- ----------- 
    1           2           6           NULL
    5           6           7           NULL(所影响的行数为 2 行)
    */--3、delete 化验室纱组表中本厂编号=1的那条数据
    delete from 化验室纱组 where 本厂编号 = 1
    go
    select * from 化验室布组
    /*
    本厂编号        客户          色号          布类          
    ----------- ----------- ----------- ----------- 
    5           6           7           NULL(所影响的行数为 1 行)
    */drop table 化验室纱组 , 化验室布组
      

  3.   

    比如说,我users表中的 user_id 字段原本值为011,当我做完这个表的更新后,把011变为099.我house表相对应ID的house_id字段的值也变为099...
      

  4.   

    Create trigger tri_house 
    on houseif update(users)
    begin
    update house
    set user_id=i.user_id from instered i join house b on i.user_id=b.user_id
    end
      

  5.   

    你说对了我写的确实是ORACLE,有些学混了。呵呵