mysql增、删、改和单表查询多表查询

一、四大名著表t_hero的相关操作:

1.进入并创建db_ck数据库:

create database if not exists db_ck;
show databases;
use db_ck;

2.创建四大名著表t_hero并且插入一些数据:

创建t_hero表:

create table t_hero (
id int,
hero_name varchar(50),
book_name varchar(50)
);

 插入数据:

insert into t_hero value
(1, '贾宝玉', '《红楼梦》'),
(2, '林黛玉', '《红楼梦》'),
(3, '薛宝钗', '《红楼梦》'),
(4, '王熙凤', '《红楼梦》');insert into t_hero value
(5, '孙悟空', '《西游记》'),
(6, '猪八戒', '《西游记》'),
(7, '沙僧', '《西游记》'),
(8, '唐僧', '《西游记》');insert into t_hero value
(9, '宋江', '《水浒传》'),
(10, '林冲', '《水浒传》'),
(11, '吴用', '《水浒传》'),
(12, '武松', '《水浒传》');insert into t_hero value
(13, '刘备', '《三国演义》'),
(14, '关羽', '《三国演义》'),
(15, '张飞', '《三国演义》'),
(16, '诸葛亮', '《三国演义》');

 

3. 添加id为主键约束:

查看表结构:

desc t_hero;

添加主键约束:

alter table t_hero modify column id int auto_increment primary key;

4. 添加hero_name为唯一约束,且不为空:

alter table t_hero modify column hero_name varchar(50) not null,
add unique unique_hero_name(hero_name);

5.增加人物出现时的事件的列:

alter table t_hero add column incident varchar(255) not null;

 

6.更新各个人物出现的事件:

update t_hero set incident = "桃园结义" where hero_name = "刘备";
update t_hero set incident = "桃园结义" where hero_name = "关羽";
update t_hero set incident = "桃园结义" where hero_name = "张飞";
update t_hero set incident = "三顾茅庐" where hero_name = "诸葛亮";
update t_hero set incident = "开天辟地" where hero_name = "孙悟空";
update t_hero set incident = "高老庄" where hero_name = "猪八戒";
update t_hero set incident = "流沙河" where hero_name = "沙僧";
update t_hero set incident = "贞观十三年" where hero_name = "唐僧";
update t_hero set incident = "红楼梦元年" where hero_name = "贾宝玉";
update t_hero set incident = "红楼梦二年二月十二" where hero_name = "林黛玉";
update t_hero set incident = "红楼梦三年正月二十一" where hero_name = "薛宝钗";
update t_hero set incident = "红楼梦前九年九月初二" where hero_name = "王熙凤";
update t_hero set incident = "题反诗" where hero_name = "宋江";
update t_hero set incident = "误入白虎堂" where hero_name = "林冲";
update t_hero set incident = "景阳冈打虎" where hero_name = "武松";
update t_hero set incident = "十万生辰纲" where hero_name = "吴用";

 

 7.增加四大名著中的各个人物:

insert into t_hero(hero_name, book_name, incident) values("李逵", "《水浒传》", "真假李逵");
insert into t_hero(hero_name, book_name, incident) values("贾母", "《红楼梦》", "主持大观园建造");
insert into t_hero(hero_name, book_name, incident) values("曹操", "《三国演义》", "官渡之战击败袁绍");
insert into t_hero(hero_name, book_name, incident) values("白龙马", "《西游记》", "往西取经去,向东驮经来");

 

 8.删除四大名著当中的一位人物:

delete from t_hero where hero_name = "贾母";
delete from t_hero where hero_name = "李逵";
delete from t_hero where hero_name = "曹操";
delete from t_hero where hero_name = "白龙马";

9. 相关查询语句(不完全):

查询表中所以信息:

select * from t_hero;

查询需要的字段信息 :

select id, hero_name from t_hero;

 查询一个字段,一个等值条件:

select hero_name from t_hero where id = 1;

 查询所有西游记的人物信息:

select * from t_hero where book_name = "《西游记》";

 查询所有西游记以外的人物信息:

select * from t_hero where book_name != "《西游记》";

  id降序或升序排序查询:

