Quick revision
The short answer
The _ai_ci suffix means accent-insensitive and case-insensitive, so under utf8mb4_0900_ai_ci the values jose, JOSE and the accented spelling compare as equal, and a unique index treats them as duplicates. The missing join rows come from mixed collations between tables: comparisons either fail with Illegal mix of collations or need a conversion, which can stop index use, and the 0900 collations also stop ignoring trailing spaces. The policy is to choose a collation per column by meaning (accent-sensitive or binary for usernames, ai_ci for free text), keep joined tables on one database-level collation with documented exceptions, and find conflicting data before any conversion.
What to remember
In this answer, we saw that utf8mb4_0900_ai_ci makes case and accents irrelevant, so a unique index on username treats jose, JOSE and the accented spelling as duplicates. Mixed collations between joined tables either fail with Illegal mix of collations or force conversions that can stop index use, and the NO PAD behaviour of the 0900 collations makes trailing spaces significant, which explains the lost join rows. The policy chooses collations per column by meaning, keeps joined tables on one database-level default with documented exceptions, and finds conflicting data with GROUP BY under the new collation before converting.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The _ai_ci suffix means accent-insensitive and case-insensitive, so under utf8mb4_0900_ai_ci the values jose, JOSE and the accented spelling compare as equal, and a unique index treats them as duplicates. The missing join rows come from mixed collations between tables: comparisons either fail with Illegal mix of collations or need a conversion, which can stop index use, and the 0900 collations also stop ignoring trailing spaces. The policy is to choose a collation per column by meaning (accent-sensitive or binary for usernames, ai_ci for free text), keep joined tables on one database-level collation with documented exceptions, and find conflicting data before any conversion.
Why this question is asked
Collations look like a configuration detail until a conversion silently changes the meaning of equality. Then users cannot sign up, joins lose rows, and the cause is a suffix on a collation name. Interviewers ask this scenario because it combines understanding how comparison rules work with making a policy decision that affects identity, search and data quality. In this answer, we will recap what a collation is, see why the unique index now rejects names, explain the join problems, and then set a collation policy and a safe way to apply it.
1A Quick Recap of Collations
A character set says how characters become bytes; a collation says how strings are compared and sorted. In MySQL the collation name encodes its rules. utf8mb4_0900_ai_ci means the utf8mb4 character set, comparison rules based on Unicode Collation Algorithm 9.0.0, accent-insensitive (ai) and case-insensitive (ci). Other suffixes include as for accent-sensitive, cs for case-sensitive, and bin for comparing the raw code points.
The collation applies everywhere a comparison happens: WHERE clauses, ORDER BY, GROUP BY, DISTINCT, joins, and unique indexes. That last one is the source of the first symptom.
2Why the Unique Index Rejects New Users
Under an ai_ci collation, two strings that differ only in case or accents are equal. A unique index enforces that no two equal values exist, so once JOSE exists, inserting jose is a duplicate, and so is the spelling with U+00E9 in place of the final e.
Whether this is a bug depends on the product. Treating JOSE and jose as the same username is usually right, because people should not be able to register look-alike names. Treating Jose and the accented name as the same may be wrong in a country where they are different names. The collation encodes a product decision that nobody made consciously.
3Why Some Joins Return Fewer Rows
Joins compare columns from two tables, and each column has its own collation. If one table was converted and the other was not, MySQL must decide which rules to use. When both sides have equal standing, for example two columns with different implicit collations, MySQL refuses and raises an error. When it can resolve the conflict, it converts one side, and an index on the converted column can no longer be used for the join.
| Collation | Case | Accents | Trailing spaces |
|---|---|---|---|
| utf8mb4_0900_ai_ci | ignored | ignored | significant (NO PAD) |
| utf8mb4_0900_as_ci | ignored | significant | significant (NO PAD) |
| utf8mb4_0900_as_cs | significant | significant | significant (NO PAD) |
| utf8mb4_bin | exact code points | exact code points | ignored (PAD SPACE) |
| utf8mb4_general_ci (legacy) | ignored | mostly ignored | ignored (PAD SPACE) |
The second part explains the fewer rows. Older collations such as utf8mb4_general_ci and utf8mb3_general_ci are PAD SPACE: trailing spaces are ignored in comparisons. The 0900 collations are NO PAD, so 'abc' and 'abc ' are now different. If one table stored codes with trailing spaces, perhaps from an old import, rows that used to join no longer match. Different rules for some letters between general_ci and 0900_ai_ci can have similar effects.
4Deciding a Collation Policy
The policy assigns collations by what a column means, not by habit. Identifiers that people type to log in, such as usernames and email addresses, need equality rules the product chooses on purpose. A common choice is utf8mb4_0900_as_ci: case-insensitive so JOSE and jose are one account, accent-sensitive so different names stay different. Codes and tokens that must match exactly, such as API keys, SKUs from external systems or hashes, use a binary collation such as utf8mb4_bin or utf8mb4_0900_bin. Free text that people search, such as product names and comments, stays utf8mb4_0900_ai_ci, because finding the accented spelling when typing without accents is exactly what users want.
Tables that are joined must share the collation on their join columns. So the database-level default is one collation, utf8mb4_0900_ai_ci for most companies, and every column that deviates is listed as an exception with its reason in the schema guide. Join columns between tables should normally be ids rather than strings anyway, which avoids most of the problem.
5Finding Conflicts Before Converting
Before converting a column, we run the new comparison rules over the existing data and look for values that would collide or stop matching. GROUP BY with an explicit COLLATE shows exactly which existing values the unique index would reject.
Each conflict group needs a decision before the conversion: merge accounts, rename one, or pick a stricter collation for that column. Trailing spaces are cleaned with a data fix first. Only then do we convert, and we run the join queries of the main reports on a copy before and after to compare row counts.
6Follow-up questions
Can we keep ai_ci but still allow both spellings for one column?
Yes, by giving that column a different collation, for example ALTER TABLE users MODIFY username VARCHAR(64) COLLATE utf8mb4_0900_as_ci, and rebuilding its unique index. The rest of the table can stay ai_ci.
Would storing a lowercased copy of the username work instead?
It is a common and portable pattern: keep the display name as typed and a normalised username_key column, lowercased and Unicode-normalised in the application, with a binary unique index. Then the equality rule lives in code we control and test, and behaves the same on any database.
Does the collation affect performance?
Somewhat. Binary comparison is the cheapest, and the 0900 collations are fast for collations that follow the full Unicode rules. The bigger performance risk is a collation mismatch that forces a conversion in a join and prevents index use.
7Conclusion
In this answer, we saw that utf8mb4_0900_ai_ci makes case and accents irrelevant, so a unique index on username treats jose, JOSE and the accented spelling as duplicates. Mixed collations between joined tables either fail with Illegal mix of collations or force conversions that can stop index use, and the NO PAD behaviour of the 0900 collations makes trailing spaces significant, which explains the lost join rows. The policy chooses collations per column by meaning, keeps joined tables on one database-level default with documented exceptions, and finds conflicting data with GROUP BY under the new collation before converting.
Open with
The suffix explains it: ai_ci means accent-insensitive and case-insensitive. Under utf8mb4_0900_ai_ci, jose, JOSE and the accented spelling compare as equal, and a unique index uses the same rule, so it rejects them as duplicates.
Then, if they want more
The joins are a second effect. If some tables were converted and others still use an older collation like utf8mb4_general_ci, comparing the columns either fails with Illegal mix of collations or forces a conversion that can stop the index being used.
And the 0900 collations are NO PAD, so trailing spaces now count, while the old general collations ignored them, so codes with trailing spaces stop matching and joins return fewer rows. My policy would decide collations per column by meaning.
Usernames and emails would be case-insensitive but accent-sensitive, utf8mb4_0900_as_ci, or a normalised key column with a binary collation if I want the rule in code. Exact codes and tokens would be binary. Free text that people search stays ai_ci. Tables that are joined share one collation set at database level, with exceptions documented.
Before converting anything, I would group the existing data by the new collation to find collisions, check for trailing spaces, resolve each conflict with the product team, and compare report row counts on a copy before and after.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "After converting tables to utf8mb4_0900_ai_ci, a unique index on username starts...": https://scrivio-interview-library.netlify.app/databases/mysql/ecd405154d/