当前位置: 首页 > news >正文

数据库练习

 完善t_hero表

-- 添加作者字段
alter table t_hero add author varchar(100);

-- 更新数据

update  t_hero set author = "曹雪芹" where id = 1;
update  t_hero set author = "曹雪芹" where id = 2;
update  t_hero set author = "曹雪芹" where id = 3;
update  t_hero set author = "曹雪芹" where id = 4;

update  t_hero set author = "吴承恩" where id = 5;
update  t_hero set author = "吴承恩" where id = 6;
update  t_hero set author = "吴承恩" where id = 7;
update  t_hero set author = "吴承恩" where id = 8;

update  t_hero set author = "罗贯中" where id = 9;
update  t_hero set author = "罗贯中" where id = 10;
update  t_hero set author = "罗贯中" where id = 11;
update  t_hero set author = "罗贯中" where id = 12;
update  t_hero set author = "罗贯中" where id = 13;

update  t_hero set author = "施耐庵" where id = 14;
update  t_hero set author = "施耐庵" where id = 15;
update  t_hero set author = "施耐庵" where id = 16;
update  t_hero set author = "施耐庵" where id = 17;
update  t_hero set author = "施耐庵" where id = 18;

-- 查看t_hero表
select * from t_hero;

-- 根据id删除数据

delete from t_hero where id = 18;

select * from t_hero;

 

-- 增加数据
insert into t_hero(name, book, author) VALUES ('武松', '水浒传', '施耐庵');
select * from t_hero;

单表查询 

素材: 表名: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(`工资`) from worker;
select min(`工资`) from worker;

5、列出职工的平均工资和总工资。

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


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

CREATE TABLE `工作日期表` (
         `职工号` int(11) NOT NULL,
         `姓名` varchar(20) NOT NULL,
         `出生日期` date NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

7、显示所有党员的名字。 

select `姓名` from worker where `政治面貌` = '党员';


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

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


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

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


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

select `姓名` from worker where "工资" > 2000 and "工资" < 3000;


11、列出所有陈姓和李姓的职工姓名。

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

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

select `职工号`, `姓名`, `政治面貌` from worker where `部门号` = 102 or `部门号` = 103; 


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

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


14、显示工资最高的前3名职工的职工号和姓名。 

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


15、求出各部门党员的人数。 

select count(*) as 党员人数 from worker where 政治面貌="党员";


16、统计各部门的工资和平均工资

select `工资`,  (select  avg(`工资`) from worker where `部门号` = 101) as 平均工资  from worker where `部门号` = 101;
select `工资`,  (select  avg(`工资`) from worker where `部门号` = 102) as 平均工资  from worker where `部门号` = 102;


17、列出总人数大于4的部门号和总人数。

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

多表查询 

1.创建student和score表

-- 创建student和score表
CREATE  TABLE student (
id  INT(10)  NOT NULL  UNIQUE  PRIMARY KEY ,
name  VARCHAR(20)  NOT NULL ,
sex  VARCHAR(4) ,
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 where id = 902 or id = 904;

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

select id, name,  department from student;


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

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


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

select * from student where (2025 - birth) between 2007 and 2003;


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

select department, count(*) as 人数 from student group by department;


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

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

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

select name, c_name, grade from score,student where stu_id = 904 and student.id = stu_id;


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

select * from student, score where student.id = stu_id;


12.计算每个学生的总成绩

SELECT
         student.id AS 学生ID,
         student.name AS 姓名,
         SUM(score.grade) AS 总成绩
     FROM
         student
     INNER JOIN
         score ON student.id = score.stu_id
     GROUP BY
         student.id, student.name;


13.计算每个考试科目的平均成绩

select c_name, avg(grade) from score group by c_name;


14.查询计算机成绩低于95的学生信息

select * from student,score where student.id = stu_id and  grade < 95 and c_name = "计算机";


15.查询同时参加计算机和英语考试的学生的信息

select * from student,score 
where student.id = stu_id 
and 
student.id in (select stu_id from score where c_name = "计算机") 
and 
student.id in (select stu_id from score where c_name = "英语")
; 


16.将计算机考试成绩按从高到低进行排序

select c_name,grade from score  where c_name="计算机" order by  grade desc;


17.从student表和score表中查询出学生的学号,然后合并查询结果

SELECT id AS 学号 FROM student
UNION
SELECT stu_id AS 学号 FROM score;


18.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩

SELECT
         s.name AS 姓名,
         s.department AS 院系,
         sc.c_name AS 考试科目,
         sc.grade AS 成绩
     FROM
         student s
     JOIN
         score sc ON s.id = sc.stu_id
     WHERE
         s.name LIKE '张%' OR s.name LIKE '王%';


19.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩

select name, birth, department, c_name, grade from student join score on student.id = score.stu_id where address like "湖南%";

相关文章:

  • 使用ZMQ和protobuf实现C++程序与Python程序的通信
  • 多行为推荐综述
  • 混境之地1
  • 批量删除 PDF 中的所有图片、所有二维码图片以及指定的某张图片
  • CCF CSP 第33次(2024.03)(2_相似度计算_C++)(字符串中字母大小写转换+哈希集合)
  • Mysql的单表查询和多表查询
  • Cookie、sessionStorage、localStorage
  • vue3(笔记)5.0--pinia工具的知识扩展
  • 系统工程-信息系统的分类
  • How to use pgbench to test performance for PostgreSQL?
  • 【C++】String类的模拟实现
  • [Qt5] QMetaObject::invokeMethod使用
  • Netty源码—7.ByteBuf原理三
  • 蓝桥云客-染色时间
  • 1424.对角线遍历
  • 322 零钱兑换
  • 【大模型基础_毛玉仁】4.4 低秩适配方法
  • RocketMQ 4.x、5.x 性能对比
  • 绩效考核如何从形式化任务升级为公司战略工具?
  • 2025.3.25
  • “一节课、两小时”,体育正在回归“C位”
  • 被取消总统候选人资格,金文洙:将采取政治法律措施讨回公道
  • 国家发改委副主任谈民营经济促进法:以法治的稳定性增强发展的确定性
  • 两部门发布外汇领域行刑反向衔接案例,织密金融安全“防护网”
  • 普京:“胜利日停火”已开始生效
  • 上海:5月8日起5年以上首套个人住房公积金贷款利率下调至2.6%