MySQL数据库操作常用命令小结
创建数据库
最简单的方式:
create database my_db;
或者是:
create database if not exists my_db;
创建utf8编码的数据库:
create database if not exists my_db default character set utf8 collate utf8_general_ci;
那么在这个数据库下创建的所有数据表的默认字符集都会是utf8了,注意后面这句话 "collate utf8_general_ci",大致意思是在排序时根据utf8变码格式来排序。
查看数据库列表:
show databases;
使用数据库:
use my_db;
查看数据库默认编码:
show variables like 'character_set_%'
+--------------------------+-------------------------------------------------------+
| variable_name | value |
+--------------------------+-------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.5.28-linux2.6-i686/share/charsets/ |
+--------------------------+-------------------------------------------------------+
可以看到character_set_database是lantin1.
修改数据库编码:
alter database my_db character set latin1;
删除数据库:
drop database my_db;
或者是:
drop database if exists my_db;