select * from t_hero order by id desc;
select * from t_hero order by id asc;

 查询id小于12大于等于3的所有信息:

select * from t_hero where id >= 3 and id < 12;
select * from t_hero where id between 3 and 11;

二、worker表的单表查询:

创建worker表:

create table worker (部门号 int(11) not null,职工号 int(11) not null,工作时间 date not null,工资 float(8, 2) not null,政治面貌 varchar(10) not null default "群众",姓名 varchar(20) not null,出生日期 date not null,primary key(职工号)
) engine=InnoDB default charset=utf8 row_format=dynamic;

插入相关数据:

insert into worker (部门号, 职工号, 工作时间, 工资, 政治面貌, 姓名, 出生日期)
values (101, 1001, '2015-5-4', 3500.00, '群众', '张三', '1990-7-1');
insert into worker (部门号, 职工号, 工作时间, 工资, 政治面貌, 姓名, 出生日期)
values (101, 1002, '2017-2-6', 3200.00, '团员', '李四', '1997-2-8');
insert into worker (部门号, 职工号, 工作时间, 工资, 政治面貌, 姓名, 出生日期)
values (102, 1003, '2011-1-4', 8500.00, '党员', '王亮', '1983-6-8');
insert into worker (部门号, 职工号, 工作时间, 工资, 政治面貌, 姓名, 出生日期)
values (102, 1004, '2016-10-10', 5500.00, '群众', '赵六', '1994-9-5');
insert into worker (部门号, 职工号, 工作时间, 工资, 政治面貌, 姓名, 出生日期)
values (102, 1005, '2014-4-1', 4800.00, '党员', '钱七', '1992-12-30');
insert into worker (部门号, 职工号, 工作时间, 工资, 政治面貌, 姓名, 出生日期)
values (102, 1006, '2017-5-5', 4500.00, '党员', '孙八', '1996-9-2');

1.显示所有职工的基本信息:

select * from worker;

2.查询所有职工所属部门的部门号,不显示重复的部门号:

select distinct 部门号 from worker;


3.求出所有职工的人数:

select count(*) from worker;


4.列出最高工和最低工资:

select max(工资), min(工资) from worker;

5.列出职工的平均工资和总工资:

select avg(工资), sum(工资) from worker;

6.创建一个只有职工号、姓名和参加工作的新表,名为工作日期表:

create table worker_date (职工号 int(11) not null,姓名 varchar(50) not null,参加工作 varchar(255) not null,primary key(职工号)) engine=innodb default charset=utf8 row_format=dynamic;

insert into (职工号, 姓名, 参加工作)
values (1007, "王小白", "网络工程师");
insert into (职工号, 姓名, 参加工作)
values (1008, "刘姥姥", "渗透工程师");
insert into (职工号, 姓名, 参加工作)
values (1009, "陈小明", "网络安全工程师");
insert into (职工号, 姓名, 参加工作)
values (1010, "李小红", "");
insert into (职工号, 姓名, 参加工作)
values (1011, "李家明", "逆向工程师");
insert into (职工号, 姓名, 参加工作)
values (1012, "何下岑", "安全规划与设计");

7.显示所有女职工的年龄:

在worker表中添加性别字段:

alter table worker add column gender enum("男", "女") not null;

 

 修改worker表中员工性别信息:

update worker set gender = "女" where between 1003 and 1005;

添加worker表中年龄字段默认值为18:

修改年龄字段信息(年龄信息自己编的与实际不符):

 update worker set age = 28 where 姓名 = "张三";update worker set age = 25 where 姓名 = "李四";update worker set age = 24 where 姓名 = "王亮";update worker set age = 19 where 姓名 = "赵六";update worker set age = 30 where 姓名 = "钱七";

所有女职工的年龄:

select 姓名, gender, age from worker where gender = "女";

8.列出所有姓刘的职工的职工号、姓名和出生日期:

将姓刘的职工信息插入worker表:

insert into worker(部门号, 职工号, 工作时间, 工资, 政治面貌, 姓名, 出生日期, gender, age)
values(103, 1007, "2018-06-06", 6000.00, "党员", "刘姥姥", "2000-02-02", "女", 25);
insert into worker(部门号, 职工号, 工作时间, 工资, 政治面貌, 姓名, 出生日期, gender, age)
values(103, 1008, "2017-06-06", 6500.00, "团员", "刘老城", "1999-02-02", "男", 26);

