MySQL 中的 OR 非空约束

发布于 2024-07-06 21:00:48 字数 132 浏览 4 评论 0原文

在 MySQL 中创建非 NULL 约束以使 fieldA 和 fieldB 不能同时为 NULL 的最佳方法是什么? 我不在乎其中一个字段本身是否为 NULL,只要另一个字段具有非 NULL 值即可。 如果它们都有非 NULL 值,那就更好了。

What's the best way to create a non-NULL constraint in MySQL such that fieldA and fieldB can't both be NULL. I don't care if either one is NULL by itself, just as long as the other field has a non-NULL value. And if they both have non-NULL values, then it's even better.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

可遇━不可求 2024-07-13 21:00:48

这不是直接回答您的问题,而是一些附加信息。

在处理多列并检查是否全部为空或其中一列不为空时,我通常使用 COALESCE() - 它简短、可读且易于维护(如果列表增长):

COALESCE(a, b, c, d) IS NULL -- True if all are NULL

COALESCE(a, b, c, d) IS NOT NULL -- True if any one is not null

这可以在触发器中使用。

This isn't an answer directly to your question, but some additional information.

When dealing with multiple columns and checking if all are null or one is not null, I typically use COALESCE() - it's brief, readable and easily maintainable if the list grows:

COALESCE(a, b, c, d) IS NULL -- True if all are NULL

COALESCE(a, b, c, d) IS NOT NULL -- True if any one is not null

This can be used in your trigger.

眼泪也成诗 2024-07-13 21:00:48

@Sklivvz:使用 MySQL 5.0.51a 进行测试,我发现它解析 CHECK 约束,但不强制执行它。 我可以插入(NULL,NULL)而没有错误。 测试了 MyISAM 和 InnoDB。 随后使用 SHOW CREATE TABLE 显示 CHECK 约束不在表定义中,即使我定义表时没有给出错误。

这与 MySQL 手册 相匹配,其中显示:“CHECK子句已解析,但被所有存储引擎忽略。”

因此对于 MySQL,您必须使用触发器来强制执行此规则。 唯一的问题是 MySQL 触发器无法引发错误或中止 INSERT 操作。 在触发器中可以执行的导致错误的一件事是将 NOT NULL 列设置为 NULL。

CREATE TABLE foo (
  FieldA INT,
  FieldB INT,
  FieldA_or_FieldB TINYINT NOT NULL;
);

DELIMITER //
CREATE TRIGGER FieldABNotNull BEFORE INSERT ON foo
FOR EACH ROW BEGIN
  IF (NEW.FieldA IS NULL AND NEW.FieldB IS NULL) THEN
    SET NEW.FieldA_or_FieldB = NULL;
  ELSE
    SET NEW.FieldA_or_FieldB = 1;
  END IF;
END//

INSERT INTO foo (FieldA, FieldB) VALUES (NULL, 10); -- OK
INSERT INTO foo (FieldA, FieldB) VALUES (10, NULL); -- OK
INSERT INTO foo (FieldA, FieldB) VALUES (NULL, NULL); -- gives error

在更新之前您还需要一个类似的触发器。

@Sklivvz: Testing with MySQL 5.0.51a, I find it parses a CHECK constraint, but does not enforce it. I can insert (NULL, NULL) with no error. Tested both MyISAM and InnoDB. Subsequently using SHOW CREATE TABLE shows that a CHECK constraint is not in the table definition, even though no error was given when I defined the table.

This matches the MySQL manual which says: "The CHECK clause is parsed but ignored by all storage engines."

So for MySQL, you would have to use a trigger to enforce this rule. The only problem is that MySQL triggers have no way of raising an error or aborting an INSERT operation. One thing you can do in the trigger to cause an error is to set a NOT NULL column to NULL.

CREATE TABLE foo (
  FieldA INT,
  FieldB INT,
  FieldA_or_FieldB TINYINT NOT NULL;
);

DELIMITER //
CREATE TRIGGER FieldABNotNull BEFORE INSERT ON foo
FOR EACH ROW BEGIN
  IF (NEW.FieldA IS NULL AND NEW.FieldB IS NULL) THEN
    SET NEW.FieldA_or_FieldB = NULL;
  ELSE
    SET NEW.FieldA_or_FieldB = 1;
  END IF;
END//

INSERT INTO foo (FieldA, FieldB) VALUES (NULL, 10); -- OK
INSERT INTO foo (FieldA, FieldB) VALUES (10, NULL); -- OK
INSERT INTO foo (FieldA, FieldB) VALUES (NULL, NULL); -- gives error

You also need a similar trigger BEFORE UPDATE.

岁月静好 2024-07-13 21:00:48

MySQL 5.5引入了SIGNAL,所以我们不需要额外的Bill Karwin 的回答中不再有专栏。 比尔指出您还需要一个更新触发器,因此我也将其包含在内。

CREATE TABLE foo (
  FieldA INT,
  FieldB INT
);

DELIMITER //
CREATE TRIGGER InsertFieldABNotNull BEFORE INSERT ON foo
FOR EACH ROW BEGIN
  IF (NEW.FieldA IS NULL AND NEW.FieldB IS NULL) THEN
    SIGNAL SQLSTATE '45000'
    SET MESSAGE_TEXT = '\'FieldA\' and \'FieldB\' cannot both be null';
  END IF;
END//
CREATE TRIGGER UpdateFieldABNotNull BEFORE UPDATE ON foo
FOR EACH ROW BEGIN
  IF (NEW.FieldA IS NULL AND NEW.FieldB IS NULL) THEN
    SIGNAL SQLSTATE '45000'
    SET MESSAGE_TEXT = '\'FieldA\' and \'FieldB\' cannot both be null';
  END IF;
END//
DELIMITER ;

