-1

我在 SQLite DB 中创建触发器时遇到问题,该触发器将值插入到 2 个不同的表中,然后将这些表中的“ID”值和插入数据中的值插入到第三个表中。所以基本的想法是......

CREATE TABLE [TBL1] (ID UNIQUE INT AUTOINCREMENT,VAL1);
CREATE TABLE [TBL2] (ID UNIQUE INT AUTOINCREMENT,VAL2);
CREATE TABLE [TBL3] (ID1 INT,ID2 INT,VAL3);
CREATE VIEW [v_TBL3] AS 
  SELECT (TBL1.VAL1,TBL2.VAL2,TBL3.VAL3) 
  FROM TBL3 
  INNER JOIN TBL1 ON TBL3.ID1 = TBL1.ID
  INNER JOIN TBL2 ON TBL3.ID2 = TBL2.ID;

========== heres the problem ==========
CREATE TRIGGER [t_TBL3_INSERT] INSTEAD OF INSERT ON v_TBL3
  BEGIN
    INSERT OR IGNORE INTO [TBL1] (VAL1) VALUES NEW.VAL1;
    INSERT OR IGNORE INTO [TBL2] (VAL2) VALUES NEW.VAL2;

    INSERT INTO [TBL3] (ID1,ID2,v_TBL3.VAL3) 
      SELECT (TBL1.ID,TBL2.ID,VAL3)
      FROM TBL1,TBL2,v_TBL3
      WHERE TBL1.VAL1 = v_TBL3.VAL1 AND TBL2.VAL2 = v_TBL3.VAL2;
  END;

我在网上四处查看,但我没有找到让我到达那里所需的东西。有人可以帮我到达那里吗?

4

1 回答 1

1

您没有提到您的问题是什么,但是当使用正确的 SQL 语法时,您的架构将如下所示:

CREATE TABLE TBL1 (ID INTEGER PRIMARY KEY AUTOINCREMENT, VAL1);
CREATE TABLE TBL2 (ID INTEGER PRIMARY KEY AUTOINCREMENT, VAL2);
CREATE TABLE TBL3 (ID1 INT, ID2 INT, VAL3);
CREATE VIEW v_TBL3 AS 
  SELECT TBL1.VAL1, TBL2.VAL2, TBL3.VAL3
  FROM TBL3 
  INNER JOIN TBL1 ON TBL3.ID1 = TBL1.ID
  INNER JOIN TBL2 ON TBL3.ID2 = TBL2.ID;

CREATE TRIGGER t_TBL3_INSERT
INSTEAD OF INSERT ON v_TBL3
BEGIN
    INSERT OR IGNORE INTO TBL1 (VAL1) VALUES (NEW.VAL1);
    INSERT OR IGNORE INTO TBL2 (VAL2) VALUES (NEW.VAL2);

    INSERT INTO TBL3 (ID1, ID2, VAL3) 
      SELECT TBL1.ID, TBL2.ID, VAL3
      FROM TBL1, TBL2, v_TBL3
      WHERE TBL1.VAL1 = v_TBL3.VAL1 AND TBL2.VAL2 = v_TBL3.VAL2;
END;
于 2013-04-12T21:47:22.130 回答