0

我有一个tag表和三个tableA, tableB,tableC表可以与三个tableA_tag,tableB_tagtableC_tag表一起标记以加入它们。

我的应用程序允许从 中删除标签tableAtableB或者tableC我只想保留其中一个中当前使用的标签。到目前为止,我已经编写了以下触发器:

CREATE TRIGGER plop after DELETE
ON tableA_tag
FOR EACH row
  DELETE FROM tag
  WHERE  tag.id = old.tag_id
         AND (SELECT Sum(countTags.c) AS s
              FROM   (SELECT Count(*) AS c
                      FROM   tableC_tag
                      WHERE  tag_id = old.tag_id
                      UNION
                      SELECT Count(*) AS c
                      FROM   tableB_tag
                      WHERE  tag_id = old.tag_id) countTags) = 0; 

/* Same trigger for table tableB_tag  */

/* And same again for table tableC_tag */

但它不起作用,因为它会检查标签是否仍在使用中tableC_tagtableB_tag不签入tableA_tag,因此即使标签仍在使用中也可以删除。而且我无法检查tableA_tag,因为它是触发触发器的同一张表。

那么,我该怎么办?

4

1 回答 1

0

希望这有效...

CREATE TRIGGER plop after DELETE

ON tableA_tag

FOR EACH row

  DELETE FROM tag

  WHERE  tag.id = old.tag_id 

    and not exists(select ' ' from tableC_tag where tableC_tag.tag_id =tag.id)
    and not exists(select ' ' from tableB_tag where tableB_tag.tag_id =tag.id)   
    and not exists(select ' ' from tableA_tag where tableA_tag.tag_id =tag.id);
于 2012-08-10T11:04:20.880 回答