姓刘的职工的职工号、姓名和出生日期:

select 职工号, 姓名, 出生日期 from worker where 姓名 like "刘%";


9.列出1960年以前出生的职工的姓名、参加工作日期:

插入1960年以前出生的职工信息:

insert into worker(部门号, 职工号, 工作时间, 工资, 政治面貌, 姓名, 出生日期, gender, age)
values(103, 1009, "1998-06-06", 6500.00, "团员", "陈平安", "1950-02-02", "男", 75);

1960年以前出生的职工的姓名、参加工作日期:

select 姓名, 工作时间 from worker where 出生日期 < "1960-01-01";


10.列出工资在1000-2000之间的所有职工姓名:

插入工资在1000-2000之间的职工信息:

insert into worker(部门号, 职工号, 工作时间, 工资, 政治面貌, 姓名, 出生日期, gender, age)
values(104, 1010, "2024-06-06", 1500.00, "团员", "顾灿", "2002-02-02", "男", 23);

 工资在1000-2000之间的所有职工姓名:

select 姓名 from worker where 工资 between 1000 and 2000;


11.列出所有陈姓和李姓的职工姓名:

select 姓名 from worker where 姓名 like "陈%" or 姓名 like "李%";


12.列出所有部门号为2和3的职工号、姓名、党员否:

 select 职工号, 姓名, 政治面貌 from worker where 部门号 like "%2" or 部门号 like "%3";


13.将职工表worker中的职工按出生的先后顺序排序:

select * from worker order by 出生日期 asc;


14.显示工资最高的前3名职工的职工号和姓名:

select 职工号, 姓名 from worker order by 工资 desc limit 3;


15.求出各部门党员的人数:

select 部门号, count(政治面貌) as 党员人数 from worker where 政治面貌 = "党员" group by 部门号;


16.统计各部门的总工资和平均工资:

 select 部门号, sum(工资) as 总工资, avg(工资) as 平均工资 from worker group by 部门号;


17.列出总人数大于3的部门号和总人数:

select 部门号, count(*) as 总人数 from worker group by 部门号 having count(*) > 3;

三、多表查询:

1.创建student和score表:

create table student (id int(10) not null unique primary key,name varchar(20) not null,sex varchar(20),birth year,department varchar(20),address varchar(50)
);

 

create table score (id int(10) not null unique primary key auto_increment,stu_id int(10) not null,c_name varchar(20),grade int(10)
);

 

2.为student表和score表增加记录:

向student表插入记录的insert语句如下:

INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');

 向score表插入记录的insert语句如下:

INSERT INTO score VALUES(NULL,901, '计算机',98);
INSERT INTO score VALUES(NULL,901, '英语', 80);
INSERT INTO score VALUES(NULL,902, '计算机',65);
INSERT INTO score VALUES(NULL,902, '中文',88);
INSERT INTO score VALUES(NULL,903, '中文',95);
INSERT INTO score VALUES(NULL,904, '计算机',70);
INSERT INTO score VALUES(NULL,904, '英语',92);
INSERT INTO score VALUES(NULL,905, '英语',94);
INSERT INTO score VALUES(NULL,906, '计算机',90);
INSERT INTO score VALUES(NULL,906, '英语',85);

 

3.查询student表的所有记录:

select * from student;


4.查询student表的第2条到4条记录:

select * from student order by id asc limit 3 offset 1;


5.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息:

select id, name, department from student;


6.从student表中查询计算机系和英语系的学生的信息:

select * from student where department = "计算机系" or department = "英语系";


7.从student表中查询年龄18~22岁的学生信息:


8.从student表中查询每个院系有多少人:

 select department, count(department) from student group by department;


9.从score表中查询每个科目的最高分:

 select c_name, max(grade) as 最高分 from score group by c_name;


10.查询李四的考试科目(c_name)和考试成绩(grade):

select c_name, grade from student, score where student.id = score.stu_id and name = "李四";


11.用连接的方式查询所有学生的信息和考试信息:


