Search code examples
mysqlindexingpartialpartial-index

Workaround in MySQL for partial Index or filtered Index?


I am using a MySQL database. 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.

I couldn't find a direct way to do this in MySQL. But, is there a workaround to achieve it?


Solution

  • 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
    

    Hope it helps