【问题标题】:How to refer to transition tables如何参考转换表
【发布时间】:2012-11-05 17:30:32
【问题描述】:

我有这两张表:

create table possiede (
soc1 integer not null,
soc2 integer not null,
primary key (soc1,soc2),
perc double
);

create table contr (
soc1 integer not null,
soc2 integer not null,
primary key(soc1, soc2)
);

我在通用 SQL 语法中有这两个触发器,我需要将它们翻译成 MySQL 语法:

create trigger contrDir
after insert on possiede
for each row
when percent > 0.5 and (soc1,soc2) not in (select * from contr)
insert into contr values (soc1,soc2);

create trigger contrIndir
after insert on possiede
referencing new table as newTable
for each row
insert into possiede values
(select P.soc1, N.soc2, P.perc+N.perc
from newTable as N join possiede as P on N.soc1 = P.soc2);

这是我的第一次尝试,但它给了我“引用”关键字的错误(“语法错误,意外 IDENT_QUOTED,期待 FOR_SYM”),我不确定翻译是否正确:

create trigger controllo
after insert on possiede
REFERENCING new table as newTable
for each row
begin
    insert into possiede (select P.soc1, N.soc2, P.perc+N.perc from
    newTable as N join possiede as P on N.soc1=P.soc2);
    if percent > 0.5 and (soc1,soc2) not in (select * from contr) then
    insert into contr values (soc1,soc2);
    end if;
end;

正如您所注意到的,由于 MySQL 的一些限制,我不得不将两个触发器压缩为一个。谁能给我正确的翻译?

【问题讨论】:

    标签: mysql sql triggers


    【解决方案1】:

    请将列名放在大括号内并使用NEW_TABLE。此外,我认为IN CLAUSE within IF BLOCK 是不正确的,因为您正在对照select * from... 检查两列(soc1 和 soc2)。请尝试使用更新后的查询,如下所示:

      CREATE TRIGGER controllo
      AFTER INSERT on possiede
      REFERENCING NEW_TABLE AS newTable
      FOR EACH ROW
       BEGIN
          INSERT INTO possiede (soc1, soc2, perc) 
          SELECT P.soc1, N.soc2, P.perc+N.perc 
          FROM newTable AS N JOIN possiede AS P ON N.soc1=P.soc2;
          IF percent > 0.5 and soc1 not in (select soc1 from contr)
              and soc2 not in (select soc2 from contr)
            THEN
              INSERT INTO contr VALUES (soc1,soc2);
          END IF;
        END;
    

    【讨论】:

    • 我听不懂你的回答。我不需要放入 mi 表(soc1、soc2、perc),而是选择查询返回的内容。我很确定这一点是正确的..
    • @user1221297 放置大括号时,它需要列名。请删除您的选择查询周围的大括号。
    • 不正确!您消除了第一个插入中的连接,我需要它来构建一种递归!
    • 另外,我在关键字“引用”上仍然有同样的错误..似乎我不能在那个地方使用它..
    • @user1221297:我不知道。现在检查答案,如果有帮助,请告诉我。
    猜你喜欢
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多