SQL——结构化查询语言(Structured Query Language)
1. 字符集和大小写 SQL语言不区分大小写,建议关键字用大写,但是字符串常量区分大小写
字符集
1 2 3 4 5 6 7 8 9 character_set_client:服务器将系统变量character_set_client作为客户端发送语句时使用的字符集。 character_set_connection:用于没有字符集介绍器指定的字面量和用于数字到字符串转换的字符集。 character_set_database:默认数据库使用的字符集。每当默认数据库更改时,服务器都会设置此变量。 character_set_filesystem:文件系统字符集。 character_set_results:用于向客户端返回查询结果的字符集。这包括结果数据(如列值)、结果元数据(如列名)和错误消息。 character_set_server:服务器的默认字符集。如果你设置了这个变量,你还应该设置collation_server来指定字符集的排序规则。 character_set_system:服务器用于存储标识符的字符集。 character_sets_dir:安装字符集的目录。
MySQL5.7的默认字符集character和排序字符集collation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 mysql> show variables like '%character%'; +--------------------------+--------------------------------------------------------------+ | Variable_name | Value | +--------------------------+--------------------------------------------------------------+ | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | latin1 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /opt/rh/rh-mysql57/root/usr/share/rh-mysql57-mysql/charsets/ | +--------------------------+--------------------------------------------------------------+ 8 rows in set (0.00 sec) mysql> show variables like '%collation%'; +----------------------+-------------------+ | Variable_name | Value | +----------------------+-------------------+ | collation_connection | latin1_swedish_ci | | collation_database | latin1_swedish_ci | | collation_server | latin1_swedish_ci | +----------------------+-------------------+ 3 rows in set (0.00 sec)
MySQL8.0的默认字符集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 mysql> show variables like '%character%'; +--------------------------+--------------------------------+ | Variable_name | Value | +--------------------------+--------------------------------+ | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | latin1 | | character_set_server | utf8mb4 | | character_set_system | utf8mb3 | | character_sets_dir | /usr/share/mysql-8.0/charsets/ | +--------------------------+--------------------------------+ 8 rows in set (0.00 sec) mysql> show variables like '%collation%'; +-------------------------------+--------------------+ | Variable_name | Value | +-------------------------------+--------------------+ | collation_connection | latin1_swedish_ci | | collation_database | utf8mb4_0900_ai_ci | | collation_server | utf8mb4_0900_ai_ci | | default_collation_for_utf8mb4 | utf8mb4_0900_ai_ci | +-------------------------------+--------------------+ 4 rows in set (0.00 sec)
查看mysql table字段的字符集
MySQL5.7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 mysql> create table user ( id bigint unsigned auto_increment comment '主键' primary key, username varchar(128) not null comment '用户名', email varchar(128) not null comment '邮箱' ) comment '用户表' charset=utf8mb4; mysql> SELECT COLUMN_NAME, CHARACTER_SET_NAME, COLLATION_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'default' and TABLE_NAME = 'user'; +-------------+--------------------+--------------------+ | COLUMN_NAME | CHARACTER_SET_NAME | COLLATION_NAME | +-------------+--------------------+--------------------+ | id | NULL | NULL | | username | utf8mb4 | utf8mb4_general_ci | | email | utf8mb4 | utf8mb4_general_ci | +-------------+--------------------+--------------------+ 3 rows in set (0.00 sec)
或者
1 2 3 4 5 6 7 8 9 10 11 mysql > CREATE TABLE IF NOT EXISTS `t_user` (`username` varchar(64) NOT NULL,`password` varchar(11) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8; mysql > SELECT COLUMN_NAME, CHARACTER_SET_NAME, COLLATION_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'default' and TABLE_NAME = 't_user'; +-------------+--------------------+-----------------+ | COLUMN_NAME | CHARACTER_SET_NAME | COLLATION_NAME | +-------------+--------------------+-----------------+ | username | utf8 | utf8_general_ci | | password | utf8 | utf8_general_ci | +-------------+--------------------+-----------------+ 2 rows in set (0.00 sec)
MySQL8.0
1 2 3 4 5 6 7 8 9 10 11 mysql> CREATE TABLE IF NOT EXISTS `t_user123` (`username` varchar(64) NOT NULL,`password` varchar(11) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected, 1 warning (0.02 sec) mysql> SELECT COLUMN_NAME, CHARACTER_SET_NAME, COLLATION_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'test' and TABLE_NAME = 't_user123'; +-------------+--------------------+-----------------+ | COLUMN_NAME | CHARACTER_SET_NAME | COLLATION_NAME | +-------------+--------------------+-----------------+ | password | utf8mb3 | utf8_general_ci | | username | utf8mb3 | utf8_general_ci | +-------------+--------------------+-----------------+
utf8 (别名utf8mb3)utf8 是 MySQL 早期版本使用的字符集,它实际上是 UTF-8 的一种实现,只支持最多 3 字节来存储一个字符。utf8 是 utf8mb3 的别名。从 MySQL 8.0.28 版本开始,utf8 已经被明确标识为 utf8mb3,但在此之前,它们是同义的。utf8适用于大多数情况,但不能用于需要存储 4 字节 UTF-8 字符的场景(如表情符号)。
utf8mb4 是 MySQL 完整实现的 UTF-8 字符集,支持最多 4 字节来存储一个字符。支持所有 Unicode 字符,包括所有表情符号和扩展字符。
utf8mb4_general_ci 的 ci 表示 case-insensitive ,即不区分大小写 。
utf8mb4_bin 的 bin 表示 binary ,这种排序规则是区分大小写 的。
将字段类型修改成可区别大小写
1 2 CREATE TABLE your_table_name (column_name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin);
在创建表的时候指定可区别大小写
1 2 3 4 5 6 7 CREATE TABLE user ( id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键' PRIMARY KEY, username VARCHAR(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '用户名', email VARCHAR(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '邮箱' ) COMMENT '用户表' CHARSET=utf8mb4;
如果想区分case insensitive的字段,也可以使用binary函数将其转换成大小写敏感的,如下
1 2 select * from user where binary username = 'mlinnockax'
2. Database 1.列出MySQL服务器主机上的数据库 1 2 SHOW DATABASES[LIKE wild];
2. 用给定的名字创建一个数据库 语法:CREATE DATABASE[IF NO EXISTS]
全文 >>