— show databases : 展示所有库列表
    show databases;
    — use 库名 : 使用哪个库
    use ruizhi;
    — show tables : 展示库中所有的表
    show tables;
    — desc:展示表结构
    desc t_user;

    1. SQL语句
    2. 数据库
    3. 创建数据库
    4. 删除数据库
    5. 创建表
    6. 修改表
    7. 删除表
    8. 导入/导出
    9. 数据:CURD 增删改查
    10. 新增
    11. 删除
    12. 修改
    13. 查询数据
    14. 简单查询 单值查询 多值查询 匹配where查询
    15. limit分页查询
    16. 排序
    17. 分组查询
    18. 分组筛选
    19. 关联查询
    20. 子查询
    21. 内连接
    22. 左连接(用ORM框架才能体会到,JDBC操作左连接起来都比较累)
    1. <br />--------------------------------------------------------------------<br />-- 数据库名字:英文 数值 下划线 组合<br />-- if not exists:可选的 如果不存在则创建<br />create database if not exists day0408;

    — 1007:错误码 MySQL错误码都是固定的 可以通过错误码去查阅文档
    create database day0408;
    — ERROR 1007 (HY000): Can’t create database ‘day0408’; database exists

    — 指定编码

    1. CREATE DATABASE IF NOT EXISTS mall2
    2. DEFAULT CHARACTER SET 'utf8'
    3. DEFAULT COLLATE 'utf8_general_ci';

    — 删除数据库
    DROP DATABASE if exists mall2;

    — 使用数据库
    use ruizhi2;

    —创建表
    — 表名: 英文 数字 下划线 组合
    — 规范: 项目名称_表名称 为了不冲突
    — id一般都是Java中 Long MySQL BIGINT
    — id一般都是不为空 not null:不为空
    — null:可以为空
    — primary key:代表主键 唯一 不为空
    — AUTO_INCREMENT:自增 类似于++ 每次加1
    — varchar:Java中的String 可变字符串 使用多少开辟多大空间 类似于Java中可变数组
    — COMMENT:注释

    1. create table if not exists t_user(
    2. id bigint(10) not null AUTO_INCREMENT primary key COMMENT 'ID主键',
    3. user_name varchar(50) null COMMENT '用户名',
    4. password varchar(50) DEFAULT '123456' COMMENT '密码 默认值123456'
    5. );
    1. drop table if exists t_user;
    1. -- PRIMARY KEY (id):选择哪个列作为主键
    2. create table if not exists t_goods(
    3. id bigint(10) not null AUTO_INCREMENT COMMENT 'ID主键',
    4. goods_name varchar(100) null COMMENT '商品名称',
    5. goods_price NUMERIC(8) null COMMENT'商品名称',
    6. PRIMARY KEY (id)
    7. );

    主键、外键都是表中数据的约束
    主键:唯一不重复的
    当主键重复会有提示
    外键:两个表建立关键关系

    购物:
    User
    OrderForm

    Goods

    user(
    id,
    name,
    pwd
    )

    orderform(
    id,
    addTime,
    user_id :代表这一行数据属于User表中这个id的
    )

    1. create table t_user(
    2. id bigint(10) primary key,
    3. user_name varchar(20)
    4. );
    5. create table t_orderform(
    6. id bigint(10) primary key,
    7. orderform_name varchar(10) null,
    8. user_id bigint(10)
    9. );
    1. -- 没有添加外键之前数据没有约束 删除的时候没有提示
    2. -- 需要在主数据删除的时候 从数据也删除
    3. -- 如果主数据删除 从数据没有删除 就出现了多余的数据 -----数据冗余
    4. -- FK_ID:外键名字
    5. -- 添加外键需要注意:主表有关联数据
    6. ALTER TABLE 表名 add constraint FK_ID foreign key(外键字段名)
    7. REFERENCES 外表表名(对应的表的主键字段名);
    8. alter table t_orderform add constraint fk_user_id foreign key(user_id)
    9. REFERENCES t_user(id);
    10. -- FOREIGN KEY fk_user_id:外键名称 (外键列)
    11. -- references 主表(主表中的列)
    12. create table t_orderform(
    13. id bigint(10) primary key,
    14. orderform_name varchar(50),
    15. user_id bigint(10),
    16. FOREIGN KEY fk_user_id(user_id)
    17. references t_user(id)
    18. );

    QQ图片20220408104655.png

    1. -- 查看表关系
    2. desc t_orderform;

    外键带来的优势:数据约束

    外键带来的问题:
    删除主表数据,需要先删除子表数据,一般我们开发中主、子表关联关系会非常复杂,如果每次删除数据,都去
    找子表删除数据,会非常麻烦。
    在真实开发中为了省事,不去使用外键,主表数据删除,子表数据就让他存在———数据冗余(垃圾数据)

    数据冗余(垃圾数据)只是占用空间,其他都不影响

    500G
    几亿条数据压缩之后可能就几十M

    1. create table t_address(
    2. id bigint(10) auto_increment primary key,
    3. address varchar(50) not null default '未设置名字',
    4. code int(5) default 0
    5. );

    数据库引擎:
    MySQL有很多,常见的就是InnoDB、MyISAM
    InnoDB:支持事务,同时成功或者同时失败,增删改查都可以的,效率比较低,因为需要操作事务
    MyISAM:不支持事务,适合于:查询多,插入少
    MEMORY:内存引擎,数据存在内存中,使用完成后就清除掉了

    每个数据引擎都是一种数据结构,不同的数据结构查询效率不一样
    InnoDB:B树
    主要是体现在查询上面,数据量多的情况就要看哪个数据结构比较合适

    1. -- 给表添加字段
    2. ALTER TABLE <表名> ADD <新字段名> <数据类型>;
    3. alter table t_user add password varchar(50);
    4. -- 修改字段数据类型
    5. ALTER TABLE <表名> MODIFY <字段名> <数据类型>;
    6. alter table t_user modify password int(10);
    7. alter table t_user modify password varchar(100);
    8. -- 删除字段
    9. ALTER TABLE <表名> DROP <字段名>;
    10. alter table t_user drop password;
    11. alter table t_user drop;
    12. -- 修改字段名称
    13. ALTER TABLE <表名> CHANGE <旧字段名> <新字段名> <新数据类型>;
    14. alter table t_user change user_name userName varchar(50);
    15. -- 修改表名称
    16. ALTER TABLE <旧表名> RENAME [TO] <新表名>;
    17. alter table t_user rename to t_emp;
    1. -- 添加外键:
    2. -- 学生
    3. create table t_student(
    4. id bigint(10) auto_increment primary key,
    5. stu_name varchar(50) null
    6. );
    7. -- 课程
    8. create table t_class(
    9. id bigint(10) auto_increment primary key,
    10. class_name varchar(20) null,
    11. student_id bigint(10),
    12. CONSTRAINT fk_student_id FOREIGN KEY (student_id )
    13. references t_student(id)
    14. );
    1. ALTER [IGNORE] TABLE tbl_name DROP FOREIGN KEY fk_symbol
    2. alter table t_class drop FOREIGN KEY fk_student_id(student_id);
    3. alter table t_class drop foreign key t_class_ibfk_1;
    1. ALTER TABLE table 表名 add constraint FK_ID foreign key(外键字段名) REFERENCES 外表表名(对应的表的主键字段名);
    2. Alter table
    3. ALTER TABLE orderitems ADD CONSTRAINT
    4. fk_orderitems_orders FOREIGN KEY (order_num) REFERENCES orders (order_num);

    添加外键两种方式:
    1、创建表的时候添加
    2、修改表结构的形式:alter table。。。,可以设置外键名字

    1. create database company;
    2. -- 导入数据 不需要登录进去
    3. -- mysql -u用户名 -p 数据库名 < 数据库名.sql
    4. mysql -u root -p company < d:/sql/create.sql
    5. -- mysqldump -u [数据库用户名] -p [要备份的数据库名称]>[备份文件的保存路径]
    6. mysqldump -u root -p company > d:/sql/company.sql
    1. INSERT INTO table_name ( field1, field2,...fieldN )
    2. VALUES
    3. ( value1, value2,...valueN );
    4. -- 一种是列全部加上
    5. insert into t_emp(id,userName) values(null,'张三');
    6. -- 不写列 让值默认去对应
    7. insert into t_emp values(null,'小张同学');
    1. -- 批量插入:插入多行数据
    2. insert into t_emp(id,userName) values
    3. (null,'A'),
    4. (null,'B'),
    5. (null,'D'),
    6. (null,'C'),
    7. (null,'E');
    1. -- 从另一张表查询出来结果 再去插入
    2. INSERT INTO table_name(column_list)
    3. SELECT
    4. select_list
    5. FROM
    6. another_table;
    7. create table t_emp_plus(
    8. id bigint(10) auto_increment primary key,
    9. userName varchar(50) null
    10. );
    11. -- 数据拷贝:从一张表向另一张表拷贝数据;从t_empt_emp_plus插入数据
    12. insert into t_emp_plus(id,userName) select id,userName from t_emp;
    1. UPDATE table_name SET field1=new-value1, field2=new-value2
    2. [WHERE Clause]
    3. update t_emp set userName='小强';
    4. update t_emp set userName='小张' where id = 2;
    5. update emp set ename='小黑',job='开发人员',sal=3000 where empno = 7369 or empno = 7521 ;
    1. DELETE FROM table_name [WHERE Clause]
    2. -- 删除需要慎重
    3. delete from t_emp;
    4. delete from t_emp_plus where userName='张三';
    5. delete from t_emp_plus where id > 5;
    6. --:> < != <>
    1. --查询:检索
    2. SELECT 1,列2,列3 FROM table_name [WHERE Clause];
    3. -- *:代表所有列都搜索出来
    4. select * from emp;
    5. --指定列
    6. select empno,ename,sal from emp;
    7. -- 薪水>
    8. select empno,ename,sal from emp where sal>=3000 and sal<5000;
    1. SELECT
    2. column1,column2,...
    3. FROM
    4. table
    5. LIMIT offset , count;
    6. --offset:其实位置 0开始
    7. --count:返回多少条数据
    8. -- 0位置 返回5条数据
    9. select * from emp limit 0,5;
    10. select * from emp limit 5,5;
    11. select * from emp limit 10,5;
    12. -- 分页
    13. -- pageNo->1 2
    14. -- pageSize:5
    15. select * from emp limit (pageNo-1) * pageSize,pageSize;
    16. select * from emp limit (pageNo-1) * pageSize,pageSize;
    17. 1->(1-1) * 5 : 0
    18. 2->(2-1) * 5 : 5
    19. 3->(3-1) * 5 : 10
    20. SELECT
    21. column1,column2,...
    22. FROM
    23. table
    24. LIMIT count;
    25. select * from emp limit 10;
    1. +-------+--------+-----------+------+------------+---------+---------+--------+
    2. | empno | ename | job | mgr | hiredate | sal | comm | deptno |
    3. +-------+--------+-----------+------+------------+---------+---------+--------+
    4. | 7369 | 小黑 | 开发人员 | 7902 | 1980-12-17 | 3000.00 | NULL | 20 |
    5. | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
    6. | 7521 | 小黑 | 开发人员 | 7698 | 1981-02-22 | 3000.00 | 500.00 | 30 |
    7. | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
    8. | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
    9. | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
    10. | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
    11. | 7788 | SCOTT | ANALYST | 7566 | 1987-07-13 | 3000.00 | NULL | 20 |
    12. | 7839 | KING | PRESIDENT | NULL | 1981-11-07 | 5000.00 | NULL | 10 |
    13. | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
    14. | 7876 | ADAMS | CLERK | 7788 | 1987-07-13 | 1100.00 | NULL | 20 |
    15. | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
    16. | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
    17. | 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
    18. +-------+--------+-----------+------+------------+---------+---------+--------+
    1. SELECT column1, column2,...
    2. FROM tbl
    3. ORDER BY column1 [ASC|DESC], column2 [ASC|DESC],...
    4. --asc:正序 desc:逆序
    5. select * from emp order by empno asc;
    6. select deptno,empno,ename from emp order by deptno asc;
    7. -- 前面根据部门编号排序后 再根据员工编号排序
    8. select deptno,empno,ename from emp order by deptno asc,empno desc;
    9. -- 多列排序 前面先排序好之后 再去排序后面字段列
    10. -- 如果order by 字段 后面没有写asc/desc 默认是正序排序
    11. select deptno,empno,ename from emp order by deptno;
    1. -- 排序和limit查询最大值、最小值
    2. limit 1
    3. select * from emp order by sal desc limit 1;
    4. select * from emp order by sal asc limit 1;
    1. -- where 条件
    2. SELECT
    3. select_list
    4. FROM
    5. table_name
    6. WHERE
    7. search_condition;
    8. -- =判断
    9. select * from emp where ename = '小黑';
    10. -- 不等于判断:!=、<>
    11. select * from emp where ename != '小黑';
    12. select * from emp where sal <> 5000 order by sal desc;
    13. -- BETWEEN AND 两边的值都是带上的
    14. select * from emp where sal between 3000 and 5000;
    15. -- 部门1020 薪水最高、最低的人
    16. select * from emp where deptno=10 order by sal asc limit 1;
    SELECT子句及其顺序
    SELECT 要返回的列或表达式
    FROM 从中检索数据的表
    WHERE 行级过滤
    ORDER BY 输出排序顺序
    LIMIT 要检索的行数
    1. -- 为空null判断:is null,不为空判断:is not null
    2. select * from emp where comm is null;
    3. select * from emp where comm is not null;
    1. -- and 薪水在2000 3000之间
    2. select * from emp where sal > 2000 and sal<=3000;
    3. -- 部门20 薪水在2000 3000之间
    4. select * from emp where deptno = 20 and (sal > 2000 and sal <= 3000);
    5. -- 名字叫 小黑 comm不为空
    6. select * from emp where ename='小黑' and comm is not null;
    7. -- 搜索部门10 或者 20
    8. select * from emp where deptno=10 or deptno=20;
    9. -- 部门10 20 中薪水最高的人
    10. select * from emp where deptno = 10 or deptno =20 order by sal desc;
    11. -- 当我们的WHERE SQL容易产生歧义 我们可以通过加() 分开计算
    12. select * from emp where deptno =10 and deptno=20 or sal < 3000;
    13. --empno 73697654 薪水 < 3000
    14. select * from emp where (empno < 7654 and empno > 7369) and sal < 3000;
    1. 员工号 769877827839,并且薪资小于3000
    2. 拿到薪资大于3000,或者薪资小于2000
    3. select * from emp where (empno=7698 or empno=7782 or empno = 7839) and sal <3000;
    4. mysql> select * from emp where empno=7698 or empno=7782 or empno = 7839 and sal > 3000;
    5. +-------+-------+-----------+------+------------+---------+------+--------+
    6. | empno | ename | job | mgr | hiredate | sal | comm | deptno |
    7. +-------+-------+-----------+------+------------+---------+------+--------+
    8. | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
    9. | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
    10. | 7839 | KING | PRESIDENT | NULL | 1981-11-07 | 5000.00 | NULL | 10 |
    11. +-------+-------+-----------+------+------------+---------+------+--------+
    12. 3 rows in set (0.00 sec)
    13. mysql> select * from emp where (empno=7698 or empno=7782 or empno = 7839) and sal > 3000;
    14. +-------+-------+-----------+------+------------+---------+------+--------+
    15. | empno | ename | job | mgr | hiredate | sal | comm | deptno |
    16. +-------+-------+-----------+------+------------+---------+------+--------+
    17. | 7839 | KING | PRESIDENT | NULL | 1981-11-07 | 5000.00 | NULL | 10 |
    18. +-------+-------+-----------+------+------------+---------+------+--------+
    19. 1 row in set (0.00 sec)
    20. select * from emp where sal > 3000 or sal < 2000;
    1. select * from emp where (empno=7698 or empno=7782 or empno = 7839) and sal <3000;
    2. select * from emp where empno in (7698,7782,7839) and sal < 3000;
    3. select * from emp where ename in ('小黑','JONES');
    4. select * from emp where ename = '小黑' or ename='JONES';
    5. --inor更加便捷
    1. mysql> select * from dept;
    2. +--------+------------+----------+
    3. | deptno | dname | loc |
    4. +--------+------------+----------+
    5. | 10 | ACCOUNTING | NEW YORK |
    6. | 20 | RESEARCH | DALLAS |
    7. | 30 | SALES | CHICAGO |
    8. | 40 | OPERATIONS | BOSTON |
    9. +--------+------------+----------+
    10. mysql> select * from emp;
    11. +-------+--------+-----------+------+------------+---------+---------+--------+
    12. | empno | ename | job | mgr | hiredate | sal | comm | deptno |
    13. +-------+--------+-----------+------+------------+---------+---------+--------+
    14. | 7369 | 小黑 | 开发人员 | 7902 | 1980-12-17 | 3000.00 | NULL | 20 |
    15. | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
    16. | 7521 | 小黑 | 开发人员 | 7698 | 1981-02-22 | 3000.00 | 500.00 | 30 |
    17. | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
    18. | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
    19. | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
    20. | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
    21. | 7788 | SCOTT | ANALYST | 7566 | 1987-07-13 | 3000.00 | NULL | 20 |
    22. | 7839 | KING | PRESIDENT | NULL | 1981-11-07 | 5000.00 | NULL | 10 |
    23. | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
    24. | 7876 | ADAMS | CLERK | 7788 | 1987-07-13 | 1100.00 | NULL | 20 |
    25. | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
    26. | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
    27. | 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
    28. +-------+--------+-----------+------+------------+---------+---------+--------+
    29. 14 rows in set (0.00 sec)
    30. -- 筛选部门 ACCOUNTING 或者 SALES 中的员工出来
    31. select * from emp
    32. where deptno in ( select deptno from dept where dname in ('ACCOUNTING','SALES') );
    33. 1、拿到ACCOUNTING 或者 SALES 部门编号 10/30
    34. select deptno from dept where dname in ('ACCOUNTING','SALES');
    35. +--------+
    36. | deptno |
    37. +--------+
    38. | 10 |
    39. | 30 |
    40. +--------+
    41. 2、从emp中使用in来根据部门编号筛选
    42. select * from emp where deptno in (10,30);
    43. mysql> select * from emp where deptno in (10,30);
    44. +-------+--------+-----------+------+------------+---------+---------+--------+
    45. | empno | ename | job | mgr | hiredate | sal | comm | deptno |
    46. +-------+--------+-----------+------+------------+---------+---------+--------+
    47. | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
    48. | 7839 | KING | PRESIDENT | NULL | 1981-11-07 | 5000.00 | NULL | 10 |
    49. | 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
    50. | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
    51. | 7521 | 小黑 | 开发人员 | 7698 | 1981-02-22 | 3000.00 | 500.00 | 30 |
    52. | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
    53. | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
    54. | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
    55. | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
    56. +-------+--------+-----------+------+------------+---------+---------+--------+
    57. 9 rows in set (0.00 sec)
    58. 3、整合到一起
    59. select * from emp
    60. where deptno in (select deptno from dept where dname in ('ACCOUNTING','SALES'));
    1. -- like 'XX%'
    2. -- like '%XX';
    3. -- like 'XX%YYY%MMM'
    4. -- %:代表任意长度数据
    5. -- 名字是J开头的
    6. select * from emp where ename like 'J%';
    7. --名字前面是任意的 后面是TT结尾
    8. select * from emp where ename like '%TT';
    9. --中间带O 前后是任意的
    10. select * from emp where ename like '%O%';
    11. -- %:任意字符 长度也是任意的
    12. -- _:任意字符 长度是一个长度
    13. -- 名字是SCOT_
    14. select * from emp where ename like 'SCOT_';
    1. 小黑:开发人员:3000
    2. select concat(ename,':',job) as ej from emp;
    3. select concat(ename,':',job,':',sal) as ej from emp;
    4. (小黑+开发人员+3000)
    5. select concat('(',ename,'+',job,'+',sal,')') as ej from emp;
    6. select concat('abc=','def',ename) from emp;
    7. -- 薪水+奖金comm
    8. select (sal + comm) as '薪水奖金' from emp where comm is not null;
    9. select (sal - comm) as '薪水奖金' from emp where comm is not null;
    10. select (sal * comm) as '薪水奖金' from emp where comm is not null;
    11. select (sal / comm) as '薪水奖金' from emp where comm is not null;
    12. select (sal % comm) as '薪水奖金' from emp where comm is not null;
    13. as/AS:重新起一个别名
    14. select ename as '员工编号', ename as '员工姓名' from emp;
    1. id bigint(20) '主键id,这里为自增类型',
    2. addTime datetime '添加时间',
    3. ac_title varchar(255) ' 活动标题',
    4. create table activity(
    5. id bigint(10) not null auto_increment primary key COMMENT '主键ID',
    6. addTime datetime null COMMENT '入库时间',
    7. ac_title varchar(255) null COMMENT '活动标题'
    8. );
    9. -- now():返回当前时间
    10. insert into activity(addTime,ac_title) values
    11. (now(),'活动A'),
    12. (now(),'活动B'),
    13. (now(),'活动C'),
    14. (now(),'活动D'),
    15. (now(),'活动E');
    16. -- limit offset,count
    17. -- offset:从0开始 从第一行开始
    18. -- count:返回数量
    19. -- pageNo:分页码 1开始的
    20. -- pageSize:每页多少条数据 == count
    21. limit (pageNo - 1) * pageSize,pageSize
    22. select * from emp where deptno=10 order by sal desc limit 1;

    2022年4月9日09:23:09
    知识点回顾:
    1、创建数据库、删除数据库
    2、创建表、删除表、修改表结构,主键、外键关键关系
    3、插入数据、修改数据、查询数据(简单查询、带条件查询、limit、排序)

    1. CREATE TABLE IF NOT EXISTS tbl_name (column_name column_type);
    2. -- auto_increment:自增 1开始 每次增加1
    3. create table if not exists t_user(
    4. id bigint(10) not null auto_increment primary key,
    5. userName varchar(50) null,
    6. pwd varchar(50) null
    7. );
    8. insert into t_user(userName,pwd) values
    9. ('张三','123'),
    10. ('李四','123'),
    11. ('王五','123'),
    12. ('赵六','123'),
    13. ('小明','123')
    14. ;
    15. create table t_orderform(
    16. id bigint(10) not null auto_increment primary key,
    17. sn varchar(10) null,
    18. user_id bigint(10)
    19. );
    20. insert into t_orderform(sn,user_id) values
    21. ('1234561',1),
    22. ('1234562',2),
    23. ('1234563',3),
    24. ('1234564',4),
    25. ('1234565',5);

    主键:唯一不重复的
    外键:为了和主表建立关联关系(就是为了数据有约束)
    用户表
    订单表
    没有外键的时候:主表删除,从表不会受影响,也没有任何提示,因为两个表就完全不相关。

    1. ALTER TABLE 表名 add
    2. constraint FK_ID foreign key(外键字段名) REFERENCES 外表表名(对应的表的主键字段名);
    3. alter table t_orderform add
    4. constraint fk_user_id foreign key(user_id) REFERENCES t_user(id);

    外键带来的问题:如果系统比较庞大,表结构比较复杂,有外键的情况下,删除数据会非常麻烦。删除的时候需要先删除子表。
    一般开发中为了省事,不用外键,删除主表,子表数据还是存在的,属于没有用的垃圾数据——数据冗余

    数据冗余/垃圾数据:问题,只有占用物理空间

    1. -- mysql -u用户名 -p 数据库名 < 数据库名.sql
    2. mysql -u root -p ruizhi < d:/sql/create.sql
    3. mysql -u root -p ruizhi_bak < d:/sql/ruizhi.sql
    4. -- mysqldump -u [数据库用户名] -p [要备份的数据库名称]>[备份文件的保存路径]
    5. mysqldump -u root -p ruizhi > d:/sql/ruizhi.sql
    1. insert into t_user(id,userName,pwd) values(null,'小张同学','123');
    2. insert into t_user(userName,pwd) values('小张同学','123');
    3. insert into t_user(userName,pwd) values
    4. ('小张同学','123'),
    5. ('小张同学','123'),
    6. ('小张同学','123'),
    7. ('小张同学','123'),
    8. ('小张同学','123');
    9. create table t_user2(
    10. id bigint(10) not null auto_increment primary key,
    11. userName varchar(50) null,
    12. pwd varchar(50) null
    13. );
    14. INSERT INTO table_name(column_list)
    15. SELECT
    16. select_list
    17. FROM
    18. another_table;
    19. insert into t_user2(userName,pwd) select userName,pwd from t_user;
    1. UPDATE table_name SET field1=new-value1, field2=new-value2
    2. [WHERE Clause]
    3. -- 一般做updatedelete会把数据更新出问题,要做备份(导出)
    4. -- 在表中加字段 状态值 stuts:0已删除 1未删除
    5. -- t_user2 t_user
    6. update t_user set pwd='123456',userName='未命名';
    7. update t_user2 set userName='未命名',pwd='123456' where id=10;
    8. DELETE FROM table_name [WHERE Clause]
    9. delete from t_user2;
    10. delete from t_user where id=1;
    1. SELECT 1,列2,列3 FROM table_name [WHERE Clause];
    2. -- *:查询所有列
    3. select * from emp;
    4. select empno,ename,job as '工作' ,sal '薪水' from emp;
    5. select empno,ename,job as '工作' ,sal '薪水' from emp where empno=7934;
    1. SELECT
    2. column1,column2,...
    3. FROM
    4. table
    5. LIMIT offset , count;
    6. --offset:从0开始
    7. --count:返回多少数据量
    8. select * from emp limit 0,10;
    9. SELECT
    10. select_list
    11. FROM
    12. table
    13. LIMIT count; --只带一个count 表示直接返回多少数据量
    14. select * from emp limit 10;
    1. SELECT column1, column2,...
    2. FROM tbl
    3. ORDER BY column1 [ASC|DESC], column2 [ASC|DESC],...
    4. -- asc:正序 默认就是正序 可以不写
    5. -- desc:逆序
    6. select * from emp order by empno asc;
    7. select * from emp order by empno asc,deptno desc;
    8. -- 多列排序 前面排序完成后 后面再去排序
    1. select * from emp where sal = 3000;
    2. select * from emp where sal > 2000;
    3. select * from emp where sal >= 2000;
    4. select * from emp where sal < 2000;
    5. select * from emp where sal <= 2000;
    6. select * from emp where sal <> 3000;
    7. select * from emp where sal != 3000;
    8. select * from emp where sal between 3000 and 5000;
    9. -- 空值查询 is null
    10. -- 非空值查询 is not null
    11. select * from emp where comm is null;
    12. select * from emp where comm is not null;
    13. -- and or ()
    14. select * from emp where sal >3000 or sal < 2000;
    15. select * from emp where (sal > 3000 or sal < 2000) and deptno=10;
    1. -- in : or类似的
    2. select * from emp where deptno=10 or deptno = 20;
    3. select * from emp where deptno in (10,20);
    4. --SALES OPERATIONS
    5. select deptno from dept where dname in('SALES','OPERATIONS');
    6. select * from emp where deptno in ( select deptno from dept where dname in('SALES','OPERATIONS') );
    1. -- like:模糊匹配 %:代表0~n任意字符
    2. -- like '%abcdef'
    3. -- like 'abcdef%'
    4. -- like '%abcdef%'
    5. -- like 'a%bc%def'
    6. select * from emp where ename like 'J%';
    7. select * from emp where ename like '%S';
    8. select * from emp where ename like '%O%';
    9. -- _:下划线 代表1个任意字符
    10. select * from emp where ename like 'WAR_';
    11. -- 员工姓名-员工编号-薪水
    12. select concat( ename,'-',empno, '-',sal,deptno) from emp;
    1. Length():计算字符串长度的
    2. select length(ename) as '名字长度',ename from emp;
    3. Lower():转换为小写
    4. select lower(ename),ename from emp;
    5. -- 在所有ename后面加一个字符串 abc
    6. update emp set ename=concat(ename,'abc');
    7. MySQL 中替换函数 REPLACE(ss1s2) 使用字符串 s2 替换字符串 s 中所有的字符串 s1
    8. update emp set ename = replace(ename,'abc','');
    9. update emp set ename = replace(ename,' ','');
    10. update emp set ename=concat(' ',ename,' ');
    11. select substring(ename,1,3) from emp;
    1. -- now():当前时间 20224911:06:41
    2. create table t_goods(
    3. id bigint(10) not null auto_increment primary key,
    4. addTime datetime null default now(),
    5. goodsName varchar(50) null
    6. );
    7. insert into t_goods(goodsName) values
    8. ('华为手机'),
    9. ('苹果手机'),
    10. ('小米手机'),
    11. ('一加手机'),
    12. ('锤子手机');
    13. select id , date_format(addTime,'%Y/%m/%d %H:%i:%s') , goodsName from t_goods;
    14. select id , date_format(addTime,'%Y-%m-%d %H-%i-%s') , goodsName from t_goods;
    15. select id , date_format(addTime,'%Y:%m:%d %H:%i:%s') , goodsName from t_goods;
    1. -- avg() count()
    2. select avg(sal) as '平均薪水' , count(*) as '员工人数' from emp;
    3. -- count:count(*) == count(ename) == count(1) :用法都是一样的
    4. -- count(*):统计所有列 效率比较低
    5. select avg(sal) as '平均薪水' , count(ename) as '员工人数' from emp;
    6. select avg(sal) as '平均薪水' , count(1) as '员工人数' from emp;
    7. -- maxmin 计算最大值 最小值 会过滤掉null
    8. select max(comm) from emp;
    9. select min(comm) from emp;
    10. -- sum() 统计和
    11. select sum(sal) from emp;
    12. -- 计算部门10 薪水总和
    13. select sum(sal) from emp where deptno=10;
    14. select
    15. avg(sal) '平均薪水',
    16. count(1) '人员数量',
    17. max(sal) '薪水最大值',
    18. min(sal) '薪水最小值',
    19. sum(sal) '薪水总和'
    20. from emp;
    1. -- 分组函数:将数据分组 group by 1,列2...
    2. -- select 分组函数,[group by 后面的列],(其他列可以放,但是不符合逻辑) from group by 分组列
    3. 1emp表中 按照部门分组 分组后 计算这个部门平均薪水、部门人数、薪水最大值
    4. select avg(sal) as '部门平均薪水',deptno from emp group by deptno;
    5. select
    6. avg(sal) as '部门平均薪水',
    7. max(sal) as '部门薪水最大',
    8. min(sal) as '部门薪水最低',
    9. count(empno) as '部门人数',
    10. sum(sal) as '部门薪水总和',
    11. deptno
    12. from emp group by deptno;
    13. 2having 如果直接使用 就是where
    14. select * from emp having sal > 3000;
    15. 3、分组后过滤/筛选 group by having
    16. 筛选是从分组后的数据进行筛选
    17. +--------------+--------------+--------------+----------+--------------+--------+
    18. | 部门平均薪水 | 部门薪水最大 | 部门薪水最低 | 部门人数 | 部门薪水总和 | deptno |
    19. +--------------+--------------+--------------+----------+--------------+--------+
    20. | 2916.666667 | 5000.00 | 1300.00 | 3 | 8750.00 | 10 |
    21. | 2175.000000 | 3000.00 | 800.00 | 5 | 10875.00 | 20 |
    22. | 1566.666667 | 2850.00 | 950.00 | 6 | 9400.00 | 30 |
    23. +--------------+--------------+--------------+----------+--------------+--------+
    24. select
    25. avg(sal) as '部门平均薪水',
    26. max(sal) as '部门薪水最大',
    27. min(sal) as '部门薪水最低',
    28. count(empno) as '部门人数',
    29. sum(sal) as '部门薪水总和',
    30. deptno
    31. from emp group by deptno having avg(sal) > 2000;
    32. select
    33. avg(sal) as '部门平均薪水',
    34. max(sal) as '部门薪水最大',
    35. min(sal) as '部门薪水最低',
    36. count(empno) as '部门人数',
    37. sum(sal) as '部门薪水总和',
    38. deptno
    39. from emp group by deptno having avg(sal) > 2000 and max(sal) > 3000 ;
    40. select
    41. avg(sal) as '部门平均薪水',
    42. max(sal) as '部门薪水最大',
    43. min(sal) as '部门薪水最低',
    44. count(empno) as '部门人数',
    45. sum(sal) as '部门薪水总和',
    46. deptno
    47. from emp group by deptno having avg(sal) > 2000 and max(sal) > 3000 and deptno=10;
    48. 4、分组后排序
    49. select
    50. avg(sal) as '部门平均薪水',
    51. max(sal) as '部门薪水最大',
    52. min(sal) as '部门薪水最低',
    53. count(empno) as '部门人数',
    54. sum(sal) as '部门薪水总和',
    55. deptno
    56. from emp group by deptno having avg(sal) > 2000
    57. order by avg(sal) desc;
    58. 5、分组后limit
    59. select
    60. avg(sal) as '部门平均薪水',
    61. max(sal) as '部门薪水最大',
    62. min(sal) as '部门薪水最低',
    63. count(empno) as '部门人数',
    64. sum(sal) as '部门薪水总和',
    65. deptno
    66. from emp group by deptno having avg(sal) > 2000
    67. limit 0,2;
    1. -- 按照部门分组 筛选平均薪水大于2000
    2. select 分组函数(avgcountmaxminsum),分组列 from group by 分组列
    3. select avg(sal),deptno from emp group by deptno;
    4. +-------------+--------+
    5. | avg(sal) | deptno |
    6. +-------------+--------+
    7. | 2916.666667 | 10 |
    8. | 2175.000000 | 20 |
    9. | 1566.666667 | 30 |
    10. +-------------+--------+
    11. 3 rows in set (0.00 sec)
    12. select 分组函数,分组列 from having 分组函数,列;
    13. select 分组函数(avgcountmaxminsum),分组列 from group by 分组列 having 分组函数,分组列
    14. select avg(sal),deptno from emp group by deptno having avg(sal)>2000;
    15. -- 按照工作分组 求每种工作最大薪水、平均薪水、最低薪水
    16. select
    17. job as '工作',
    18. max(sal) as '最高薪水',
    19. avg(sal) as '平均薪水',
    20. min(sal) as '最低薪水'
    21. from emp
    22. group by job
    23. having max(sal)>2000 or max(sal)<1000;
    1. --子查询 in =
    2. -- in 相当于 or
    3. -- 部门 10 或者 20
    4. select * from emp where deptno=10 or deptno=20;
    5. select * from emp where deptno in (10,20);
    6. -- inor更加便捷 查询效率上in更高
    7. --知道部门名称 根据部门查询员工
    8. mysql> select * from dept;
    9. +--------+------------+----------+
    10. | deptno | dname | loc |
    11. +--------+------------+----------+
    12. | 10 | ACCOUNTING | NEW YORK |
    13. | 20 | RESEARCH | DALLAS |
    14. | 30 | SALES | CHICAGO |
    15. | 40 | OPERATIONS | BOSTON |
    16. +--------+------------+----------+
    17. mysql> select * from emp;
    18. +-------+--------+-----------+------+------------+---------+---------+--------+
    19. | empno | ename | job | mgr | hiredate | sal | comm | deptno |
    20. +-------+--------+-----------+------+------------+---------+---------+--------+
    21. | 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
    22. | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
    23. ACCOUNTINGRESEARCH
    24. 1、将部门编号查询出来
    25. select deptno from dept where dname in ('ACCOUNTING','RESEARCH');
    26. 2、将查询出来的部门编号放到员工表中进行查询
    27. select * from emp where deptno in (10,20);
    28. 3、将上面两条sql语句整合成一条sql
    29. select * from emp
    30. where
    31. deptno in ( select deptno from dept where dname in ('ACCOUNTING','RESEARCH') );
    32. ===============================================================================
    33. in 可以支持多个值的
    34. = 只能支持一个值
    35. select * from emp
    36. where
    37. deptno = ( select deptno from dept where dname in ('ACCOUNTING','RESEARCH') );
    38. -- ERROR 1242 (21000): Subquery returns more than 1 row
    39. -- 通过员工编号 查找员工所属部门名称
    40. 1、通过员工编号查找部门编号
    41. select deptno from emp where empno=7782;
    42. 2、通过部门编号查找部门名称
    43. select * from dept where deptno = 10;
    44. 3、两条sql语句整合成一条sql
    45. select * from dept where deptno = (select deptno from emp where empno=7782);

    in、=
    1、知道部门名称,查找员工

    2、知道员工编号,查找部门名称

    1. -- 用户表:商品
    2. create table t_user(
    3. id bigint(10) not null auto_increment primary key,
    4. addTime datetime null default now(),
    5. userName varchar(50) null
    6. );
    7. insert into t_user(userName) values('张三'),('李四'),('王五'),('小明');
    8. create table t_goods(
    9. id bigint(10) not null auto_increment primary key,
    10. addTime datetime null default now(),
    11. goodsName varchar(50) null,
    12. user_id bigint(10)
    13. );
    14. insert into t_goods(goodsName,user_id) values
    15. ('苹果手机',1),
    16. ('小米手机',1),
    17. ('一加手机',2),
    18. ('锤子手机',3),
    19. ('华为手机',4);
    20. ---------------------------------------------
    21. 1、张三有哪些商品
    22. select id from t_user where userName='张三';
    23. select * from t_goods where user_id in ( select id from t_user where userName='张三' );
    24. 2、苹果手机属于谁的
    25. select user_id from t_goods where goodsName='苹果手机';
    26. select * from t_user where id in ( select user_id from t_goods where goodsName='一加手机' );
    1. -- 多表连接查询
    2. -- select from 1,表2 where 关联条件
    3. -- select from 1 join 2 on 关联条件
    4. -- select from 1 left join 2 on 关联条件
    5. -- 员工信息 + 部门信息 都需要显示
    6. --差生两个表所有数据一一去匹配,两个表数据量相乘:笛卡尔
    7. select * from emp as e,dept as d;
    8. -- 表别名 因为两个表可能存在列名相同
    9. select * from emp as e,dept as d where e.deptno = d.deptno;
    10. 用户-商品
    11. select * from t_user u,t_goods g where u.id = g.user_id;
    12. select
    13. u.id as '用户ID',
    14. u.addTime as '用户添加时间',
    15. u.userName as '用户名',
    16. g.id as '商品ID',
    17. g.addTime as '商品添加时间',
    18. g.goodsName as '商品名称'
    19. from t_user u,t_goods g where u.id = g.user_id;
    20. 1、用户ID=1 显示用户信息+商品信息
    21. select
    22. u.id as '用户ID',
    23. u.addTime as '用户添加时间',
    24. u.userName as '用户名',
    25. g.id as '商品ID',
    26. g.addTime as '商品添加时间',
    27. g.goodsName as '商品名称'
    28. from t_user u,t_goods g where u.id = g.user_id and u.id = 1;
    29. 2、工作是 MANAGER 员工信息+部门信息
    30. select
    31. e.empno as '员工编号',
    32. e.ename as '员工名称',
    33. e.job as '员工工作',
    34. d.deptno as '部门编号',
    35. d.dname as '部门名称'
    36. from emp e,dept d where e.deptno = d.deptno and e.job='MANAGER';
    1. -- select 列.. from 1 join 2 on 关联条件 这种用法和where是一样的
    2. -- 员工信息 + 部门信息 都需要显示
    3. select * from emp e join dept d on e.deptno = d.deptno;
    4. 1、查询员工、部门信息,工作是 ANALYST
    5. select * from emp e , dept d where e.deptno = d.deptno and e.job='ANALYST';
    6. select * from emp e join dept d on e.deptno = d.deptno and e.job='ANALYST';
    7. 2、查询用户、商品信息,用户名是张三
    8. select * from t_user u join t_goods g on u.id = g.user_id and u.userName='张三';
    9. select * from t_user u , t_goods g where u.id = g.user_id and u.userName='张三';
    1. -- left join
    2. -- 用户表有10个人 只有前面4个人有商品
    3. -- 需求:将所有用户查询出来 并且有商品也出来 没有商品的用户也查询出来
    4. select * from t_user;
    5. --where
    6. select * from t_user u,t_goods g where u.id = g.user_id;
    7. select * from t_user u join t_goods g on u.id = g.user_id;
    8. -- left join:以左边为主 进行关联查询
    9. mysql> select * from t_user u left join t_goods g on u.id = g.user_id;
    10. +----+---------------------+----------+------+---------------------+-----------+---------+
    11. | id | addTime | userName | id | addTime | goodsName | user_id |
    12. +----+---------------------+----------+------+---------------------+-----------+---------+
    13. | 1 | 2022-04-09 15:06:44 | 张三 | 2 | 2022-04-09 15:07:52 | 小米手机 | 1 |
    14. | 1 | 2022-04-09 15:06:44 | 张三 | 1 | 2022-04-09 15:07:52 | 苹果手机 | 1 |
    15. | 2 | 2022-04-09 15:06:44 | 李四 | 3 | 2022-04-09 15:07:52 | 一加手机 | 2 |
    16. | 3 | 2022-04-09 15:06:44 | 王五 | 4 | 2022-04-09 15:07:52 | 锤子手机 | 3 |
    17. | 4 | 2022-04-09 15:06:44 | 小明 | 5 | 2022-04-09 15:07:52 | 华为手机 | 4 |
    18. | 5 | 2022-04-09 16:09:35 | 大明 | NULL | NULL | NULL | NULL |
    19. | 6 | 2022-04-09 16:09:41 | Jack | NULL | NULL | NULL | NULL |
    20. | 7 | 2022-04-09 16:09:43 | Tony | NULL | NULL | NULL | NULL |
    21. | 8 | 2022-04-09 16:09:46 | Mark | NULL | NULL | NULL | NULL |
    22. | 9 | 2022-04-09 16:09:48 | Tom | NULL | NULL | NULL | NULL |
    23. | 10 | 2022-04-09 16:09:50 | Cat | NULL | NULL | NULL | NULL |
    24. +----+---------------------+----------+------+---------------------+-----------+---------+
    25. --select 列... from 1 left join 2 on 关联条件
    26. -- 左边表数据全部列出来 右边表数据符合条件就列出来 不符合条件就不列出来
    27. customers cust_id
    28. orders cust_id
    29. customers为主表 orders为从表
    30. 主表全部显示
    31. select
    32. c.cust_id,
    33. c.cust_name,
    34. o.order_num,
    35. o.order_date
    36. from customers c left join orders o on c.cust_id = o.cust_id;
    37. User(张三)
    38. List<Goods>
    39. 华为手机
    40. 苹果手机
    41. 锤子手机
    42. select * from t_user u left join t_goods g on u.id = g.user_id
    43. where u.userName = '张三';
    44. -- left join 查询完后结果集 可以通过 where 再去筛选

    1 2022-04-09 15:06:44 张三 苹果手机
    2 2022-04-09 15:06:44 李四 苹果手机
    3 2022-04-09 15:06:44 王五 苹果手机
    4 2022-04-09 15:06:44 小明 苹果手机
    5 2022-04-09 16:09:35 大明
    6 2022-04-09 16:09:41 Jack
    7 2022-04-09 16:09:43 Tony
    8 2022-04-09 16:09:46 Mark
    9 2022-04-09 16:09:48 Tom
    10 2022-04-09 16:09:50 Cat

    1. -- union:去除重复数据 union all:显示所有的不会去除重复数据
    2. create table t_user2 select id,addTime,userName from t_user;
    3. -- 两个表关联
    4. select id,userName,addTime from t_user
    5. union
    6. select id,addTime,userName from t_user2
    7. group by id
    8. order by id desc;
    9. -- 三个表关联
    10. select id,addTime,userName from t_user
    11. union
    12. select id,addTime,userName from t_user2
    13. union
    14. select id,addTime,userName from t_user2;
    15. select id,addTime,userName from t_user
    16. union all
    17. select id,addTime,userName from t_user2
    18. union all
    19. select id,addTime,userName from t_user2
    20. union all
    21. select id,addTime,userName from t_user2;