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.

SqlData Integrity

Code

snippet
-- 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

FAQ

What are the relations in play here?
`contacts`.