你可以搭起見狀要麼(A,B)
或(B,A)
失敗觸發:
這裏是觸發:
DELIMITER $$
CREATE TRIGGER edges_bi BEFORE INSERT
ON edges FOR EACH ROW
BEGIN
DECLARE found_count,dummy,diff,SomethingsWrong INT DEFAULT 0;
DECLARE errmsg VARCHAR(128);
SET diff = new.a - new.b;
IF diff = 0 THEN
SET errmsg = CONCAT('[',new.a,',',new.b,'] is Vertex, Not Edge');
SET SomethingsWrong = 1;
END IF;
SELECT COUNT(1) INTO found_count FROM edges
WHERE (a=NEW.a AND b=NEW.b) OR (a=NEW.b AND b=NEW.a);
IF found_count = 1 THEN
SET errmsg = CONCAT('[',new.a,',',new.b,'] Already Exists');
SET SomethingsWrong = 1;
END IF;
IF SomethingsWrong = 1 THEN
SELECT errmsg INTO dummy FROM edges WHERE 1=1;
END IF;
END; $$
DELIMITER ;
下面是一個示例表:
DROP DATABASE if exists saurabh;
CREATE DATABASE saurabh;
USE saurabh
CREATE TABLE edges
(
a INTEGER NOT NULL,
b INTEGER NOT NULL,
PRIMARY KEY (a,b),
UNIQUE KEY (b,a)
);
注意我有一個PRIMARY KEY和一個UNIQUE KEY,PRIMARY KEY的列反轉
讓我們創建表:
mysql> DROP DATABASE if exists saurabh;
Query OK, 1 row affected (0.01 sec)
mysql> CREATE DATABASE saurabh;
Query OK, 1 row affected (0.00 sec)
mysql> USE saurabh
Database changed
mysql> CREATE TABLE edges
-> (
-> a INTEGER NOT NULL,
-> b INTEGER NOT NULL,
-> PRIMARY KEY (a,b),
-> UNIQUE KEY (b,a)
->);
Query OK, 0 rows affected (0.12 sec)
mysql>
讓我們創建觸發器:
mysql> DELIMITER $$
mysql> CREATE TRIGGER edges_bi BEFORE INSERT
-> ON edges FOR EACH ROW
-> BEGIN
-> DECLARE found_count,dummy,diff,SomethingsWrong INT DEFAULT 0;
-> DECLARE errmsg VARCHAR(128);
-> SET diff = new.a - new.b;
-> IF diff = 0 THEN
-> SET errmsg = CONCAT('[',new.a,',',new.b,'] is Vertex, Not Edge');
-> SET SomethingsWrong = 1;
-> END IF;
-> SELECT COUNT(1) INTO found_count FROM edges
-> WHERE (a=NEW.a AND b=NEW.b) OR (a=NEW.b AND b=NEW.a);
-> IF found_count = 1 THEN
-> SET errmsg = CONCAT('[',new.a,',',new.b,'] Already Exists');
-> SET SomethingsWrong = 1;
-> END IF;
-> IF SomethingsWrong = 1 THEN
-> SELECT errmsg INTO dummy FROM edges WHERE 1=1;
-> END IF;
-> END; $$
Query OK, 0 rows affected (0.11 sec)
mysql> DELIMITER ;
下面是一些樣本數據:
INSERT INTO edges (a,b) VALUES (5,3);
INSERT INTO edges (a,b) VALUES (3,3);
INSERT INTO edges (a,b) VALUES (3,5);
INSERT INTO edges (a,b) VALUES (5,5);
SELECT * FROM edges;
讓我們嘗試加載這些進入edges
表:
mysql> INSERT INTO edges (a,b) VALUES (5,3);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO edges (a,b) VALUES (3,3);
ERROR 1366 (HY000): Incorrect integer value: '[3,3] is Vertex, Not Edge' for column 'dummy' at row 1
mysql> INSERT INTO edges (a,b) VALUES (3,5);
ERROR 1366 (HY000): Incorrect integer value: '[3,5] Already Exists' for column 'dummy' at row 1
mysql> INSERT INTO edges (a,b) VALUES (5,5);
ERROR 1366 (HY000): Incorrect integer value: '[5,5] is Vertex, Not Edge' for column 'dummy' at row 1
mysql> SELECT * FROM edges;
+---+---+
| a | b |
+---+---+
| 5 | 3 |
+---+---+
1 row in set (0.00 sec)
注意,阻擋A = B條件防止任何自循環
CAVEAT
如果
- 你開始與一個空表這個觸發不起作用
- 進入
(3,3)
作爲第一行
因爲BEFORE INSERT
觸發器不在空表上觸發。
一旦您輸入有效行A
<>B
然後所有檢查都正確執行。
試試吧!
只是澄清 - 這裏的目標是防止(1,2)_和_(2,1)都出現在表格中 - 是嗎? – 2013-03-22 13:12:53
是的,這是正確的。也不應該有任何自我循環。 – 2013-03-22 13:16:55