• mysql语法


    mysql知识点

    一、基本概念

    1、计算机:一种电子设备,能够进行数值、逻辑和储存记忆功能。
    2、数据:凡是能被计算机识别的一切功能(信息)
    3、数据库:储存数据的仓库
    优点:操作速度快 操作方便
    4、数据库的种类
    关系型数据库
    特点:用表储存数据 以行为单位操作数据(mysql、sqlServer、Oracle(甲骨文))
    非关系型数据库
    特点:以列为单位储存数据,以节点为单位(MongoDB)

    二、基本操作

    1、安装(serverOnly,密码建议设置root)
    注意事项:重新安装时候,必须要删除c盘Program Data隐藏文件夹中的mysql文件夹才能重新安装
    2、下载
    3、登陆 mysql -u root -p

    三、基本操作

    常见的约束
    not null:非空约束
    unique:唯一约束
    primary key:主键约束
    注意事项:
    主键约束并不是简单的非空和唯一的组合,他更重要的意义是唯一标识一行数据
    一张表中最多只有一个主键

    格式:insert into 字段名 values 值
    代码:insert into student values('s001','张三','男',12,'1999-3-23');

    格式:delete from 表名 where 条件;
    代码:delete from student where sid=’s002’;

    格式:update 表名 set 条件 where 条件;
    代码:update student set sname=’zhangsan’ where sid=’s004’;


    (一)、条件查询
    1、范围查询 and or between...and... in
    例:
    查询年龄在20~30岁之间的学生信息 and
    select * from student where sage>=20 and sage<=30;
    查询未成年人或超龄学生的信息 or
    select * from student where sage<=15 or sage>=30;
    另一种方式查询20~30之间 between...and...
    select * from student where sage between 20 and 30;
    查询1998年出生的学生信息
    select * from student where sbirthday between '1998-1-1' and '1998-12-31';
    select * from student where sbirthday between '1998-1-1' and '1998-12-31' order by sid;(默认:升序)
    select * from student where sbirthday between '1998-1-1' and '1998-12-31' order by sid desc(降序);
    查询学号为s002,s003,s004的学生信息 in
    select * from student where sid in ('s002','s003','s005');

    (二)、模糊查询(like)
    1、格式
    2、操作符:
    %:任意个字符
    _:一个任意字符
    例:
    查询姓名的最后一个字符是xx的学生信息
    select * from student where sname like '%八';
    查询姓名的第三个字符是xx的学生信息
    select * from student where sname like '__看%';
    查询姓名的倒数第二个字符是xx的学生信息
    select * from student where sname like '%三_';
    (三)、分组查询(group by)方便统计
    1、格式
    2、聚合函数(统计)
    sum(总和数) count(个数) avg(平均值)max(最大值)min(最小值)
    例:
    统计男女各多少人
    select ssex ,count(*) from student group by ssex;
    统计男女的 最大年龄 最小年龄 总年龄 平均年龄,要求取别名
    select ssex as '性别',max(sage) as '最大年龄', min(sage) as '最小年龄', sum(sage) as '总年龄',avg(sage) as '平均年龄' from student group by ssex;
    (四)、子查询
    1、概念:在查询中又出现了一个完全独立的查询
    例:
    查询根XX一样大的学生
    select * from student where sage=( select sage from student where sname='张三五');
    查询根xx同月出生的学生信息
    select * from student where YEAR(sbirthday)=(select YEAR(sbirthday) from student where
    sname='张五') and MONTH(sbirthday)=( select MONTH(sbirthday) from student where sname='张五');
    查询根xx同一天出生的学生信息
    select * from student where YEAR(sbirthday)=(select YEAR(sbirthday) from student where
    sname='张五') and MONTH(sbirthday)=( select MONTH(sbirthday) from student where sname='张五') and DAY(sbirthday)=(select DAY(sbirthday) from student where sname='张五');
    (五)、多表查询

    多表联合查询
    语法:select 字段名.from 表1.字段,表2.字段 [where 条件] [and 其他条件]
    例:

    select a.id,a.name,b.id,b.name from tb_demo1 as a,tb_demo2 as b where a.id=b.id and b.id='s003';

    查询人员和所有部门所有的信息
    select * from person,dept;
    [注意事项:]如果不加条件进行条件,则会出现以下效果“笛卡尔乘积” 公式:A表的数据*B表的数据=笛卡尔乘积

    多表链接查询

    语法:select 字段列表 from 表1 inner | left | right join 表2 on  表1.字段名 = 表2.字段名  [where 条件]

    on 后面接连接条件 right,left连接侧重点偏移

    select a.name,if(b.name,b.name,'无') from student as a left join class as b on a.id=b.od where a.name='张三' ;

    如果是多对多的关系 此时需要中间表 查询一样 依次连接表

  • 相关阅读:
    mysql存储过程
    命令简写 ~/.bash_aliases
    TestCafe 快速上手 (三)
    TestCafe 快速上手 (二)
    OWASP 文档
    读书笔记
    读书笔记
    类数组转化为真正的数组
    Vue子组件向父组件传递数据
    node.js取参四种方法req.body,req.params,req.param,req.body
  • 原文地址:https://www.cnblogs.com/2393920029-qq/p/12207459.html
Copyright © 2020-2023  润新知