很久很久以前学习mysql的笔记记录,很随意,但都是自己记录的,希望对需要的人有所帮助。
本文使用word2013编辑并发布
Postbird | There I am , in the world more exciting!
Postbird personal website .www.ptbird.cn
引擎(Engine):
数据库对同样的数据进行不同的管理和存储方式,称为存储引擎。
engine=myisam;
选择引擎。
XX.MYI 书的目录(索引)
XX.FRM 书名和信息
XX.MYD 书的内容
innodb引擎:默认引擎;
存储引擎和特点:
字符集,校对集,乱码:
文字本来的字符集和展示的字符集不一致导致乱码
show variables like '%charact%';
查看所有的字符集类型
set character_set_client = utf-8;
单独设置客户端的字符集
ser names gbk;
将client connection results 全部设置成utf8
php页面 utf8 创建数据库 utf8 再次设置 utf8 不会乱码
charset:字符集
校对集(服务器连接排序规则):一套字符集可能有N套校对集
索引(index):
MYD存放内容 MYI是索引
索引是数据的目录,能将快速定位行数据的位置。
索引提高了查询速度,降低了增删改的速度。
一般在查询比较频繁的列上加,在重复度低的列上加效果好(例如主键)。
key 普通索引
unique key name(name)唯一索引
primary key 主键索引 (不用起名字)
fulltext:全文索引
中文环境下,全文索引无效,要分词+索引 一般用第三方解决方案 比如sphinx
索引长度:
建立索引,可以只索引列的前一部分长度 例如前十个字符
key email(email(10));
多列索引:
将两列或多列的值,看成一个整体,然后建立索引。
key 索引名(索引1,索引2);建立多列索引
select * from where first_name='zhanwen' and last_name='li';
多列索引查询规范
show index from t9;
显示表的索引信息
explain select * from t9 where first_name='zhanwen' and last_name='li';
解释该行为
冗余索引:
在某个列上可能存在多个索引。
name(first_name,last_name),last_name(last_name);
索引的操作:
show index from 表名 \G
查看表名 (create table 表名 也能看到)
drop index 索引名 on 表名;或者 alter table 表名 drop index 索引名;
删除索引
alter table 表名 add index name(first_name,last_name);
alter table 表名 add unique key a(a);
alter table 表名 add primary key(last_name); (主键索引只有一个,不用索引名)
添加索引