sql >> Databasteknik >  >> RDS >> Mysql

Lösning i mysql för partiellt index eller filtrerat index?

Filtrerade index skulle kunna emuleras med funktionsindex och CASE expression(MySQL 8.0.13 och nyare):

CREATE TABLE t(id INT PRIMARY KEY, myColumn VARCHAR(100));

-- NULL are not taken into account with `UNIQUE` indexes   
CREATE UNIQUE INDEX myIndex ON t((CASE WHEN myColumn <> 'myText' THEN myColumn END));


-- inserting excluded value twice
INSERT INTO t(id, myColumn) VALUES(1, 'myText'), (2, 'myText');

-- trying to insert different value than excluded twice
INSERT INTO t(id, myColumn) VALUES(3, 'aaaaa');

INSERT INTO t(id, myColumn) VALUES(4, 'aaaaa');
-- Duplicate entry 'aaaaa' for key 'myIndex'

SELECT * FROM t;

db<>fioldemo

Utdata:

+-----+----------+
| id  | myColumn |
+-----+----------+
|  1  | myText   |
|  2  | myText   |
|  3  | aaaaa    |
+-----+----------+


  1. Visa alla tabeller. Beskriv-liknande funktionalitet

  2. java.sql.SQLException:Okänd systemvariabel 'query_cache_size'

  3. Star Schema vs Snowflake Schema

  4. QMYSQL-drivrutinsladdningsfel