• MySQL总复习


    目录

    登录

    显示数据库 

    创建数据库 

    删除数据库

    使用数据库

    创建表

    添加数据表数据

    查询表

    添加数据表多条数据

    查询表中某数据

    增insert 删delete 改update 查select

    where 

    like 

    ​编辑 

    范围查找

     order by

    聚合函数 

    count

     max

    min    sum    avg

    group by

    having

    limit

     空判断

    视图

    存储


    登录

    mysql -u root -p

    显示数据库 

    show databases; 

     

    创建数据库 

    craate database <数据库名> ;

    删除数据库

     drop database <数据库名> ;

     

    使用数据库

     use  <数据库名>

    创建表

    primary key   主键

    not null 非空

    unique 惟一

    default 默认值

    auto_increment 自增长

    int unsigned 无符号整数

    1. DROP TABLE IF EXISTS staffer;
    2. CREATE TABLE staffer (
    3. id int NOT NULL AUTO_INCREMENT COMMENT '员工编号',
    4. dept_id int NOT NULL COMMENT '部门编号',
    5. staff_name varchar(10) NOT NULL COMMENT '员工名字',
    6. sex enum('F','M') DEFAULT 'F' COMMENT '性别',
    7. birthday date COMMENT '生日',
    8. phone char(11) COMMENT '电话',
    9. salary decimal(11,2) UNSIGNED DEFAULT 1.00 COMMENT '工资',
    10. staff_memo varchar(100) COMMENT '备注',
    11. PRIMARY KEY (id),
    12. FOREIGN KEY (dept_id) REFERENCES department (id),
    13. CHECK ((salary > 0) and (salary < 100000))
    14. ) AUTO_INCREMENT=10512;
    1. DROP TABLE IF EXISTS item ;
    2. CREATE TABLE item(
    3. item_id int AUTO_INCREMENT,
    4. order_id int,
    5. goods_id int,
    6. quantity int,
    7. total_price decimal(11, 2),
    8. PRIMARY KEY ( item_id ),
    9. FOREIGN KEY ( goods_id ) REFERENCES goods ( goods_id ),
    10. FOREIGN KEY ( order_id ) REFERENCES orders ( order_id )
    11. );

    添加数据表数据

    1. INSERT INTO staffer VALUES (10501, 1, '燕南飞', 'M', '1995-09-18', '13011231890', '5000.10', '职员');
    2. INSERT INTO staffer VALUES (10502, 2, '陈一南', 'M', '1990-09-12', '13011233333', '6000.10', '职员');
    3. INSERT INTO staffer VALUES (10503, 4, '李思思', 'F', '1979-11-01', null, '9900.00', '总经理');
    4. INSERT INTO staffer VALUES (10504, 1, '张燕红', 'F', '1985-06-01', '13566567456', '8000.00', '部门经理');
    5. INSERT INTO staffer VALUES (10505, 3, '南海峰', 'M', '1986-04-01', null, '7000.00', '职员');
    6. INSERT INTO staffer VALUES (10506, 3, '张红燕', 'F', '1982-09-21', '13823671111', '9000.00', '部门经理');
    7. INSERT INTO staffer VALUES (10507, 2, '王南峰', 'M', '1986-04-01', '13668992222', '7000.00', '职员');
    8. INSERT INTO staffer VALUES (10508, 5, '刘燕玲', 'F', '1981-07-01', '13823679988', '6000.00', '职员');
    9. INSERT INTO staffer VALUES (10509, 5, '李玉燕', 'F', '1984-02-08', '13823677889', '9000.00', '部门经理');
    10. INSERT INTO staffer VALUES (10510, 4, '王树思', 'M', '1996-04-01', '13668998888', '7000.00', '职员');
    11. INSERT INTO staffer VALUES (10511, 1, '思灵玉', 'F', '1992-03-01', '13823679999', '6000.00', '职员');
    1. INSERT INTO item VALUES (1, 1, 1, 20, NULL);
    2. INSERT INTO item VALUES (2, 1, 2, 2, NULL);
    3. INSERT INTO item VALUES (3, 1, 3, 3, NULL);
    4. INSERT INTO item VALUES (4, 2, 1, 7, NULL);
    5. INSERT INTO item VALUES (5, 2, 2, 5, NULL);
    6. INSERT INTO item VALUES (6, 2, 3, 6, NULL);
    7. INSERT INTO item VALUES (7, 3, 1, 10, NULL);
    8. INSERT INTO item VALUES (8, 3, 4, 10, NULL);
    9. INSERT INTO item VALUES (9, 4, 1, 6, NULL);
    10. INSERT INTO item VALUES (10, 4, 2, 3, NULL);
    11. SET FOREIGN_KEY_CHECKS = 1;

    查询表

    select * from staffer;

    添加数据表多条数据

    一次性在department表中添加2条记录,分别为:(部门名称:市场部;电话:020-87993093)、

    (部门名称:宣传部;电话:020-87993065)

    1. select * from department;
    2. insert into department (dept_name,dept_phone) values ('市场部','020-87993093'),('宣传部','020-87993065');
    3. select * from department;

    查询表中某数据

    在goods表中查询各商品的3件费用

    -- 3件费用是商品单价*3的价格,可使用表达式计算

    select goods_name,unit_price,unit_price*3 as '3件费用' from goods_; 

    查询goods_表第一行开始的2条记录

    select * from goods_ limit 1,2;

    查询staffer表中姓张的员工,并显示其staff_id,staff_name,deptartment_id,sex

    select staff_id,staff_name,deptartment_id,sex from staffer where staff_name like '张%';

     item 表,按商品和供应商分组,查询各商品的各供应商提供的销售数量和总数量

    -- 分析:当汇总有2个以上字段是需要用with rollup 子句才能达到按从左到右分类的目的

    1. select * from item;
    2. select goods_id,order_id,sum(quantity) from item group by goods_id,order_id with rollup;

    增insert 删delete 改update 查select

     

     

     

    where 

    select 后面的"或者字段名,决定了返回什么样的字段(列):
    select 中 where 子句,决定了返回什么样的记录(行);

     

    like 

    范围查找

     order by

    当一条select语句出现了where和order by

    select * from 表名 where 条件 order by 字段1,字段2;

    一定要把where写在order by前面

    asc 从小到大 可以省略
    desc 从大到小
    默认从小到大 (asc)

     

    聚合函数 

    count

     max

    min    sum    avg

    group by

    select 聚合函数 from 表名 where 条件 group by 字段
    select 聚合函数 from 表名 group by 字段
    group by就是配合聚合函数使用的

    where和group by 和order by的顺序:
    select * from 表名 where 条件 group by 字段 order by 字段 

    having

    having子句
    总是出现在group by之后
    select * from 表名 group by 字段 having 条件

    对比 where 与 having:
    where 是对 from 后面指定的表进行数据筛选属于对原始数据的筛选having 是对 group by 的结果进行筛选;
    having 后面的条件中可以用聚合函数,where 后面的条件不可以使用聚合函数。

    having语法:
    select 字段1,字段2,聚合...from 表名group by 字段1,字段2,字段3...
    having 字段1,...聚合...

    limit

     空判断

     null不是0,也不是",null在SQL里面代表空,什么也没有
    null不能用比较运算符的判断
    is null ---是否为null
    is not null ---是否不为null
    (不能用字段名 = null 字段名 != null这些都是错误的)

    视图

    建立部门员工视图,显示部门名称和员工对外查阅资料  | staffer 表和 department 表

    1. select * from staffer;
    2. select * from department;
    3. drop view if exists v_staffer;
    4. create view v_staffer
    5. as select dept_name,staff_name,sex,phone from staffer
    6. inner join department on staffer.deptartment_id=department.dept_id;
    7. select * from v_staffer;

    存储

    创建带入in 参数和输出 out 参数的存储过程

    -若需要从存储过程中返回一个或多个检索或统计的值,则可以使用代out关键字定义的输出参数,将返回值传回调用环境

    在数据库db_shop中建立一个存储过程,能够通过部门编号统计该部门员工的人数,返回统计值,并调佣该存储过程

    --  分析:部门编号是输入参数,统计的员工人数是输出参数

    1. drop procedure if exists p_count;
    2. delimiter //
    3. create procedure p_count(in id int,out n int)
    4. BEGIN
    5. select count(*) into n from staffer where deptartment_id=id;
    6. end //
    7. delimiter ;
    8. call p_count(1,@a);
    9. select @a as '1号部门员工数';

    编写一个函数,可按职员编号查询员工姓名。

    1. use db_shop;
    2. -- 创建函数时,mysql 默认开启了bin-log,因含有sql语句会出错,则需参加
    3. drop function if exists staffer_search;
    4. set global log_bin_trust_function_creators = 1;
    5. delimiter //
    6. create FUNCTION staffer_search(sid int)
    7. returns varchar(10)
    8. begin
    9. declare sname varchar (10);
    10. select staff_name into sname from staffer where deptartment_id=sid;
    11. if isnull(sname) then
    12. return '无人';
    13. else
    14. return sname;
    15. end if;
    16. END //
    17. delimiter ;
    18. set @sname = staffer_search(6);
    19. select @sname,staffer_search(7);

  • 相关阅读:
    项目成本管理
    ComfyUI搭建
    初试 jmeter做压力测试
    1-字典树-实现 Trie (前缀树)
    虚拟机配置spark
    优先级队列 —— 堆
    大一学生Web课程设计 红酒美食主题网页制作(HTML+CSS+JavaScript)
    PAT甲级:1043 Is It a Binary Search Tree
    Spring基础(二):IOC概念引入
    【课上笔记】第八章 图
  • 原文地址:https://blog.csdn.net/m0_74261473/article/details/132626816