• mysql day03


    Day02回顾
    1、数据类型
      1、数值类型
      2、字符类型
      3、枚举类型
        1、enum()
        2、set() ##插入记录"girl,study"
      4、日期时间类型
        1、date
        2、time
        3、datetime #不给值默认NULL
        4、timestamp#不给值默认返回系统当前时间
    2、日期时间函数
      1、NOW()
      2、CURDATE()
      3、CURTIME()
      4、CURYEAR()
      5、YEAR(字段名)
      6、DATE(字段名)
      7、TIME(字段名)
    3、日期时间运算
      select ... from 表名
      where 字段名 运算符(now()-interval 2 day)
    4、表字段操作
      1、添加(add)
        alter table 表名 add 字段名 数据类型 first | after 字段名;
      2、修改(modify)
        alter table 表名 modify 字段名 新数据类型;
      3、删除(drop)
        alter table 表名 drop 字段名;
      4、表重命名(rename)
        alter table 原表名 rename 新表名;
      5、表字段重命名(change)
        alter table 表名 change 原名 新名 数据类型;
    5、表记录管理
      1、删除(delete)
        delete from 表名 where 条件;
      2、修改(update)
        update 表名 set 字段1=值1,字段2=值2 where 条件;

            表字段(alter table 表名)    表记录
       增加       add                   insert
       删除       drop                  delete
       修改       modify                update
       查询       desc                  select
    6、运算符
      1、数值比较、字符比较、逻辑比较
      2、范围内比较
        1、between 值1 and 值2
        2、in(值1,值2,...)
        3、not in(值1,值2,...)
      3、空、非空
        1、is NULL
        2、is not NULL
      4、模糊查询 like
        字段名 like 表达式
        % :0-n个字符
        _ :1个字符
        show tables like "%he%";
        show variables like "%character%";
    6、SQL查询
      1、order by ASC/DESC
      2、limit
        1、limit n
        2、limit m,n
        3、分页
          每页显示m条,显示第n页
          limit (n-1)*m,m
      3、聚合函数
        max(...) min(...) avg(...) sum(...)
        count(*)
      4、group by(先分组,再聚合)
        1、select后的字段名如果没有在group by之后出现,必须对该字段进行聚合处理
          select avg(gongji) from sanguo group by country;
      5、having :操作聚合函数生成的显示列
    **********************
    Day03笔记
    1、SQL查询
      1、distinct :不显示字段的重复值(去重)
        1、示例
          1、sanguo表中有哪些国家
            select distinct country from sanguo;
          2、sanguo表中有几个国家
            select count(distinct country) from sanguo;
        2、注意
          distinct和from之间所有字段值都相同才会去重
      2、查询表记录时做数学运算
        1、运算符 :+ - * / %
        2、示例
          1、查询表记录时所有英雄攻击力翻倍
            select name,gongji*2 as gonji from sanguo;
    2、嵌套查询(子查询)
      1、定义 :把内层的查询结果作为外层查询的条件
      2、语法
        select .. from 表名 where 字段名 运算符(select ...)
      3、示例
        1、把攻击值小于平均攻击值的英雄名字和攻击值查出来
          select name,gongji from sanguo where
          gongji<(select avg(gongji) from sanguo);
        2、找出每个国家攻击力最高的英雄的名字和攻击值
         
          select name,gongji from sanguo
          where (country,gongji) in
          (select country,max(gongji) from sanguo group by country);


          where (country,gongji) in
          (("吴国",100),("魏国",666),("蜀国",1005));

          蜀国 :1005 666 100
          魏国 :666
          吴国 :100

          10:50分
    3、多表查询
      1、两种方式
        1、不加where条件(笛卡尔积)
          select t1.name,t2.name from t1,t2;
        2、加where条件
          select 字段名列表 from 表1,表2 where 条件;
        3、示例
          1、查询省、市详细信息
            河北省 石家庄市
     河北省 廊坊市
     湖北省 武汉市
     select sheng.s_name,city.c_name from sheng,city where sheng.s_id=city.cfather_id;
          2、查询省、市、县详细信息
            select sheng.s_name,city.c_name,xian.x_name from sheng,city,xian
            where
            sheng.s_id=city.cfather_id and
            city.c_id=xian.xfather_id;
    4、连接查询
      1、内连接(inner join)
        1、语法格式
          select ... from 表1 inner join 表2 on 条件
          inner join 表3 on 条件;
        2、示例
          1、查找省、市详细信息
            select sheng.s_name,city.c_name from sheng
            inner join city
            on sheng.s_id=city.cfather_id;
          2、查找省、市、县详细信息
            select sheng.s_name,city.c_name,xian.x_name from
            sheng inner join city
            on sheng.s_id=city.cfather_id
            inner join xian
            on city.c_id=xian.XFATHER_ID;
      2、外连接
        1、左连接(left join)
          1、以左表为主显示查询结果
        2、右连接(right join)
          1、以右表为主显示查询结果
            select sheng.s_name,city.c_name,xian.x_name from
            sheng left join city
            on sheng.s_id=city.cfather_id
            right join xian
            on city.c_id=xian.xfather_id;
    5、约束
      1、非空约束(not null)
        1、不允许该字段的值为NULL
          ## name varchar(20) not null
      2、默认约束(default)
        1、插入记录时,不给该字段赋值,则使用默认值
          ## sex enum("M","F","S") not null default "S";
    6、索引
      1、定义
        对数据库表的一列或者多列的值进行排序的一种结构(BTree方式)
      2、优点
        加快数据的检索速度
      3、缺点
        1、占用物理存储空间
        2、当对表中数据更新时,索引需要动态维护,占用系统资源,降低数据维护速度
      4、索引示例
        1、开启运行时间检测
          set profiling=1;
        2、执行查询语句(没有索引)
          select name from t1 where name="lucy88888";
        3、在name字段创建索引
          create index name on t1(name);
        4、再执行查询语句(有索引)
          select name from t1 where name="lucy99999";
        5、对比执行时间
          show profiles;
    7、索引分类
      1、普通索引(index) && 唯一索引(unique)
        1、使用规则
          1、可设置多个字段
          2、约束
            普通索引:无约束 key标志 :MUL
     唯一索引:字段值不允许重复,可为NULL  UNI
          3、把经常用来查询的字段设置为索引字段
        2、创建表创建
          create table 表名(
          ... ...
          index(name),
          index(age),
          unique(phnumber),
          unique(cardnumber)
          );
        3、已有表创建
          create [unique] index 索引名 on 表名(字段名);
        4、查看索引
          1、desc 表名;  --> key标志
          2、show index from 表名G;
        5、删除索引
          drop index 索引名 on 表名;
      2、主键(primary key) && 自增长(auto_increment)
        1、使用规则
          1、只能有1个字段
          2、约束 :字段值不允许重复,且不能为 NULL
          3、KEY标志 :PRI
          4、通常设置编号id为主键,能唯一锁定1条记录
        2、创建表时创建
          create table 表名(
          id int primary key auto_increment,
          ...
          alter table 表名 auto_increment =1000;
          );
        3、已有表创建
          alter table 表名 add primary key(id);
        4、删除
          1、先删除自增长
            alter table 表名 modify id int;
          2、删除主键
            alter table 表名 drop primary key;
      3、外键(foreign key)
        1、定义
          让当前表字段的值在另一个表的范围内选择
        2、语法
          foreign key(参考字段名)
          references 主表(被参考字段名)
          on delete 级联动作
          on update 级联动作
        3、使用规则
          1、主表、从表字段数据类型要一致
          2、主表被参考字段 :主键
        4、示例
          1、缴费信息表(财务)
            id   姓名    班级   缴费金额
             1  唐伯虎  AID08     300
      2  点秋香  AID08     200
     create table jftab(
     id int primary key,
     name varchar(20) not null,
     class char(5) default "AID",
     money smallint
     )charset=utf8;

     insert into jftab values
     (1,"唐伯虎","AID08",300),
     (2,"点秋香","AID08",200);

          2、学生信息表(班主任)
            stu_id  姓名   缴费金额
     create table bjtab(
     stu_id int,
     name varchar(15),
     money smallint,
     foreign key(stu_id) references jftab(id)
     on delete cascade
     on update cascade
     );
        4、删除
          1、查看外键名
            show create table bjtab;
          2、删除外键
            alter table 表名 drop foreign key 外键名;
        5、在已有表中添加外键
          alter table bjtab add foreign key(stu_id)
          references jftab(id)
          on delete set null
          on update set null;
        6、级联动作
          1、cascade
            数据级联删除、更新(参考字段)
          2、set null
            从表有相关联记录,字段值设置为NULL
          3、restrict(默认)
            从表有相关联记录,不让主表删除、更新
       
           
     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    Programming tutorials and source code examples
    Easy Web Development Framework for Java
    jquery两级边动完整例子
    EWeb4JSolidBase 1.9发布,基于EWeb4J的功能平台 开源软件 ITeye资讯
    Accelerated C++中文版/克尼格 (Andrew Koenig)图书卓越亚马逊
    5.7 C 有 ``按引用传递" 吗?
    How to Install IIS on Windows 7 or Vista HowTo Geek
    The GNU C Library
    c语言程序设计现代方法
    C Array length function problem C / C++
  • 原文地址:https://www.cnblogs.com/sky-ai/p/9768491.html
Copyright © 2020-2023  润新知