Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过向其他表中插入更改SQL表的值_Sql_Database_Create Table_Insert Statement - Fatal编程技术网

通过向其他表中插入更改SQL表的值

通过向其他表中插入更改SQL表的值,sql,database,create-table,insert-statement,Sql,Database,Create Table,Insert Statement,例如: 表:Bankaccount,以id作为主键和值 表:Category,主键为id,名称和类型(布尔值,0表示正交易,1表示负交易) 表:Transaction,主键为id,值为-10000美元(用于购物),外键为类别 我的问题是,是否可以将一些交易添加到交易表中,从而导致银行账户表中的值自动更改 我希望你们中有人能在那里帮助我 必须使用触发器:插入后触发器 类似这样的内容(语法取决于使用的dbms): 希望这对你有帮助 我想这正是我要找的,非常感谢!我只是在H2数据库上试过,不知道我做错

例如:

  • 表:
    Bankaccount
    ,以
    id
    作为主键和值
  • 表:
    Category
    ,主键为
    id
    ,名称和类型(布尔值,0表示正交易,1表示负交易)
  • 表:
    Transaction
    ,主键为
    id
    ,值为-10000美元(用于购物),外键为
    类别
  • 我的问题是,是否可以将一些交易添加到
    交易
    表中,从而导致
    银行账户
    表中的值自动更改


    我希望你们中有人能在那里帮助我

    必须使用触发器:插入后触发器 类似这样的内容(语法取决于使用的dbms):


    希望这对你有帮助

    我想这正是我要找的,非常感谢!我只是在H2数据库上试过,不知道我做错了什么,也许你也可以帮我。我的触发器如下所示:

    create trigger transaction_trig_value_ai after isert of value on transaction
    declare
    v_value double;
    v_type_category boolean;
    cursor c_value IS SELECT  value from transaction where id = :NEW.idcategory;
    cursor c_type_kategorie IS SELECT type from category where id = :NEW.idcategory;
    begin
    open c_value;
    fetch c_value into v_value;
    close c_value;
    open c_type_category;
    fetch c_type_category into v_type_category;
    close c_type_category;
    
    if v_type_category == 0 then
    update bankaccount
    set value = value +  v_value;
    end if;
    if v_type_category == 1 then
    update bankaccount
    set value = value - v_value;
    end if;
    

    您可能想看看Bankaccount中的值是否为同一id的transaction.value的总和?嗨,首先,我认为在这种情况下您不必使用游标。但是,在bankaccount表的更新语句中,这个sql命令中的“where”在哪里???
    create trigger transaction_trig_value_ai after isert of value on transaction
    declare
    v_value double;
    v_type_category boolean;
    cursor c_value IS SELECT  value from transaction where id = :NEW.idcategory;
    cursor c_type_kategorie IS SELECT type from category where id = :NEW.idcategory;
    begin
    open c_value;
    fetch c_value into v_value;
    close c_value;
    open c_type_category;
    fetch c_type_category into v_type_category;
    close c_type_category;
    
    if v_type_category == 0 then
    update bankaccount
    set value = value +  v_value;
    end if;
    if v_type_category == 1 then
    update bankaccount
    set value = value - v_value;
    end if;