尝试创建断言时出现无法解释的 mysql 错误

Unexplainable mysql error when trying to CREATE ASSERTION

我目前正在学习 mysql,我的实现是 Arch 上的 MariaDB Linux,我在下面发布了 STATUS 以防相关。

问题来了,我有这个 table:

MariaDB [biodb_sam]> SELECT * FROM pwms LIMIT 10;
+---------+-----+------+-------+
| tfs_id  | pos | base | value |
+---------+-----+------+-------+
| hPDI060 |   1 | A    |  0.01 |
| hPDI060 |   1 | C    |  0.01 |
| hPDI060 |   1 | G    |  0.97 |
| hPDI060 |   1 | T    |  0.01 |
| hPDI060 |   2 | A    |  0.01 |
| hPDI060 |   2 | C    |  0.48 |
| hPDI060 |   2 | G    |   0.5 |
| hPDI060 |   2 | T    |  0.01 |
| hPDI060 |   3 | A    | 0.625 |
| hPDI060 |   3 | C    |  0.01 |
+---------+-----+------+-------+
10 rows in set (0.00 sec)

但是当我想添加一个简单的断言以确保 pos 始终为正时,我得到了这个错误:

MariaDB [biodb_sam]> CREATE ASSERTION foo CHECK(NOT EXISTS (SELECT * FROM pwms WHERE pos < 1));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ASSERTION foo CHECK(NOT EXISTS (SELECT * FROM pwms WHERE pos < 1))' at line 1

我没有看到与 MariaDB 文档 on this webpage 上的示例有什么不同。检查中的 SELECT 语句本身工作正常,所以问题一定出在其他地方,但我没有看到它。

无论如何,这里是完整的服务器信息:

MariaDB [biodb_sam]> STATUS;
--------------
mysql  Ver 15.1 Distrib 10.1.10-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:          52
Current database:       biodb_sam
Current user:           sam@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server:                 MariaDB
Server version:         10.1.10-MariaDB-log MariaDB Server
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /run/mysqld/mysqld.sock
Uptime:                 1 day 16 hours 56 min 2 sec

Threads: 4  Questions: 6264  Slow queries: 0  Opens: 142  Flush tables: 1  Open tables: 64  Queries per second avg: 0.0
42
--------------

我想引用您链接页面顶部的内容:

This page is part of the book SQL-99 Complete, Really, by Peter Gulutzan & Trudy Pelzer. The authors have graciously allowed us to reproduce the contents of the book here. Because the book is about the SQL-99 standard, the contents of this and other pages in the book may not directly apply to MariaDB. Use the navigation bar to navigate the book.

MariaDB 特定文档不同。您可以在 mariadb 的网站上找到支持的 CREATE statements 的完整列表。此列表不包括 CREATE ASSERTION,因此 MariaDB 不支持此功能,因此出现错误消息:

关于各种 CREATE 语句的文章

  • CREATE DATABASE 创建数据库
  • 创建活动 创建和安排新活动 1
  • 创建函数 创建存储函数
  • 创建函数 UDF 创建一个 user-defined 函数
  • 创建索引 在一个或多个列上创建索引
  • 创建程序 创建存储过程
  • 创建角色 添加新角色
  • 创建服务器 定义服务器 5
  • 创建TABLE 创建一个新的 table
  • 创造TABLE空间 CREATE TABLESPACE 在 MariaDB 中不可用
  • 创建触发器 创建新触发器
  • 创建用户 创建新的 MariaDB 帐户
  • 创建视图 创建或替换视图

我建议您使用检查约束而不是这个,但这在 mysql 中不起作用。正如我所想,我有一个想法,即创建一个无符号整数作为该列的类型以避免负值。