12.计算每个学生的总成绩
13.计算每个考试科目的平均成绩
14.查询计算机成绩低于95的学生信息
15.查询同时参加计算机和英语考试的学生的信息
16.将计算机考试成绩按从高到低进行排序
17.从student表和score表中查询出学生的学号,然后合并查询结果
18.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
19.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/41163.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

springboot整合couchbase(集群)

springboot整合couchbase 1、Couchbase1.1、介绍1.2、Bucket1.3、Couchbase SDK 2、(key,value)写入couchbase集群2.1、总体图2.2、依赖2.3、CouchbaseConfig 配置文件2.4、代码使用 1、Couchbase 1.1、介绍 1.2、Bucket 在 Couchbase 中&#xff0c;bucket 是一个重要的概念…

【博客节选】再谈Unity 的 root motion

节选自 【Unity实战笔记】第二十三 root motion变更方向攻击 &#xff08;OnStateMove rootmotion rigidbody 使用的一些问题&#xff09; 小伙伴们应该对root motion非常困惑&#xff0c;包括那个bake into pose。 当xz bake into pose后&#xff0c;角色攻击动画与父节点产…

从零开始的大模型强化学习框架verl解析

之前在职的时候给一些算法的同学讲解过verl的框架设计、实现细节以及超参配置&#xff0c;写这篇文章姑且作为离职修养这段时期的复健。 本文中提到的做法和思路可能随着时间推移有变化&#xff0c;或者是思想迪化&#xff0c;仅代表个人理解。如果有错漏的地方还请指出。 现…

回归——数学公式推导全过程

文章目录 一、案例引入 二、如何求出正确参数 1. 最速下降法 1&#xff09;多项式回归 2&#xff09;多重回归 2. 随机梯度下降法 一、案例引入 以Web广告和点击量的关系为例来学习回归&#xff0c;假设投入的广告费和点击量呈现下图对应关系。 思考&#xff1a;如果花了…

【AVRCP】深度解析蓝牙高速(AMP)在封面艺术传输中的应用:低延迟体验的工程实践

目录 一、AMP 技术架构与封面艺术传输需求 1.1 蓝牙高速技术背景 1.2 AMP技术原理 1.3 蓝牙协议栈演进&#xff08;AMP 协议栈架构&#xff09; 1.4 封面艺术传输的技术挑战 1.5 AMP 关键特性&#xff08;BR/EDR vs AMP 对比&#xff09; 1.6 封面艺术传输模型&#xff…

Spring Boot 连接 MySQL 配置参数详解

Spring Boot 连接 MySQL 配置参数详解 前言参数及含义常用参数及讲解和示例useUnicode 参数说明&#xff1a; 完整配置示例注意事项 前言 在 Spring Boot 中使用 Druid 连接池配置 MySQL 数据库连接时&#xff0c;URL 中 ? 后面的参数用于指定连接的各种属性。以下是常见参数…

智能科技与美学融合,赵伟辰荣膺 2025 iF 设计大奖

近日,全球设计界享有盛誉的 iF 设计奖(iF Design Award)正式公布 2025 年度获奖名单。设计师赵伟辰凭借其创新力作Multi-Scenario Modular Control System(多场景模块化控制系统),从全球 10,000 余件参赛作品中脱颖而出,斩获这一全球瞩目的奖项。他凭借卓越的用户体验优化能力与…

NotePad++与Navicat工具的下载 完全免费无套路

https://qr61.cn/o7ciDN/qINyVn3 打开链接 获取下载即可 免费 免费 免费 重要的事情说三遍&#xff01; 这是本人自己搜集资源与发布和共享的最新版&#xff0c;无任何费用 需要工具自行下载即可。 由于人少力薄 资源更新较慢 请大家耐心等待 多多关注 谢谢~ 下面给大家截图…

K8S学习之基础五十一:k8s部署jenkins

k8s部署jenkins 创建nfs共享目录&#xff0c; mkdir -p /data/v2 echo /data/v2 *(rw,no_root_squash) > /etc/exports exportfs -arv创建pv、pvc vi pv.yaml apiVersion: v1 kind: PersistentVolume metadata:name: jenkins-k8s-pv spec:capacity:storage: 1GiaccessMod…

