创建数据库指令:
CREATE DATABASE IF NOT EXISTS teaching_manager
DEFAULT CHARACTER SET utf8mb4;
官网reference:https://dev.mysql.com/doc/refman/5.7/en/create-database.html
关键字的要点
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification]
create_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
数据库目录地址:
/usr/local/mysql-5.7.28-macos10.14-x86_64/data
create_specification 存储在teaching_manager/db.opt
MYSQL 对数据库数量无限制,系统本身可能对目录数量有限制
CREATE DATABASE
is not permitted within a session that has an active LOCK TABLES
statement. # LOCK TABLES不了解,此处理解等待更新
mysql连接数据库的url写法:
jdbc:mysql://localhost:3306
JDBC的URL=协议名+子协议名+数据源名。
a .协议名总是“jdbc”。
b .子协议名由JDBC驱动程序的编写者决定。
c .数据源名也可能包含用户与口令等信息;这些信息也可单独提供。
mysql(5.7.17)字符集设置(character_set/collation)
0 查看字符集(character_set/collation)
use information_schema;
desc tables; --一定记住tables表,information_schema中的metadata都可以查到
-- 查看db的默认,DEFAULT_CHARACTER_SET_NAME/DEFAULT_COLLATION_NAME
select * from schemata where schema_name = 'db_name';
-- 查看table的,只有table_collation字段
select * from tables where table_schema = 'db_name' and table_name = 'table_name';
-- 查看column的,CHARACTER_SET_NAME/COLLATION_NAME都要
select * from columns where table_schema = 'db_name' and table_name = 'table_name';
--查看mysql支持的character_set,Default collation是默认的"大小写敏感"
select * from information_schema.character_sets;
--就是执行了上面的sql
show charset;
/*
查看各character set的collation,以gbk为例
gbk_chinese_ci是"大小写不敏感",ci是case sensitive的缩写
gbk_bin是"大小写敏感",以二进制(binary)存储
*/
select * from information_schema.collations;
show collation;
show variables like 'character_set%'; --查看当前环境的character_set
show variables like 'collation%'; --查看当前的collation
1 my.ini配置character_set
-- 注意位置,放在[client]和[mysql]都可以,效果相同,以后面的为准。放在[mysqld]下服务不能启动。
default-character-set = utf8
-- 上句设置之后,对应的全局变量改变
character_set_client = utf8
character_set_connection = utf8
character_set_results = utf8
-- 注意位置,放在[mysqld]下
character-set-server = gbk
collation-server = gbk_bin
-- 上句设置之后,对应的全局变量改变
character_set_database = gbk
character_set_server = gbk --默认的create database db时的character_set,其他的含义待测试,可根据字面单词猜想
collation_database = gbk_bin
collation_server = gbk_bin
2 数据库的character_set指定/修改
-- 默认db的character_set和@@character_set_server一致,table和db一致,column和table一致
create database db;
-- defalut可有可无,注意gbk的default collation(上面有)是gbk_chinese_ci(大小写不敏感)
-- 即使collation_database/server都是gbk_bin,创建的db默认仍是gbk_chinese_ci,这里专门指定
create datadbase db character set gbk collate gbk_bin;
create datadbase db default character set gbk collate gbk_bin; --正确
alter datadbase db character set gbk collate gbk_bin; --正确
alter datadbase db convert to character set gbk collate gbk_bin; --错误,不支持convert to
3 table的character_set指定/修改
create table t1(c1 varchar2) character set gbk collate gbk_chinese_ci;
-- 测试
insert into t1 values('a');
select * from t1 where c1 = 'A'; --返回'a',此时'a'='A'
--注意以下两种方法的差别,第1句是只是修改默认
alter table t1 character set gbk collate gbk_bin; --提示Records: 0
--测试
alter table t1 add c2 varchar(50);
insert into t1 values('b','b');
select * from t1 where c1 = 'A'; --返回'a',c1仍然是gbk_chinese_ci
select * from t1 where c2 = 'B'; --返回empty,c2是gbk_bin
--已有字段c1,c2及再添加的默认都是gbk_bin
alter table t1 convert to character set gbk collate gbk_bin; --提示Records: 1
4 column的character_set指定/修改
-- 套路相同
alter table add c1 varchar(50) character set gbk collate gbk_bin;
alter table t1 change c1 c1 varchar(50) character set gbk collate gbk_bin;
————————————————
版权声明:本文为CSDN博主「苦笔」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u014711094/article/details/78821809