MySQL基本操作
数据库操作
# 创建库
create database `test`;
# 删除库
drop database `test`;
数据表操作
# 创建表
create table `user`(
`id` int(10) unsigned not null auto_increment,
`name` varchar(50),
`email` varchar(50),
`sex` smallInt(6),
`birthday` datetime default null,
`age` smallInt(6) unsigned,
primary key (`id`),
key idx_age (`age`)
) engine=InnoDB default charset=utf8;
# 删除表
drop table `user`
#复制表结构
create table if not exists new_name (like old_name);
# 修改存储引擎
alter table user set engine = 'InnoDB';
# 更改表名称
rename table old_name to new_name;
# 设置显示字符集
set name utf8;
# 查看表结构
show create table `user`;
describe `user`;
字段操作
alter table `user` add column `status` tinyint(3);
alter table `user` change column `status` `qq` int(10);
alter table `user` drop column `qq`;
索引操作
# 创建索引
create index idx_age on user(`age`)
create unique index idx_username on user(`username`)
alter table user add index idx_age(`age`)
alter table user add unique idx_username(`age`)
# 删除索引
drop index idx_age on user
alter table user drop index idx_age
数据导入导出
# 导出数据
mysqldump -uroot -proot dbname [tbname] > user.sql
# 导入数据
mysql> source ./user.sql
# 导入文件数据
mysql> load data local infile './filename.sql' into table tbname(fed1,fed2...)
性能跟踪
# 开启性能查看
set @@profiling = 1;
# 查看性能总览
show profiles
# 查看详细性能
show profile for query 2
# 解释语句性能
explain select * from user where id = 1
Binlog日志
# 查看日志文件
show binary logs;
show master logs;
# 查看日志文件事件
show binlog events in 'log_name' [ from pos ] [limit 3, 5];
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: