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

数据库第三次和第四次作业

创建数据库mydb11_stu并进入数据库:
create database mydb11_stu; use mydb11_stu;
创建student表
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));
创建score表:
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));
插入数据:
向score表插入:
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,'计算机',49); insert into score values(null,906,'英语',83);

向student 表插入:
insert into student values(901,'张三丰','男',2002,'计算机系','北京市海淀区'); insert into student values(902,'周全有','男',2000,'中文系','北京市昌平区'); insert into student values(903,'张思维','女',2003,'中文系','湖南省永州市'); insert into student values(904,'李广昌','男',1999,'英语系','辽宁省皋新市'); insert into student values(905,'王翰','男',2004,'英语系','福建省厦门市'); insert into student values(906,'王心凌','女',1998,'计算机系','湖南省衡阳市');

查询:
(1)
select * from student; select * from score;
(2)
select * from student order by id limit 1,4;
(3)
select * from student where department in ('计算机系','英语系');
(4)
select * from student where 2025-birth<22;
(5)
select department ,count(*) as 人数 from student group by department;
(6)
select c_name,max(grade) as 最高分 from score group by c_name;
(7)
select s.c_name,s.grade from score s join student st on s.stu_id=st.id where st.name='李广昌';
(8)
select st.*,s.c_name,s.grade from student st join score s on st.id=s.stu_id;
(9)
select st.name,sum(s.grade) as 总成绩 from student st join score s on st.id=s.stu_id group by st.id;
(10)
select c_name,avg(grade) as 平均成绩 from score group by c_name;
(11)
select st.* from student st join score s on st.id=s.stu_id where s.c_name='计算机' and s.grade<95;
(12)
select st.name,s.grade from student st join score s on st.id=s.stu_id where s.c_name='计算机' order by s.grade desc;
(13)
select id as 学号 from student union select stu_id from score;
(14)
select st.name,st.department,s.c_name,s.grade from student st join score s on st.id=s.stu_id where st.name like '张%' or st.name like '王%';
(15)
select st.name,2025-st.birth as 年龄,st.department,s.c_name,s.grade from student st join score s on st.id=s.stu_id where st.address like '%湖南省%';

1、备份与恢复数据表
备份数据表:
mysqldump -uroot -p123456 mydb9_stusys student sc course > F:\Users\G15\st_msg_bak.sql
恢复数据表:
1、登录MySQL
2、创建db_test数据库
create database db_test;
3、退出MySQL(exit),在命令行中恢复命令
mysql -uroot -p123456 db_test < F:\Users\G15\st_msg_bak.sql

2、创建视图stu_info
use db_test; create view stu_info as select s.sname,s.ssex,c.cname,sc.score from student s join sc on s.sno=sc.sno join course c on sc.cno=c.cno;

3、查看mydb9_stusys库下的视图表
use mydb9_stusys; select table_name from information_schema.views where table_schema='mydb9_stusys';

4、删除视表图
use db_test; drop view if exists stu_info;

http://www.dtcms.com/a/287733.html

相关文章:

  • 异步解决一切问题 |消息队列 |减少嵌套 |hadoop |rabbitmq |postsql
  • 计算机网络体系结构
  • 【Java源码阅读系列56】深度解读Java Constructor 类源码
  • 物联网系统中-设备管理定义方法
  • 物联网iot、mqtt协议与华为云平台的综合实践(万字0基础保姆级教程)
  • Hyperliquid:探索去中心化衍生品交易的“速度与激情”
  • C++ 内存管理详解(new,delete)
  • 1. Spring AI概述
  • 暑假训练七
  • 在非Spring Boot的Spring项目中使用Lock4j
  • 让 Windows 用上 macOS 的系统下载与保姆级使用教程
  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘sqlalchemy’问题
  • 力扣经典算法篇-26-长度最小的子数组(暴力求解法,左右指针法)
  • ARINC818协议综述
  • Python+ArcGIS+AI蒸散发与GPP估算|Penman-Monteith模型|FLUXNET数据处理|多源产品融合|专业科研绘图与可视化等
  • 多式联运物流管理系统的设计与实现(原创)
  • JavaScript中的位运算符:深入理解<<和>>>
  • OpenCV 官翻 3 - 特征检测 Feature Detection
  • 语义熵怎么增强LLM自信心的
  • react17更新哪些新特性
  • 【I2C】01.I2C硬件连接I2C总线时序图讲解
  • 疯狂星期四文案网第12天运营日报
  • 提高CPU高速缓存cache命中率的主要设计方案
  • SpringBoot五分钟快速入门指南
  • 锂电池生产过程图解
  • 鼎捷T100程序开发(批次作业开发)
  • 新手向:基于 Python 的简易视频剪辑工具
  • 使用 go-redis-entraid 实现 Entra ID 无密钥认证
  • 一动一静皆消耗——IC设计之低功耗技术(Low Power Design)
  • javaweb的几大常见漏洞