day41


  1. 数据行

    #插入
    insert into 表名 (列1,列2) values (值1,值2);
    insert into student(id,name) values(2,'zs')
    insert into 表名 (列名1,列名2) select 列名1,列名2 from 表名;

    #查询
    select 列名 from 表名;
    select * from sutdnet;

    #删除
    delete fron 表名;(自增列会延续)
    truncate 表名;(自增列会重新开始)

    #修改
    update 表名 set 列名 = '值'[,列名 = '值'] where 条件

    #where ,and,or,not,<, >, != ,in, between
    delete from 表名 where id > 10
    delete from 表名 where id > 10 and name = 'xxx'
    select * from 表名 where id between 9 and 12
    select * from 表名 where id in (1,2,3,4)

    #通配符 % _
    select * from 表名 where name like 'ale%';
    select * from 表名 where name like 'ale_';

    #限制取几条 limit
    select * from 表名 limit 0,10 #(索引偏移量,取出多少条数据/offset2)

    #排序 order by
    select * from 表名 order by 列名 desc;
    select * from 表名 order by 列名 asc;
    select * from 表名 order by 列1 desc,列2 asc #如果前一列的值相等,会按照后一列的进一步的排序

    #分组 group by having
    select 列名,聚合函数(sum,count,max,min,avg) from 表名 group by 列名
    #ps:分组一般和聚合函数一起用,分组用来以某一个分组,聚合函数用来计算某一列,having用来对分组的结果进行筛选

    #连表操作
    select * from 表名,表名 where 表名.列名 = 表名.列名

    select * from 表名 left join 表名 on 表名.列名 = 表名.列名
    #左边的表全部显示,右边没有用到不显示
    select * from 表名 rigth join 表名 on 表名.列名 = 表名.列名
    #右边的表全部显示,左边没关联的用null显示
    1. 外键

      constraint fk_userinfo_depart('约束名称') foreign key('外键') references 表名('关联的键')


      #不能将创建外键的语句单独拿出来,如果在创建完表以后想要加外键关系,使用下例
      alter table 表名 add constraint fk_userinfo_depart('约束名称') foreign key('外键') references 表名('关联的键')
      #删除
      alter table 表名 drop foreign key 外键名称

      '''
      ps:外键关联的时候,必须关联的是表的主键ID
        主键的作用:加速查找,不能为空,不能重复
      '''

      #唯一索引(联合唯一索引)
      unique :该列的值不能重复
      create table t(
      id int,
         num int
         unique(num) #unique(id,num)
      )engine = Innodb charset = utf8    
原文地址:https://www.cnblogs.com/zhuqihui/p/11019877.html