Mysql知识汇总

本篇内容为Mysql相关知识。

语法

条件

查询基本语法:

select * from table_name;
  • 星号表示指定表中的所有内容。
  • 可以使用as起别名。
  • 查询多个列,之间用逗号分开。

消除重复行

select distinct gender from table_name;

条件语句

select * from tablename where 条件;

比较运算符包含: >, <, =, >=, <=, !=或者<>【不等于】
逻辑运算符: and, or, not
查询编号大于3的学生:
selecr * from students where id>3;
查询姓名不是Tom:

select * from students where name!='Tom';

查询编号大于3的女生:

select * from students where id>3 and gender=0;
模糊查询
  • 使用like关键字
  • %: 表示任意多个字符
  • _: 表示一个任意字符

查询姓王的人:

select * from tablename where name like '王%';

查询姓王的,且名字只有一个字:

select 8 from tablename where name like '王_';
范围查询
  • i: 表示在一个非连续的范围内
  • between: 表示在一个连续的范围内
    查询编号是1或者2,或者8:

    select 8 from tablename where id in (1, 2, 8);
    编号为3到6的男生:

    select * from tablename where id between 3 and 8 and gender =1;

判断空
  • null和’ ‘是不同的
  • 判断为空使用 is null
    查询没有填写地址的学生:

    select * from students where address is null;

  • 判断非空 is not null
    查询填写地址的男生:

    select * from tablename where address is not null and gender=1;

    优先级
  • 小括号,not, 比较运算符,逻辑运算符
  • 逻辑运算中,and级别高于or,如果希望先计算or,使用小括号

聚合

为了快速计算,可以使用提供的5个聚合函数,分别为:count(), max(), min(), sum(), avg()
查询学生总数:

select count(*) from table;

查询女生年龄最大的:

select max(age) from table where gender=0;

聚合函数使用都是相同的。

分组

  • 按照字段进行分组,字段相同的数据会被放在一个组
  • 分组后,只能查询相同的数据列,有差异的数据无法显示
  • 可以对分组后的数据进行统计,做聚合运算

    select 列1,列2,聚合函数…. from table group by 列1,列2…..;
    查询男女生总数:

    select gender as 性别, count(*) from studens group by gender;
    表中取出gender一列的数据,按照gender进行分组,得出组后,对每组的数量统计(聚合函数count)。上条语句得出是女生总人数和男生总人数。
    分组后再进行筛选数据:

  • 使用having或者where

查询男生总人数:

方法1:select gender, count(*) from table group by gender having gender=1;

或:

方法2: select count(*) from table where gender=1;

where和having区别:

  • where 是对from后面指定数据进行筛选,是对原始数据筛选
  • having是对group by分组后的数据进行筛选

排序

  • 关键字 order by
  • asc: 升序, 默认为升序排列
  • desc: 降序

查询未删除的男生信息,按照学号降序排列

select * from table where gender=1 and isdelete=1 order by id desc;

分页

语法:

select * from table limit start_num, count_num;
start_num是开始处,count_num是要获取的条目数。

总结

select语句完整:

select distinct * from table where... group by.... having....order by....limit start, count    
  • select * from 表名,
  • where
  • group by
  • having
  • select distinct *
  • order by
  • limit start, count
    执行时候按照以上顺序执行。使用时候也只是上述关键字的部分组合,不是全部。

视图

对于复杂的查询语句,每次重复写是很麻烦的。所以通过视图简化。
定义视图:

>create view stuscorce as select * from .....[as 后是select语句]

视图的使用就是方便查询,使用时:

>select * from stuscorce;

事务

数据库的事务是作为单个逻辑工作单元执行的一系列操作,要么完全的执行,要么完全的不执行。数据库引擎为innodb或bdb是能对表使用事务。
事务语句:

  • 开启 begin
  • 提交 commit
  • 回滚 rollback

索引【重点】

推荐:http://tech.meituan.com/mysql-index.html
MySQL索引背后的数据结构及算法原理

建立索引:

create INDEX 索引名 on 表名(列名);
create INDEX INDEX_NAME in emp(en_name);

即:在emp表的en_name列上建立索引,索引名字是inx_name。
使用索引的影响:

  • 磁盘占用多
  • 对 insetr, delete, update语句的效率有影响,因为执行插入,删除,修改记录后,会对索引进行维护,花费时间。
  • 使用磁盘空间换取的是时间,提升查询效率。

索引的分类:
Mysql中主要是主键索引, 唯一索引,普通索引, 全文索引。

主键索引

当某个字段设置为primary key后,这个字段就是主键索引了。

唯一索引

当某个字段设置为unique key后,这个字段就是唯一索引了,如果再出现相同的字段则会报错。

特点:

  1. 可以存在多个唯一索引。
  2. 要求索引的字段值唯一不能重复。
  3. 如果将唯一索引设置为not null, 使用效果上等价于primary key。

普通索引

使用create INDEX 为某个字段创建普通索引,普通索引不要求字段内容唯一。

普通索引创建:
方法1:
创建表的时候,直接指定普通索引:

create table `table_name` (
id int unsigned primary key,
name varchar(64) not null ,default '',
index (name)    /*这里指定普通索引*/
) charset=utf8;

方法2:
创建表之后在指定索引:
create table table_name (
id int unsigned primary key,
name varchar(64) not null ,default ‘’,
) charset=utf8;

--创建表之后指定普通索引,第一种方法:
alter table `table_name` add index (name);
--创建表之后指定普通索引,第二种方法[同开始介绍的方式]:
create INDEX index_name on table_name(name);

特点:

  1. 一个表中可以存在多个普通索引。
  2. 使用最多的是普通索引。
  3. 普通索引的效率没有主键索引的高。
  4. 如果不确定某一列的值是否有重复的值,则只能用普通索引优化查询。

全文索引

进行关键字检索的时,使用全文索引.