5
   CREATE TABLE nodes (
        id INTEGER PRIMARY KEY,
        name VARCHAR(10) NOT NULL,
        feat1 CHAR(1), -- e.g., age
        feat2 CHAR(1)  -- e.g., school attended or company
   );

   CREATE TABLE edges (
        a INTEGER NOT NULL REFERENCES nodes(id) ON UPDATE CASCADE ON DELETE CASCADE,
        b INTEGER NOT NULL REFERENCES nodes(id) ON UPDATE CASCADE ON DELETE CASCADE,
        PRIMARY KEY (a, b)
   );

   CREATE INDEX a_idx ON edges (a);
   CREATE INDEX b_idx ON edges (b);

如果我们想表示一个无向图,我们需要对对的唯一性添加一个 CHECK 约束。

由于 SQL 标准不允许在 CHECK 约束中使用子查询,我如何检查该对的唯一性?

4

4 回答 4

7

您可以安装一个触发器,该触发器在看到(A,B)or时失败(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 条件可防止任何自循环

警告

此触发器不起作用,如果

  • 你从一张空桌子开始
  • (3,3)作为第一行输入

因为BEFORE INSERT触发器不会在空表上触发。

使用A<>输入有效行B后,所有检查都会正确执行。

试试看 !!!

于 2013-03-25T21:53:52.487 回答
3

MySQL 不支持 CHECK 约束。

您可以创建 BEFORE INSERT 和 BEFORE UPDATE 触发器来检查这种情况,并在需要时抛出错误。

例子:

CREATE TABLE edges(
  a INT(11) NOT NULL,
  b INT(11) NOT NULL
);

DELIMITER $$

CREATE TRIGGER trigger1
BEFORE INSERT
ON edges
FOR EACH ROW
BEGIN
  SET @cnt = NULL;

  SELECT COUNT(*) INTO @cnt FROM edges
    WHERE a = new.a AND b = new.b OR a = new.b AND b = new.a;

  IF @cnt > 0 THEN
    SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'Error: uniqueness of pair';
  END IF;
END
$$

DELIMITER ;

此外,创建类似的 BEFORE UPDATE 触发器以避免更新时出现新的错误值,或者只使用存储过程,因为代码是相同的。

于 2013-03-22T13:16:41.787 回答
2

CHECKCREATE TABLEMySQL不支持,如文档所述

CHECK 子句被所有存储引擎解析但忽略

事实上,自 2004 年以来就有一个关于该问题的开放错误报告(!)。

我将采取的方法是在插入和更新时创建一个存储过程触发器,如果​​该对存在,则故意失败。

于 2013-03-25T13:59:54.183 回答
0

我认为答案可能取决于您如何填充edges表格,这在您的问题中并不清楚。如果它是从nodes表中填充的,那么您可以基于排除镜像对(即 1,2 和 2,1)的 SELECT 查询构建一个 VIEW。这也可能解决级联和删除要求。

于 2013-03-28T16:00:01.473 回答