計(jì)算機(jī)二級(jí)《MySQL數(shù)據(jù)庫程序設(shè)計(jì)》知識(shí)點(diǎn)總結(jié)_第1頁
計(jì)算機(jī)二級(jí)《MySQL數(shù)據(jù)庫程序設(shè)計(jì)》知識(shí)點(diǎn)總結(jié)_第2頁
計(jì)算機(jī)二級(jí)《MySQL數(shù)據(jù)庫程序設(shè)計(jì)》知識(shí)點(diǎn)總結(jié)_第3頁
計(jì)算機(jī)二級(jí)《MySQL數(shù)據(jù)庫程序設(shè)計(jì)》知識(shí)點(diǎn)總結(jié)_第4頁
計(jì)算機(jī)二級(jí)《MySQL數(shù)據(jù)庫程序設(shè)計(jì)》知識(shí)點(diǎn)總結(jié)_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

MySQL知識(shí)點(diǎn)總結(jié).數(shù)據(jù)操作:檢索、排序、過濾、分組、匯總、計(jì)算、聯(lián)接、子查詢與組合查詢.表操作:表的創(chuàng)建、修改、刪除和重命名、表數(shù)據(jù)的插入、更新和刪除.索引(含主、外鍵)、視圖.難度編程:存儲(chǔ)過程與存儲(chǔ)函數(shù)、觸發(fā)器與事件、PHP.數(shù)據(jù)庫管理:事務(wù)處理、用戶與權(quán)限、備份與還原、數(shù)據(jù)庫維護(hù)檢索數(shù)據(jù):select?from…Select[distinct]prod_id,prod_namefromproducts[limit4,5];檢索排序:orderby…Select*fromproductsorderbyprod_id[asc|desc],prod_name[asc|desc];過濾數(shù)據(jù):where字句=<>!=>>=<<=between普通where字句Selectprod_id,prod_namefromproductswhereprod_name=’liyang’;Selectprod_id,prod_namefromproductswhereprod_idbetween10and50;Selectprod_id,prod_namefromproductswhereprod_nameis[not]null;組合where字句:使用AND和OR連接多個(gè)條件表達(dá)式,且AND次序優(yōu)于OR;IN與NOT操作符Selectprod_id,prod_namefromproductswhereprod_id[not]in(1,2,3)|prod_namein(’zhangsan’,’lisi’,’wangwu’);LIKE操作符與通配符:"%”與"_”Selectprod_id,prod_namefromproductswhereprod_namelike’%liu%’;Selectprod_id,prod_namefromproductswhereprod_namelike’_u%’;找出u位于名字的第二個(gè)位置的prod_id和prod_name。正則表達(dá)式計(jì)算字段拼接字段:concat(…,…)Selectconcat(姓氏,名字)as姓名fromorders;Selectconcat(vend_name,’(’,vend_country,’)’)fromvendors;算術(shù)運(yùn)算Selectprod_name,prod_price,prod_num,prod_price*prod_numasprod_moneyfromproducts;使用數(shù)據(jù)處理函數(shù):文本處理函數(shù)、日期和時(shí)間處理函數(shù)、數(shù)值處理函數(shù)。匯總數(shù)據(jù):聚集函數(shù)SUM()AVG()COUNT()MAX()MIN()Selectavg(distinctprod_price)fromproducts;Selectavg(prod_price)均價(jià),max(prod_price)最高價(jià)fromproducts;分組數(shù)據(jù):groupby…創(chuàng)建分組、過濾分組、分組排序Selectcount(prod_id),prodidfromproductswhereprod_id>1000groupbyprodidhavingcount(prod_id)>2orderbyprod_id求出prod_id大于1000且產(chǎn)品數(shù)量大于2的產(chǎn)品數(shù)量,并按prod_id排序,注意分組語句中對(duì)象要前后一致,如下劃線部分。使用子查詢:進(jìn)行過濾select?where?in(select?where?in(select?))、作為計(jì)算字段使用子查詢。聯(lián)接:join??on…普通聯(lián)接Selectoi.order_num,d_id,d_name,p.vend_id,v.vend_d_id=d_idjoinvendorsvonр.vend_id=v.vend_idwherevend_name=’liyang’;可同時(shí)聯(lián)接多個(gè)表且可同時(shí)用于數(shù)據(jù)過濾,這種類型的聯(lián)接一般為內(nèi)部聯(lián)接。自聯(lián)接:一個(gè)表自己與自己聯(lián)接,注意判斷好各字段與前后兩個(gè)表的關(guān)系。自然聯(lián)接:基本上簡(jiǎn)歷的內(nèi)部聯(lián)接都是自然聯(lián)接。外部聯(lián)接:在關(guān)系表中沒有關(guān)聯(lián)的信息的行也能顯示出來的聯(lián)接,根據(jù)表在join字句的左邊還是右邊可分為左聯(lián)接與右聯(lián)接。帶聚集函數(shù)的聯(lián)接Selectc.cust_id,count(o.order_num)num_ordfromcustomerscjoinordersoonс.cust_id=o.cust_idorderbyc.cust_id;找出客戶對(duì)應(yīng)的訂單數(shù)。組合查詢:連接多個(gè)(至少兩個(gè))查詢語句,滿足其中一個(gè)查詢語句條件的結(jié)果都會(huì)顯示出來union(不重復(fù)顯示)/unionall(可重復(fù)顯示即全部顯示)Selectvend_id,prod_id,prod_pricefromproductswhereprod_price<=5Union[all]Selectvend_id,prod_id,prod_pricefromproductswherevend_idin(1001,1002)orderbyprod_id;注意每個(gè)查詢必須包含相同的列、表達(dá)式或者聚集函數(shù),列的數(shù)據(jù)類型必須兼容,排序語句只能放在最后面,該排序語句對(duì)組合查詢語句中的所有select語句都適用。全文本搜索:只支持引擎為MyISAM的表,不支持引擎為InnoDB的表,可對(duì)搜索結(jié)果進(jìn)行智能排序后輸出,具有較高等級(jí)的行先返回。Match(全文本搜索字段)against(’全文本搜索內(nèi)容’[withqueryexpansion])其中下劃線部分為拓展語句,使用該語句,除了可以返回符合所設(shè)置的“全文本搜索內(nèi)容”的數(shù)據(jù)結(jié)果,還可返回與“全文本搜索內(nèi)容”有較高相似度的數(shù)據(jù)結(jié)果。啟用全文本搜索支持Createtablefs(idintnotnullprimarykey,ctext,c1text,fulltext(c,c1))engine=MyISAM;進(jìn)行全文本搜索Selectnote_textfromproductnoteswherematch(note_text)against(’liyang’withqueryexpansion);插入數(shù)據(jù):insertinto?{values|select}…Insertintoproducts(prod_id,prod_name,prod_price)values(1,’豆?jié){’,2),(3,’雞蛋’,1);可同時(shí)插入多行數(shù)據(jù)。Insertintoproducts(prod_id,prod_name,prod_price)selectvend_id,vend_name,vend_pricefromvendorswherevend_id<=10;更新數(shù)據(jù):update[ignore]???set…,一般情況下,若更新的數(shù)據(jù)中有部分?jǐn)?shù)據(jù)出錯(cuò),則全部數(shù)據(jù)返回到原來的數(shù)據(jù),而ignore的作用在于即使更新的數(shù)據(jù)中出現(xiàn)錯(cuò)誤,只對(duì)出現(xiàn)錯(cuò)誤的數(shù)據(jù)返回到原來數(shù)據(jù),而未出現(xiàn)錯(cuò)誤的數(shù)據(jù)返回更新后的結(jié)果實(shí)現(xiàn)更新。updateproductssetprod_name='饅頭',prod_price=1whereprod_id=1;updatecustomerssetcust_city=concat(cust_city,’市’)|cust_city=replace(cust_city,'市',’city’)wherecust_id>1000;刪除數(shù)據(jù):deletefrom…Deletefromproductswhereprod_idbetween10an50;表的相關(guān)操作創(chuàng)建表:對(duì)表結(jié)構(gòu)進(jìn)行設(shè)置createtable…Createtableproducts(prod_idintnullauto_incrementprimarykey,prod_namevarchar(50),prod_priceint,prod_cityvarchar(50)default’廣州’)engine=InnoDB;每個(gè)字段名后需要設(shè)置數(shù)據(jù)類型,default為指定默認(rèn)值,只支持常量不支持函數(shù),且只在插入數(shù)據(jù)時(shí)起作用而在更新數(shù)據(jù)時(shí)不起作用,InnoDB是一個(gè)可靠的事務(wù)處理引擎,但不支持全文本搜索。更新表:對(duì)表結(jié)構(gòu)進(jìn)行修改altertable{add|drop}…Altertableproductsaddprod_cityvarchar(50);Altertableproductsdropprod_price;刪除表:一旦刪除,無法撤銷droptable…Droptableproducts;重命名表:renametable?to…Renametableproductstonew_products;索引的相關(guān)操作創(chuàng)建索引:常用于數(shù)據(jù)的快速檢索,MySQL中,常用索引在物理可分為:BTREE、HASH索引兩類;在具體用途上可分為:INDEX、UNIQUE、PRIMARYKEY>FOREIGNKEY>FULLTEXT、SPATIAL等。G使用createindex語句創(chuàng)建索引,對(duì)已存在的表創(chuàng)建索引Create[unique|fulltext|spatial]indexindex_name[usingBTREE|HASH]ontbl_name(index_col_name[,index_col_name?]);Createuniqueindexindex_productsonproducts(prod_name(2)desc,prod_price);⑥使用createtable語句創(chuàng)建索引,創(chuàng)建表的同時(shí)創(chuàng)建索引Createtableseller(seller_idintnotnullauto_increment,seller_namechar(50),seller_adresschar(50),seller_contactchar(50),product_typeint,salesint,primarykey(seller_id,product_type),[unique|fulltext|spatial]indexindex_seller(sales));G使用altertable語句創(chuàng)建索引,修改表的同時(shí)添加索引Altertabletbl_nameadd{[unique|fulltext|spatial]indexindex_tbl_name(字段名)|primarykey(字段名)foreignkey(字段名)referenceselsetbl_name(相同字段名)};查看索引:Showindexfromtbl_name[whereexpr];刪除索引:dropindexindex_nameontbl_name語句或altertable語句Dropindexindex_nameontbl_name;Altertabletbl_namedrop{[unique|fulltext|spatial]indexindex_tbl_name字段名)|primarykey(字段名)foreignkey(字段名)referenceselsetblname(相同字段名};(下劃線部分不確定)視圖的相關(guān)操作視圖:虛擬的表,視圖本身不包含表中的列和數(shù)據(jù),它包含只是一個(gè)SQL查詢,常用于檢索數(shù)據(jù)。*視圖的作用與規(guī)則。創(chuàng)建視圖:Createviewview_nameasselect?[where…];Createviewview_productsasselectprod_id,prod_name,prod_price,prod_num,prod_price*prod_numasprod_moneyfromproductswhereprod_id<=10[withcheckoption];--下劃線部分表示今后對(duì)該視圖數(shù)據(jù)的修改都必須符合prod_id<=10查看視圖(用法同表):select*fromview_name;刪除視圖:dropviewview_name;完整性:實(shí)體完整性(主鍵與候選鍵)、參照完整性(主鍵與外鍵)、用戶定義的完整性(非空約束與check約束)。創(chuàng)建主鍵約束:createtable語句或altertable語句Createtableproducts(prod_idintnotnullauto_incrementprimarykey,cint);作為列的主鍵約束;Createtableproducts(prod_idintnotnullauto_increment,cint,c1int,primarykey(prod_id));作為表的主鍵約束,且復(fù)合主鍵職能用這種形式創(chuàng)建Altertableproductsaddprimarykey(prod_id);備注:實(shí)體完整性通過主鍵約束與候選鍵約束來實(shí)現(xiàn),候選鍵約束的創(chuàng)建類似主鍵約束的創(chuàng)建,實(shí)質(zhì)上同索引。設(shè)置表外鍵:createtable語句或altertable語句,外鍵中列的數(shù)目和數(shù)據(jù)類型必須與被參照表的主鍵中列的數(shù)目和對(duì)應(yīng)數(shù)據(jù)類型一致。altertabletbl_nameadd[constraintfk_name]foreignkey(…)references…Createtableproducts(prod_idintnotnullauto_increment,cint,c1int,foreignkey(prod_id)referencescustomers(prod_id));altertableproductsaddconstraintfk_products_custforeignkey(cust_id)referencescust(cust_id);存儲(chǔ)過程:為了以后的使用而保存的一條或多條SQL語句的集合--建立存儲(chǔ)過程:建立一個(gè)可通過輸入item_id,輸出對(duì)應(yīng)訂單總金額的存儲(chǔ)過程->Delimiter//--改變分割符為//->createprocedureordertotal(ino_idint,outo_totaldecimal(10,2))過程名字輸入?yún)?shù)及類型輸出參數(shù)及類型->begin->selectsum(item_price*item_num)fromorderitemswhereitem_id=o_idintoo_total;->ifo_totalisnullthen->select’不存在該訂單號(hào)’;->endif;->end;->//--執(zhí)行存儲(chǔ)過程:當(dāng)item_id=200005時(shí),得出對(duì)應(yīng)訂單總金額->delimiter;--將分割符改回分號(hào)->callordertotal(200005,@total);--由于不存在輸出參數(shù),故定義一個(gè)輸出變量,變量必須用@開頭->select@total;返回結(jié)果為149.87備注:書本第十一章后的編程題,使用update語句,兩個(gè)參數(shù)類型都需要為in。--顯示存儲(chǔ)過程->Showcreateprocedureordertotal;—?jiǎng)h除存儲(chǔ)過程->Dropprocedureordertotal;存儲(chǔ)函數(shù)存儲(chǔ)函數(shù)與存儲(chǔ)過程的區(qū)別:.存儲(chǔ)函數(shù)不能擁有輸出參數(shù);.存儲(chǔ)函數(shù)可直接調(diào)用,且不需使用call語句,而存儲(chǔ)過程的調(diào)用必須使用call語句;.存儲(chǔ)函數(shù)中必須包含一條return語句,而這條特殊的SQL語句不允許包含于存儲(chǔ)過程。--建立存儲(chǔ)函數(shù):根據(jù)給定的cust_id返回客戶所在的州名(縮寫),若庫中無給定的cust_id,則返回“不存在該客戶”。->delimiter//->createfunctionfn_search(c_idint)->returnsvarchar(50)—定義返回的數(shù)據(jù)類型,與函數(shù)部分中的數(shù)據(jù)類型需統(tǒng)一,如函數(shù)中的“不存在該客戶,,為6個(gè)字符,如果這里設(shè)置為char(5),則無法輸出該結(jié)果->deterministic-表示對(duì)于相同的輸入值,返回值也相同->begin->declarestatechar(2);--聲明一個(gè)變量state,作為輸出的州變量->selectcust_statefromcustomerswherecust_id=c_idintostate;->ifstateisnullthen->return(select’不存在該客戶’);--注意這里return不用加s->else->return(selectstate);->endif;->end;->//--執(zhí)行存儲(chǔ)函數(shù)->selectfn_search(10001);--刪除存儲(chǔ)函數(shù)->dropfunctionfn_search;--刪除前要確定該函數(shù)無依賴關(guān)系,即不存在其他存儲(chǔ)過程或存儲(chǔ)函數(shù)調(diào)用過該存儲(chǔ)函數(shù)。觸發(fā)器:MySQL響應(yīng)insert、delete、update語句時(shí)自動(dòng)執(zhí)行的一條MySQL語句,創(chuàng)建觸發(fā)器時(shí)需要給出的4條信息:唯一的觸發(fā)器名、觸發(fā)器相關(guān)的表、觸發(fā)器應(yīng)該響應(yīng)的活動(dòng)(insertdelete、update)、觸發(fā)器何時(shí)執(zhí)行(處理前或處理后)。insert觸發(fā)器:當(dāng)對(duì)表插入數(shù)據(jù)時(shí)起作用,含有一個(gè)虛擬表New,可訪問增加的行,只能用after--建立一個(gè)insert觸發(fā)器,用于記錄insert語句操作時(shí)的系統(tǒng)時(shí)間和插入的order_num->delimiter//->createtriggertrg_order_insertafterinsertonordersforeachrow觸發(fā)器觸發(fā)器名執(zhí)行時(shí)間相關(guān)表->begin->insertintoorder_log(o_date,order_num)values(now(),new.ordernum);--order_log是事先建立好的表,用于記錄insert語句操作時(shí)的系統(tǒng)時(shí)間和插入的order_num->end;->//―執(zhí)行insert觸發(fā)器->delimiter;->insertintoorders(order_date,cust_id)values(’2010-9-15’,10001);—由于order_num是自動(dòng)遞增的,故在這里不作為插入對(duì)象delete觸發(fā)器:當(dāng)對(duì)表刪除數(shù)據(jù)時(shí)起作用,含有一個(gè)虛擬表Old,可訪問被刪除的行,只能用after,創(chuàng)建方法與insert類似,區(qū)別在于delete和old--建立一個(gè)delete觸發(fā)器,用于記錄delete語句操作時(shí)的系統(tǒng)時(shí)間和刪除的order_num->delimiter//->createtriggertrg_order_deleteafterdeleteonordersforeachrow觸發(fā)器觸發(fā)器名執(zhí)行時(shí)間相關(guān)表->begin->insertintoorder_log(o_date,order_num)values(now(),old.ordernum);--order_log是事先建立好的表,用于記錄delete語句操作時(shí)的系統(tǒng)時(shí)間和刪除的order_num->end;->//--執(zhí)行delete觸發(fā)器->delimiter;->deletefromorderswhereorder_num=20010;update觸發(fā)器:當(dāng)對(duì)表修改數(shù)據(jù)時(shí)起作用,同時(shí)含有new和old兩個(gè)虛擬表。結(jié)合New可訪問更新行的記錄;結(jié)合old可訪問更新前行的記錄,可用after,也可用before。Q用after--建立一個(gè)update觸發(fā)器,用于記錄update語句操作時(shí)的系統(tǒng)時(shí)間和更新數(shù)據(jù)的order_num->delimiter//->createtriggertrg_order_updateafterupdateonordersforeachrow觸發(fā)器觸發(fā)器名執(zhí)行時(shí)間相關(guān)表->begin->insertintoorder_log(o_date,order_num)values(now(),old.ordernum);->end;->//--執(zhí)行update觸發(fā)器->delimiter;->updateorderssetorder_date=’2015-9-18’wherecust_id=10001;⑥用before--建立一個(gè)update觸發(fā)器,如果更新后的prod_price大于原來的1.2倍,則用原來的1.2倍作為當(dāng)前價(jià)格->delimiter//->createtriggertrg_order_updatebeforeupdateonordersforeachrow觸發(fā)器觸發(fā)器名執(zhí)行時(shí)間相關(guān)表->begin->d_price>d_price*1.2then->d_price=d_price*1.2;->endif;->end;->//刪除觸發(fā)器:droptriggertrg_name;事件:臨時(shí)觸發(fā)器,要使用事件調(diào)度器,必須開啟“event_scheduler”.查看:showvariableslike’event_scheduler’;.開啟:setglobalevent_scheduler=1;創(chuàng)建事件CREATEEVENTEVENT_NAMEONSCHEDULEscheduleDOevent_body;其中schedule的語法格式為ATtimestamp[+INTERVALinterval"|everyinterval—指定事件執(zhí)行的時(shí)間,可以為某時(shí)刻點(diǎn)即timestamp,或某時(shí)刻點(diǎn)開始的interval時(shí)間后,或者為每隔interval時(shí)間執(zhí)行一次[startstimestamp[+INTERVALinterval]]—設(shè)置事件開始執(zhí)行的時(shí)間[endstimestamp[+INTERVALinterval]]—設(shè)置事件終止執(zhí)行的時(shí)間—建立一個(gè)事件,用于每個(gè)月向customers表中插入一條數(shù)據(jù)“l(fā)iyang、廣州”,該事件從下個(gè)月開始并于2015-12-31結(jié)束->delimiter//->createeventevent_insertonscheduleevery1month->startscurdate()+interval1month->ends’2015-12-31’->do->begin->ifyear(curdate())<2015then->insertintocustomers(cust_name,cust_adress)values(’liyang’,’廣州’);->endif;->end;->//修改事件,用于修改時(shí)間的狀態(tài):altereventevent_name{enable|disable};刪除事件:dropeventevent_name;管理實(shí)務(wù)處理:starttransaction—實(shí)務(wù)處理的術(shù)語:實(shí)務(wù)(transaction):一組SQL語句;回退(rollback):撤銷指定SQL語句的過程;提交(commit):指定未存儲(chǔ)的SQL語句結(jié)果寫入到數(shù)據(jù)庫表里,提交后無法回退;保留點(diǎn)(savepoint):實(shí)務(wù)處理中設(shè)置的臨時(shí)占位符。安全管理(用戶創(chuàng)建修改與刪除以及用戶權(quán)限的查看設(shè)置與撤銷)創(chuàng)建用戶賬號(hào):createuserbenidentifiedby’ben’;修改用戶賬號(hào):updatemysql.usersetuser=’new_ben’whereuser=’ben’;--從mysql數(shù)據(jù)庫中的用戶表user進(jìn)行修改查看訪問權(quán)限:showgrantsfornew_ben;設(shè)置訪問權(quán)限:grant?to…

