【狂神说Java】MySQL最新教程通俗易懂--MySQL基础笔记

狂神视频地址

https://www.bilibili.com/video/BV1NJ411J79W?p=45


1.初识MySQL

JavaEE :企业级JavaWeb开发 Web

前端(页面:展示,数据)
后台 (连接点:连接数据库JDBC,连接前端(控制 、 视图跳转、和给前端传递数据))
数据库存数据,Txt,Excel,word)

只会写代码,学好数据库,基本混饭吃
操作系统,数据结构与算法!当一个不错的程序员!
离散数学,数字电路,体系结构,编译原理。+实战经验,高级程序员~优秀程序员~

1.1为什么要学习数据库

  1、岗位需求
  2、现在的世界,大数据时代~得数据者得天下
  3、被迫需求:存数据、去IOE
  4、数据库是所以软件体系中最核心的存在 DBA


1.2什么是数据库

数据库(DB,DataBase)
概念:数据仓库,软件,安装在操作(Windows、Linux、Mac)系统之上的!
作用存数据,管理数据 Excel


1.3 数据库分类

关系型数据库:Excel 行、列(SQL)

  • MySQL、Oracle、SqlServer、DB2、SQLlite
  • 通过标和表之间,行和列之间的关系进行数据的存储;学员信息表,考勤表,

非关系型数据库:{key:value} (No SQL)

  • Redis,MongDB
  • 非关系型数据库:对象存储,通过对象的自身的属性来决定。

  DB(DataBase) 存数据
  DBMS 管理和操作数据

扫描二维码关注公众号,回复: 13296909 查看本文章

  DBMS(DataBase Manager System)数据库管理系统
  数据库的管理软件,科学有效的管理我们的数据。维护和获取数据;
MySQL ,本质是数据库管理系统

在这里插入图片描述


1.4MySQL 简介

MySQL 是一个关系型数据库管理系统
前世: 瑞典MySQL AB 公司
今生:属于Oracle 旗下产品

开源的数据库软件

体积小、速度快、总体拥有成本低,招人成本低、所有人必须会~
中小型网站、或者大型网站

5.7 稳
8.0

官网:

安装建议:
1、尽量不要使用ext安装,会把一些配置中放到注册表
2、尽可能使用压缩包

1.5、安装MySQL

详细安装MySQL 5.7 请查看这篇博客

https://blog.csdn.net/qq_42025798/article/details/119933631


1.6、我使用的是Navicat 不是SQLyong

1.7、连接数据库

命令行连接

mysql -uroot -p123

在这里插入图片描述

查看所有数据库

show databases;

切换数据库

use 数据库名

查看所有的表

show tables;

创建一个数据库

create database westos(数据库名);

退出连接

exit;

注释

-- 单行注释(SQL 本来的注释)
/* (SQL的多行注释)
 hello
*/

数据库 xxx语言 CRUD 增删改查! CV程序员 API程序员 CRUD 程序员(业务!)

  1. DDL 定义
  2. DML 操作
  3. DQL 查询
  4. DCL 控制

2、操作数据库

操作数据库 》 操作数据库中的表 》 操作数据库中表的数据

操作数据库

2.1、创建数据库

create database [if not exists] westos
--if not exists 如果不存在则创建数据库

1、使用数据库

drop database [if exists] westos
-- if exists 如果存在则删除

2、使用数据库

use westos

3、查看数据库

show databases;
--查看所有的数据库

2.2、数据库的列类型

1、数值

类型 描述 长度
tinyint 十分小的数据 1个字节
smallint 较小的数据 2个字节
mediumint 中等大小的数据 3个字节
int 标准的整数 4个字节 常用的
bigint 较大的数据 8个字节
float 单精度浮点数 4个字节
double 双精度浮点数 8个字节(精度问题)
decimal 字符串形式的浮点数 金融计算的时候使用

2、字符串

类型 描述 长度
char 字符串固定大小的 0-255
varchar 可变字符串 0-65535 常用的变量 String
tinytext 微型文本 2^8-1
text 文本串 2^16-1 保存大文本

3、时间日期
java.util.Date

类型 描述
date YYYY-MM-DD 日期格式
time HH:MM:SS 时间格式
datetime YYYY-MM-DD HH:MM:SS 最常用的时间格式
timestamp 时间戳 ,1970年1月1日到现在的毫秒数 (全球统一的)

4、null

  • 没有值,未知
  • 注意:不要使用NULL进行运算

2.3数据库的字段属性(重点)

Unsigned

  • 无符号的整数
  • 声明了该列不能声明为负数

zerofill

  • 0填充的
  • 不足的位数,使用0 来填充,int(3) , 5----005

自增 auto_increment

  • 通常理解为自增,自动在上一条记录的基础上+1(默认)
  • 通常用来设置唯一的主键~index,必须是整数类型
  • 可以自定义设置主键自增的起始值和步长

非空 NULL not null

  • 假设设置为not null,如果不给它赋值,就会报错!
  • NULL,如果不填写值,默认就是null!

默认

  • 设置默认值
  • sex ,默认值为 男,

拓展
  每一个表都必须存在以下五个字段!未来做项目用的,表示一个记录存在的意义

id              主键
version         乐观锁
is  delete      伪删除
qmt create      创建时间
qmt  update     修改时间

创建表

-- AUTO_INCREMENT 自增
-- 所有的语句后面加 ,(英文的),最后一个不用加
-- default 默认值
-- 注释
-- primary key 主键,一般一个表只有一个唯一的主键;
-- 设置引擎ENGINE=INNODB
-- CHARSET 编码
create table student(
	`id` int(4) not null AUTO_INCREMENT comment '学号', 
	`name` varchar(30) not null default '匿名' comment '姓名',
    `pwd`  varchar(20) not null default '123456' comment '密码',
	`sex`  varchar(2) not null default '男' comment '性别',
	`birthday` datetime default null comment '出生日期',
	`address` varchar(100) default null comment '家庭住址',
	`email` varchar(50) default null comment '邮箱',
	primary key(`id`)	
)ENGINE=INNODB DEFAULT CHARSET=utf8

