MySQL实操

01MySQL常用命令的使用

  1. 显示数据库和表

    show databases;-- 显示当前所有数据库
    use databasesname;-- 选择指定数据库
    show tables;-- 显示当前数据库下所有表
    

  2. 查看表结构、版本、日期

    desc tablename;-- 查看表的描述
    -- describe tablenames; 查看表的描述
    show create table tables;-- 查看建表信息  (字符集、编码等)
    select version(),current_date();-- 显示当前MySQL数据库的版本和当前日期(自带的函数)
    select CURRENT_USER(),CURRENT_TIME(),CURRENT_TIMESTAMP();
    -- 结果为 root@localhost (账号的名称@授权的IP),21:40:21,2020-09-09 21:40:21
    

  3. 查看当前线程以及系统变量值

    show processlist;-- 显示MySQL哪些线程正在运行
    show variables like "%tx_isolation%";-- 查看指定系统变量的值 
    -- (tx_isolation数据库默认的隔离级别,like模糊查询)
    -- 全局变量  共享的一个变量 不可修改;会话变量  不同客户端不同 可修改
    

02MySQL DDL应用

DDL(Data Definition Language) 数据库核心语句 主要是对数据库对象进行管理,主要包含数据库、数据表、视图、存储过程、函数、触发器等
  1. DDL定义、创建、删除库

    -- DDL(Data Definition Language)数据库定义语言,定义了不同的数据段、数据库、表、列、索引等数据库对象  常用的关键字为create drop alter
    create database test1;-- 创建数据库test1
    drop database test1;-- 删除指定数据库test1
    

  2. 创建表、查看表的结构、alter表字段(改、增、删)

    create table emp(ename varchar(10),hiredata data,sal decimal(10,2),deptno int(2));-- 创建表
    desc emp;-- 查看表
    alter table emp modify ename varchar(20);-- 修改表字段长度
    alter table emp add column age int(3);-- 增加表字段
    alter table emp drop column age;-- 删除表字段
    

03MySQL DML应用

DML(Data Manipulation language)数据操作语言 主要是对数据库的数据进行管理,包含数据的增、删、改、查 并检查数据的完整性 常用关键字包含insert、delete、update、select
  1. 数据的增

    insert into emp(ename,hiredata,sal,deptno) values("汪芜",'2020-09-09',9000,1);-- 插入一条记录
    create table dept(deptno int(3),deptnoname varchar(20));-- 插入多条记录
    -- select * from dept;-- 查询表
    -- select *from emp;-- 查询表
    

  2. 数据的改

    update emp set sal=100000 where ename = '汪芜';-- 更新某个记录
    

  3. 数据的删

    delete from emp where ename = '汪芜';-- 删除某个记录
    truncate table emp;-- 截断表(会清除表内所有数据 自增长会从头开始;不支持数据回滚)
    delete from emp;-- 删除表内所有的数据 (自增长会接着之前的数据自增;支持数据回滚)
    
    alter table emp add column id int(2) auto_increment first;-- 添加一个字段  注意约束为主键才可以自增会,设置为第一列
    insert into emp(ename,hiredata,sal,deptno) values('章散','2020-09-09',9000,1);-- 插入数据  注意自增长不需要添加
    

  4. 数据的查

    select ename,hiredata from emp;-- 查询指定的列
    select distinct deptno from emp;-- 查询不重复的部门的记录
    

04MySQL select应用

主要时怎样对数据库的数据进行高效地检索,包含单表查询、多表关联的查询、多条件过滤查询、分组过滤查询、统计查询、多维度聚合查询等
  1. 多条件查询

    select * from emp where deptno=1;
    select * from emp where deptno=1 and sal=9000;-- 交
    select * from emp where deptno=1 or sal=9000;-- 或
    

  2. 排序

    select * from emp order by sal;-- 按照薪资由低到高排序
    select * from emp where deptno=1 order by sal;
    select * from emp order by sal desc;-- 按照薪资降序排列
    select * from emp order by sal desc limit 2;-- 按照薪资降序排列,且只取前两个
    select * from emp order by sal,deptno desc limit 2;-- 按照薪资升序,部门降序排列,且只取前两个
    

  3. 统计及分组统计

    select count(*) from emp;-- 包含所有的列,统计时不会忽略列值为null
    select count(列名) from emp;-- 只包含列名那一列,统计时会忽略列值为null
    -- select count(1) from emp;
    -- select count(0) from emp;
    select count(*) as empnum from emp;-- 将count(*)字段重命名,as可省略
    
    select deptno,count(*) empnum from emp group by deptno;-- 按照部门号分组统计
    

  4. 分组统计+条件过滤

    select deptno,count(*) as empnum from emp group by deptno with rollup;-- 既要统计各部门人数又要统计总人数 
    select deptno,count(*) as empnum from emp group by deptno having empnum>1;-- 统计人数大于1的部门
    

  5. 聚合函数与多表关联查询

    select sum(sal),max(sal),min(sal) from emp;-- 统计所有员工的薪水总额、最高和最低薪水
    select ename,deptnoname from emp,dept where emp.deptno=dept.deptno;-- 查询所有员工的名字和所在的部门的名称
    select ename,deptnoname from emp e join dept d on e.deptno=d.deptno;-- 同上   查询所有员工的名字和所在的部门的名称 
    

猜你喜欢

转载自blog.csdn.net/qq_45063518/article/details/108540772
今日推荐