zoukankan      html  css  js  c++  java
  • mysql02

    一、约束
    1)主键 主键约束=非空约束+唯一约束 主键可以是一个列或者多个列

    1)创表添加多个列作为主键
    create table score1(
    sid int not null,
    cid int not null,
    PRIMARY key(sid,cid)
    );
    2)删除主键约束
    alter table 表名 drop primary key
    3)增加主键约束
    alter table 表名 add primary key(字段名)

    2)外键 跟其他表建立关联的字段
    1)添加外键约束:
    alter table 表名 add foreign key(外键字段) references 关联表(关联字段)
    2)创建表添加约束
    创表语句后面 FOREIGN KEY (外键字段) REFERENCES 关联表(关联字段)

    3)删除外键约束
    alter table 表名 drop foreign key 外键名
    (如何知道外键名?
    show create table 表名;可以看到外键名)


    3)唯一(unique) 值是唯一的,但是可以为空
    1)创建表时添加唯一约束
    create table s1(id int PRIMARY key auto_increment,
    sname VARCHAR(20) UNIQUE);

    2)删除唯一约束
    alter table 表名 drop index 字段名;

    3)添加唯一约束
    alter table 表名 add unique(字段名)

    4)默认
    1)创建添加默认值 default 默认值
    create table s2(id int PRIMARY key auto_increment,
    sname VARCHAR(20) UNIQUE,
    sex char(2) default '男'

    );
    insert into s2(sname)VALUES('feng');
    select * from s2;
    2)删除默认约束
    alter table 表名 alter 列名 drop default
    3)添加默认值约束
    alter table 表名 alter 列名 set default 默认值

    5)非空 not null
    1)创建表添加非空约束
    -- create table score1(
    -- sid int not null,
    -- cid int not null,
    -- PRIMARY key(sid,cid)
    -- );

    2)取消非空约束
    alter table 表名 modify 字段名 类型
    案例:alter table score1 modify sid int ;
    3)添加非空约束
    alter table 表名 modify 字段名 类型 not null;
    案例:alter table score1 modify sid int not null ;

    6)检查约束(check) mysql中检查约束是没有

    二、查看数据表信息
    1)查看表结构
    desc 表名;
    2)查看表的详细信息
    show create table 表名;

    3)删除表(慎重) 表结构、索引、约束以及数据都会删除
    drop table 表名

    4)表结构修改 alter table 表名

    1)删除字段

    alter table 表名 drop 字段名

    alter table s2 drop sex;

    2)添加字段
    alter table 表名 add 新字段 类型;

    alter table s2 add sex1 enum('男','女');
    desc s2;

    3)修改字段名
    alter table 表名 change 旧字段 新字段 类型;

    alter table s2 change sex1 sex enum('男','女');
    desc s2;

    4)修改字段类型
    alter table 表名 modify 字段 新数据类型

    alter table s2 modify sex char(2);
    desc s2;


    5)修改表名

    alter table 表名 旧表名 rename 新表名

    alter table s2 rename s22;
    desc s22;
    三、数据操作语句

    1)增加
    1)指定部分字段插入:insert into 表名(字段列表) values(值列表);
    2)所有字段赋值 :insert into 表名 values(值列表);
    3)一次性添加多条记录
    insert into 表名(字段列表) values(值列表1),(值列表2).....;

    insert into student(sname) values('tifa');
    insert into student values(5,'幽思寒蝉');
    insert into student values(6,'y'),(7,'越幸运'),(8,'枫');
    select * from student;

    2)删除数据

    1)delete from 表 [ where 条件 ]

    2)truncate table 表名

    3)两者的区别
    1)delete 后面可以带上where 而truncate不可以

    2)delete可以回滚,而truncate不可回滚,并且truncate删除速度比delete要快

    3)修改

    update 表名 set 字段名1=字段值,字段名2=字段值 where 条件

    update student set sname='tifa' where sid=2;
    select * from student;

    4)查询 select

    1)查询整个表的数据
    select 字段1,字段2[*] from 表名

    2)筛选表中的记录,用到where查询

    条件结构: 表达式/字段名 比较运算符 值

    比较运算符:> < = != >= <=

    select 字段1,字段2[*] from 表名 where 条件1 and/or 条件2 and/or 条件3

    and : 结果集同时满足所有条件
    or: 结果集只要满足其中一个条件即可

    优先级从高到低依次为 () and or

    select * from student where sname='y' or sid=7


    select 字段1,字段2[*] from 表名 where 条件1 OR 条件2 AND 条件3

    select 字段1,字段2[*] from 表名 where 条件1 OR (条件2 AND 条件3)


    3)特殊比较运算符

    1)like 看起来像 模糊查询 当知晓某个字段部分值

    匹配符:% 匹配0或者多个任意字符
    _ 占位
    __

    1)查询名称中包含a的学员信息
    select * from student where sname like '%a%';
    2)查询名称中首字母是a的学员信息
    select * from student where sname like 'a%';
    3)查询第三个字符是f的学员信息
    select * from student where sname like '__f%';

    2)between...and... 在..之间 包含边界值

    select * from score where score BETWEEN 90 and 100;

    3)in(1或者多个值) 在..里面
    select * from student where sid in(1,2,5);

    4)is null 是空 null不等于空格

    is not null 不是空

    select * from student where sid is not null;

    5)not 非 一般情况跟 not like, not between..and not in 一起使用
    select * from student where sname not like '%a%';
    select * from score where score not BETWEEN 90 and 100;
    select * from student where sid not in(1,2,5);

    4)limit 读取前几行
    -- select * from emp limit 偏移数,N行 注意:偏移数默认是0,可以忽略不写
    select * from emp limit 0,5;
    select * from emp limit 5;
    -- 需要展示第二页的数据,每页展示五行 6~10
    select * from emp limit 5,5;

    5)distinct 去重复

    --查询有选修课的学生sid
    select distinct sid from score

    6)排序 order by 放在sql语句的最后


    语法:select 字段1,字段2 from 表名 where 条件1 and/or 条件2
    order by 字段1 desc/asc, 字段2 desc/asc ...

    排序规则:desc 降序(从大到小) asc 升序 默认按升序排列

    select * from score order by score desc;
    select * from score order by score asc;
    select * from score order by score ;

    5)分组 涉及统计就用分组

    1)自动分组 作用于一组数据返回一个结果
    分组函数:
    max() 求最大
    min() 求最小值
    avg() 求平均值
    sum() 求和
    count()求个数

    --查询选修成绩最高成绩,总成绩,平均成绩,最低成绩的分数
    select max(score),sum(score),avg(score),min(score) from score;

    2)手动分组 group by

    难点1)什么情况下用到手动分组group by
    涉及到多组数据的统计

    2)根据什么字段来分组
    把具有相同字段的值归于一组,就根据这个字段分组

    3)手动分组sql语句中,除分组函数之外group by 后面有的字段,select后面才可以有

    注意:多个列进行分组,先后顺序不影响结果集

    2)语法:select 字段1,字段2 from 表名 where 条件1 and/or 条件2
    group by 分组字段1,分组字段1


    3)手动分组分为单列分组和多列分组

    4)单列分组
    --查询每个学员的最高成绩
    select sid,max(score) from score group by sid ;
    select sid,max(score),min(score) from score group by sid ;

    5)多列分组
    -- 查询每个学员每门科目最高成绩 --涉及统计用到分组
    select sid,cid,max(score) from score group by sid,cid

    6)对分组后数据筛选 having
    语法:
    select 字段1,字段2 from 表名 where 条件1 and/or 条件2
    group by 分组字段1,分组字段1
    having 条件1 and/or 条件2

    注意:1)包含分组函数的条件只能放在having 后面,其他条件都可以放在where后面

    -- --查询每个学员最高成绩大于95的学生编号
    select sid from score group by sid
    having max(score)>95;
    6)子查询
    求未知值的情况下,一般用到子查询
    特点:1)子查询包含()
    2)先执行子查询(括号里面的sql),再执行括号外面的sql语句(主查询)

    子查询分为单行子查询和多行子查询

    1)单行子查询:子查询返回单行多列或者单行多列数据 跟> < = >= <=一起使用

    -- --查询最高成绩sid

    select sid from score where score=(select max(score) from score);
    select sid from score where score=98;

    2)多行子查询:子查询返回多行多列或者多行单列的数据

    -- 查询全部及格的学生信息(名称编号)
    select sname,sid from student where
    sid in(select sid from score group by sid having min(score)>=60) ;

    3)exists 存在

    select sname,sid from student
    where exists( select sid from score where score.sid=student.sid
    group by sid having min(score)>=60 )








  • 相关阅读:
    还在使用golang 的map 做Json编码么?
    Golang 性能测试(2) 性能分析
    golang 性能测试 (1) 基准性能测试
    消息队列 NSQ 源码学习笔记 (五)
    消息队列 NSQ 源码学习笔记 (四)
    消息队列 NSQ 源码学习笔记 (三)
    消息队列 NSQ 源码学习笔记 (二)
    消息队列 NSQ 源码学习笔记 (一)
    你不知道的空格
    Supervisor 使用和进阶4 (Event 的使用)
  • 原文地址:https://www.cnblogs.com/Murraya/p/12996640.html
Copyright © 2011-2022 走看看