格式

create table [if not exists] 表名(
     `字段名` 列类型 [属性][索引][注释],
     `字段名` 列类型 [属性][索引][注释],
     ...
     `字段名` 列类型 [属性][索引][注释]
)[表类型][表字符集设置][注释]

常用命令

show create database school -- 查看创建数据库的语句
show create table student   -- 查看student数据表的定义语句
desc student                -- 显示表的结构

2.4、数据表的类型

关于数据库引擎

INNODB   默认使用
MYUSAM   

区别

功能 MYISAM INNODB
事务支持 不支持 支持
数据行锁定 不支持(表锁) 支持
外键约束 不支持 支持
全文索引 支持 不支持
表空间的大小 较小 较大,约为2倍

常规使用操作:

  • MYISAM 节约空间,速度较快
  • INNODB 安全性高,事务的处理,多表多用户操作

在物理空间存在的位置

  • 所有的数据库文件都存在data目录下
  • 本质还是文件存储!

在这里插入图片描述
在这里插入图片描述

MySQL 引擎在物理文件上的区别
  INNODB 在数据库表中只有一个 *.frm 文件,以及上级目录下的ibdata1 文件
  以下截图是使用 INNODB 引擎创建创建表的物理文件
在这里插入图片描述

MYISAM对应的文件
  *.frm :表结构的定义文件
  MYD :数据文件
   *.MYI :索引文件(index)
在这里插入图片描述

设置数据库表的字符集编码
  不设置的话,会是mysql默认的字符集编码
  MySQL的默认编码是latin1,不支持中文

charset=utf8

2.5、修改和删除表

修改

--  修改表名
alter table teacher rename as  teacher1 
alter table 旧表名   rename as 新表名

-- 增加表的字段
alter table teacher1  add age int(11)
alter table 表名     add  字段名  列属性

-- 修改表字段(重命令,修改约束)
alter table teacher1 modify age varchar(11)
alter table 表名  modify  字段名  列属性      -- 修改约束(change)

alter table teacher1  change  age ag1 int(1) -- 重命令(change )
alter table 表名   chage  旧字段名  新字段名 列属性 

-- 删除表的字段
alter table teacher1 drop age1
alter table 表名  drop 字段名
删除
drop table teacher1
drop table 表名

注意点:

  • `` 字段名,用这个包裹
  • 注释 … /**/
  • sql 关键字大小写不敏感,建议写小写

3、MQL数据管理

3.1、外键

create table grade(
	`gradeid` int(11) not null AUTO_INCREMENT comment '年级id',
	`grandename` varchar(30) not null comment '年级班级',
	primary key(`gradeid`)
)engine=INNODB default charset=utf8

-- 学生表的gradeid 字段 要去引用年级表的gradeid
-- 定义外键key
-- 给这个外键添加约束(执行引用)  references 引用

create table student(
	`id` int(4) not null AUTO_INCREMENT comment '学号', 
	`name` varchar(30) not null default '匿名' comment '姓名',
  `pwd`  varchar(20) not null default '123456' comment '密码',
	`sex`  varchar(2) not null default '男' comment '性别',
	`birthday` datetime default null comment '出生日期',
	`address` varchar(100) default null comment '家庭住址',
	`email` varchar(50) default null comment '邮箱',
	`gradeid` int(11) not null comment '年级名称',
	primary key(`id`),
  key `FK_grandeid` (`gradeid`),
  constraint `FK_grandeid`	foreign key (`gradeid`) references `grade` (`gradeid`)
)ENGINE=INNODB DEFAULT CHARSET=utf8

  删除右外键关系的表的时候,必须要先删除引用别人的表(从表),在删除被引用的表(主表)

  以上外键是物理外键,数据库级别的外键,不建议使用(避免数据库过多造成困扰)

最佳实践:
  数据库就是单纯的表,只用来存储行和列,只有行(数据)和列(字段)
我们想使用多账表,想使用外键(程序去实现)

3.2、 DML语言(全部记住)

数据库意义:数据存储和数据管理

DML语句:数据操作语言

  • insert
  • update
  • delete

3.3、添加

insert

insert into 表名([字段一,字段二,字段三]) values ("值一","值二","值三")

-- 由于主键自增,可以填null,或者不写该字段
insert into grade(gradename) values("物联专升本203")
insert into grade(gradeid,gradename) values(NULL,"大一")

-- 一般插入数据,一定要数据和字段 一一对应

-- 插入多个字段
insert into grade(gradename) values("大三"),("大四")
语法:insert into 表名([字段一,字段二,字段三]) values ("值一","值二","值三")

注意事项:

  1. 字段与字段之间用英文逗号隔开
  2. 字段可以省略,但是后面的值必须要一一对应
  3. 可以同时插入多条数据,values后面的值,需要使用 , 隔开即可 values(),(),…()

3.4、修改

update 修改谁 (条件) set 原来的值 = 新值

-- 修改学员名字,带了条件
update `student` set name="闲言" where id=1

-- 修改多个值
update `student` set name="闲言",`email`="[email protected]" where id=1
-- 修改学员名字,不带条件
update student set name="it闲言"


-- 语法 update 表名 set column_name=value,...[column_name=value]  where  [条件]

  条件:where 子句 运算符 id 等于某个值, 大于某个值,在某区间内修改
在这里插入图片描述

-- 通过多个条件定位数据
update student set `name`="长江七号" where id=1 and sex="女"

注意事项:

  1. column_name 是数据库的列、,尽量带上``
  2. 条件,筛选的条件,如果没有指定则会修改所有的列
  3. value ,是一个具体的值,也可以是一个变量
  4. 多个设置的属性之间用逗号隔开

3.5、删除

delete

语法:delete  from 表名 [where 条件]

-- 删除数据(避免这样写,会全部删除)
delete from student

-- 删除指定数据
delete from student where id = 1
  • truncate 清空表
