一、MySQL常用的命令:
mysql -h 192.168.0.200 -P 3306 -u root -p
2. mysql>status; 查看运行环境信息
3. 显示所有数据库 : show databases;
4. 切换连接数据库的语法 : use mydatabase;
5. 显示数据库中的所有表 : show tables;
6. 显示某个表创建时的全部信息 : show create table table_name;
7. 查看表的具体属性信息及表中各字段的描述:desc table_name;
8、导入sql文件
1链接数据 2选择数据库 3 source /data/tmp/sql.txt
二、MySQL常用的SQL修改语句 (我们以blog_archive表为例):
2、创建数据表:create table blog_archive(aid int unsigned auto_increment,
cid smallint unsigned not null default 0,
title varchar(50) not null default '',
contents text not null default '',
click mediumint unsigned not null default 0,
primary key(aid),
key cid(cid)
)engine=MyISAM default charset=utf8;
3、增加一个字段:
alter table blog_archive add column sort smallint not null default 0 after contens;
alter table blog_archive add column is_del tinyint(1) not null default 1;
4、删除一个字段:
alter table blog_archive drop column is_del;
alter table blog_archive drop is_del; // 不加column也可以
5、修改字段名和字段类型:
alter table blog_archive change click click_num smallint not null default 0;
alter table blog_archive change click_num click_num mediumint not null default 0;
6、修改表名:alter table blog_archive rename to blog_article;
7、修改表引擎:alter table blog_archive engine=InnoDB
7、清空某张表:truncate table blog_archive;
三、mysql索引相关语句
1、增加一个索引:
mysql>CREATE index index_name ON `table_name`(`column`)
6、查看索引:show index from blog_archive;
7、创建多列索引
create table tomener(
id int unsigned not null auto_increment,
name varchar(10) not null default '',
age smallint unsigned not null default 0,
primary key(id),
index index1(name,age)
)engine=MYISAM default charset=utf8;
8、创建全文索引
fulltext index index2(name)
9、删除索引
drop index 索引名称 on 表名
四、MySQL常用的SQL查询语句
1、查询时过滤重复的数据:select DISTINCT title FROM blog_archive where aid > 10 LIMIT 5
7、随机读取几条数据:select * from blog_archive ORDER BY rand() limit 10
五、MySQL优化语句
SELECT table_schema,table_name,data_free,engine FROM information_schema.tables WHERE table_schema NOT IN('information','mysql') AND data_free > 0;
2、优化mysql数据碎片
optimize table 表名
3、mysql修复表
repair table 表名
4、explain select * from table
desc select * from table