Nondeterministic Collation Matches Accents
Create an ICU nondeterministic collation at primary strength so equality folds accents, and the unique index treats jose and josé as one key.
Code
-- nondeterministic collation (ICU):level1 初级强度下 equality matches accents
CREATE COLLATION ci_ai (provider = icu, locale = 'und-u-ks-level1', deterministic = false);
CREATE TABLE contacts (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text COLLATE ci_ai UNIQUE
);
INSERT INTO contacts (name) VALUES ('jose');
-- 第二次插入会命中唯一约束:level1 强度下 'jose' 与 'josé' 被视为同一个键
-- INSERT INTO contacts (name) VALUES ('josé');
SELECT 'jose' = 'josé' COLLATE ci_ai AS accent_equal;
-- 对照:默认 deterministic collation 区分重音
SELECT 'jose' = 'josé' AS default_equal;
SELECT id, name FROM contacts ORDER BY id;Inside the snippet
What runs here is SELECT / INSERT / CREATE — contacts are the objects involved.
Breaking the statements into their working parts: an ORDER BY that fixes the output order; a PRIMARY KEY that enforces row identity; UNIQUE constraints that keep duplicate values out; a GENERATED ALWAYS/GENERATED BY clause that computes the column value.
Putting it to use
Run the statement against the contacts table.
Core lines
CREATE TABLE contacts (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text COLLATE ci_ai UNIQUE
);
CREATE COLLATION ci_ai (provider = icu, locale = 'und-u-ks-level1', deterministic = false);
SELECT 'jose' = 'josé' COLLATE ci_ai AS accent_equal;
Interactive Tools for This Topic
PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.SQL FormatterReformat SQL by uppercasing reserved words and breaking lines before SELECT, FROM, WHERE, and other clauses.
FAQ
- What are the relations in play here?
- `contacts`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.