-- 清空表
truncate `student`

delete 和 truncate 区别

  • 相同点:都能删除数据,不会影响表结构
    不同
  • truncate 重写设置自增列,计数器会归零
  • truncate 不会影响事务
create table test(
	`id` int(10) not null auto_increment,
	`name` varchar(20) not null,
	primary key(`id`)
)engine=INNODB default charset=utf8
 
delete from test  -- 不会影响自增
truncate test     -- 自增会归零

了解即可:delete 删除的问题,重启数据库,现象

  1. INNODB 自增列从1开始,(存在内存中的,断电即失)
  2. MyISAM 继续从上一个增量开始,(存在文件中,不会丢失)

4、DQL查询数据(最重点)

4.1 DQL

select 语法

SELECT
    [ALL | DISTINCT | DISTINCTROW ] 
      [HIGH_PRIORITY]
      [STRAIGHT_JOIN]
      [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
      [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr [, select_expr ...] 
    [

     FROM table_references
      [PARTITION partition_list]
    [WHERE where_condition] -- 指定结果需要满足的条件
    [GROUP BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]] -- 指定结果按照哪几个字段来分组
    [HAVING where_condition] -- 过滤分组的记录必须满足的次要条件
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...] -- 指定查询记录按照一个或多个排序
    [LIMIT {
   
   [offset,] row_count | row_count OFFSET offset}] 
    -- 指定查询的记录从哪条至哪条

(Data Query LANGUAGE:数据查询语言

  1. 所有的查询操作都用它 select
  2. 简单的查询,复杂的查询它都能做!~
  3. 数据库中最核心的语言,最重要的语句

4.2、指定查询字段

--  查询全部的学生   select 字段 from 表名
select * from student

-- 查询指定字段
select `studentno`,`studentname` from student


-- 别名,给结果集其别名,可以给字段起别名,也可以给表起别名 
select `studentno` as 学号,`studentname` as 姓名 from  student as s

-- 函数Concat(a,b)
select Concat("姓名:",studentname) as 新姓名 from student


语法: select 字段,.. from 表名

有的时候,列名不是那么的见名知意,我们起别名, as 字段名  as 别名 

P.16 建表和插入数据

年级表

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `grade`;
CREATE TABLE `grade`  (
  `gradeid` int(11) NOT NULL AUTO_INCREMENT COMMENT '年级编号',
  `gradename` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '年级名称',
  PRIMARY KEY (`gradeid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `grade` VALUES (1, '大一');
INSERT INTO `grade` VALUES (2, '大二');
INSERT INTO `grade` VALUES (3, '大三');
INSERT INTO `grade` VALUES (4, '大四');
INSERT INTO `grade` VALUES (5, '预科');
SET FOREIGN_KEY_CHECKS = 1;

学生表

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `studentno` int(4) NOT NULL COMMENT '学号',
  `loginpwd` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `studentname` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '学生姓名',
  `sex` tinyint(1) NULL DEFAULT NULL COMMENT '性别,0或1',
  `gradeid` int(11) NULL DEFAULT NULL COMMENT '年级编号',
  `phone` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '联系电话,允许为空',
  `address` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '地址,允许为空',
  `borndate` datetime(0) NULL DEFAULT NULL COMMENT '出生时间',
  `email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '邮箱账号允许为空',
  `identitycard` varchar(18) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '身份证号',
  PRIMARY KEY (`studentno`) USING BTREE,
  UNIQUE INDEX `identitycard`(`identitycard`) USING BTREE,
  INDEX `email`(`email`) USING BTREE
) ENGINE = MyISAM CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `student` VALUES (1000, '111111', '周丹', 1, 1, '13500000001', '北京海淀区1号', '2021-08-28 11:27:25', '[email protected]', '452128199908080851');
INSERT INTO `student` VALUES (1001, '123456', '周颖', 1, 2, '13500000002', '河南洛阳', '2021-08-28 11:28:34', '[email protected]', '452128199908080852');
INSERT INTO `student` VALUES (1002, '111111', '杨文瑞', 1, 1, '13500000003', '天津市和平区', '2021-08-28 11:29:17', '[email protected]', '452128199908080853');
INSERT INTO `student` VALUES (1003, '123456', '韩萌', 1, 3, '13500000004', '上海卢湾区', '2021-08-28 11:30:00', '[email protected]', '452128199908080854');
INSERT INTO `student` VALUES (1004, '123456', '刘丽霞', 1, 4, '13500000005', '北京市通州', '2021-08-28 11:30:42', '[email protected]', '452128199908080855');
INSERT INTO `student` VALUES (1005, '123456', '姜佳航', 2, 1, '13500000006', '广西桂林市灵川县', '2021-08-28 11:31:42', '[email protected]', '452128199908080856');
INSERT INTO `student` VALUES (1006, '123456', '郑佳祥', 2, 4, '13500000007', '地址不详', '2021-08-28 11:32:29', '[email protected]', '452128199908080857');
INSERT INTO `student` VALUES (1007, '111111', '刘洋', 1, 1, '13500000008', '北京东城区', '2021-08-28 11:33:23', '[email protected]', '452128199908080858');
INSERT INTO `student` VALUES (1008, '111111', '刘洋洋', 1, 1, '13500000009', '河南洛阳', '2021-08-28 11:34:08', '[email protected]', '452128199908080859');
INSERT INTO `student` VALUES (1009, '123456', '刘毅', 1, 2, '13500000011', '安微', '2021-08-28 11:34:52', '[email protected]', '452128199908080810');
INSERT INTO `student` VALUES (1010, '111111', '赵杰', 1, 1, '13500000012', '河南洛阳', '2021-08-28 11:35:23', '[email protected]', '45212819990808011');
INSERT INTO `student` VALUES (1011, '111111', '赵成', 1, 1, '13500000013', '北京市海淀区中关村大街1号', '2021-08-28 11:36:16', '[email protected]', '452128199908080812');
INSERT INTO `student` VALUES (1012, '123456', '刘恒', 2, 3, '13500000014', '广西南宁中央大街', '2021-08-28 11:37:10', '[email protected]', '452128199908080813');
INSERT INTO `student` VALUES (1013, '123456', '张伟奇', 2, 1, '13500000015', '上海卢湾区', '2021-08-28 11:37:53', '[email protected]', '45212819990808014');
INSERT INTO `student` VALUES (1014, '123456', '牛恩来', 2, 4, '13500000016', '北京市中关村大街*号', '2021-08-28 11:38:45', '[email protected]', '45212819990808015');
INSERT INTO `student` VALUES (1015, '123456', '马辉', 1, 4, '13500000017', '广西桂林市灵川县', '2021-08-28 11:39:31', '[email protected]', '45212819990808016');
INSERT INTO `student` VALUES (1016, '111111', '陈勉', 1, 1, '13510000018', '上海卢湾区', '2021-08-28 11:40:10', '[email protected]', '45212819990808017');
INSERT INTO `student` VALUES (1017, '123456', '赵宇航', 2, 3, '13500000019', '北京长安街1号', '2021-08-28 11:41:01', '[email protected]', '4521281999080818');
SET FOREIGN_KEY_CHECKS = 1;

课程表

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `subject`;
CREATE TABLE `subject`  (
  `subjectno` int(11) NOT NULL AUTO_INCREMENT COMMENT '课程编号',
  `subjectname` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '课程名称',
  `classhour` int(4) NULL DEFAULT NULL COMMENT '学时',
  `gradeid` int(4) NULL DEFAULT NULL COMMENT '年级编号',
  PRIMARY KEY (`subjectno`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 18 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `subject` VALUES (1, '高等数学-1', 110, 1);
INSERT INTO `subject` VALUES (2, '高等数学-2', 110, 2);
INSERT INTO `subject` VALUES (3, '高等数学-3', 100, 3);
INSERT INTO `subject` VALUES (4, '高等数学-4', 130, 4);
INSERT INTO `subject` VALUES (5, 'C语言-1', 110, 1);
INSERT INTO `subject` VALUES (6, 'C语言-2', 110, 2);
INSERT INTO `subject` VALUES (7, 'C语言-3', 100, 3);
INSERT INTO `subject` VALUES (8, 'C语言-4', 130, 4);
INSERT INTO `subject` VALUES (9, 'Java第一学年', 110, 1);
INSERT INTO `subject` VALUES (10, 'Java第二学年', 110, 2);
INSERT INTO `subject` VALUES (11, 'Java第三学年', 100, 3);
INSERT INTO `subject` VALUES (12, 'Java第四学年', 130, 4);
INSERT INTO `subject` VALUES (13, '数据库结构-1', 110, 1);
INSERT INTO `subject` VALUES (14, '数据库结构-2', 110, 2);
INSERT INTO `subject` VALUES (15, '数据库结构-3', 100, 3);
INSERT INTO `subject` VALUES (16, '数据库结构-4', 130, 4);
INSERT INTO `subject` VALUES (17, 'C# 基础', 130, 1);
SET FOREIGN_KEY_CHECKS = 1;

成绩表

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `result`;
CREATE TABLE `result`  (
  `studentno` int(4) NOT NULL COMMENT '学号',
  `subjectno` int(4) NOT NULL COMMENT '课程编号',
  `examdate` datetime(0) NOT NULL COMMENT '考试日期',
  `studentresult` int(4) NOT NULL COMMENT '考试成绩',
  INDEX `subjectno`(`subjectno`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `result` VALUES (1, 1, '2021-08-28 14:40:25', 89);
INSERT INTO `result` VALUES (1, 2, '2021-08-28 14:40:38', 87);
INSERT INTO `result` VALUES (1, 3, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (1, 4, '2021-08-28 14:41:00', 80);
INSERT INTO `result` VALUES (1, 5, '2021-08-28 14:41:08', 90);
INSERT INTO `result` VALUES (1, 6, '2021-08-28 14:41:16', 87);
INSERT INTO `result` VALUES (1, 7, '2021-08-28 14:41:23', 67);
INSERT INTO `result` VALUES (1, 8, '2021-08-28 14:41:31', 85);
INSERT INTO `result` VALUES (1, 9, '2021-08-28 14:41:38', 65);
INSERT INTO `result` VALUES (1, 10, '2021-08-28 14:41:46', 83);
INSERT INTO `result` VALUES (1, 11, '2021-08-28 14:41:55', 76);
INSERT INTO `result` VALUES (1, 12, '2021-08-28 14:42:02', 77);
INSERT INTO `result` VALUES (1, 13, '2021-08-28 14:42:11', 80);
INSERT INTO `result` VALUES (1, 14, '2021-08-28 14:42:18', 90);
INSERT INTO `result` VALUES (1, 15, '2021-08-28 14:42:29', 68);
INSERT INTO `result` VALUES (2, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (2, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (2, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (2, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (2, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (2, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (2, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (2, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (2, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (2, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (2, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (2, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (2, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (2, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (2, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (2, 16, '2021-08-28 14:40:50', 99);
INSERT INTO `result` VALUES (3, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (3, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (3, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (3, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (3, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (3, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (3, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (3, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (3, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (3, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (3, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (3, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (3, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (3, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (3, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (3, 16, '2021-08-28 14:40:50', 99);
INSERT INTO `result` VALUES (4, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (4, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (4, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (4, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (4, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (4, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (4, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (4, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (4, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (4, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (4, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (4, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (4, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (4, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (4, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (4, 16, '2021-08-28 14:40:50', 99);
INSERT INTO `result` VALUES (5, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (5, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (5, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (5, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (5, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (5, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (5, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (5, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (5, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (5, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (5, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (5, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (5, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (5, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (5, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (5, 16, '2021-08-28 14:40:50', 99);
INSERT INTO `result` VALUES (6, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (6, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (6, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (6, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (6, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (6, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (6, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (6, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (6, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (6, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (6, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (6, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (6, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (6, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (6, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (6, 16, '2021-08-28 14:40:50', 99);
INSERT INTO `result` VALUES (7, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (7, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (7, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (7, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (7, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (7, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (7, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (7, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (7, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (7, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (7, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (7, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (7, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (7, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (7, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (7, 16, '2021-08-28 14:40:50', 99);
INSERT INTO `result` VALUES (8, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (8, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (8, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (8, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (8, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (8, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (8, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (8, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (8, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (8, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (8, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (8, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (8, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (8, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (8, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (8, 16, '2021-08-28 14:40:50', 99);
INSERT INTO `result` VALUES (9, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (9, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (9, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (9, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (9, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (9, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (9, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (9, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (9, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (9, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (9, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (9, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (9, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (9, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (9, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (9, 16, '2021-08-28 14:40:50', 99);
INSERT INTO `result` VALUES (10, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (10, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (10, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (10, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (10, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (10, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (10, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (10, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (10, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (10, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (10, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (10, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (10, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (10, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (10, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (10, 16, '2021-08-28 14:40:50', 99);
INSERT INTO `result` VALUES (11, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (11, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (11, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (11, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (11, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (11, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (11, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (11, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (11, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (11, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (11, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (11, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (11, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (11, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (11, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (11, 16, '2021-08-28 14:40:50', 99);
INSERT INTO `result` VALUES (12, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (12, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (12, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (12, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (12, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (12, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (12, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (12, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (12, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (12, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (12, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (12, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (12, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (12, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (12, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (12, 16, '2021-08-28 14:40:50', 99);
INSERT INTO `result` VALUES (13, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (13, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (13, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (13, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (13, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (13, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (13, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (13, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (13, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (13, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (13, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (13, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (13, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (13, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (13, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (13, 16, '2021-08-28 14:40:50', 99);
INSERT INTO `result` VALUES (14, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (14, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (14, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (14, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (14, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (14, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (14, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (14, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (14, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (14, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (14, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (14, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (14, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (14, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (14, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (14, 16, '2021-08-28 14:40:50', 99);
INSERT INTO `result` VALUES (15, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (15, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (15, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (15, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (15, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (15, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (15, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (15, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (15, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (15, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (15, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (15, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (15, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (15, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (15, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (15, 16, '2021-08-28 14:40:50', 99);
INSERT INTO `result` VALUES (16, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (16, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (16, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (16, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (16, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (16, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (16, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (16, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (16, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (16, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (16, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (16, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (16, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (16, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (16, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (16, 16, '2021-08-28 14:40:50', 99);
INSERT INTO `result` VALUES (17, 1, '2021-08-28 14:40:50', 86);
INSERT INTO `result` VALUES (17, 2, '2021-08-28 14:40:50', 76);
INSERT INTO `result` VALUES (17, 3, '2021-08-28 14:40:50', 96);
INSERT INTO `result` VALUES (17, 4, '2021-08-28 14:40:50', 80);
INSERT INTO `result` VALUES (17, 5, '2021-08-28 14:40:50', 87);
INSERT INTO `result` VALUES (17, 6, '2021-08-28 14:40:50', 65);
INSERT INTO `result` VALUES (17, 7, '2021-08-28 14:40:50', 30);
INSERT INTO `result` VALUES (17, 8, '2021-08-28 14:40:50', 50);
INSERT INTO `result` VALUES (17, 9, '2021-08-28 14:40:50', 60);
INSERT INTO `result` VALUES (17, 10, '2021-08-28 14:40:50', 36);
INSERT INTO `result` VALUES (17, 11, '2021-08-28 14:40:50', 70);
INSERT INTO `result` VALUES (17, 12, '2021-08-28 14:40:50', 26);
INSERT INTO `result` VALUES (17, 13, '2021-08-28 14:40:50', 16);
INSERT INTO `result` VALUES (17, 14, '2021-08-28 14:40:50', 66);
INSERT INTO `result` VALUES (17, 15, '2021-08-28 14:40:50', 98);
INSERT INTO `result` VALUES (17, 16, '2021-08-28 14:40:50', 99);
SET FOREIGN_KEY_CHECKS = 1;

将sql语句分别执行即可
在这里插入图片描述

去重distinct
  作用:去除SELECT 查询出来的结果中重复的数据,重复的数据只显示一条。

-- 查询一下哪些同学参加了考试
select * from result -- 查询全部的考试成绩
select student from result -- 查询有哪些同学参加了考试
select distinct studentno from result -- 发现重复属性,去重

数据库的列(表达式)

select version() -- 查询系统版本(函数)
select 100*3-1 as 计算结果  -- 查询计算结果(表达式)
select @@AUTO_INCREMENT_INCREMENT  -- 查询自增的步长(变量)


-- 学员成绩提分后查看
select studentno,studentresult+1 as 提分后 from result

数据中的表达式:文本值、列、Null、函数、计算表达式、系统变量。
select 表达式 from 表


4.3、where 条件子句

作用:检索数据中 符合条件 的值
  搜索的条件由一个或多个表达式组成,结果 布尔值

逻辑运算符

运算符 语法 描述
and && a and b a && b 逻辑与,两个为真,结果为真
or || a or b a || b 逻辑或,其中一个为真,及结果为真
not ! not a ! a 逻辑非,真为假,假为真!
-- 查询1000以外的学生成绩
select studentno,studentresult
from result
where studentno != 1

select studentno,studentresult
from result
where not studentno = 1

尽量使用英文字母

模糊查询
  本质上还是比较运算符!

运算符 语法 描述
is null a is null 如果操作为符为null,则结果为真
is not null a is not null 如果操作符为 not null,则结果为真
between and a betwenn b and c 若,a在b和c之间,则结果为真
Like a like b SQL匹配,如果a匹配b,则结果为真
in a in(a1,a2,a3…) 假设a在a1,或者a2,… 结果为真
-- =====================模糊查询=======================
-- 查询姓刘的同学
-- like 结合,  %(代表0到任意个字符)   _(一个字符) 
select * from student where studentname like "刘%"

-- 查询姓刘的同学,后面只有一个字
select * from student where studentname like "刘_"

-- 查询名字中间有佳字的同学
select *  from student where studentname like "%佳%"

-- ===================in(具体的一个或多个值)===================
-- 查询1001 1002 1003 号学员
select * from student where studentno in(1001,1002,1003)

-- 查询在北京的学生
select * from student where address in ('安徽','河南洛阳')


-- ===============null   not null =============================

-- 查询地址为空的学生
select * from student where address is null or address = ''

-- 查询有出生日期的同学,不为空
select * from student where borndate is not null

4.4联表查询

JOIN 对比
在这里插入图片描述

在这里插入图片描述

思路
  1.分析需求,分析查询的字段来自哪些表,(连接查询)
  2.确定使用哪种连接查询? 7种

  确定交叉点(这两个表中哪个数据是相同的)
  判断的条件:学生表的中 studentNo = 成绩表 studentNo

  join (连接的表) on (判断的条件) 连接查询
  where 等值查询

-- ============== 链表查询 ==================
-- 查询参加了考试的同学(学号,姓名,科目编号,分数)

select s.studentno,studentname,subjectno,studentresult
from student as s inner join result as r
on s.studentno = r.studentno

--  right join
select s.studentno,studentname,subjectno,studentresult
from student as s right join result r
on s.studentno = r.studentno

--  left join
select s.studentno,studentname,subjectno,studentresult
from student as s left join result r
on s.studentno = r.studentno

-- 查询缺考的同学
select s.studentno,studentname,subjectno,studentresult
from student as s left join result as r
on s.studentno = r.studentno
where studentresult is null
操作 描述
inner join 如果表中至少有一个匹配,就返回行
right join 会从右表中返回所有的值,即使左表中没有匹配
left join 会从左表中返回所有值,即使右表中没有匹配
-- 查询参加考试的同学信息(学号,姓名,课程名,学生成绩)
-- 思路:我要查询哪些数据?,需要查询的数据来自哪些表?
-- 假设存在一种多张表查询,慢慢来,先查询两种表然后慢慢增加
select s.studentno,studentname,subjectname,studentresult
from `student` as s right join `result` as r
on s.studentno = r.studentno
inner join `subject` as sb
on r.subjectno = sb.subjectno

自连接
  自己的表和自己的表连接,核心:一张表拆为两张一样的表
p21使用到的表

CREATE TABLE category (
	categoryid INT(10) UNSIGNED NOT NULL auto_increment COMMENT "主题ID",
	pid INT(10) NOT NULL COMMENT "父ID",
	categoryName VARCHAR(50) NOT NULL COMMENT "主题名字",
	PRIMARY KEY(categoryid)
) ENGINE=INNODB auto_increment=9 DEFAULT CHARSET = utf8;

INSERT INTO category(categoryid,pid,categoryName)
VALUES(2,1,"信息技术"),(3,1,"软件开发"),(4,3,"数据库"),
(5,1,"美术设计"),(6,3,"web开发"),(7,5,"PS技术"),(8,2,"办公信息");

父类
在这里插入图片描述

子类

在这里插入图片描述

操作:查询父类对应的子类关系

在这里插入图片描述

-- 查询父子信息:把一张表拆为两张表
select a.categoryName as 父栏目,b.categoryName as 子栏目
from category as a,category as b
where a.categoryid = b.pid

-- 查询学生所属的年级信息(学号,姓名,年级名称)
select studentno,studentname,gradename
from student,grade
where student.gradeid = grade.gradeid

-- 查询科目所属的年级(科目名称,年级名称)
select subjectname,gradename
from `subject`,grade
where `subject`.gradeid = grade.gradeid


-- 查询参加了 数据库结构-1考试的同学信息:学号,姓名,科目名,分数

-- 查询来数据来自哪些表 student , result ,subject
select student.studentno,studentname,subjectname,studentresult
from student 
inner join result 
on student.studentno = result.studentno 
inner join `subject` 
on result.subjectno = `subject`.subjectno 
where subjectname = '数据库结构-1'

4.5、分页和排序

排序(order by)

-- 排序:升序 asc  降序 desc
-- 查询的结果根据成绩排序
select student.studentno,studentname,subjectname,studentresult
from student 
inner join result 
on student.studentno = result.studentno 
inner join `subject` 
on result.subjectno = `subject`.subjectno 
where subjectname = '数据库结构-1'
order by studentresult desc

分页(limit)

-- 分页 为什么要分页? 缓解数据库压力,给人好的体验
-- 每页只显示5条数据
-- 语法: limit 起始值,页面大小
-- 网页:当前页,每页条数条数,总页数
-- limit 0,5 1~5
select student.studentno,studentname,subjectname,studentresult
from student 
inner join result 
on student.studentno = result.studentno 
inner join `subject` 
on result.subjectno = `subject`.subjectno 
where subjectname = '数据库结构-1'
order by studentresult desc
limit 0,5

-- 第一页   limit 0,5         (1-1)* 5, 5
-- 第二页   limit 5,5 				(2-1)* 5, 5
-- 第三页   limit 10,5 				(3-1)* 5, 5
-- 第四页   limit 15,5 				(4-1)* 5, 5
-- 第N页    limit (n-1)*pageSize,pageSize 
-- [pageSize:页面大小]
-- [(n-1) * pageSize   起始值]
-- [n: 当前页]
-- [总数页数: 总条数/pageSize ]

语法:limit 查询起始下标,每页显示条数

练习
  查询 Java 第一学年课程成绩排名前十的学生,并且分数要大于80分的学生信息(学号,姓名,课程名称,分数)

select student.studentno,studentname,subjectname,studentresult
from student 
inner join `result`
on student.studentno = result.studentno
inner join  `subject`
on result.subjectno  =  `subject`.subjectno
where `subject`.subjectname = "Java第一学年"   and studentresult > 80
order by studentresult desc
limit 0,10

注意:如果查询不出来,看自己有没有对应的数据

4.6、子查询

  本质: 在where 中嵌套一个子查询语言

-- 查询分数不小于80分的学生信息(学号,姓名)
-- 使用连接查询
select distinct student.studentno,studentname
from student inner join result
on student.studentno = result.studentno
where studentresult >= 80



-- 查询分数不小于80分的学生信息(学号,姓名)
-- 在这个基础上 增加一个科目 高等数学-2
-- 方式一 使用子查询
select distinct student.studentno,studentname
from student inner join result
on student.studentno = result.studentno
where studentresult >= 80 and subjectno = (select subjectno from `subject` where subjectname = "高等数学-2")

-- 方式二 使用嵌套子查询
select studentno,studentname 
from student
where studentno in (
	select studentno 
	from result 
  where studentresult >= 80 and subjectno = (
		select subjectno 
		from `subject`
		where subjectname = "高等数学-2"
	)
)

-- 方式三 使用内连接查询
select student.studentno,studentname
from student INNER JOIN result
on student.studentno = result.studentno
INNER JOIN `subject`
on result.subjectno = `subject`.subjectno
where subjectname = "高等数学-2" and studentresult >= 80

练习:查询C语言-1 前5名同学的成绩信息(学号,姓名,分数)

select student.studentno,studentname,studentresult
from student inner join result 
on student.studentno = result.studentno
where subjectno = (
	select subjectno 
	from `subject`
	where subjectname = "C语言-1"
)
order by studentresult
limit 0,5

4.7、分组和过滤

-- 查询不同课程的平均分、最高分、最低分
-- 核心(根据不同的课程)
select subjectname,avg(studentresult),max(studentresult),min(studentresult)
from result inner join `subject` 
on result.subjectno = `subject`.subjectno
GROUP BY result.subjectno  -- 通过什么来分组


-- 查询不同课程的平均分、最高分、最低分、平均分大于80分的
-- 核心(根据不同的课程)
select subjectname,avg(studentresult),max(studentresult),min(studentresult)
from result inner join `subject` 
on result.subjectno = `subject`.subjectno
GROUP BY result.subjectno  -- 通过什么来分组
having avg(studentresult) > 80 

4.8、select 小结

在这里插入图片描述


5.MySQL 函数

5.1、常用函数

-- 数学运算
select abs(-8) -- 绝对值
select  ceiling(9.4) -- 向上取整
select floor(9.4)  -- 向下取整
select rand()  -- 返回一个0~1之间的随机数 
select sign(-10) -- 判断一个数的符号     负数返回-1 ,正数返回1 ,0返回0

-- 字符串
select CHAR_LENGTH("人一定要靠自己")   -- 返回字符串的长度
select CONCAT('我',"是谁")  -- 拼接字符串
select insert("我爱写 hello",1,2,"超级热爱")  -- 替换
select upper("abc") -- 转大写
select lower("ABCa") -- 转小写
select instr("helloworld h","h")  -- 找到某字符第一次出现的位置
select replace("狂神说坚持就能成功","坚持","努力") -- 替换出现的指定字符串
select substr("狂神说坚持就能成功",4,6)  -- 返回指定字符串(原字符串,截取的位置,截取的长度)
select reverse("狂神说坚持就能成功") -- 反转字符串

-- 时间和日期函数
select CURRENT_DATE -- 获取当前日期
select CURRENT_DATE() -- 获取当前日期
select now()  --  获取当前时间
select LOCALTIME() -- 获取本地时间
select SYSDATE()  -- 获取系统时间

-- 系统 
select SYSTEM_USER()
select USER()
select version()

5.2、聚合函数

名称 描述
count() 计数
sum() 求和
avg() 求平均值
max() 求最大值
min() 求最小值
-- =========== 聚合函数 ======================
-- 都能统计表中的记录(想查询一个表中有多少条记录就用count)
select count(studentname) from student  -- count(字段) ,会忽略所有的null 值
select count(*) from student -- count(*)  不会忽略null值
select count(1) from student -- count(1)  不会忽略null值

select  sum(studentresult) as 求和 from result
select avg(studentresult) as 平均分from result
select max(studentresult) as 最高分 from result
select min(studentresult) as 最低分 from result 

5.3、数据库级别的MD5加密
什么是md5?

  • 主要增强算法复杂度和不可逆性。
  • MD5 不可逆,具体的值的md 5是一样的。

6.事务

6.1什么是事务

要么都成功,要么都失败
————————————————————————
1、SQL执行, A给B转账 A1000——》200 B200
2、SQL执行, B收到A的钱 A800 —》 B 400
————————————————————————

将一组SQL放在一个批次中去执行

事务原则

  • ACID 原则,原则性,一致性,隔离性,持久性 (脏读)

博客参数链接

https://blog.csdn.net/dengjili/article/details/82468576

原子性

  • 要么都成功,要么都失败

一致性

  • 事务前后的完整性要保证一致

隔离性

  • 事务的隔离性是多用户并发数据库时,数据库为每一个用户开启的事务,不能被其他事务的操作所干扰,多个并发事务之间要相互隔离。

持久性
事务一旦提交不可逆,被持久化到数据库中

事务的隔离级别

  • 脏读:

    • 指一个事务读取了另外一个事务未提交的数据。
  • 不可重复读:

    • 在一个事务内读取表中的某一行数据,多次读取结果不同。(这个不一定是错误,只是某些场合不对)
  • 虚读(幻读)

    • 是指在一个事务内读取到了别的事务插入的数据,导致前后读取数量总量不一致。
      在这里插入图片描述

7、索引

7.1、索引的分类

主键索引 primary key

  • 唯一标识,主键不可重复,只能有一个列作为主键

唯一索引 unique key

  • 避免重复的列出现,唯一索引可以重复,多个列可以表示为 唯一索引

常规索引 index

  • 默认的 index

全文索引 fullText

  • 在特定的数据库引擎下才有,MYISAM
  • 快速定位数据
-- 索引的使用
-- 1、在创建表的时候给字段增加索引
-- 2、创建完毕后,增加索引

-- 显示所有的索引信息

show index from student

-- 增加一个索引
alter table student
add fulltext index `studentname` (`studentname`)

-- explain 分析sql 执行状况
explain select * from student -- 非全文索引

explain select * from student where match(studentname) 	against('刘')

7.2、测试索引

p.31建表语句

create TABLE `app_user` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) DEFAULT "" COMMENT "用户昵称",
`email` VARCHAR(50) NOT NULL COMMENT "用户邮箱",
`phone` VARCHAR(20) DEFAULT ""  COMMENT "手机号",
`gender` TINYINT(4) UNSIGNED DEFAULT "0" COMMENT "性别(0:男;1:女)",
`password` VARCHAR(100) NOT NULL COMMENT "密码",
`age` TINYINT(4) DEFAULT "0"  COMMENT "年龄",
`create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT = "app用户表"
-- 写函数之前必须要写,标志

DELIMITER $$
CREATE FUNCTION mock_data ()
returns INT
BEGIN
	DECLARE num INT DEFAULT 1000000;
	DECLARE i INT DEFAULT 0;
		WHILE i<num DO
			INSERT INTO `app_user`(`name`,`email`,`phone`,`gender`,`password`,`age`)VALUES(CONCAT("用户",i),"[email protected]",CONCAT("18",FLOOR(RAND()*((999999999-100000000)+100000000))),FLOOR(RAND()*2),UUID(),FLOOR(RAND()*100));
			 SET i = i+1;
		END WHILE;
	  RETURN i;
END;

select * from app_user where `name` = '用户9999';
select * from app_user where `name` = '用户9999';
select * from app_user where `name` = '用户9999';

explain select * from app_user where `name` = "用户9999";

-- id_表名_字段名
-- create index 索引名 on 表(`字段`)
create index id_app_user_name on app_user(`name`)

select * from app_user where `name` = '用户9999';
select * from app_user where `name` = '用户9999';
explain select * from app_user where `name` = "用户9999";

索引在小数据量的时候,用处不大,但是在大数据的时候,区别十分明显


7.3、索引原则

  1. 索引不是越多越好
  2. 不要对经常变动的数据加索引
  3. 小数据量的表不需要加索引
  4. 索引一般加在常用来查询的字段上

索引的数据结构

  • Hash 类型的索引
  • Btree :INNODB的默认数据结构

博客地址

http://blog.codinglabs.org/articles/theory-of-mysql-index.html

8、权限管理和和备份

8.1 用户管理

-- 创建用户  create  user 用户名 IDENTIFIED by '密码'
create user xy  IDENTIFIED by "123456"

-- 修改密码(修改当前用户密码)
set password = PASSWORD('123456')

-- 修改密码(修改指定用户密码)
set password for xy = PASSWORD('111')

-- 重命名  rename user 原名字 to 新名字
rename user xy to xy1

-- 用户授权  ALL PRIVILEGES 全部的权限 ,库,表
-- ALL PRIVILEGES 除了给别人授权,其他权限都干
grant ALL PRIVILEGES  on *.* to xy1
 

-- 查询权限
show grants for xy1  -- 查询指定用户权限
show grants for root@localhost 

-- 撤销权限  revoke 哪些权限,在哪个库撤销,给谁撤销
revoke all PRIVILEGES on *.* from xy1

8.2 MySQL备份

为什么要备份:

  1. 保证重要的数据不丢失
  2. 数据转移 A---->B

MySQL数据库备份的方式

  1. 直接拷贝物理文件
  2. 在Navicat 等可视化工具中手动导出
  3. 使用命令行 导出
语法:mysqldump  -h主机 -u用户名 -p密码 数据库名 表1  > 导出的位置/文件名
语法:mysqldump  -h主机 -u用户名 -p密码 数据库名 表12 > 导出的位置/文件名
语法:mysqldump  -h主机 -u用户名 -p密码 数据库名 > 导出的位置/文件名

C:\Users\wei>mysqldump -hlocalhost -uroot -p123 school student >D:/q.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

9、规范数据库设计

9.1、为什么需要设计

当数据库比较复杂的时候,我们就需要设计了

糟糕的数据库设计:

  1. 数据冗余,浪费空间
  2. 数据插入和删除都会麻烦/异常【屏蔽物理外键】
  3. 程序的性能查

良好的数据库设计

  1. 节省内存空间
  2. 包装数据的完整性
  3. 方便我们开发系统

软件开发中,关于数据库的设计

  1. 分析需求:分析业务和需要处理的数据库的需求
  2. 概要设计:设计关系图E-R

设计数据库的步骤:(个人博客)

  1. 收集信息,分析需求
      用户表(用户登录注销,用户的个人信息,写博客,创建分类)
      分类表(文章分类,谁创建的)
      分组表(文章信息)
      友链表(友链信息)
      自定义表(系统信息,某个关键字,或者一些主字段)key:value
  2. 标识实体(把需求落地到字段)
  3. 标识实体之间的关系

9.2、三大范式

为什么需要数据规范化

  1. 信息重复
  2. 更新异常
  3. 插入异常
  4. 无法正常显示信息
  5. 删除异常
  6. 丢失有效的信息

三大范式

  • 第一范式(1NF)
    原子性:保证每一列不可再分

  • 第二范式(2NF)
    前提:满足第一范式的前提下,每张表只描述一件事情

  • 第三范式(3NF)
    前提:满足第二范式的前提下,需要确保数据表中的每一列和主键相关,而不能间接相关。

规范性和性能问题
  1.关联查询的表不超过3张表
  2.考虑商业化的需求和目标(成本,用户体验)数据库的性能更重要
  3.在规范性能问题的时候,需要适当考虑一下 规范性
  4.故意给某些表增加一些冗余字段。(从多表查询变为单表查询)
  5.故意增加一些计算列


猜你喜欢

转载自blog.csdn.net/qq_42025798/article/details/119936527