【问题标题】:how to create check constraint in maria DB for checking char type multiple values?如何在 mariaDB 中创建检查约束以检查 char 类型的多个值?
【发布时间】:2018-08-01 04:13:19
【问题描述】:

我在 StackOverflow 等 QuestionAnswer 网站的大学项目中工作,但在 Maria DB 中创建表时遇到了一个问题

我使用最新版本的 XAMPP,默认情况下使用 maria DB 而不是 MYSQL

所以我想创建包含帖子类型 (p_type) 的帖子表,例如(问题、答案、评论),

我使用以下代码

create table post
(
 p_type char(1) check(p_type = 'q' OR p_type = 'a' OR p_type = 'c')
);

我使用 InnoDB 存储引擎,Maria DB 版本是 10.1.30
但是当我插入像 (s,z,x) 这样的其他字符时存储在数据库中 这意味着未应用检查约束,

我还访问了检查约束的 Maria DB 手册,但没有任何与 Char 类型相关的示例。

所以任何答案将不胜感激 提前致谢

【问题讨论】:

    标签: xampp mariadb mariasql mariadb-connect-engine


    【解决方案1】:

    您的语法很好,版本错误。 MariaDB 10.2 之前的版本(以及所有可用的 MySQL 版本)解析 CHECK 子句,但完全忽略它。 MariaDB 10.2 performs the actual check:

    MariaDB [test]> create table post ( p_type char(1) check(p_type = 'q' OR p_type = 'a' OR p_type = 'c') );
    Query OK, 0 rows affected (0.18 sec)
    
    MariaDB [test]> insert into post values  ('s');
    ERROR 4025 (23000): CONSTRAINT `p_type` failed for `test`.`post`
    
    MariaDB [test]> insert into post values  ('q');
    Query OK, 1 row affected (0.04 sec)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      相关资源
      最近更新 更多