INSERT INTO foo (FieldA, FieldB) VALUES (NULL, 10); -- OK
INSERT INTO foo (FieldA, FieldB) VALUES (10, NULL); -- OK
INSERT INTO foo (FieldA, FieldB) VALUES (NULL, NULL); -- gives error
UPDATE foo SET FieldA = NULL; -- gives error

MySQL 5.5 introduced SIGNAL, so we don't need the extra column in Bill Karwin's answer any more. Bill pointed out you also need a trigger for update so I've included that too.

CREATE TABLE foo (
  FieldA INT,
  FieldB INT
);

DELIMITER //
CREATE TRIGGER InsertFieldABNotNull BEFORE INSERT ON foo
FOR EACH ROW BEGIN
  IF (NEW.FieldA IS NULL AND NEW.FieldB IS NULL) THEN
    SIGNAL SQLSTATE '45000'
    SET MESSAGE_TEXT = '\'FieldA\' and \'FieldB\' cannot both be null';
  END IF;
END//
CREATE TRIGGER UpdateFieldABNotNull BEFORE UPDATE ON foo
FOR EACH ROW BEGIN
  IF (NEW.FieldA IS NULL AND NEW.FieldB IS NULL) THEN
    SIGNAL SQLSTATE '45000'
    SET MESSAGE_TEXT = '\'FieldA\' and \'FieldB\' cannot both be null';
  END IF;
END//
DELIMITER ;

INSERT INTO foo (FieldA, FieldB) VALUES (NULL, 10); -- OK
INSERT INTO foo (FieldA, FieldB) VALUES (10, NULL); -- OK
INSERT INTO foo (FieldA, FieldB) VALUES (NULL, NULL); -- gives error
UPDATE foo SET FieldA = NULL; -- gives error
酒儿 2024-07-13 21:00:48

这是此类约束的标准语法,但 MySQL 之后会很高兴地忽略该约束

ALTER TABLE `generic` 
ADD CONSTRAINT myConstraint 
CHECK (
  `FieldA` IS NOT NULL OR 
  `FieldB` IS NOT NULL
) 

This is the standard syntax for such a constraint, but MySQL blissfully ignores the constraint afterwards

ALTER TABLE `generic` 
ADD CONSTRAINT myConstraint 
CHECK (
  `FieldA` IS NOT NULL OR 
  `FieldB` IS NOT NULL
) 
清醇 2024-07-13 21:00:48

我在 SQL Server 中做了类似的事情,我不确定它是否可以直接在 MySQL 中工作,但是:

ALTER TABLE tableName ADD CONSTRAINT constraintName CHECK ( (fieldA IS NOT NULL) OR (fieldB IS NOT NULL) );

至少我相信这是语法。

但是,请记住,您无法跨表创建检查约束,只能检查一张表中的列。

I've done something similar in SQL Server, I'm not sure if it will work directly in MySQL, but:

ALTER TABLE tableName ADD CONSTRAINT constraintName CHECK ( (fieldA IS NOT NULL) OR (fieldB IS NOT NULL) );

At least I believe that's the syntax.

However, keep in mind that you cannot create check constraints across tables, you can only check the columns within one table.

度的依靠╰つ 2024-07-13 21:00:48

使用 GENERATED ALWAYS 列和 COALESCE ... NOT NULL: 完成了此操作

DROP TABLE IF EXISTS `error`;

CREATE TABLE IF NOT EXISTS `error` (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    left_id BIGINT UNSIGNED NULL,
    right_id BIGINT UNSIGNED NULL,
    left_or_right_id BIGINT UNSIGNED GENERATED ALWAYS AS (COALESCE(left_id, right_id)) NOT NULL,
    when_occurred TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    message_text LONGTEXT NOT NULL,
    INDEX id_index (id),
    INDEX when_occurred_index (when_occurred),
    INDEX left_id_index (left_id),
    INDEX right_id_index (right_id)
);

INSERT INTO `error` (left_id, right_id, message_text) VALUES (1, 1, 'Some random text.');  -- Ok.
INSERT INTO `error` (left_id, right_id, message_text) VALUES (null, 1, 'Some random text.'); -- Ok.
INSERT INTO `error` (left_id, right_id, message_text) VALUES (1, null, 'Some random text.'); -- Ok.
INSERT INTO `error` (left_id, right_id, message_text) VALUES (null, null, 'Some random text.'); -- ER_BAD_NULL_ERROR: Column 'left_or_right_id' cannot be null

我在 MySQL 版本 8.0.22 上

I accomplished this using a GENERATED ALWAYS column with COALESCE ... NOT NULL:

DROP TABLE IF EXISTS `error`;

CREATE TABLE IF NOT EXISTS `error` (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    left_id BIGINT UNSIGNED NULL,
    right_id BIGINT UNSIGNED NULL,
    left_or_right_id BIGINT UNSIGNED GENERATED ALWAYS AS (COALESCE(left_id, right_id)) NOT NULL,
    when_occurred TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    message_text LONGTEXT NOT NULL,
    INDEX id_index (id),
    INDEX when_occurred_index (when_occurred),
    INDEX left_id_index (left_id),
    INDEX right_id_index (right_id)
);

INSERT INTO `error` (left_id, right_id, message_text) VALUES (1, 1, 'Some random text.');  -- Ok.
INSERT INTO `error` (left_id, right_id, message_text) VALUES (null, 1, 'Some random text.'); -- Ok.
INSERT INTO `error` (left_id, right_id, message_text) VALUES (1, null, 'Some random text.'); -- Ok.
INSERT INTO `error` (left_id, right_id, message_text) VALUES (null, null, 'Some random text.'); -- ER_BAD_NULL_ERROR: Column 'left_or_right_id' cannot be null

on MySQL version 8.0.22

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文