MySQL第七章 數(shù)據(jù)庫管理與應(yīng)用

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級', '軟件技術(shù)');

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基礎(chǔ)', 201); INSERT INTO one20210606.subject(id, subjectname, teacherid) VALUES (103, '面向?qū)ο?, 202); INSERT INTO one20210606.subject(id, subjectname, teacherid) VALUES (104, '網(wǎng)頁設(shè)計(jì)', 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', '女');

-- 子查詢 用子查詢實(shí)現(xiàn)桐款,查詢出學(xué)生“林平之”的同班同學(xué) --1 查出林平之 的班級 select classid from studentinfo where studentname='林平之'; --2 根據(jù)班級查詢同班學(xué)生信息 select * from studentinfo where chassid=2001; -- 合并 select * from studentinfo where classid=(select classid from studentinfo where studentname='林平之');

-- 查詢 《軟件技術(shù)》 考試成績剛好等于90分的學(xué)生名單 -- 1 需要查詢學(xué)生名單 再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分的學(xué)生名單

根據(jù)學(xué)生id查詢學(xué)生的名單

<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>

在成績表中找到成績?yōu)?5的學(xué)生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”成績?yōu)?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 = "網(wǎng)頁設(shè)計(jì)" -- 根據(jù)名字找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 = "網(wǎng)頁設(shè)計(jì)" and subject.teacherid = (select id from teacher where teachername='dj' ))</pre>

-- 刪除“岳靈珊”的所有考試成績 -- 1 在studentinfo表中找到岳靈珊的id select studentid from studentinfo where studentname="岳靈珊" -- 2 根據(jù)id在exam表中刪除成績 delect from exam where studentid=?

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

-- 插入的子查詢 把查詢結(jié)果作為條件 插入到新的數(shù)據(jù) insert into studentinfo values (select * from studentinfo where studentid=5)

-- 查詢《mysql》考試成績剛好等于85分的學(xué)生名單 in的使用 在....范圍之內(nèi)

根據(jù)學(xué)生id查詢學(xué)生的名單

select studentname from studentinfo where studentid = ?

在成績中找到成績?yōu)?5的學(xué)生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的考試成績中是否存在不及格的學(xué)生崭别,如果存在不及格的學(xué)生就將參加科目編號102考試的學(xué)生編號和成績?nèi)坎樵冿@示出來 -- 是否存在不及格的學(xué)生 SELECT StudentID fromEXAM WHERE Exam<60 -- 參加科目編號102考試的學(xué)生 WHERE SubjectID=102 -- 學(xué)生編號和成績?nèi)坎樵冿@示出來 SELECT StudentID把鉴,Exam FROM EXAM -- 融合 SELECT StudentID,Exam FROM EXAM WHERE SubjectID=102 AND EXISTS (SELECT StudentID from EXAM WHERE Exam<60);

-- 查詢成績比科目編號為“1”的這門課程的所有成績都大的學(xué)生考試信息 -- 1 查詢....學(xué)生考試信息 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);

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末唤殴,一起剝皮案震驚了整個濱河市欢策,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌移国,老刑警劉巖篇恒,帶你破解...
    沈念sama閱讀 212,686評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異琅拌,居然都是意外死亡缨伊,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,668評論 3 385
  • 文/潘曉璐 我一進(jìn)店門进宝,熙熙樓的掌柜王于貴愁眉苦臉地迎上來刻坊,“玉大人,你說我怎么就攤上這事党晋√放撸” “怎么了?”我有些...
    開封第一講書人閱讀 158,160評論 0 348
  • 文/不壞的土叔 我叫張陵未玻,是天一觀的道長灾而。 經(jīng)常有香客問我,道長扳剿,這世上最難降的妖魔是什么旁趟? 我笑而不...
    開封第一講書人閱讀 56,736評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮庇绽,結(jié)果婚禮上锡搜,老公的妹妹穿的比我還像新娘。我一直安慰自己瞧掺,他們只是感情好耕餐,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,847評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著辟狈,像睡著了一般肠缔。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上哼转,一...
    開封第一講書人閱讀 50,043評論 1 291
  • 那天明未,我揣著相機(jī)與錄音,去河邊找鬼壹蔓。 笑死亚隅,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的庶溶。 我是一名探鬼主播煮纵,決...
    沈念sama閱讀 39,129評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼偏螺!你這毒婦竟也來了行疏?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,872評論 0 268
  • 序言:老撾萬榮一對情侶失蹤套像,失蹤者是張志新(化名)和其女友劉穎酿联,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體夺巩,經(jīng)...
    沈念sama閱讀 44,318評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡贞让,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,645評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了柳譬。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片喳张。...
    茶點(diǎn)故事閱讀 38,777評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖美澳,靈堂內(nèi)的尸體忽然破棺而出销部,到底是詐尸還是另有隱情,我是刑警寧澤制跟,帶...
    沈念sama閱讀 34,470評論 4 333
  • 正文 年R本政府宣布舅桩,位于F島的核電站,受9級特大地震影響雨膨,放射性物質(zhì)發(fā)生泄漏擂涛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,126評論 3 317
  • 文/蒙蒙 一聊记、第九天 我趴在偏房一處隱蔽的房頂上張望撒妈。 院中可真熱鬧,春花似錦甥雕、人聲如沸踩身。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,861評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽挟阻。三九已至,卻和暖如春峭弟,著一層夾襖步出監(jiān)牢的瞬間附鸽,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,095評論 1 267
  • 我被黑心中介騙來泰國打工瞒瘸, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留坷备,地道東北人。 一個月前我還...
    沈念sama閱讀 46,589評論 2 362
  • 正文 我出身青樓情臭,卻偏偏與公主長得像省撑,于是被迫代替她去往敵國和親赌蔑。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,687評論 2 351

推薦閱讀更多精彩內(nèi)容