.grant{all|select,update,delete,insert}on{*.*|crashcourse.*|crashcourse.customers}tonew_ben;.grantselect(cust_id,cust_name)oncrashcourse.customerstonew_ben;--可針對(duì){整個(gè)服務(wù)器|整個(gè)數(shù)據(jù)庫|數(shù)據(jù)庫中某個(gè)表|數(shù)據(jù)庫中某個(gè)表的某些字段},對(duì)用戶同時(shí)設(shè)置全部或一種或多種權(quán)限(5)撤銷訪問權(quán)限:revoke?from…,用法與grant?to…類似(6)更改口令(密碼)Setpasswordfornew_ben=password(’new_ben’);(7)刪除用戶:dropusernew_ben;數(shù)據(jù)庫備份與還原.使用SQL語句backuptabletbl_nameto?/restoretabletbl_namefrom…(只用于MyISAM表)select?intooutfile?/loaddata?infile?intotabletlb_name.使用命令行實(shí)用程序:mysqlhotcopy(只用于MyISAM表)或mysqldump/mysql(1)使用select?intooutfile?/loaddata?infile?intotabletlb_name.備份數(shù)據(jù):Select*ductsintooutfile’d:\products.txt’[Fieldsterminatedby’,’optionallyenclosedby’”’linesterminatedby’\n\r’;--定義字段間的分割符、字符型數(shù)據(jù)的存放形式、行與行之間的分割符.恢復(fù)數(shù)據(jù)Loaddatainfile’d:\products.txt’intotablecustomers.copy[Fieldsterminatedby’,’optionallyenclosedby’”’linesterminatedby’\n\r’;--必須與備份時(shí)一致(2)使用命令行實(shí)用程序mysqldump/mysql(文本形式)進(jìn)入cmd運(yùn)行界面(mysqldump—help可用于獲取mysqldump的選項(xiàng)表及更多幫助信息).備份整個(gè)數(shù)據(jù)庫服務(wù)器、或整個(gè)數(shù)據(jù)庫或數(shù)據(jù)庫中某個(gè)表Mysqldump-uroot-proot-P3306-hlocalhost{all-databases|mysql_test[products]}>d:\data.sql.恢復(fù)數(shù)據(jù)Mysql-uroot-proot-P3306-hlocalhost{all-databases|mysql_test[products]}<d:\data.sqlanalyzetabletbl_name;更新表的索引散列程度,檢查表鍵是否正確checktabletbl_name;檢查一個(gè)或多個(gè)表是否有錯(cuò)誤checksumtabletbl_name;對(duì)數(shù)據(jù)庫中的表進(jìn)行校驗(yàn),保證數(shù)據(jù)的一致性optimizetabletbl_name;利用表中未使用的空間并整理數(shù)據(jù)文件碎片,保證數(shù)據(jù)讀取效率repairtabletbl_name;修復(fù)一個(gè)或多個(gè)可能被損害的MylSAM表數(shù)據(jù)庫維護(hù)語句(可同時(shí)對(duì)一個(gè)或多個(gè)表進(jìn)行操作)(1)(2)(3)(4)(5)28.二進(jìn)制日志文件的使用:mysqlbinlog29.使用PHPanalyzetabletbl_name;更新表的索引散列程度,檢查表鍵是否正確checktabletbl_name;檢查一個(gè)或多個(gè)表是否有錯(cuò)誤checksumtabletbl_name;對(duì)數(shù)據(jù)庫中的表進(jìn)行校驗(yàn),保證數(shù)據(jù)的一致性optimizetabletbl_name;利用表中未使用的空間并整理數(shù)據(jù)文件碎片,保證數(shù)據(jù)讀取效率repairtabletbl_name;修復(fù)一個(gè)或多個(gè)可能被損害的MylSAM表.首先建立與MySQL數(shù)據(jù)庫服務(wù)器的連接;.然后選擇要對(duì)其進(jìn)行操作的數(shù)據(jù)庫;.再執(zhí)行相應(yīng)的數(shù)據(jù)庫操作,包括對(duì)數(shù)據(jù)的添加、刪除、修改和查詢等;.最后關(guān)閉與MySQL數(shù)據(jù)庫服務(wù)器的連接。(1)數(shù)據(jù)庫服務(wù)器連接、選擇數(shù)據(jù)庫.使用mysql_connect()建立非持久連接<?Php$con=mysql_connect("localhost:3306”,“root”,“123456”);if(!$con){echo“數(shù)據(jù)庫服務(wù)器連接失??!<br>”;die();}echo“數(shù)據(jù)庫服務(wù)器連接成功!<br>”;?>//將connect.php部署在已開啟的WAMP平臺(tái)環(huán)境中,并在瀏覽器地址中輸入“http://localhost/connect.php”.使用mysql_pconnect()建立持久連接<?php$con=mysql_pconnect(“l(fā)ocalhost:3306”,“root”,“123456”);if(!$con){die(“數(shù)據(jù)庫服務(wù)器連接失??!”.mysql_error());//終止程序運(yùn)行,并返回錯(cuò)誤信息}echo“MySQL服務(wù)器:localhost:3306<br>”;echo“用戶名:root<br>”;echo“使用函數(shù)mysql_pconnect()永久連接數(shù)據(jù)庫。<br>”;?>.使用mysql_select_db(databases[,connection])選擇數(shù)據(jù)庫<?php$con=mysql_connect(“l(fā)ocalhost:3306”,“root”,“123456”);if(!$con)//或者為if(mysql_errno()){echo“數(shù)據(jù)庫服務(wù)器連接失??!<br>”;die();}mysql_select_db(“mysql_test”,$con);if(mysql_errno()){echo“數(shù)據(jù)庫選擇失敗!<br>”;die();}echo"數(shù)據(jù)庫選擇成功!<br>”?>(2)數(shù)據(jù)的添加、更新和刪除操作,mysql_query(SQL語句[,connection]),insert、update、delete語句可置于函數(shù)mysql_query()中從而實(shí)現(xiàn)數(shù)據(jù)的添加、更新和刪除操作.數(shù)據(jù)的添加/*向數(shù)據(jù)庫mysql_test中的表customers添加一個(gè)名為“李中華”的客戶的全部信息*/<?php$con=mysql_connect("localhost:3306”,“root”,“123456”)ordie(“數(shù)據(jù)庫服務(wù)器連接失??!<br>”);Mysql_select_db(“mysql_test”,$con)ordie(“數(shù)據(jù)庫選擇失?。?lt;br>”);Mysql_query("setnames’gbk’”);//設(shè)置中文字符集$sql=“insertintocustomers。cust_id’,’cust_name’,’cust_sex’)”;$sql=$sql."values(null,’李中華,,’M’)”;if(mysql_query($sql,$con))echo“客戶信息添加成功!<br>”;elseecho“客戶信息添加失??!<br>”;?>.數(shù)據(jù)的更新/*將數(shù)據(jù)庫mysql_test的表customers中的一個(gè)名為“李中華”的客戶的地址修改為“廣州市”*/<?php$con=mysql_connect(“l(fā)ocalhost:3306”,“root”,“123456”)ordie(“數(shù)據(jù)庫服務(wù)器連接失??!<br>”);Mysql_select_db(“mysql_test”,$con)ordie(“數(shù)據(jù)庫選擇失??!<br>”);Mysql_query(“setnames’gbk’”);$sql="updatecustomerssetcust_address=’廣州市’”;$sql=$sql."wherecust_name=’李中華’”;if

溫馨提示

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

評(píng)論

0/150

提交評(píng)論