資料庫操作2

2021-08-14 21:02:30 字數 2483 閱讀 3966

1.備份資料至新錶進行操作

select * into mark_level from m_level 

select * into mark_readingoption from m_articleoption

select * into yubin_materiallevel from materiallevel

select * into yubin_readingmaterial from readingmaterial

select * into yubin_readingquestion from readingquestion

2.truncate table mark_readingoption

3.向mark_level 中新增materiallevel欄位

update mark_level set materiallevel=(levelno+8)

4.向yubin_readingmaterial表中加入 levelid 字段

查詢 select a.materiallevel,a.levelid,b.materiallevel,b.id from yubin_readingmaterial as a join 

mark_level as b on a.materiallevel = b.materiallevel

更新 update a set a.levelid=b.id from yubin_readingmaterial as a join 

mark_level as b on a.materiallevel = b.materiallevel  

5.向yubin_readingquestion表中加入articleid欄位

查詢 select * from yubin_readingquestion as a join yubin_readingmaterial as b on a.materialcode=b.materialcode 

更新 update a set a.articleid=b.id from yubin_readingquestion as a 

join yubin_readingmaterial as b on a.materialcode=b.materialcode 

6.向mark_readingoption表中加入answer欄位,匯入選項資料

insert into mark_readingoption(questionid,body,sn,answer) select id,optiona,'a',answer from yubin_readingquestion 

insert into mark_readingoption(questionid,body,sn,answer) select id,optionb,'b',answer from yubin_readingquestion 

insert into mark_readingoption(questionid,body,sn,answer) select id,optionc,'c',answer from yubin_readingquestion 

insert into mark_readingoption(questionid,body,sn,answer) select id,optiond,'d',answer from yubin_readingquestion 

刪除內容為空的選項 delete from mark_readingoption where body is null

7.填入答案  

update mark_readingoption set isanswer=1 where sn=answer

刪除 answer 字段

8.整理4張資料表,改為以a開頭的對應表,備份為b表。

9.修改a表相應的字段。

10.資料檢查

select a.name,b.title,b.body from a_level as a join a_article as b on b.levelid=a.id

where b.title='a clever bird'

select a.name,b.title,b.body,c.body from a_level as a join a_article as b on b.levelid=a.id 

join a_articlequestion as c on c.articleid=b.id

where b.title='a clever bird'

select a.name,b.title,b.body,c.body,d.body,d.isanswer from a_level as a join a_article as b on b.levelid=a.id

join a_articlequestion as c on c.articleid=b.id

join a_articleoption as d on d.questionid=c.id

where b.title='a clever bird' order by c.body

MySQL資料庫操作(2)基本操作

建立資料庫 create database if not exists 庫名 例子 create database mydb create database if not exists mydb 注意 如果庫名已經存在,不加上if not exists則會報錯,加上只會警告。刪除資料庫 drop d...

資料庫2之 庫的操作

1.資料庫的建立 語法 create database if no exists db name create specitification create specification create specification default character set charset name d...

JAVA 資料庫操作封裝(2)

相關資料 function executeupdate throws sqlexception public void executebatch throws sqlexception function addbatch param value string throws sqlexception ...