MySQL-charset(字符编码)

1 创建时设置编码

1.1 创建数据库设置字符编码

mysql>CREATE DATABASE `test` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';  

1.2 创建表设置字符编码

mysql>CREATE TABLE `database_user` (  
    `ID` varchar(40) NOT NULL default '',  
    `UserID` varchar(40) NOT NULL default '',  
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

2 查看字符编码

2.1 查看数据库的编码

mysql>show create database test;

+----------+-------------------------------------------------------------------+
| Database | Create Database                                                   |
+----------+-------------------------------------------------------------------+
| test   | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)

2.2 查看表的编码

mysql>show create table user;

+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table     | Create Table                                                                                                                                                                                                                                                                                                                                                                                              |
+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| translate | CREATE TABLE `translate` (
  `_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `_username` varchar(100) CHARACTER SET utf8 NOT NULL COMMENT '用户名',
  `_language` varchar(100) CHARACTER SET utf8 NOT NULL COMMENT '语言',
  PRIMARY KEY (`_id`)
) ENGINE=InnoDB AUTO_INCREMENT=215 DEFAULT CHARSET=utf8 |
+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

3 设置编码格式

3.1 设置数据库编码格式

mysql>ALTER DATABASE `db_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

alter database atomdu default character set utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

3.2 设置表的编码格式

mysql>ALTER TABLE `tb_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

你可能感兴趣的:(MySQL)