在mysql中针对部分索引或过滤索引的解决方法? [英] Workaround in mysql for partial Index or filtered Index?
问题描述
我正在使用mysql db.我知道postgresql和SQL Server支持部分索引.就我而言,我想做这样的事情:
I am using mysql db. I know postgresql and SQL server supports partial Indexing. In my case I want to do something like this:
CREATE UNIQUE INDEX myIndex ON myTable (myColumn) where myColumn <> 'myText'
我想创建一个唯一约束,但是如果它是特定文本,则应该允许重复.
I want to create a unique constraint but it should allow duplicates if it is a particular text.
我找不到在mysql中执行此操作的直接方法.但是,是否有解决方法可以实现?
I couldn't find a direct way to do this in mysql. But, is there a workaround to achieve it?
推荐答案
我想只有一种方法可以实现它.您可以使用以下条件在表中添加另一列,在其上创建索引并创建触发器,或者在存储过程中进行插入/更新以填充此列.
I suppose there is only one way to achieve it. You can add another column to your table, create index on it and create trigger or do insert/update inside your stored procedures to fill this column using following condition:
if value = 'myText' then put null
otherwise put value
希望有帮助
这篇关于在mysql中针对部分索引或过滤索引的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!