当前位置:Gxlcms > 数据库问题 > mysql字符集那些事

mysql字符集那些事

时间:2021-07-01 10:21:17 帮助过:21人阅读

1.、查看mysql当前使用的字符集。

登录mysql 在mysql 里输入 show variables like ‘character_set%‘

  1. mysql> show variables like ‘character_set%‘;
  2. +--------------------------+-------------------------------------------+
  3. | Variable_name | Value |
  4. +--------------------------+-------------------------------------------+
  5. | character_set_client | latin1 |
  6. | character_set_connection | latin1 |
  7. | character_set_database | utf8 |
  8. | character_set_filesystem | binary |
  9. | character_set_results | latin1 |
  10. | character_set_server | utf8 |
  11. | character_set_system | utf8 |
  12. | character_sets_dir | /application/mysql-5.5.32/share/charsets/ |
  13. +--------------------------+-------------------------------------------+
  14. 8 rows in set (0.00 sec)

说明:show variables 是查看mysql的变量 like 是模糊查询  后面的%号是指任意字符 相当于linux里的*,以上语句表示查看mysql 里的以character_set开头变量的值。

从上面的表中我们可以看到mysql 客户端是用的latin1 mysql连接也是latin1 还有mysql返回也是latin1。接下来我们看下我们要测试的库和表的字符集。

  1. mysql> show create database qiuhom;
  2. +----------+-----------------------------------------------------------------+
  3. | Database | Create Database |
  4. +----------+-----------------------------------------------------------------+
  5. | qiuhom | CREATE DATABASE `qiuhom` /*!40100 DEFAULT CHARACTER SET utf8 */ |
  6. +----------+-----------------------------------------------------------------+
  7. 1 row in set (0.00 sec)

