Every database is created with a default character set and collation.
Specify the character set and collation of a database
You can execute the CREATE DATABASE statement to specify the character set and collation of a database by using the following syntax:
CREATE DATABASE database_name
[DEFAULT] CHARACTER SET [=] charset_name
[[DEFAULT] COLLATE [=] collation_name]
ALTER DATABASE database_name
[[DEFAULT] CHARACTER SET charset_name]
[[DEFAULT] COLLATE collation_name]
The CHARACTER SET and COLLATE clauses allow you to create databases of different character sets and collations on the same OBServer node.
Here are some examples:
/* Create a latin1 database */
CREATE DATABASE latin1_db CHARSET latin1;
/* Change a utf8 database to a latin1 database */
CREATE DATABASE db CHARSET=utf8mb4;
ALTER DATABASE db CHARSET latin1;
Select the character set and collation of a database
You can select the character set and collation of a database by using the following methods in OceanBase Database:
If you specify both the
CHARACTER SET charset_nameandCOLLATE collation_nameparameters, thecharset_namecharacter set andcollation_namecollation are used.If you specify only the
CHARACTER SET charset_nameparameter, the specifiedcharset_namecharacter set and the associated default collation are used. To view the default collation for each character set, execute theSHOW CHARACTER SETstatement.If you specify only the
COLLATE collation_nameparameter, the specifiedcollation_namecollation and the associated character set are used.If you do not specify the
CHARACTER SETandCOLLATEparameters, the character set and collation for the server are used.
You can specify the default character set and collation of a database by setting the character_set_database and collation_database variables. To view the default character set and collation of a specific database, execute the following statement:
USE database_name;
SELECT @@character_set_database, @@collation_database;
Or
SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME
FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'database_name';
Here is an example:
obclient> SET collation_database = 'latin1_swedish_ci';
Query OK, 0 rows affected