MySQL第七章 数据库管理与应用

create database two20210610 default charset=utf8;

use one20210606; CREATE TABLE studentinfo ( StudentID char(10) DEFAULT NULL, StudentName varchar(20) DEFAULT NULL, Gender varchar(2) DEFAULT NULL, Birthday date DEFAULT NULL, ClassID int(4) DEFAULT NULL, BeginYear year(4) DEFAULT NULL, Phone varchar(11) DEFAULT NULL, Province varchar(20) DEFAULT NULL, City varchar(20) DEFAULT NULL, Email varchar(50) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

create table teacher ( id int, teachername VARCHAR(10), hiredate DATE, gender char

);

create table classinfo ( id int, # 班级编号 classname varchar(10), gradeid int, # 年级编号 beginyear varchar(10) # 开始年份

);

create table subject ( id int , subjectname varchar(10), teacherid int );

create table exam ( id int, exam int, subjectid int, studentid int, remark varchar(10) # 备注 );

create table grade( id int , gradename varchar(10), major varchar(20) );

INSERT INTO one20210606.classinfo(id, classname, gradeid, beginyear) VALUES (2001, '20级1班', 20, '2020'); INSERT INTO one20210606.classinfo(id, classname, gradeid, beginyear) VALUES (2002, '20级2班', 20, '2020');

INSERT INTO one20210606.exam(id, exam, subjectid, studentid, remark) VALUES (1, 98, 101, 1, '无'); INSERT INTO one20210606.exam(id, exam, subjectid, studentid, remark) VALUES (2, 89, 102, 1, NULL); INSERT INTO one20210606.exam(id, exam, subjectid, studentid, remark) VALUES (3, 79, 103, 1, ''); INSERT INTO one20210606.exam(id, exam, subjectid, studentid, remark) VALUES (4, 96, 104, 1, NULL); INSERT INTO one20210606.exam(id, exam, subjectid, studentid, remark) VALUES (5, 85, 101, 2, NULL); INSERT INTO one20210606.exam(id, exam, subjectid, studentid, remark) VALUES (6, 89, 102, 2, NULL); INSERT INTO one20210606.exam(id, exam, subjectid, studentid, remark) VALUES (7, 79, 103, 2, NULL); INSERT INTO one20210606.exam(id, exam, subjectid, studentid, remark) VALUES (8, 83, 104, 2, NULL);

INSERT INTO one20210606.grade(id, gradename, major) VALUES (20, '20级', '软件技术');

INSERT INTO one20210606.studentinfo(StudentID, StudentName, Gender, Birthday, ClassID, BeginYear, Phone, Province, City, Email) VALUES ('1', '张无忌', '男', '2021-06-10', 2001, 2020, '13012340001', '河南', '许昌', 'zwj@qq.com'); INSERT INTO one20210606.studentinfo(StudentID, StudentName, Gender, Birthday, ClassID, BeginYear, Phone, Province, City, Email) VALUES ('2', '张铁牛', '男', '2021-06-16', 2001, 2020, '13012340002', '河南', '许昌', 'ztn@qq.com'); INSERT INTO one20210606.studentinfo(StudentID, StudentName, Gender, Birthday, ClassID, BeginYear, Phone, Province, City, Email) VALUES ('3', '林平之', '男', '2021-06-15', 2001, 2020, '13012340003', '河南', '安阳', 'lpz@qq.com'); INSERT INTO one20210606.studentinfo(StudentID, StudentName, Gender, Birthday, ClassID, BeginYear, Phone, Province, City, Email) VALUES ('4', '令狐冲', '男', '2021-06-08', 2002, 2019, '13012340004', '河南', '濮阳', 'lhc@qq.com'); INSERT INTO one20210606.studentinfo(StudentID, StudentName, Gender, Birthday, ClassID, BeginYear, Phone, Province, City, Email) VALUES ('5', '岳灵珊', '女', '2021-06-02', 2002, 2019, '13012340005', '河南', '南阳', 'yls@qq.com');

INSERT INTO one20210606.subject(id, subjectname, teacherid) VALUES (101, 'mysql', 201); INSERT INTO one20210606.subject(id, subjectname, teacherid) VALUES (102, 'java基础', 201); INSERT INTO one20210606.subject(id, subjectname, teacherid) VALUES (103, '面向对象', 202); INSERT INTO one20210606.subject(id, subjectname, teacherid) VALUES (104, '网页设计', 202);

INSERT INTO one20210606.teacher(id, teachername, hiredate, gender) VALUES (201, 'hys', '2021-06-04', '男'); INSERT INTO one20210606.teacher(id, teachername, hiredate, gender) VALUES (202, 'dj', '2021-06-01', '女');

-- 子查询 用子查询实现,查询出学生“林平之”的同班同学 --1 查出林平之 的班级 select classid from studentinfo where studentname='林平之'; --2 根据班级查询同班学生信息 select * from studentinfo where chassid=2001; -- 合并 select * from studentinfo where classid=(select classid from studentinfo where studentname='林平之');

-- 查询 《软件技术》 考试成绩刚好等于90分的学生名单 -- 1 需要查询学生名单 再studentinfo -- 2 科目是mysql 再subject表中 id -- 3 成绩是90 再exam表中subjectid

select studentname from studentinfo inner join exam on studentinfo.StudentIDexam.studentid inner join subject.idexam.subjectid where subject.subjectname='mysql' and exam.exam=85;

-- 查询 《mysql》考试成绩刚好等于85分的学生名单

根据学生id查询学生的名单

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n23" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">select studentname from studentinfo where id = ?</pre>

在成绩表中找到成绩为85的学生id

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n25" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">select studentid from exam where exam=85;</pre>

在课程表中找到mysql的课程id

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n27" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">select id from subject where subjectname='mysql';</pre>

-- 融合 select studentname from studentinfo where id = (select studentid from exam where exam=85 and subject = (select id from subject where subjectname='mysql') ) ; -- 更新“dj”的“mysql”成绩为+5分

-- 找到....成绩 +5 exam id subjectid studentid update exam set exam = exam + 5 where subjectid = ? -- 找到mysql课程的id subject id subjectname select id from subject where subjectname = "网页设计" -- 根据名字找id teacher select id from teacher where teachername='dj'

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n30" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">-- 融合
update exam set exam = exam + 5 where subjectid = (select id from subject where subjectname = "网页设计" and subject.teacherid = (select id from teacher where teachername='dj' ))</pre>

-- 删除“岳灵珊”的所有考试成绩 -- 1 在studentinfo表中找到岳灵珊的id select studentid from studentinfo where studentname="岳灵珊" -- 2 根据id在exam表中删除成绩 delect from exam where studentid=?

-- 合并 delect from exam where studentid= (select studentid from studentinfo where studentname="张无忌");

-- 插入的子查询 把查询结果作为条件 插入到新的数据 insert into studentinfo values (select * from studentinfo where studentid=5)

-- 查询《mysql》考试成绩刚好等于85分的学生名单 in的使用 在....范围之内

根据学生id查询学生的名单

select studentname from studentinfo where studentid = ?

在成绩中找到成绩为85的学生id

select studentid from exam where exam=85;

在课程表上找到mysql的课程id

select id from subject where subjectname='mysql'; -- 融合 select studentname from studentinfo where studentid in (select student from exam=85 and subjectid in (select id from subject where subjectname='mysql') );

-- 查询成绩表中科目编号为102的考试成绩中是否存在不及格的学生,如果存在不及格的学生就将参加科目编号102考试的学生编号和成绩全部查询显示出来 -- 是否存在不及格的学生 SELECT StudentID fromEXAM WHERE Exam<60 -- 参加科目编号102考试的学生 WHERE SubjectID=102 -- 学生编号和成绩全部查询显示出来 SELECT StudentID,Exam FROM EXAM -- 融合 SELECT StudentID,Exam FROM EXAM WHERE SubjectID=102 AND EXISTS (SELECT StudentID from EXAM WHERE Exam<60);

-- 查询成绩比科目编号为“1”的这门课程的所有成绩都大的学生考试信息 -- 1 查询....学生考试信息 SELECR * FROM Exam WHERE where ? -- 2 成绩比科目编号为“101”的这门课程的所有成绩都大 all(SELECT Exam from EXAM WHERE SubjectID=1) -- 合并 SELECT * FROM exam where exam > all(SELECT Exam from EXAM WHERE subjectid=101);

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,378评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,356评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,702评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,259评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,263评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,036评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,349评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,979评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,469评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,938评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,059评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,703评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,257评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,262评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,501评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,792评论 2 345

推荐阅读更多精彩内容