说明:以上语句可以看出我们qiuhom库是用的utf8字符集

  1. mysql> use qiuhom
  2. Database changed
  3. mysql> show tables;
  4. +------------------+
  5. | Tables_in_qiuhom |
  6. +------------------+
  7. | test |
  8. +------------------+
  9. 1 row in set (0.00 sec)
  10. mysql> show create table test;
  11. +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
  12. | Table | Create Table |
  13. +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
  14. | test | CREATE TABLE `test` (
  15. `id` int(4) NOT NULL AUTO_INCREMENT,
  16. `name` char(8) DEFAULT NULL,
  17. PRIMARY KEY (`id`)
  18. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
  19. +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
  20. 1 row in set (0.00 sec)

说明:以上语句可以看出我们test表用的是utf8字符集。

现在我们往test里面插入数据看看是什么情况呢

  1. mysql> insert into test values(1,‘a1‘),(2,‘a2‘);
  2. Query OK, 2 rows affected (0.01 sec)
  3. Records: 2 Duplicates: 0 Warnings: 0
  4. mysql> select * from test;
  5. +----+------+
  6. | id | name |
  7. +----+------+
  8. | 1 | a1 |
  9. | 2 | a2 |
  10. +----+------+
  11. 2 rows in set (0.00 sec)

  可以看出 当我们用latin1的字符集往utf8的库表插入非中文字符数可以正常显示的。

那我们现在来插入中文字符来看看会是什么结果呢

  1. mysql> insert into test values(3,"小明"),(4,"小红");
  2. Query OK, 2 rows affected (0.01 sec)
  3. Records: 2 Duplicates: 0 Warnings: 0
  4. mysql> select * from test;
  5. +----+--------+
  6. | id | name |
  7. +----+--------+
  8. | 1 | a1 |
  9. | 2 | a2 |
  10. | 3 | 小明 |
  11. | 4 | 小红 |
  12. +----+--------+
  13. 4 rows in set (0.00 sec)

  恩?怎么中文也可以显示呢,我们这样退出mysql重新进入查看数呢?

  1. mysql> select * from test;
  2. +----+--------+
  3. | id | name |
  4. +----+--------+
  5. | 1 | a1 |
  6. | 2 | a2 |
  7. | 3 | ?°???? |
  8. | 4 | ?°??o¢ |
  9. +----+--------+
  10. 4 rows in set (0.00 sec)  

为什么我们退出了mysql重新登录后就乱码了呢?

我们在来看看系统的字符集

  1. mysql> show variables like ‘character_set%‘;
  2. +--------------------------+-------------------------------------------+
  3. | Variable_name | Value |
  4. +--------------------------+-------------------------------------------+
  5. | character_set_client | utf8 |
  6. | character_set_connection | utf8 |
  7. | character_set_database | utf8 |
  8. | character_set_filesystem | binary |
  9. | character_set_results | utf8 |
  10. | character_set_server | utf8 |
  11. | character_set_system | utf8 |
  12. | character_sets_dir | /application/mysql-5.5.32/share/charsets/ |
  13. +--------------------------+-------------------------------------------+
  14. 8 rows in set (0.00 sec)

  可以看出现在系统的字符集变成了utf8,以上例子说明了一点就是,当库表的字符集和你当前数据库的字符集不同时 我们插入数据(特别是中文)时一定要调整字符集,一般是调整当前字符集和库表一致,否则会导致乱码。

2、说说设置字符集的方法

2.1临时设置方法 set names +字符集

  1. mysql> show variables like ‘character_set%‘;
  2. +--------------------------+-------------------------------------------+
  3. | Variable_name | Value |
  4. +--------------------------+-------------------------------------------+
  5. | character_set_client | utf8 |
  6. | character_set_connection | utf8 |
  7. | character_set_database | utf8 |
  8. | character_set_filesystem | binary |
  9. | character_set_results | utf8 |
  10. | character_set_server | utf8 |
  11. | character_set_system | utf8 |
  12. | character_sets_dir | /application/mysql-5.5.32/share/charsets/ |
  13. +--------------------------+-------------------------------------------+
  14. 8 rows in set (0.00 sec)
  15. mysql> set names latin1;
  16. Query OK, 0 rows affected (0.00 sec)
  17. mysql> show variables like ‘character_set%‘;
  18. +--------------------------+-------------------------------------------+
  19. | Variable_name | Value |
  20. +--------------------------+-------------------------------------------+
  21. | character_set_client | latin1 |
  22. | character_set_connection | latin1 |
  23. | character_set_database | utf8 |
  24. | character_set_filesystem | binary |
  25. | character_set_results | latin1 |
  26. | character_set_server | utf8 |
  27. | character_set_system | utf8 |
  28. | character_sets_dir | /application/mysql-5.5.32/share/charsets/ |
  29. +--------------------------+-------------------------------------------+
  30. 8 rows in set (0.00 sec)

  set names 是临时生效的方式设置,当我们退出mysql后重新登录字符集又会变成设置前的字符集。当然临时设置的方式有很多,但是不管以怎么的方式 最后都是以执行set names +字符集生效的

比如 我们可以在导入数据的时候指定字符集 mysql -uroot -padmin --default-character-set=utf8 < /work/backup.sql

我们首先我们用mysqldump把qiuhom库给逻辑备份出来,并指定字符集

  1. [root@qiuhom ~]# mysqldump -uroot -padmin --default-character-set=latin1 -B qiuhom > /work/backup.sql 

  如果执行以上命令没有报错就表示备份成功,简单说下mysqldump命令吧,它作用就是把mysql里面的数据以sql语句的形式给备份出来,备份出来的不是实际的数据文件,而是一堆sql语句,其中-B的作用是指定要备份的数据库,可以同时备份多个库。

以上语句我们指定以latin1的字符集导出数据是防止我们到处的数据是乱码。我们可以vim 查看导出的SQL语句

  1. [root@qiuhom ~]# vim /work/backup.sql
  2. 1 -- MySQL dump 10.13 Distrib 5.5.32, for Linux (x86_64)
  3. 2 --
  4. 3 -- Host: localhost Database: qiuhom
  5. 4 -- ------------------------------------------------------
  6. 5 -- Server version 5.5.32-log
  7. 6
  8. 7 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
  9. 8 /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
  10. 9 /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
  11. 10 /*!40101 SET NAMES latin1 */;
  12. 11 /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
  13. 12 /*!40103 SET TIME_ZONE=‘+00:00‘ */;
  14. 13 /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
  15. 14 /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
  16. 15 /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO‘ */;
  17. 16 /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
  18. 17
  19. 18 --
  20. 19 -- Current Database: `qiuhom`
  21. 20 --
  22. 21
  23. 23
  24. 24 USE `qiuhom`;
  25. 25
  26. 26 --
  27. 27 -- Table structure for table `test`
  28. 28 --
  29. 29
  30. 30 DROP TABLE IF EXISTS `test`;
  31. 31 /*!40101 SET @saved_cs_client = @@character_set_client */;
  32. 32 /*!40101 SET character_set_client = utf8 */;
  33. 33 CREATE TABLE `test` (
  34. 34 `id` int(4) NOT NULL AUTO_INCREMENT,
  35. 35 `name` char(8) DEFAULT NULL,
  36. 36 PRIMARY KEY (`id`)
  37. 37 ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
  38. 38 /*!40101 SET character_set_client = @saved_cs_client */;
  39. 39
  40. 40 --
  41. 41 -- Dumping data for table `test`
  42. 42 --
  43. 43
  44. 44 LOCK TABLES `test` WRITE;
  45. 45 /*!40000 ALTER TABLE `test` DISABLE KEYS */;
  46. 46 INSERT INTO `test` VALUES (1,‘a1‘),(2,‘a2‘),(3,‘小明‘),(4,‘小红‘);
  47. 47 /*!40000 ALTER TABLE `test` ENABLE KEYS */;
  48. 48 UNLOCK TABLES;
  49. 49 /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
  50. 50
  51. 51 /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
  52. 52 /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
  53. 53 /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
  54. 54 /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
  55. 55 /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
  56. 56 /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
  57. 57 /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
  58. 58
  59. 59 -- Dump completed on 2018-10-05 11:05:28

  可以看出我们导出的sql语句插入语句的数据是不乱码的。如果我们不指定字符集,导出来就是乱码的,它默认以库表的字符集导出数据。

然后我们把qiuhom库给drop掉。然后在通过备份还原。

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | oldboy |
  8. | performance_schema |
  9. | qiuhom |
  10. +--------------------+
  11. 5 rows in set (0.00 sec)
  12. mysql> drop database qiuhom;
  13. Query OK, 1 row affected (0.01 sec)
  14. mysql> show databases;
  15. +--------------------+
  16. | Database |
  17. +--------------------+
  18. | information_schema |
  19. | mysql |
  20. | oldboy |
  21. | performance_schema |
  22. +--------------------+
  23. 4 rows in set (0.00 sec)

  可以看出现在qiuhom库已经给删除掉了。接下来我们用刚才备份的sql文件,以指定的字符集导入数据。

指定字符集latin1导入数据

  1. [root@qiuhom ~]# mysql -uroot -padmin --default-character-set=latin1 < /work/backup.sql
  2. [root@qiuhom ~]# mysql -uroot -padmin -e "use qiuhom;select * from test;"
  3. +----+--------------+
  4. | id | name |
  5. +----+--------------+
  6. | 1 | a1 |
  7. | 2 | a2 |
  8. | 3 | ?°???? |
  9. | 4 | ?°??o¢ |
  10. +----+--------------+

  查看数据还是乱码,这个乱码的原因上面已经解释过了,是因为插入数据以latin1的字符集插入,而系统本身是utf8的字符集,所以乱码,我们可以指定字符集查看

  1. [root@qiuhom ~]# mysql -uroot -padmin --default-character-set=latin1 -e "use qiuhom;select * from test;"
  2. +----+--------+
  3. | id | name |
  4. +----+--------+
  5. | 1 | a1 |
  6. | 2 | a2 |
  7. | 3 | 小明 |
  8. | 4 | 小红 |
  9. +----+--------+
  10. [root@qiuhom ~]# mysql -uroot -padmin -e "show variables like ‘character_set%‘;"<br>+--------------------------+-------------------------------------------+
  11. | Variable_name | Value |
  12. +--------------------------+-------------------------------------------+
  13. | character_set_client | utf8 |
  14. | character_set_connection | utf8 |
  15. | character_set_database | utf8 |
  16. | character_set_filesystem | binary |
  17. | character_set_results | utf8 |
  18. | character_set_server | utf8 |
  19. | character_set_system | utf8 |
  20. | character_sets_dir | /application/mysql-5.5.32/share/charsets/ |
  21. +--------------------------+-------------------------------------------+

  这时就不乱码,是因为我们指定了临时的字符集为latin1,系统本身还是utf8。所以 --default-character-set 是指定临时字符集的方式之一。

说明:我们这里容易产生一个误区就是说,字符集和库表的字符相同后,库表里的内容就不乱码了,不是这样的。库表里的内容乱不乱码在于你插入数据时指定的字符集和你查看数据是指定的字符集是否一样,所以不管你以什么样的方式导入和导出 我们要查看数据是指定插入数据时的字符集就不乱码,否则怎么都乱码。所以在往别人的库表里插入数据时一定要注意看库表的默认字符集是否和系统默认字符集是否一致,不一致用set names 调成一致在插入数据,这样就不乱码,比如库表的字符集是latin1 而我们系统默认字符集是utf8 那么我们先把系统字符用set names tatin1 临时更换成latin1,然后在插入数据,这样插入的数据字符集才是和库表字符集一致。

以上用set names 的方式和 --default-character-set 的方式都是临时生效,当我们退出mysql登录时,在进就失效了,反反复复很麻烦有没有什么办法搞成永久的呢?答案是肯定用

我们在mysql的配置文件更改字符集这样我们每次登录进入mysql就是配置文件的字符集了

  1. [client]
  2. default-character-set=latin1
  3. port = 3306
  4. socket = /application/mysql-5.5.32/tmp/mysql.sock

  我们在/etc/my.cnf里找到[client] 在[client]下加入default-character-set=latin1,表示mysql客户端的字符集为latin1

  1. [mysqld]
  2. character-set-server=latin1

  找到[mysqld] 在其下面加入character-set-server=latin1 ,表示mysql服务端的字符集为latin1。

说明:以上就是永久更改字符集配置文件需要修改的地方,值得注意的是修改client 不需要重启服务,退出数据库重新进入字符集就发生了变化,但是服务端必须要重启服务,还有就是mysql5.1之前在服务端更字符集应该写成default-character-set=latin1,本人是用的mysql5.5 。

修改好配置文件 重新启动mysql服务 我们查看字符集

  1. [root@qiuhom ~]# mysql -uroot -padmin -e "show variables like ‘character_set%‘;"
  2. +--------------------------+-------------------------------------------+
  3. | Variable_name | Value |
  4. +--------------------------+-------------------------------------------+
  5. | character_set_client | latin1 |
  6. | character_set_connection | latin1 |
  7. | character_set_database | latin1 |
  8. | character_set_filesystem | binary |
  9. | character_set_results | latin1 |
  10. | character_set_server | latin1 |
  11. | character_set_system | utf8 |
  12. | character_sets_dir | /application/mysql-5.5.32/share/charsets/ |
  13. +--------------------------+-------------------------------------------+

  这样我们以后每次登录进mysql就都是latin1的字符集了。

3、建库建表指定字符集

3.1建库指定字符集。

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | performance_schema |
  8. | qiuhom |
  9. +--------------------+
  10. 4 rows in set (0.00 sec)
  11. mysql> create database qiuhom_gbk default character set gbk collate gbk_chinese_ci;
  12. Query OK, 1 row affected (0.00 sec)
  13. mysql> show databases;
  14. +--------------------+
  15. | Database |
  16. +--------------------+
  17. | information_schema |
  18. | mysql |
  19. | performance_schema |
  20. | qiuhom |
  21. | qiuhom_gbk |
  22. +--------------------+
  23. 5 rows in set (0.00 sec)
  24. mysql> show create database qiuhom_gbk;
  25. +------------+--------------------------------------------------------------------+
  26. | Database | Create Database |
  27. +------------+--------------------------------------------------------------------+
  28. | qiuhom_gbk | CREATE DATABASE `qiuhom_gbk` /*!40100 DEFAULT CHARACTER SET gbk */ |
  29. +------------+--------------------------------------------------------------------+
  30. 1 row in set (0.00 sec)

  提示:查看校对规则 可以用show character set;语句在mysql里查字符集对应校对规则。

3.2建表指定字符集

  1. mysql> create table test(
  2. -> id int not null,
  3. -> name char not null,
  4. -> primary key(id)
  5. -> )engine=innodb default charset=latin1;
  6. Query OK, 0 rows affected (0.00 sec)
  7. mysql> show tables;
  8. +----------------------+
  9. | Tables_in_qiuhom_gbk |
  10. +----------------------+
  11. | test |
  12. +----------------------+
  13. 1 row in set (0.00 sec)
  14. mysql> show create table test;
  15. +-------+---------------------------------------------------------------------------------------------------------------------------------------+
  16. | Table | Create Table |
  17. +-------+---------------------------------------------------------------------------------------------------------------------------------------+
  18. | test | CREATE TABLE `test` (
  19. `id` int(11) NOT NULL,
  20. `name` char(1) NOT NULL,
  21. PRIMARY KEY (`id`)
  22. ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
  23. +-------+---------------------------------------------------------------------------------------------------------------------------------------+
  24. 1 row in set (0.00 sec)

  提示:创建表的后面可以不用写engine=innodb,因为mysql5.5是默认引擎是innodb

4、总结

关于mysql字符集的问题 我们要有一个思想就是要统一字符集思想,怎么统一字符集呢,如果有中英字符混杂的环境建议用utf8字符集。统一字符集我们从linux系统 到mysql 系统 再到应用程序。具体设置方式如下:

1.调整linux服务端系统字符集

  1. [root@qiuhom ~]# cat /etc/sysconfig/i18n
  2. LANG=en_US.UTF-8
  3. SYSFONT=latarcyrheb-sun16
  4. [root@qiuhom ~]# echo $LANG
  5. en_US.UTF-8

  如果是echo $LANG返回的是我们设置的字符集,说明linux服务端的字符集设置好了

2.调整linux客户端字符集,所谓Linux客户端就是我们用的xshell和SecureCRT远程连接软件。

2.1SecureCRT设置方法和位置

技术分享图片

2.2xshell设置位置

技术分享图片

3.调整mysql客户端的字符集

3.1临时设置方式

方法一:

  1. mysql> set names latin1;
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> show variables like ‘character_set%‘;
  4. +--------------------------+-------------------------------------------+
  5. | Variable_name | Value |
  6. +--------------------------+-------------------------------------------+
  7. | character_set_client | latin1 |
  8. | character_set_connection | latin1 |
  9. | character_set_database | utf8 |
  10. | character_set_filesystem | binary |
  11. | character_set_results | latin1 |
  12. | character_set_server | utf8 |
  13. | character_set_system | utf8 |
  14. | character_sets_dir | /application/mysql-5.5.32/share/charsets/ |
  15. +--------------------------+-------------------------------------------+
  16. 8 rows in set (0.00 sec)

方法二:

  1. [root@qiuhom ~]# mysql -uroot -padmin --default-character-set=latin1
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 4
  4. Server version: 5.5.32-log Source distribution
  5. Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
  10. mysql> show variables like ‘character_set%‘;
  11. +--------------------------+-------------------------------------------+
  12. | Variable_name | Value |
  13. +--------------------------+-------------------------------------------+
  14. | character_set_client | latin1 |
  15. | character_set_connection | latin1 |
  16. | character_set_database | utf8 |
  17. | character_set_filesystem | binary |
  18. | character_set_results | latin1 |
  19. | character_set_server | utf8 |
  20. | character_set_system | utf8 |
  21. | character_sets_dir | /application/mysql-5.5.32/share/charsets/ |
  22. +--------------------------+-------------------------------------------+
  23. 8 rows in set (0.00 sec)

 3.2永久设置编辑/etc/my.cnf找到[client]

  1. [client]
  2. default-character-set=latin1

  然后再进mysql字符集就变成latin1了

4.设置mysql服务端字符集

4.1临时设置

  1. mysql> show variables like ‘character_set%‘;
  2. +--------------------------+-------------------------------------------+
  3. | Variable_name | Value |
  4. +--------------------------+-------------------------------------------+
  5. | character_set_client | latin1 |
  6. | character_set_connection | latin1 |
  7. | character_set_database | utf8 |
  8. | character_set_filesystem | binary |
  9. | character_set_results | latin1 |
  10. | character_set_server | utf8 |
  11. | character_set_system | utf8 |
  12. | character_sets_dir | /application/mysql-5.5.32/share/charsets/ |
  13. +--------------------------+-------------------------------------------+
  14. 8 rows in set (0.00 sec)
  15. mysql> set character_set_server=latin1;
  16. Query OK, 0 rows affected (0.00 sec)
  17. mysql> show variables like ‘character_set%‘;
  18. +--------------------------+-------------------------------------------+
  19. | Variable_name | Value |
  20. +--------------------------+-------------------------------------------+
  21. | character_set_client | latin1 |
  22. | character_set_connection | latin1 |
  23. | character_set_database | utf8 |
  24. | character_set_filesystem | binary |
  25. | character_set_results | latin1 |
  26. | character_set_server | latin1 |
  27. | character_set_system | utf8 |
  28. | character_sets_dir | /application/mysql-5.5.32/share/charsets/ |
  29. +--------------------------+-------------------------------------------+
  30. 8 rows in set (0.00 sec)

4.2永久设置,在my.cnf里找到[mysqld]

  1. [mysqld]
  2. character-set-server=latin1

  说明:mysql5.1之前 是 default-character-set=latin1

5.创建库设置字符集

  1. mysql> create database qiuhom_gbk default character set gbk collate gbk_chinese_ci;
  2. Query OK, 1 row affected (0.00 sec)
  3. mysql> show create database qiuhom_gbk;
  4. +------------+--------------------------------------------------------------------+
  5. | Database | Create Database |
  6. +------------+--------------------------------------------------------------------+
  7. | qiuhom_gbk | CREATE DATABASE `qiuhom_gbk` /*!40100 DEFAULT CHARACTER SET gbk */ |
  8. +------------+--------------------------------------------------------------------+
  9. 1 row in set (0.00 sec)  

6.创建表设置字符集

  1. mysql> create table test(
  2. -> id int not null,
  3. -> name char not null,
  4. -> primary key(id)
  5. -> )engine=innodb default charset=latin1;
  6. Query OK, 0 rows affected (0.00 sec)
  7. mysql> show create table test;
  8. +-------+---------------------------------------------------------------------------------------------------------------------------------------+
  9. | Table | Create Table |
  10. +-------+---------------------------------------------------------------------------------------------------------------------------------------+
  11. | test | CREATE TABLE `test` (
  12. `id` int(11) NOT NULL,
  13. `name` char(1) NOT NULL,
  14. PRIMARY KEY (`id`)
  15. ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
  16. +-------+---------------------------------------------------------------------------------------------------------------------------------------+
  17. 1 row in set (0.00 sec)

统一以上6步的字符集就不会出现乱码。

 

mysql字符集那些事

标签:sql文件   查询   sha   xshell   mysql-5.5   lan   utf-8   变量   val   

人气教程排行