0
点赞
收藏
分享

微信扫一扫

MySQL分类


  1. DDL(Data Definition Langua ge):操作数据库,表(create,drop,alter等)
  1. 操作数据库:CRUD
  1. C(Create):创建
  1. 创建数据库:create database 数据库;
  2. 判断数据库是否存在并创建数据库(如果存在就不创建):create database if not exists 数据库名称;
  3. 按照指定的字符集创建数据库:create database db1 character set 字符集;
  1. 例:创建一个db1的数据库,判断是否存在,并指定字符集为gbk:create database if not exists db1 character set gbk;
  1. R(Retrieve):查询
  1. 查询所有数据库的名称:show databases;
  2. 查询某个数据库的字符集(查看创建 某个数据库的语法):show create database 数据库名称:  ;
  1. U(Update):修改
  1. 修改数据库的字符集:alter database 数据库名称  character  set 字符集;
  1. D(Delete):删除
  1. 删除数据库:drop database  数据库名称;
  2. 判断数据库是否存在并删除(如果存在就删除):drop database if exists 数据库名称;
  1. 使用数据库
  1. 查询当前正在使用的数据库名称:select database();
  2. 使用数据库:use 数据库名称;
  1. 操作表
  1. C(Create):创建
  1. 创建表:  create table  表名称;

create table 表名(
         列名1  数据类型1,
         列名2  数据类型2,
         列名3  数据类型3,
         列名4  数据类型4
);
注意:最后一列,不需要加(,)否则会报错
数据库类型:
    1.int    整数类型
        age  int,
    2.double  小数类型
        score  double(5,2)
    3.date  日期,只包含年月日,yyyy-MM-dd
    4.datetime    日期,包含年月日时分秒     yyyy-MM-dd HH:mm:ss
    5.timestamp    时间戳   包含年月日时分秒     yyyy-MM-dd HH:mm:ss 
        如果是timestamp 不给这个字段赋值,或赋值为null,默认使用当前的系统时间,来自动赋值
    6.varchar    字符串类型
         name  varchar (20):姓名大于20个字符

创建表
    create table student(
        id  int,
        name  varchar(32),
        age  int,
        score double(4,1),
        birthday  date,
        insert  timestamp
);

3.复制表:

  1. create  table  表名  like  被复制的表名;
  1. R(Retrieve):查询
  1. 查询某个数据库所有的表名称:show tables;
  2. 查询表结构:desc 表名;
  1. U(Update):修改
  1. 修改表的表名
  1. alter table 表名 rename  to  新的表名;
  1. 修改表的字符集
  1. alter  table  表名  character  set  字符集的名称;
  1. 修改列名称  类型
  1. 添加一列
  1. alter  table  表名  add  列名  数据类型;
  1. 即修改列名又修改数据类型
  1. alter  table  表名  change  旧列名  新列名   新数据类型;
  1. 只修改数据类型
  1. alter  table  表名  modify  列名   新数据类型;
  1. 删除列
  1. alter table  表名  drop  列名;
  1. D(Delete):删除
  1. drop table  表名;
  2. drop table if  exists  表名;
  1. DML(Data Manipulation Language):增删改表中的数据(insert,delete,update)
  1. 添加数据
  1. insert  into  表名  (列名1,列名2,列名3,.....列名4)values(值1,指2,值3...值4);
  2. 注意:
  1. 列名和值要一一对应,(名字和数据类型)
  2. 如果表名之后,不定义列名,默认给所有的添加;
  3. insert   into   表名  values(值1,指2,值3...值4);
  1. 修改数据
  1. update   表名  set   列名1=值1,列名2=值2    where    条件;
  1. 注意:
  1. 如果不加任何条件,就会对表中的所有数据都进行更改;
  1. 删除数据
  1. delete   from   表名   [where   条件];   (不推荐使用,表中有多少条数据就会删除多少次记录)
  1. 注意
  1. 如果不加条件,就会删除表中的所有的记录;
  1. truncate   table   表名;     (先删除表,在创建一条一样的表)
  1. DQL(Data Query Language):查询表中的数据(select ,where)
  1. select  *  from   表名;
  2. 语法: 

select
    字段列表
from
    表名列表
where
    条件列表
group  by
    分组字段
order  by
    排序
