1、展示表结构:desc或者使用describe
describe test_collate;
2、展示表的创建语句:可以看到表的存储引擎,编码格式等信息
show create table test_collate;
3、展示数据表的详细信息:如下
select * from information_schema.columns
where table_schema = 'mydatabase' and table_name = 'test_collate';
也可以展示想要的列:如下
select column_name, column_comment, data_type
from information_schema.columns
where table_schema ='mydatabase' and table_name = 'test_collate' ;
4、展示当前表的存储引擎、编码格式
show engines; -- 查看已提供的存储引擎
show variables like '%storage_engine%'; -- 查看mysql当前默认的存储引擎
5、查看当前数据库的版本:
select version();
6、查看数据库中表的相关信息:
-- 指定表:
select * from information_schema.tables where table_schema = 'mydatabase' and table_name = 'test_collate';
-- 指定数据库:
select * from information_schema.tables where table_schema = 'mydatabase';
比如:
-- 查看指定表的自增值:
select auto_increment from information_schema.tables
where table_schema = 'mydatabase' and table_name = 'test_collate';
-- 查看指定表的字符集:
select auto_increment from information_schema.tables
where table_schema = 'mydatabase' and table_name = 'test_collate';
7、创建表时除了对表中的字段进行名称,类型,默认值,注释等的设置,还可以设置以下参数:
ENGINE 存储引擎
CHARSET 字符集
COLLATE 校对集(字符序)
COMMENT 表注释
8、上面的几个参数除了注释之外其他的可以采用默认值,其中校对集是指:字符的排序。首先字符本来是不分大小的,那么对字符的>, = , < 操作就需要有个字符排序的规则。collate做的就是这个事情,你可以对表进行字符序的设置,也可以单独对某个字段进行字符序的设置。
校对集有三种格式
_bin:binary,二进制比较,取出二进制位,一位一位的比较,区分大小写
_ca:case sensitive,大小写敏感,区分大小写
_ci:case insensitive,大小写不敏感,不区分大小写
如:
CREATE TABLE `test_collate` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(100) COLLATE utf8mb4_0900_ai_ci COMMENT '名称',
PRIMARY KEY (`id`)
)COMMENT='测试字符序表';
那么下面的查询语句会产生相同的结果:
select * from test_collate where name like '%yes%';
select * from test_collate where name like '%YEs%';
如果我们将字符序修改成 utf8mb4_bin
alter table test_collate change name name varchar(100) COMMENT '姓名' COLLATE utf8mb4_bin;
继续上面的语句,你会发现结果不一样:
9、查看当前数据库的编码:
show variables like '%character%';
10、查看当前数据库编码对应的字符序:
show variables like '%collation%';
11、查看数据库支持的所有编码集:
show character set;
12、查看指定编码集对应的校对集:
show collation where charset = 'utf8mb4';
13、修改表的默认字符集:
alter table_name default character set character_name;
-- 如:
alter test_collate default character set utf8mb4;
14、修改表字段的默认字符集:
alter table table_name change field field field_type character set character_name [other_attribute];
-- 如:
alter table test_collate change name name varchar(100) character set utf8mb4;
15、修改表的默认字符集和所有列的字符集:
alter table table_name convert to character set character_name;
-- 如:
alter table test_collate convert to character set utf8mb4;
未执行的sql请大家自行验证,此处不再一一列举。