首页 > 数据库 >MYSQL详解 及 习题

MYSQL详解 及 习题

时间:2022-11-16 10:34:19浏览次数:76  
标签:insert MYSQL 详解 values SID sc 习题 where select


常用操作

创建表

CREATE TABLE `xxl_job_group` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`app_name` varchar(64) NOT NULL COMMENT '执行器AppName',
`title` varchar(12) NOT NULL COMMENT '执行器名称',
`address_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '执行器地址类型:0=自动注册、1=手动录入',
`address_list` text COMMENT '执行器地址列表,多地址逗号分隔',
`sync_status` tinyint(4) DEFAULT NULL COMMENT '同步状态 0:同步中 1:成功 2:失败',
`creator` varchar(50) NOT NULL DEFAULT '' COMMENT '创建人',
`updator` varchar(50) NOT NULL DEFAULT '' COMMENT '更新人',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`modified_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
`is_deleted` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否删除(1:是,0:否)',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='账户表';

新建表时,自动设置创建数据行时间与修改数据行时间

CREATE TABLE `test` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
`task_id` varchar(20) COLLATE utf8_bin NOT NULL COMMENT '任务id',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

更新字段

ALTER TABLE 表名 MODIFY COLUMN 列名 datetime DEFAULT CURRENT_TIMESTAMP NOT NULL;
ALTER TABLE 表名 MODIFY COLUMN 列名 datetime DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL;

获得时间

sql 语句

