我正在尝试定义下表:
CREATE TABLE IF NOT EXISTS `test` (
`gid` INT NOT NULL,
`x` INT NOT NULL,
`y` INT NOT NULL,
`z` INT NOT NULL,
`type` INT NOT NULL,
...
PRIMARY KEY ( `gid`, `x`, `y`, `z` ),
UNIQUE INDEX `type_index` ( `gid`, ( CASE WHEN `type` = 1 THEN `type` END ) ),
UNIQUE INDEX `tp_index` ( `gid`, ( CASE WHEN `type` = 2 THEN ... END ) ),
);
当 type
为 1 时,我希望表中每个 gid
只允许一个条目,而每个 gid
可以有多个条目> 对于任何其他类型。在 MySQL 中,此查询运行良好。但是,使用 MariaDB 查询根本不起作用。
MariaDB 中的结果:
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 '( CASE WHEN
type
= 1 THENtype
END ) ), UNIQUE INDEXtppad
(gid
, ( CASE' at line 1
我该如何解决这个错误?
换句话说:MariaDB 的等价物是什么?
我正在使用版本 10.2.20-MariaDB-10.2.20+maria~xenial-log
最佳答案
使用生成的列:
type_is_1 boolean generated always as (case when type = 1 then 1 end),
type_is_2 boolean generated always as (case when type = 2 then 1 end),
UNIQUE INDEX type_index ( gid, type_is_1 ),
UNIQUE INDEX tp_index ( gid, type_is_2 )
那就是MariaDB不允许表达式作为索引键。但是您可以使用生成的列轻松解决这个问题。
关于java - MariaDB 功能索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59818683/