印刷电路板 (PCB) 的影响何时重要?在模拟环境中导航

我和我的同事们经常被问到关于 PCB 效应的相同问题&#xff0c;例如&#xff1a; 仿真何时需要 PCB 效果&#xff1f; 为什么时域仿真需要 PCB 效应&#xff1f; 当 PCB 效应必须包含在仿真中时&#xff0c;频率是否重要&#xff1f; 设计人员应该在多大程度上关注 VRM 模型中包…

车载以太网网络测试 -24【SOME/IP概述】

目录 1 摘要2 车载SOME/IP 概述2.1发展背景以及应用2.1.1车载 SOME/IP 背景2.1.2 车载 SOME/IP 应用场景 2.3 什么是SOME/IP2.3.1 SOME/IP定义2.3.2 SOME/IP在协议栈中的位置 3 SOA是什么4 SOME/IP主要功能5 SOME/IP标准 1 摘要 本文主要介绍SOME/IP的背景以及在车载行业的发展…

如何在 Postman 中导入和导出 cURL 命令?

cURL 是一款广受欢迎的命令行工具&#xff0c;专门用于执行 HTTP 请求。它在 Web 应用或 API 测试中极为实用&#xff0c;让用户得以借助在 API 开发者社区广为流行的成熟语法&#xff0c;直接通过命令行与 API 进行交互。若你需要在多个环境下运行众多 cURL 命令&#xff0c;可…

K8S学习之基础五十五:k8s中jenkins部署blueOcean

jenkins部署blueOcean 安装插件 BLUE OCEAN 之后会多出一个菜单&#xff0c;可以更详细方便的查看pipeline流程

宝塔docker flarum默认登录账号密码,crazymax/flarum镜像默认登录账号密码

docker flarum默认账号密码 刚创建完毕时的登录账号和密码都是flarum 来源说明 宝塔安装的这个1.8.5版本的docker flarum的版本是&#xff0c;用的是 Docker库 https://hub.docker.com/r/crazymax/flarum Github库 https://github.com/crazy-max/docker-flarum

3.26学习总结

今天主要学习了内部类&#xff0c;但总感觉有点混乱&#xff0c;和之前的抽象啊&#xff0c;接口&#xff0c;多态等概念联系在一起感觉更混乱了&#xff0c;所以打算先把最近学的理清一遍&#xff0c;敲一遍代码再往后学

如何快速解决 Postman 报错?

介绍一些 Postman 常见的报错与处理方法&#xff0c;希望能够对大家有所帮助。 Postman 一直转圈打不开的问题 Postman 报错处理指南&#xff1a;常见报错与解决方法

高铁监控存储扩容-DS SAN存储磁盘阵列

国家高铁建设之内蒙包银高铁线路段--近期升级改造车站监控设备存储扩容&#xff0c;每个车站的机房承载本站数百个监控点位的数据&#xff0c;现因监控服务器存储空间不足&#xff0c;选用Infortrend DS SAN存储磁盘阵列升级设备存储空间&#xff0c;单台DS最高支持448颗硬盘容…

rnn的ho的维度 (num_layers * num_directions, batchsize, hidden_size)

因为是多层rnn 所以h0 需要 num_layers 来定义几层 这里的hidden_size 其实也就是h中有多少个神经元的个数

同旺科技USB to I2C 适配器 ---- 多从机设备混合调试

所需设备&#xff1a; 内附链接 1、同旺科技USB to I2C 适配器 1、还在为一条I2C总线上出现多个从机设备而烦恼吗&#xff1f;现在这些都可以轻松解决了&#xff0c;在 "发送数据" 栏里面&#xff0c;修改指令的从机地址就可以了&#xff0c;同时支持7Bit、8Bit地址…

如何下载 Postman?快速指南!

Postman 是一款非常受欢迎的 API 测试工具。它最初是作为一个 Chrome 插件发布&#xff0c;后来发展成为一款独立的跨平台软件&#xff0c;支持 Windows、Mac、Linux 等操作系统。 Postman 怎么下载教程&#xff08;2025最新版&#xff09;&#xff1f;