I have a table that has 3 columns:
id(int 11) | user_id(int 4) | title(varchar 512)
____________|________________|___________________
1 | 3 | Thing X
2 | 3 | Something Else
3 | 5 | Thing X
And i need to make a unique combination between user_id and title. For that i'm using this simple query:
ALTER TABLE posts ADD UNIQUE `unique_post`(`user_id`, `title`)
The charset for the title column is utf8mb4_unicode_ci. The database was created using the charset utf8mb4:
CREATE DATABASE learning_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
And the table type is InnoDB.
I've tried to set a few globals:
SET GLOBAL innodb_file_format=Barracuda;
SET GLOBAL innodb_file_per_table=on;
SET GLOBAL innodb_large_prefix=on;
But i'm getting the same error 1071:
Specified key was too long; max key length is 767 bytes
Is there anything else that i might be able to do?
P.S: if i set to index only the first 191 characters of the title i don't get that error:
ALTER TABLE posts ADD UNIQUE `unique_post`(`user_id`, `title`(191))
...but i have titles related to the same user that might be different only at the very last letter(aka last 4 bytes considering my charset)
P.S.S: i'm on localhost (using xampp).
value = hash(title), where 'hash' is any built-in chechsum function (https://dev.mysql.com/doc/refman/8.0/en/encryption-functions.html). Then create unique index by user_id and this calculated field. Of course, collision is possible, but its probability is too low (dependent by hash length). – Akina Mar 04 '19 at 11:55