limit
    分页限定

  1. 基础查询
  1. 多个字段查询
  1. select   字段名1, 字段名2, 字段名3, 字段名4  from   表名;
  2. 注意:
  1. 如果使用所用的字段可以使用*代替;
  1. 去除重复
  1. select    distinct    字段名     表名;
  1. select    distinct    name     student;
  1. 如何计算列
  1. 一般使用四则运算计算一些列的值(一般只会进行计算数值型的数据)
  2. 计算成绩总分
  1. select  name ,math,English,math+English   from    表名;
  2. 如果存在null 就要将null 转换,null参与计算的结果都为null;
  1. select  name ,math,English,math+if  null(English ,0)  from    表名;
  1. 起别名
  1. 在计算之后的列名是非常难看的,所以就可以给他起一个别名
  1. select  name ,math 数学,English  英语,math+ if  null(English ,0)as(as可写可不写)  总分  from    表名;
  1. 条件查询
  1. where 字句后面跟条件
  2. 运算符
  1. 两种不等于
  1. !=
  2. <>
  1. 中间查询
  1. select   *  from    表名     where    age   >=20  &&
  2. select   *  from    表名     where    age   >=20  and
  3. select   *  from    表名     where    age   between   20   and
  1. 查询或条件
  1. select   *  from  student    where    age=10  or age=3 or  age=12;
  2. select   *  from  student    where    age    in (10  ,3, 12);
  1. 如果查询条件为null,要使用is而不是=;
  1. select   *  from   表名     字段  is   null;
  1. 模糊查询  like
  1. 占位符
  1. _   :单个任意字符
  1. 例如:要查询名字的第二个字为中
  1. select    *    from   表名     where   name   like   ' _中%';
  1. %:多个任意字符
  1. 例如:要查询姓张的人
  1. select   *    from     表名    where    name  like    '张%';
  1. 例如:要查询姓名中包含张字的人
  1. select   *    from     表名    where    name  like    '%张%';
  1. DCL(Data Control Language):数据库授权(grant,revoke)
  1. 排序查询
  1. order   by    字句
  1. order   by    排序字段1  排序方式1,排序字段2  排序方式2....;
  1. 排序方式:升序(asc)   降序(desc)
  1. select   *    from    student   order   by   math     asc;
  2. select   *    from    student   order   by   math     desc;
  3. select   *    from    student   order   by   math     desc,English    asc;
  1. 注意:
  1. 如果有多个排序条件,则当前面的条件值一样时,才会判断第二条件;
  1. 聚合函数
  1. 将一列的数据看作为一个整体,进行纵向的计算;
  1. count:计算个数
  1. 一般会选择非空 的列,主键;
  2. count(*)
  1. max:计算最大值
  1. select  max(math)  from   student;
  1. min:计算最小值
  1. select  min(math)  from   student;
  1. sum:计算和
  1. select  sum(math)  from   student;
  1. avg:计算平均值
  1. 计算表中人数
  1. select    count(name)  from    表名;
  2. 注意:
  1. 聚合函数的计算会排出null值;
  1. 解决方案:
  1. 选择不包含非空的列进行计算;
  2. if null   函数
  1. 分组查询
  1. group  by     分组字段;
  1. select   
  1. 注意:
  1. 分组之后查询的字段, 分组字段,聚合函数
  1. 例:
  1. 查询男女同学的平均分
  1. select   sex,avg(math)from  student   group  by   sex;
  1. 查询男女同学的平均分及人数
  1. select   sex,avg(math),count (id)from  student   group  by   sex; 
  1. 查询男女同学的平均分及人数,成绩小于70的不参与分组,分组之后的人数要大于2人
  1. select   sex,avg(math),count (id)from  student   where  math>70  group  by   sex  having    count(id)>2;
  1. where和having的区别?
  1. where之后不可以跟聚合函数;having可以进行聚合函数的判断
  2. where在分组之前进行限定,如果不满足条件,则不参与分组,having在分组之后进行限定,如果不满足结果,则不会被查询出来
  1. 分页查询
  1. 例:每页显示三条记录
  1. select   *  from  limit   0,3;   第一页
  2. select   *  from  limit   3,3;   第二页
  3. select   *  from  limit   6,3;   第三页
  1. 公式:  开始的索引等于《(当前的页码-1)*每页显示的条数》;
  2. limit  是一个mysql的"方营"(只在mysql中使用)
举报

相关推荐

0 条评论