MySQL 创建表 复制表结构的两种方法 插入相同数据 重命名表 排查并修改字符集
?
中位数简单案例数据准备
这里是我为中位数简单案例准备数据的过程,过程中也回顾下一些命令的使用
?
创建表(奇数个)
create table `median_test` (`id` int unsigned auto_increment, `marks` int not null, primary key(`id`));
insert into median_test (marks) values (66);
insert into median_test (marks) values (90);
insert into median_test (marks) values (100);
?
重命名
rename table `median_test` to `median_odd`;
?
复制表
方式一:
create table `median_even` like `median_odd`
(Mysql5之后支持)
方式二:
show create table median_odd;
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| median_odd | CREATE TABLE `median_odd` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`marks` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 |
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
这就是为什么我要写修改表字符集
的原因了 --- Latin1?怎么能忍!要统治世界的可是utf8
(从上面的语句我们也可以看到官方默认的直接指定字符集的形式为DEFAULT CHARSET
)
处理过程:
因为mysql、数据库、数据表,每一级都会设定字符集,除非你在创建时直接指定字符集,否则它使用默认值。因此从最顶层开始,一级一级检查为妙
- 检查mysql
show variables like ‘character%’;
(如果你的未修改为utf8,详见这里)
- 检查数据库
show create database test
+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| test | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)
哦,在这里我们发现问题所在了,先治本:
alter database test default character set utf8;
检查一下:
+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| test | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)
ok,那我们的数据表应该不会自动改
- 数据表
修改默认字符集
alter table median_odd default charset utf8 collate utf8_general_ci;
修改默认字符集并修改所有已有字段的字符集:
alter table median_odd convert to charset utf8 collate utf8_general_ci;
?
复制(批量插入)数据:
insert into median_even select * from median_odd;
?
最终数据结果为:
mysql> select * from median_even;
+----+-------+
| id | marks |
+----+-------+
| 1 | 66 |
| 3 | 100 |
| 4 | 95 |
| 5 | 80 |
+----+-------+
4 rows in set (0.00 sec)
mysql> select * from median_odd;
+----+-------+
| id | marks |
+----+-------+
| 1 | 66 |
| 2 | 90 |
| 3 | 100 |
+----+-------+
3 rows in set (0.00 sec)
?