國家開放大學(xué)電大《MySQL數(shù)據(jù)庫應(yīng)用》網(wǎng)絡(luò)核心課實(shí)驗(yàn)訓(xùn)練3及4答案_第1頁
國家開放大學(xué)電大《MySQL數(shù)據(jù)庫應(yīng)用》網(wǎng)絡(luò)核心課實(shí)驗(yàn)訓(xùn)練3及4答案_第2頁
國家開放大學(xué)電大《MySQL數(shù)據(jù)庫應(yīng)用》網(wǎng)絡(luò)核心課實(shí)驗(yàn)訓(xùn)練3及4答案_第3頁
國家開放大學(xué)電大《MySQL數(shù)據(jù)庫應(yīng)用》網(wǎng)絡(luò)核心課實(shí)驗(yàn)訓(xùn)練3及4答案_第4頁
國家開放大學(xué)電大《MySQL數(shù)據(jù)庫應(yīng)用》網(wǎng)絡(luò)核心課實(shí)驗(yàn)訓(xùn)練3及4答案_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、最新國家開放大學(xué)電大mysql數(shù)據(jù)庫應(yīng)用網(wǎng)絡(luò)核心課實(shí)驗(yàn)訓(xùn)練3及4答案盜傳必究本課程分為形成性考核和終結(jié)性末考兩種考核形式,形成性考核占50% (滿分100分),末考為網(wǎng)絡(luò)考試占 50% (滿分100),其中形成性考核為完成課程網(wǎng)站上的形考作業(yè)任務(wù),形考作業(yè)均為主觀題,包括四個(gè)實(shí) 訓(xùn):實(shí)訓(xùn)一:在mysql創(chuàng)建數(shù)據(jù)庫和表,25分;實(shí)訓(xùn)二:數(shù)據(jù)查詢操作,25分;實(shí)訓(xùn)三:數(shù)據(jù)增刪改操 作,25分;實(shí)訓(xùn)四:數(shù)據(jù)庫系統(tǒng)維護(hù),25分。實(shí)驗(yàn)訓(xùn)練3數(shù)據(jù)增刪改操作答案:實(shí)驗(yàn)?zāi)康模夯趯?shí)驗(yàn)1創(chuàng)建的汽車用品網(wǎng)上商城數(shù)據(jù)庫shopping,練習(xí)insert、delete. truncate table. update

2、語句的操作方法,理解單記錄插入與批量插入、delete與truncate table語句、單表修改與多表修改的 區(qū)別。實(shí)驗(yàn)內(nèi)容:【實(shí)驗(yàn)3-1插入數(shù)據(jù)(1)使用單記錄插入insert語句分別完成汽車配件表autoparts、商品類別表category用戶表 client、用戶類別表clientkind、購物車表shoppingcart、訂單表order 訂單明細(xì)表 order_has_autoparts評論comment的數(shù)據(jù)插入,數(shù)據(jù)值自定;并通過select語句檢查插入前后的記錄 情況。insert into 目標(biāo)表 select * from 來源表;insert into insertt

3、est select * from inserttest2;insert into目標(biāo)表(字段1,字段2,.)select字段1,字段2, . from來源表;(這里的話字段 必須保持一致)insert into inserttest2(id) select id from inserttest2;insert into 目標(biāo)表(字段1,字段2,.)select 字段 1,字段 2,.from來源表where not exists (select * from 目標(biāo)表where目標(biāo)表.比較字段=來源表.比較字段);1.插入多條記錄:insert into inserttest2(id, nam

4、e )select id, namefrom inserttestwhere not exists ( select * from inserttest2where inserttest2. id=inserttest. id);2>.插入一條記錄:insert into inserttest (id, name )select 100, 'liudehua'from dualwhere not exists ( select * from inserttest where inserttest. id = 100);(2) 使用帶select的insert語句完成汽車配

