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;
-- 还支持语句级别、会话级别