create table Student(SID varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10));
insert into Student values('01','赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
create table Course(CID varchar(10),Cname nvarchar(10),TID varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
create table Teacher(TID varchar(10),Tname nvarchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
create table SC(SID varchar(10),CID varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);

常用概念

事务

题目

案例

# 选择课程1的学生
select * from sc where sc.CID = '01';

select * from sc where sc.CID = '02';
# 找到课程1的分数大于课程2的分数的学生
select * from student right join (select t1.SID ,t1.CID ,t1.score,t2.CID as 't2.CID',t2.score as 't2.score' from (select * from sc where sc.CID = '01') as t1 ,(select * from sc where sc.CID = '02') as t2 where t1.score>t2.score) r
on student.SID = r.SID;

# 查看某个教师看了哪几门课程
select Tname ,Cname from course,teacher where course.TID = teacher.TID;

# 内联的写法 差集
select * from course inner join teacher t on course.TID = t.TID;

# 查询参加两个课程的学生两门课程的成绩
select Sname ,c.ascore,c.bscore from student right join (select a.SID,a.CID as acid,a.score as ascore,b.CID as bcid,b.score as bscore from (select * from sc where sc.CID = '01') as a INNER JOIN (select * from sc where sc.CID = '02') as b on a.SID = b.SID) as c on student.SID = c.SID;

# 查询学生不存在" 01 "课程但存在" 02 "课程的情况
select * from sc where SID not in (select SID from sc where CID = '01') and CID = '02';

# 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与聚合函数一起使用。
# 查询叙述选择课程的均分 AVING 子句可以让我们筛选分组后的各组数据。
select t1.sid,t1.sname,t2.avgscore from student as t1 inner join (select SID,avg(sc.score) as avgscore from sc group by sc.SID having avgscore >= 60) as t2 on t1.SID = t2.SID;

# distinct 用在
select Sname from student where Sid in (select distinct SID from sc);

#EXISTS 运算符用于判断查询子句是否有记录,如果有一条或多条记录存在返回 True,否则返回 False。
# 查询没有选课的学生是否存在
select * from student where exists (select SID from student where SID not in (select DISTINCT SID from sc));
select Tname from teacher where exists(select * from course where teacher.TID = course.TID );
select exists(select * from course where TID = '03') as 'flag';
# 返回的之列每一行都会有一个true false true就显示
select Tname from teacher where exists(select * from course where teacher.TID = course.TID);
select Tname from teacher where exists(select * from course where teacher.TID = course.TID and course.TID = '04');
select Tname from teacher where true;

# 查询李老师的数量
select count(*) from teacher where teacher.Tname like '李%';

# 课程按得分进行排列
select * from sc order by score DESC limit 3;

# union 表的使用 将相同的列 合并起来
select SID from student union select SID from sc;


# 查询张三老师教授过的课程
# 张三老师教授过的ID
select TID from teacher where Tname = '张三';
# 01 课程号教授的课程
select CID from course where TID in (select TID from teacher where Tname = '张三');
# 03 学生学习过这个课程号的
select t1.SID from student as t1 right join (select SID, CID,score from sc where CID in (select CID from course where TID in (select TID from teacher where Tname = '张三'))) as t2 on t2.SID = t1.SID;

# 查询什么课都没选的学生成绩
# 查询课程表中存在的学生SID
select DISTINCT SID from sc;
#
select Sname from student where SID not in (select DISTINCT SID from sc);


# 查询两门不及格的学生的成绩
# 查询所有不及格的学生成绩
select SID, cid as 'c' , score from sc where score < 60;
select Sname ,t2.avgscore,t2.c from student t1 right join (select SID, count(cid) as c , avg(score) as avgscore from sc where score < 60 group by SID) as t2 on t1.SID = t2.SID

# 查询课程1 成绩比课程2 成绩低的学生 展示课程1 成绩 和课程2 成绩
# 查询课程1 的成绩
select * from sc where CID = '01';
select * from sc where CID = '02';

# 学生课程1 成绩 大于课程2 成绩的学生
select t1.SID,t1.CID,t1.score,t2.CID,t2.score from sc t1 right join (select * from sc where CID = '02') t2 on t1.SID = t2.SID where t1.score > t2.score;

# 学生按总成绩排名
# 统计每个学生总成绩
select SID,sum(score) from sc group by SID;

# 查询男女生人数
select Ssex ,count(*) from student group by student.Ssex;

# 查询每个课程的人数
SELECT sc.CId, count(sc.SId)
FROM sc
GROUP BY sc.CID;

# 查询各个学生的年龄段
select SID,Sname,(year(CURDATE())-year(Sage)) as age from student order by age;

# 增加两天
select DATE_ADD(Sage,INTERVAL 2 DAY ) from student;

# 设置变量
set @day = 8-dayofweek(curdate());
select @day;

# for 循环插入数据
# https://www.jianshu.com/p/cb0152efac32
create procedure for_insert(n int)
begin
declare i int default 1;
loop_name:loop
if i>n then
leave loop_name;
end if;
insert into student values (concat(i,'SID'),concat(i,'Sname'),curdate(),'female');
set i=i+1;
end loop;
end;
# 执行存储过程
call for_insert(5);
# 删除存储过程
drop procedure if exists sums;

# 查询本周的生日
select * from student
where date_format(Sage,'%m%d')
between date_format(curdate(),'%m%d')
and date_format(date_add(curdate(),interval @day day),'%m%d');

# 查询本月过生日的学生
select * from student
where month(Sage) = month(curdate())

# 查询下个月过生日的学生
select * from student
where MONTH(Sage) = month(DATE_ADD(CURDATE(),interval 1 month ));

# 查询在 上个月过生日的学习
select * from student
where month(Sage) = month(DATE_SUB(curdate(),interval 1 month ))

# 查询在6月到10月过生日的学生
select * from student
where month(Sage) between '04' and '11';

函数大全

​​参考​​

不常用语法

  1. UNION
    MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据。
  2. 设置变量
# 设置变量
set @day = 8-dayofweek(curdate());
select @day;
  1. 循环插入数值
# for 循环插入数据
# https://www.jianshu.com/p/cb0152efac32
create procedure for_insert(n int)
begin
declare i int default 1;
loop_name:loop
if i>n then
leave loop_name;
end if;
insert into student values (concat(i,'SID'),concat(i,'Sname'),curdate(),'female');
set i=i+1;
end loop;
end;
# 执行存储过程
call for_insert(5);
# 删除存储过程
drop procedure if exists sums;

常见问题

  1. ​​sql单引号 双引号问题​​
  2. ​​sql 事务详解​​
  3. ​​mysql 面试题​​

参考内容

​mysql 官方语法大全​​​​mysql 菜鸟教程​


标签:insert,MYSQL,详解,values,SID,sc,习题,where,select
From: https://blog.51cto.com/u_15704977/5855043

相关文章

  • Linux(Centos7)安装MySQL 8.0.28 详细步骤
    一、下载MySQL安装包下载地址:https://downloads.mysql.com/archives/community/ 二、解压并安装1.解压tar-xvfmysql-8.0.28-1.el7.x86_64.rpm-bundle.tar2.......
  • 3 django 配置mysql数据库
    1.MYSQL安装https://blog.csdn.net/qq_59636442/article/details/123058454数据库密码:123456端口号和用户名输入密码,点击check......
  • Pod详解之Pod调度(亲和性调度)
    亲和性调度两种定向调度的方式,使用起来非常方便,但是也有一定的问题,那就是如果没有满足条件的Node,那么Pod将不会被运行,即使在集群中还有可用Node列表也不行,这就限制了它的......
  • linux中mysql基础命令
    mysql服务命令1.linux下启动mysql的命令mysqladminstart/ect/init.d/mysqlstart(前面为mysql的安装路径)2.linux下重启mysql的命令:mysqladminrestart/ect/i......
  • MYSQL performance schema详解
    0、performance_schema的介绍​ MySQL的performanceschema用于监控MySQLserver在一个较低级别的运行过程中的资源消耗、资源等待等情况。​ 特点如下:​ 1、提供了......
  • MongoDB配置文件详解
    一配置文件说明MongoDB有两种配置文件格式,分别是:3.2版官方yaml配置文件选项参考用=号的常规格式类似my.conf等常规配置的文件yaml语法的新格式mongodb3.x版本后就......
  • Python locust工具使用详解
    今年负责部门的人员培养工作,最近在部门内部分享和讲解了locust这个工具,今天再博客园记录下培训细节。相信你看完博客,一定可以上手locust这个性能测试框架了。一、简介1......
  • 基于docker容器的MySQL主从设置及efcore读写分离
    1、基于docker部署MySQL,设置主从本操作基于已经拉取的镜像(dockerpullmysql)创建一主一从两个数据库容器dockerrun-d-p3307:3306-eMYSQL_ROOT_PASSWORD=123456--......
  • MySQL InnooDB引擎之并发事务问题以及隔离级别的作用和区别
    最近在复习MySQL事务,但网上很多博客和资料可以说讲的不是模棱两可就是只有文字描述不够形象易懂,下面通过我的学习来详细讲一讲事务并发都会引起哪些问题?以及隔离级别是什么......
  • Mysql操作学习总结
    Cmd控制台mysql-uroot-p--连接数据库flushprivileges;--刷新权限showdatabates;--查看所有的数据库use数据库名;--切换数据库showtables;--查看数据......