5、件表autoparts中數(shù)據(jù)的批量追加;并通過select語句 檢查插入前后的記錄情況。create table tablel ( a varchar( 10 ), b varchar ( 10 ), c varchar ( 10 ), constraint pk_tablel primary key clustered ( a asc )on primarycreate table table2 ( a varchar( 10 ), c varchar ( 10 ), d int ,constraint pk_table2 primary key clustered ( a asc )on

6、primary go【實(shí)驗(yàn)3-2】刪除數(shù)據(jù)(1 ) 使用delete語句分別完成購物車表shoppingcart、訂單表order、訂單明細(xì)表 order_has_autoparts評論comment的數(shù)據(jù)刪除,刪除條件白定;并通過select語句檢查刪除前后的記 錄情況。一刪除數(shù)據(jù)delete from meta_order_itemswhere meta_order_items_id in(select b. meta order items id from (-主意:同一張表要外包這一層視圖 select meta_order_items_id from meta_order_itewh

7、ere state='create'and created_stamp '2015-11-19'(2)使用truncate table語句分別完成購物車表shoppingcart.評論comment的數(shù)據(jù)刪除。【實(shí)驗(yàn)3-3】修改數(shù)據(jù)使用update分別完成汽車配件表autoparts、商品類別表category>用戶表client、用戶類別表 clientkind、購物車表 shoppingcart> 訂單表 order、訂單明細(xì)表 order_has_autoparts> 評論 comment 的數(shù)據(jù)修改,修改后數(shù)據(jù)值自定,修改條件自定;并通

8、過select語句檢查修改前后的記錄情況。update low_priority ignore tbl_name set col_namel=exprl , col_name2=expr2 . where where_definition order by . limit row_count:實(shí)驗(yàn)要求:1. 所有操作必須通過mysql workbench完成;2. 每執(zhí)行一種插入、刪除或修改語句后,均要求通過mysql workbench 2看執(zhí)行結(jié)果及表中數(shù)據(jù)的變化 情況;3. 將操作過程以屏幕抓圖的方式拷貝,形成實(shí)驗(yàn)文檔。實(shí)驗(yàn)訓(xùn)練4:數(shù)據(jù)庫系統(tǒng)維護(hù)答案:1.1 實(shí)驗(yàn)?zāi)康耐ㄟ^創(chuàng)建數(shù)據(jù)庫、并進(jìn)

9、行相應(yīng)的維護(hù),了解并掌握mysql數(shù)據(jù)庫的創(chuàng)建和維護(hù)的不同方法和途徑,并通 過這一具體的數(shù)據(jù)庫理解實(shí)際數(shù)據(jù)庫所包含的各要素。1.2 實(shí)驗(yàn)內(nèi)容1 .數(shù)據(jù)庫創(chuàng)建與維護(hù)(1)創(chuàng)建數(shù)據(jù)庫表創(chuàng)建學(xué)生選課數(shù)據(jù)庫中所包含的數(shù)據(jù)庫表,數(shù)據(jù)庫表結(jié)構(gòu)如下:學(xué)生(學(xué)號,姓名,性別,年齡,系別,班級)課程(課程編號,課程名稱,學(xué)分)選修(學(xué)號,課程編號,學(xué)生成績)要求為各數(shù)據(jù)庫表屬性選擇合適的數(shù)據(jù)類型。列名、數(shù)據(jù)類型(長度列、精度、小數(shù)位數(shù)項(xiàng))、是否允許空值等。(2)查看和修改表結(jié)構(gòu)。選擇一個(gè)數(shù)據(jù)庫表,練習(xí)對其表結(jié)構(gòu)進(jìn)行相應(yīng)的修改。(3)練習(xí)用交互式的sql語句分別完成以上操作。(4) 對學(xué)生數(shù)據(jù)庫中的三張表分別插入

10、數(shù)據(jù);(5) 對表中的數(shù)據(jù)進(jìn)行修改;(6) 對表中的數(shù)據(jù)進(jìn)行刪除操作2 .( 1 )建立如下視圖:學(xué)生選修課程信息視圖,包括以下內(nèi)容:學(xué)生學(xué)號、姓名、所在系、選修的課程編號、課程名稱、成績(2 )修改以上視圖,增加學(xué)生所在班級信息。(3 )對以上視圖刪除。1.3 實(shí)驗(yàn)要求1 .數(shù)據(jù)庫創(chuàng)建與維護(hù)實(shí)驗(yàn)則要求面向具體應(yīng)用領(lǐng)域,利用sql server相關(guān)機(jī)制,創(chuàng)建并維護(hù)數(shù)據(jù)庫系統(tǒng),為后續(xù)各個(gè)實(shí)驗(yàn)提供前期準(zhǔn)備2 .要求學(xué)生根據(jù)以上要求確定實(shí)驗(yàn)步驟,獨(dú)立完成以上實(shí)驗(yàn)內(nèi)容。并在安裝和數(shù)據(jù)庫運(yùn)行后熟悉sqlserver的各種運(yùn)行管理。3實(shí)驗(yàn)完成后完成實(shí)驗(yàn)報(bào)告1.4 實(shí)驗(yàn)環(huán)境windows8 pc 機(jī)mysq

11、l配置的環(huán)境mysql命令行工具workbench可視化工具1. 5實(shí)驗(yàn)步驟及結(jié)果分析1. 5. 1 student數(shù)據(jù)庫創(chuàng)建打開命令行工具,輸入密碼,用use語句使用相應(yīng)數(shù)據(jù)。u se student使用老師的給的數(shù)據(jù)庫。在 workbench中我們可以看到創(chuàng)建數(shù)據(jù)庫的代碼:下面是輸入的數(shù)據(jù)i create table * student' ('sno' varchar(6) not null,就用禮測i/: o做、郵勉圳 戒虬'卻炒。1 -應(yīng)、理了mb) (* *滯7鼻忡1/«對,廣沖') (' w丁眈匕饋七f ,' sna

12、me' varchar( :) default null,* sex* varchar(?) default null,'bdate' datetime default null,"dept, varchar(8) default null,'classno' varchar( ) default null,primary key ('sno*) engine=innodb default charset=utf8;此處我們會發(fā)現(xiàn)亂碼問題,老師給的代碼中寫的是latin,我們改成utf -8 ,便解決了中文亂碼問題。創(chuàng)建數(shù)據(jù)庫語句,cr

13、eate table +'數(shù)據(jù)庫名' ('屬性名 ' 屬性類型約束)1. 5. 2 數(shù)據(jù)庫操作一-屬性查詢查詢表結(jié)構(gòu):即各個(gè)屬性和屬性的完整性約束等。(descstudent )nysql> desc student; -«4;field;type;hull ; key ; default ; extra ; 44;sno;uarchar<6>;no; pri ;null;sname;uarchar<6>;yes;nullii;sex;uarchar<2>;yes;:nullii;bdate;datetime;

14、yes;:nullii;dept:uarchar<8>;yes;:nullii!:classno;uarchar<3>;yes;:nullii 446rowsinset<0.00 sec>該語句給我們展示了我們創(chuàng)建的數(shù)據(jù)的所有(屬性名+類型+默認(rèn)初始情況+約束等)信息。1. 5. 3 數(shù)據(jù)庫操作- -數(shù)據(jù)結(jié)構(gòu)修改在老師給的默認(rèn)數(shù)據(jù)庫中,我發(fā)現(xiàn)最后一個(gè)屬性classno的屬性為varchar ( 3 );但是數(shù)據(jù)庫數(shù)據(jù) 中classno本來是4位數(shù),所以導(dǎo)致存儲時(shí)與原數(shù)據(jù)不匹配。因此,我們修改一下該屬性。即:將它改 為4位長度的:alter table 

15、9;student' modify classno' varchar(4)default null;mysql> alter table student modify classno uarchar<4> default null; query ok. 0 rows affected <0.12 sec> records: 0 duplicates: 0 warnings: 0我們在數(shù)據(jù)插入操作中進(jìn)行檢測。1. 5. 4數(shù)據(jù)庫操作一數(shù)據(jù)插入例如,我們加入一個(gè)學(xué)生信息,以自己為例:insert into ' student' valu

16、es ('33333','茹興康,男,1997/7/7 8:00','計(jì)算機(jī),314 6 ');nysql> insert into "student" values33333'茹興康男'1997/7/7 8=00° . ,計(jì)算 機(jī)query ok. 1 row affected <0.09 sec>插入成功,因?yàn)椴迦氲腸lassno為3146是4位的,所以說明數(shù)據(jù)庫結(jié)構(gòu)修改成功。我們查詢一下:|nysql> select * from student v/here sno=3

17、3333 ; .,hsno;sname;sex;bdate;dept;classno ;<,.+;33333;茹興康;男;1997-07-07 08:00:00 ;計(jì)算機(jī):3146;-i+.<bl row in set <0.00 sec>插入成功,數(shù)據(jù)庫結(jié)構(gòu)修改成功。1. 5.5數(shù)據(jù)庫操作-數(shù)據(jù)查詢 例如:我們查詢學(xué)號為30201的學(xué)生的所有信息。采用語句結(jié)構(gòu):select+查詢的屬性from+要查找的表where +約束條件:s elect * from student where sno=30201;|mysql> select * fron student

18、 v/here sno=30201 ; 1+i!sno;snanesex;bdate;dept;classno ;+ :30201:吳磊:男:1980-01-02 00:00:00 :電信:302:1+n. row in set <0.00 sec>1. 5. 6數(shù)據(jù)庫操作一-數(shù)據(jù)修改我們按學(xué)號為30201 ,即第一個(gè)數(shù)據(jù)為例,修改他的名字,update student set sname = ' abc' where sno=30201;”ysql> update student set snane=*abcj where sno=30201; query

19、ok. 1 row affected <0.09 secrows matched: 1 changed: 1 l/arnings: 0mysql> select * from student v/here sno=30201 ;sno! snane ! sex !bdate!dept! classno ;44 44- 4;30201i abc ;男 ;1980-01-0200:00:00;電信;302;1. 5.7數(shù)據(jù)庫操作一-數(shù)據(jù)刪除我們按學(xué)號為30201 ,即第一個(gè)數(shù)據(jù)為例:delete from 'student' where sno=,30201nysql&

20、gt; delete f ron "student% v/here sno = > 30201 ; query ok. 1 row affected <0.10 sec>nysql> select * fron student where sno=30201; empty set <0.00 sec>因?yàn)槲覀円呀?jīng)刪除了該數(shù)據(jù),所以查詢?yōu)榭铡?.5.8 視圖操作視圖創(chuàng)建視圖,用來更方便的展示某些信息,或者為了更安全的選擇展示或隱藏某些信息。我們將student表,sc表,course表連接起來創(chuàng)建新的視圖,從而讓學(xué)生信息更直觀,更方 便。創(chuàng)建視圖所需

21、語句:create view +視圖名+as +查詢語句。create view viewl asselect sno, sname, dept, eno, cname, grade, classno from student natural join sc natural join course;use student(database changedlnpsql> create uiew ulev/1 as-> select ame.gvade.classno-> from student natural join sc natural join course; ique

22、ry ok. 0 rov/s affected <0.06 sec我們在下而的視圖查詢 操作中檢測試圖是否創(chuàng)建成功。1.5.9 視圖操作視圖查詢由于在上述實(shí)驗(yàn)中我們將第一個(gè)學(xué)生的數(shù)據(jù)刪除掉了,所以我們選擇查詢后面的同學(xué),查詢視圖操作代碼和查詢表格是一樣的。代碼:select * from viewl where sno=30203;(其中viewl是我們創(chuàng)建的視圖名稱);nysql> select * from uiewl where sno=30203;i sno ; snane ! dept ; eno ! cname! 30203;30203;30203伶日后日后日 目日可日

23、可s -b-rr -b-rr -b-rt ihnjpnuttfu信信信作ua算操ja計(jì)* grade;classno ;57;302;:50:302;40;302;由于該同學(xué)選擇了三門不同的課程,因此在進(jìn)行表連接的時(shí)候,增加了該同學(xué)的元組數(shù)量,最終查詢出3 條結(jié)果。1. 5. 10 視圖操作-一視圖結(jié)構(gòu)修改在視圖創(chuàng)建中,我們創(chuàng)建了視圖的classno班級信息,現(xiàn)在我們刪除這一屬性。使用語句: create or replace view。c reate or replace view viewl asselect sno,sname, dept, eno, cname, gradefrom s

24、tudent natural join sc natural join course;然后我們在查詢一下viewl的結(jié)構(gòu),同table操作一樣,用desc語句。結(jié)果如下:mysql> create or replace uiew uiewl as-> select sno>snanedepteno>cnane>grade-> from student natural join sc natural join course; query ok. 0 rows affected <0.06 sec>mysql> desc uiewl;+44+ fie id i type; null ; key ; default ; extra ;4+1 1snoi iuarchar<6>i ino ;1 1nu

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論