sql server, mysql, postgresql都支持针对字符串类型定义排序规则的概念(collate),一般来说,排序规则分为三种:基于二进制,是否区分大小写,是否区分重音。
例如sql server中:
SELECT * FROM MyTable WHERE MyField = ‘BobDillon‘ COLLATE Latin1_General_CI_AI
oracle可以使用:
SELECT * FROM MyTable WHERE NLSSORT(MyField, ‘NLS_SORT = Latin_CI‘) = NLSSORT(‘BobDillon‘, ‘NLS_SORT = Latin_CI‘)
postgresql中:
CREATE TABLE test1 ( a text COLLATE "de_DE", b text COLLATE "es_ES", ... ); SELECT a < (‘foo‘ COLLATE "fr_FR") FROM test1;
https://www.postgresql.org/docs/13/collation.html
oracle 12.2开始支持collate的概念,使用也类似:
CREATE TABLE t1 ( id NUMBER, company VARCHAR2(15 CHAR) COLLATE BINARY_CI, CONSTRAINT t1_pk PRIMARY KEY (id) ); INSERT INTO t1 VALUES (1, ‘L?wenbrauerei‘); INSERT INTO t1 VALUES (2, ‘L?wenbrauerei‘); INSERT INTO t1 VALUES (3, ‘Lowenbrauerei‘); INSERT INTO t1 VALUES (4, ‘LOwenbrauerei‘); COMMIT; ALTER TABLE t1 ADD ( location VARCHAR2(15 CHAR) COLLATE BINARY_AI ); UPDATE t1 SET location = ‘Br?unlingen‘ WHERE id = 1; UPDATE t1 SET location = ‘Br?unlingen‘ WHERE id = 2; UPDATE t1 SET location = ‘Braunlingen‘ WHERE id = 3; UPDATE t1 SET location = ‘BrAunlingen‘ WHERE id = 4; COMMIT; SELECT * FROM t1 WHERE location LIKE ‘%?%‘; ID COMPANY LOCATION ---------- --------------- --------------- 1 L?wenbrauerei Br?unlingen 2 L?wenbrauerei Br?unlingen 3 Lowenbrauerei Braunlingen 4 LOwenbrauerei BrAunlingen -- 也支持表级别默认的COLLATE CREATE TABLE t1 ( id NUMBER, company VARCHAR2(15 CHAR), CONSTRAINT t1_pk PRIMARY KEY (id) ) DEFAULT COLLATION BINARY_CI; -- schema级别也支持默认的COLLATE CREATE USER test2 IDENTIFIED BY test2 DEFAULT TABLESPACE users QUOTA UNLIMITED ON users DEFAULT COLLATION BINARY_CI; -- 还支持语句级别、会话级别
更多可参考:
https://oracle-base.com/articles/12c/column-level-collation-and-case-insensitive-database-12cr2