第一篇:計(jì)算機(jī)二級(jí)《MySQL數(shù)據(jù)庫(kù)程序設(shè)計(jì)》知識(shí)點(diǎn)總結(jié)
MySQL知識(shí)點(diǎn)總結(jié)
.數(shù)據(jù)操作:檢索、排序、過(guò)濾、分組、匯總、計(jì)算、聯(lián)接、子查詢(xún)與組合查詢(xún).表操作:表的創(chuàng)建、修改、刪除和重命名、表數(shù)據(jù)的插入、更新和刪除.索引(含主、外鍵)、視圖
.難度編程:存儲(chǔ)過(guò)程與存儲(chǔ)函數(shù)、觸發(fā)器與事件、PHP.數(shù)據(jù)庫(kù)管理:事務(wù)處理、用戶(hù)與權(quán)限、備份與還原、數(shù)據(jù)庫(kù)維護(hù)
1.檢索數(shù)據(jù):select?from?
Select [distinct] prod_id,prod_name from products [limit 4,5];2.檢索排序:order by?
Select * from products order by prod_id [asc|desc],prod_name [asc|desc];3.過(guò)濾數(shù)據(jù):where 字句
= <>!= >>= <<= between(1)普通where 字句
Select prod_id,prod_name from products where prod_name=’liyang’;Select prod_id,prod_name from products where prod_id between 10 and 50;Select prod_id,prod_name from products where prod_name is [not] null;(2)組合where字句:使用AND和OR連接多個(gè)條件表達(dá)式,且AND次序優(yōu)于OR;(3)IN 與NOT操作符
Select prod_id,prod_name from products where prod_id [not] in(1,2,3)|prod_name in(’zhangsan’,’lisi’,’wangwu’);(4)LIKE操作符與通配符:“%”與“_”
Select prod_id,prod_name from products where prod_name like ’%liu%’;Select prod_id,prod_name from products where prod_name like ’_u%’;找出u位于名字的第二個(gè)位置的prod_id和prod_name。(5)正則表達(dá)式 4.計(jì)算字段
(1)拼接字段:concat(?,?)Select concat(姓氏,名字)as 姓名 from orders;Select concat(vend_name,’(’,vend_country,’)’)from vendors;(2)算術(shù)運(yùn)算
Select prod_name,prod_price,prod_num,prod_price*prod_num as prod_money from products;4.使用數(shù)據(jù)處理函數(shù):文本處理函數(shù)、日期和時(shí)間處理函數(shù)、數(shù)值處理函數(shù)。5.匯總數(shù)據(jù):聚集函數(shù) SUM()AVG()COUNT()MAX()MIN()Select avg(distinct prod_price)from products;Select avg(prod_price)均價(jià),max(prod_price)最高價(jià) from products;6.分組數(shù)據(jù):group by? 創(chuàng)建分組、過(guò)濾分組、分組排序
Select count(prod_id),prod_id from products where prod_id>1000 group by prod_id having count(prod_id)>2 order by prod_id;求出prod_id大于1000且產(chǎn)品數(shù)量大于2的產(chǎn)品數(shù)量,并按prod_id排序,注意分組語(yǔ)句中對(duì)象要前后一致,如下劃線部分。7.使用子查詢(xún):進(jìn)行過(guò)濾select?where?in(select?where?in(select?))、作為計(jì)算字段使用子查詢(xún)。8.聯(lián)接:join?on?(1)普通聯(lián)接
Select oi.order_num,oi.prod_id,p.prod_name,p.vend_id,v.vend_name from orderitems oi join products p on oi.prod_id=p.prod_id join vendors v on p.vend_id=v.vend_id where vend_name=’liyang’;可同時(shí)聯(lián)接多個(gè)表且可同時(shí)用于數(shù)據(jù)過(guò)濾,這種類(lèi)型的聯(lián)接一般為內(nèi)部聯(lián)接。
(2)自聯(lián)接:一個(gè)表自己與自己聯(lián)接,注意判斷好各字段與前后兩個(gè)表的關(guān)系。(3)自然聯(lián)接:基本上簡(jiǎn)歷的內(nèi)部聯(lián)接都是自然聯(lián)接。
(4)外部聯(lián)接:在關(guān)系表中沒(méi)有關(guān)聯(lián)的信息的行也能顯示出來(lái)的聯(lián)接,根據(jù)表在join字句的左邊還是右邊可分為左聯(lián)接與右聯(lián)接。(5)帶聚集函數(shù)的聯(lián)接
Select c.cust_id,count(o.order_num)num_ord from customers c join orders o on c.cust_id=o.cust_id order by c.cust_id;找出客戶(hù)對(duì)應(yīng)的訂單數(shù)。
9.組合查詢(xún):連接多個(gè)(至少兩個(gè))查詢(xún)語(yǔ)句,滿(mǎn)足其中一個(gè)查詢(xún)語(yǔ)句條件的結(jié)果都會(huì)顯示出來(lái) union(不重復(fù)顯示)/union all(可重復(fù)顯示即全部顯示)Select vend_id,prod_id,prod_price from products where prod_price<=5 Union [all] Select vend_id,prod_id,prod_price from products where vend_id in(1001,1002)order by prod_id;注意每個(gè)查詢(xún)必須包含相同的列、表達(dá)式或者聚集函數(shù),列的數(shù)據(jù)類(lèi)型必須兼容,排序語(yǔ)句只能放在最后面,該排序語(yǔ)句對(duì)組合查詢(xún)語(yǔ)句中的所有select語(yǔ)句都適用。10.全文本搜索:只支持引擎為MyISAM的表,不支持引擎為InnoDB的表,可對(duì)搜索結(jié)果進(jìn)行智能排序后輸出,具有較高等級(jí)的行先返回。
Match(全文本搜索字段)against(’全文本搜索內(nèi)容’[with query expansion])其中下劃線部分為拓展語(yǔ)句,使用該語(yǔ)句,除了可以返回符合所設(shè)置的“全文本搜索內(nèi)容”的數(shù)據(jù)結(jié)果,還可返回與“全文本搜索內(nèi)容”有較高相似度的數(shù)據(jù)結(jié)果。(1)啟用全文本搜索支持
Create table fs(id int not null primary key,c text,c1 text,fulltext(c,c1))engine=MyISAM;(2)進(jìn)行全文本搜索
Select note_text from productnotes where match(note_text)against(’liyang’ with query expansion);11.插入數(shù)據(jù):insert into?{values|select}?
Insert into products(prod_id,prod_name,prod_price)values(1,’豆?jié){’,2),(3,’雞蛋’,1);可同時(shí)插入多行數(shù)據(jù)。
Insert into products(prod_id,prod_name,prod_price)select vend_id,vend_name, vend_price from vendors where vend_id<=10;12.更新數(shù)據(jù):update [ignore]?set?,一般情況下,若更新的數(shù)據(jù)中有部分?jǐn)?shù)據(jù)出錯(cuò),則全部數(shù)據(jù)返回到原來(lái)的數(shù)據(jù),而ignore的作用在于即使更新的數(shù)據(jù)中出現(xiàn)錯(cuò)誤,只對(duì)出現(xiàn)錯(cuò)誤的數(shù)據(jù)返回到原來(lái)數(shù)據(jù),而未出現(xiàn)錯(cuò)誤的數(shù)據(jù)返回更新后的結(jié)果實(shí)現(xiàn)更新。update products set prod_name='饅頭',prod_price=1 where prod_id=1;update customers set cust_city=concat(cust_city,’市’)| cust_city =replace(cust_city,’市’,’city’)where cust_id>1000;13.刪除數(shù)據(jù):delete from? Delete from products where prod_id between 10 an 50;14.表的相關(guān)操作
(1)創(chuàng)建表:對(duì)表結(jié)構(gòu)進(jìn)行設(shè)置create table?
Create table products(prod_id int null auto_increment primary key,prod_name varchar(50),prod_price int,prod_city varchar(50)default ’廣州’)engine= InnoDB;每個(gè)字段名后需要設(shè)置數(shù)據(jù)類(lèi)型,default為指定默認(rèn)值,只支持常量不支持函數(shù),且只在插入數(shù)據(jù)時(shí)起作用而在更新數(shù)據(jù)時(shí)不起作用,InnoDB是一個(gè)可靠的事務(wù)處理引擎,但不支持全文本搜索。
(2)更新表:對(duì)表結(jié)構(gòu)進(jìn)行修改 alter table {add|drop}?
Alter table products add prod_city varchar(50); Alter table products drop prod_price;(3)刪除表:一旦刪除,無(wú)法撤銷(xiāo) drop table?
Drop table products;(4)重命名表:rename table?to?
Rename table products to new_products;15.索引的相關(guān)操作
(1)創(chuàng)建索引:常用于數(shù)據(jù)的快速檢索,MySQL中,常用索引在物理可分為:BTREE、HASH索引兩類(lèi);在具體用途上可分為:INDEX、UNIQUE、PRIMARY KEY、FOREIGN KEY、FULL TEXT、SPATIAL等。
1使用create index 語(yǔ)句創(chuàng)建索引,對(duì)已存在的表創(chuàng)建索引 ○Create [unique|fulltext|spatial] index index_name [using BTREE|HASH] on tbl_name(index_col_name[,index_col_name?]);Create unique index index_products on products(prod_name(2)desc,prod_price);2使用create table 語(yǔ)句創(chuàng)建索引,創(chuàng)建表的同時(shí)創(chuàng)建索引 ○Create table seller(seller_id int not null auto_increment,seller_name char(50),seller_adress char(50),seller_contact char(50),product_type int,sales int,primary key(seller_id,product_type),[unique|fulltext|spatial] index index_seller(sales));3使用alter table語(yǔ)句創(chuàng)建索引,修改表的同時(shí)添加索引 ○Alter table tbl_name add {[unique|fulltext|spatial] index index_tbl_name(字段名)|primary key(字段名)|foreign key(字段名)references elsetbl_name(相同字段名)};(2)查看索引:Show index from tbl_name [where expr];(3)刪除索引:drop index index_nameon tbl_name語(yǔ)句或alter table語(yǔ)句
Drop index index_name on tbl_name;Alter table tbl_name drop {[unique|fulltext|spatial] index index_tbl_name(字段名)|primary key(字段名)|foreign key(字段名)references elsetbl_name(相同字段名};(下劃線部分不確定)16.視圖的相關(guān)操作
視圖:虛擬的表,視圖本身不包含表中的列和數(shù)據(jù),它包含只是一個(gè)SQL查詢(xún),常用于 檢索數(shù)據(jù)。*視圖的作用與規(guī)則。(1)創(chuàng)建視圖:Create view view_name as select?[where?];Create view view_products as select prod_id,prod_name,prod_price,prod_num, prod_price*prod_num as prod_money from products where prod_id<=10 [with check option];--下劃線部分表示今后對(duì)該視圖數(shù)據(jù)的修改都必須符合prod_id<=10(2)查看視圖(用法同表): select * from view_name;(3)刪除視圖:drop view view_name;17.完整性:實(shí)體完整性(主鍵與候選鍵)、參照完整性(主鍵與外鍵)、用戶(hù)定義的完整性(非空約束與check約束)。
18.創(chuàng)建主鍵約束:create table語(yǔ)句或alter table語(yǔ)句
Create table products(prod_id int not null auto_increment primary key,c int);作為列的主鍵約束;
Create table products(prod_id int not null auto_increment,c int,c1 int,primary key(prod_id));作為表的主鍵約束,且復(fù)合主鍵職能用這種形式創(chuàng)建 Alter table products add primary key(prod_id);備注:實(shí)體完整性通過(guò)主鍵約束與候選鍵約束來(lái)實(shí)現(xiàn),候選鍵約束的創(chuàng)建類(lèi)似主鍵約束的創(chuàng)建,實(shí)質(zhì)上同索引。
19.設(shè)置表外鍵:create table語(yǔ)句或alter table語(yǔ)句,外鍵中列的數(shù)目和數(shù)據(jù)類(lèi)型必須與被參照表的主鍵中列的數(shù)目和對(duì)應(yīng)數(shù)據(jù)類(lèi)型一致。
alter table tbl_name add [constraint fk_name] foreign key(?)references? Create table products(prod_id int not null auto_increment,c int,c1 int,foreign key(prod_id)references customers(prod_id));alter table products add constraint fk_products_cust foreign key(cust_id)references cust(cust_id);20.存儲(chǔ)過(guò)程:為了以后的使用而保存的一條或多條SQL語(yǔ)句的集合
--建立存儲(chǔ)過(guò)程:建立一個(gè)可通過(guò)輸入item_id,輸出對(duì)應(yīng)訂單總金額的存儲(chǔ)過(guò)程->Delimiter //--改變分割符為//->create procedure ordertotal(in o_id int,out o_total decimal(10,2))過(guò)程名字輸入?yún)?shù)及類(lèi)型輸出參數(shù)及類(lèi)型->begin->select sum(item_price*item_num)from orderitems where item_id=o_id into o_total;->if o_total is null then->select ’不存在該訂單號(hào)’;->end if;->end;->//--執(zhí)行存儲(chǔ)過(guò)程:當(dāng)item_id=200005時(shí),得出對(duì)應(yīng)訂單總金額->delimiter;--將分割符改回分號(hào)->call ordertotal(200005,@total);--由于不存在輸出參數(shù),故定義一個(gè)輸出變量,變量必須用@開(kāi)頭->select @total;返回結(jié)果為149.87 備注:書(shū)本第十一章后的編程題,使用update語(yǔ)句,兩個(gè)參數(shù)類(lèi)型都需要為in。--顯示存儲(chǔ)過(guò)程->Show create procedure ordertotal;--刪除存儲(chǔ)過(guò)程
->Drop procedure ordertotal;21.存儲(chǔ)函數(shù)
存儲(chǔ)函數(shù)與存儲(chǔ)過(guò)程的區(qū)別:.存儲(chǔ)函數(shù)不能擁有輸出參數(shù);.存儲(chǔ)函數(shù)可直接調(diào)用,且不需使用call語(yǔ)句,而存儲(chǔ)過(guò)程的調(diào)用必須使用call語(yǔ)句;.存儲(chǔ)函數(shù)中必須包含一條return語(yǔ)句,而這條特殊的SQL語(yǔ)句不允許包含于存儲(chǔ)過(guò)程。--建立存儲(chǔ)函數(shù):根據(jù)給定的cust_id返回客戶(hù)所在的州名(縮寫(xiě)),若庫(kù)中無(wú)給定的cust_id,則返回“不存在該客戶(hù)”。->delimiter //->create function fn_search(c_id int)->returns varchar(50)--定義返回的數(shù)據(jù)類(lèi)型,與函數(shù)部分中的數(shù)據(jù)類(lèi)型需統(tǒng)一,如函數(shù)中的“不存在該客戶(hù)”為6個(gè)字符,如果這里設(shè)置為char(5),則無(wú)法輸出該結(jié)果->deterministic– 表示對(duì)于相同的輸入值,返回值也相同->begin->declare state char(2);--聲明一個(gè)變量state,作為輸出的州變量->select cust_state from customers where cust_id=c_id into state;->if state is null then->return(select ’不存在該客戶(hù)’);--注意這里return不用加s->else->return(select state);->end if;->end;->//--執(zhí)行存儲(chǔ)函數(shù)
->select fn_search(10001);--刪除存儲(chǔ)函數(shù)
->drop function fn_search;--刪除前要確定該函數(shù)無(wú)依賴(lài)關(guān)系,即不存在其他存儲(chǔ)過(guò)程或存儲(chǔ)函數(shù)調(diào)用過(guò)該存儲(chǔ)函數(shù)。
22.觸發(fā)器:MySQL響應(yīng)insert、delete、update語(yǔ)句時(shí)自動(dòng)執(zhí)行的一條MySQL語(yǔ)句,創(chuàng)建觸發(fā)器時(shí)需要給出的4條信息:唯一的觸發(fā)器名、觸發(fā)器相關(guān)的表、觸發(fā)器應(yīng)該響應(yīng)的活動(dòng)(insert delete、update)、觸發(fā)器何時(shí)執(zhí)行(處理前或處理后)。
(1)insert觸發(fā)器:當(dāng)對(duì)表插入數(shù)據(jù)時(shí)起作用,含有一個(gè)虛擬表New,可訪問(wèn)增加的行,只能用after--建立一個(gè)insert觸發(fā)器,用于記錄insert語(yǔ)句操作時(shí)的系統(tǒng)時(shí)間和插入的order_num->delimiter //->create trigger trg_order_insert after insert on orders for each row 觸發(fā)器 觸發(fā)器名 執(zhí)行時(shí)間 相關(guān)表->begin->insert into order_log(o_date,order_num)values(now(),new.order_num);--order_log是事先建立好的表,用于記錄insert語(yǔ)句操作時(shí)的系統(tǒng)時(shí)間和插入的order_num->end;->//--執(zhí)行insert觸發(fā)器->delimiter;->insert into orders(order_date,cust_id)values(’2010-9-15’,10001);--由于order_num是自動(dòng)遞增的,故在這里不作為插入對(duì)象(2)delete觸發(fā)器:當(dāng)對(duì)表刪除數(shù)據(jù)時(shí)起作用,含有一個(gè)虛擬表Old,可訪問(wèn)被刪除的行,只能用after,創(chuàng)建方法與insert類(lèi)似,區(qū)別在于delete和old--建立一個(gè)delete觸發(fā)器,用于記錄delete語(yǔ)句操作時(shí)的系統(tǒng)時(shí)間和刪除的order_num->delimiter //->create trigger trg_order_delete after delete on orders for each row 觸發(fā)器 觸發(fā)器名 執(zhí)行時(shí)間 相關(guān)表->begin->insert into order_log(o_date,order_num)values(now(),old.order_num);--order_log是事先建立好的表,用于記錄delete語(yǔ)句操作時(shí)的系統(tǒng)時(shí)間和刪除的order_num->end;->//--執(zhí)行delete觸發(fā)器->delimiter;->delete from orders where order_num=20010;(3)update觸發(fā)器:當(dāng)對(duì)表修改數(shù)據(jù)時(shí)起作用,同時(shí)含有new和old兩個(gè)虛擬表。結(jié)合New可訪問(wèn)更新行的記錄;結(jié)合old可訪問(wèn)更新前行的記錄,可用after,也可用before。1用after ○--建立一個(gè)update觸發(fā)器,用于記錄update語(yǔ)句操作時(shí)的系統(tǒng)時(shí)間和更新數(shù)據(jù)的order_num->delimiter //->create trigger trg_order_update after update on orders for each row 觸發(fā)器 觸發(fā)器名 執(zhí)行時(shí)間 相關(guān)表->begin->insert into order_log(o_date,order_num)values(now(),old.order_num);->end;->//--執(zhí)行update觸發(fā)器->delimiter;->update orders set order_date=’2015-9-18’ where cust_id=10001;2用before ○--建立一個(gè)update觸發(fā)器,如果更新后的prod_price大于原來(lái)的1.2倍,則用原來(lái)的1.2倍作為當(dāng)前價(jià)格->delimiter //->create trigger trg_order_update before update on orders for each row 觸發(fā)器 觸發(fā)器名 執(zhí)行時(shí)間 相關(guān)表->begin->if new.prod_price>old.prod_price*1.2 then->set new.prod_price=old.prod_price*1.2;->end if;->end;->//(4)刪除觸發(fā)器:drop trigger trg_name;23.事件:臨時(shí)觸發(fā)器,要使用事件調(diào)度器,必須開(kāi)啟“event_scheduler”.查看:show variables like ’event_scheduler’;.開(kāi)啟:set global event_scheduler=1;(1)創(chuàng)建事件
CREATE EVENT EVENT_NAME ON SCHEDULE schedule DO event_body;其中schedule的語(yǔ)法格式為
AT timestamp [+INTERVAL interval]?|every interval--指定事件執(zhí)行的時(shí)間,可以為某時(shí)刻點(diǎn)即timestamp,或某時(shí)刻點(diǎn)開(kāi)始的interval時(shí)間后,或者為每隔interval時(shí)間執(zhí)行一次
[starts timestamp [+INTERVAL interval]]--設(shè)置事件開(kāi)始執(zhí)行的時(shí)間 [ends timestamp [+INTERVAL interval]]--設(shè)置事件終止執(zhí)行的時(shí)間
--建立一個(gè)事件,用于每個(gè)月向customers表中插入一條數(shù)據(jù)“l(fā)iyang、廣州”,該事件從下個(gè)月開(kāi)始并于2015-12-31結(jié)束->delimiter //->create event event_insert on schedule every 1 month->starts curdate()+interval 1 month->ends ’2015-12-31’->do->begin->if year(curdate())<2015 then->insert into customers(cust_name,cust_adress)values(’liyang’,’廣州’);->end if;->end;->//(2)修改事件,用于修改時(shí)間的狀態(tài):alter event event_name{enable|disable};(3)刪除事件:drop event event_name;24.管理實(shí)務(wù)處理:start transaction?
實(shí)務(wù)處理的術(shù)語(yǔ):
(1)實(shí)務(wù)(transaction):一組SQL語(yǔ)句;(2)回退(rollback):撤銷(xiāo)指定SQL語(yǔ)句的過(guò)程;(3)提交(commit):指定未存儲(chǔ)的SQL語(yǔ)句結(jié)果寫(xiě)入到數(shù)據(jù)庫(kù)表里,提交后無(wú)法回退;(4)保留點(diǎn)(savepoint):實(shí)務(wù)處理中設(shè)置的臨時(shí)占位符。
25.安全管理(用戶(hù)創(chuàng)建修改與刪除以及用戶(hù)權(quán)限的查看設(shè)置與撤銷(xiāo))(1)創(chuàng)建用戶(hù)賬號(hào):create user ben identified by ’ben’;(2)修改用戶(hù)賬號(hào):update mysql.user set user=’new_ben’ where user=’ben’;--從mysql數(shù)據(jù)庫(kù)中的用戶(hù)表user進(jìn)行修改(3)查看訪問(wèn)權(quán)限:show grants for new_ben;(4)設(shè)置訪問(wèn)權(quán)限:grant?to?.grant {all|select,update,delete,insert}on {*.*|crashcourse.*|crashcourse.cus tomers} to new_ben;.grant select(cust_id,cust_name)on crashcourse.customers to new_ben;--可針對(duì){整個(gè)服務(wù)器|整個(gè)數(shù)據(jù)庫(kù)|數(shù)據(jù)庫(kù)中某個(gè)表|數(shù)據(jù)庫(kù)中某個(gè)表的某些字段},對(duì)用戶(hù)同時(shí)設(shè)置全部或一種或多種權(quán)限
(5)撤銷(xiāo)訪問(wèn)權(quán)限:revoke?from?,用法與grant?to?類(lèi)似(6)更改口令(密碼)
Set password for new_ben=password(’new_ben’);(7)刪除用戶(hù):drop user new_ben;26.數(shù)據(jù)庫(kù)備份與還原.使用SQL語(yǔ)句
backup table tbl_name to?/restore table tbl_name from?(只用于MyISAM表)select?intooutfile?/load data?infile?into table tlb_name.使用命令行實(shí)用程序:mysqlhotcopy(只用于MyISAM表)或mysqldump/mysql(1)使用select?intooutfile?/load data?infile?into table tlb_name.備份數(shù)據(jù):
Select * from mysql.products into outfile ’d:products.txt’ [Fields terminated by ’,’ optionally enclosed by ’”’
lines terminated by ’nr’;--定義字段間的分割符、字符型數(shù)據(jù)的存放形式、行與行之間的分割符.恢復(fù)數(shù)據(jù)
Load data infile ’d:products.txt’into table customers.copy [Fields terminated by ’,’ optionally enclosed by ’”’
lines terminated by ’nr’;--必須與備份時(shí)一致(2)使用命令行實(shí)用程序mysqldump/mysql(文本形式)
進(jìn)入cmd運(yùn)行界面(mysqldump—help 可用于獲取mysqldump的選項(xiàng)表及更多幫助信息).備份整個(gè)數(shù)據(jù)庫(kù)服務(wù)器、或整個(gè)數(shù)據(jù)庫(kù)或數(shù)據(jù)庫(kù)中某個(gè)表
Mysqldump –u root –proot –P 3306 –h localhost {all-databases|mysql_test [products]}>d:data.sql.恢復(fù)數(shù)據(jù)
Mysql –u root –proot –P 3306 –h localhost {all-databases|mysql_test [products]} (1)analyze table tbl_name;更新表的索引散列程度,檢查表鍵是否正確(2)check table tbl_name;檢查一個(gè)或多個(gè)表是否有錯(cuò)誤 (3)checksum table tbl_name;對(duì)數(shù)據(jù)庫(kù)中的表進(jìn)行校驗(yàn),保證數(shù)據(jù)的一致性 (4)optimize table tbl_name;利用表中未使用的空間并整理數(shù)據(jù)文件碎片,保證數(shù)據(jù)讀取效率 (5)repair table tbl_name;修復(fù)一個(gè)或多個(gè)可能被損害的MyISAM表 28.二進(jìn)制日志文件的使用:mysqlbinlog 29.使用PHP進(jìn)行MySQL數(shù)據(jù)庫(kù)編程 編程步驟: .首先建立與MySQL數(shù)據(jù)庫(kù)服務(wù)器的連接;.然后選擇要對(duì)其進(jìn)行操作的數(shù)據(jù)庫(kù); .再執(zhí)行相應(yīng)的數(shù)據(jù)庫(kù)操作,包括對(duì)數(shù)據(jù)的添加、刪除、修改和查詢(xún)等;.最后關(guān)閉與MySQL數(shù)據(jù)庫(kù)服務(wù)器的連接。(1)數(shù)據(jù)庫(kù)服務(wù)器連接、選擇數(shù)據(jù)庫(kù) .使用mysql_connect()建立非持久連接 Php $con=mysql_connect(“l(fā)ocalhost:3306”,“root”,“123456”);if(!$con){ echo “數(shù)據(jù)庫(kù)服務(wù)器連接失敗! .使用mysql_pconnect()建立持久連接 ”;echo “用戶(hù)名:root .使用mysql_select_db(databases[,connection])選擇數(shù)據(jù)庫(kù) ”;die();} mysql_select_db(“mysql_test”,$con);if(mysql_errno()){ echo “數(shù)據(jù)庫(kù)選擇失敗! /*向數(shù)據(jù)庫(kù)mysql_test中的表customers添加一個(gè)名為“李中華”的客戶(hù)的全部信息*/ ”);Mysql_select_db(“mysql_test”,$con)or die(“數(shù)據(jù)庫(kù)選擇失敗! $sql=“insert into customers(’cust_id’,’cust_name’,’cust_sex’)”;$sql=$sql.“values(null,’李中華’,’M’)”;if(mysql_query($sql,$con))echo “客戶(hù)信息添加成功! .數(shù)據(jù)的更新 /*將數(shù)據(jù)庫(kù)mysql_test的表customers中的一個(gè)名為“李中華”的客戶(hù)的地址修改為“廣州市”*/ ”);Mysql_select_db(“mysql_test”,$con)or die(“數(shù)據(jù)庫(kù)選擇失敗! .數(shù)據(jù)的刪除 /*將數(shù)據(jù)庫(kù)mysql_test的表customers中一個(gè)名為“李中華”的客戶(hù)信息刪除*/ ”);Mysql_select_db(“mysql_test”,$con)or die(“數(shù)據(jù)庫(kù)選擇失敗! (3)數(shù)據(jù)庫(kù)的查詢(xún) .使用mysql_fetch_array(data[,array_type])讀取結(jié)果集中的記錄 /*在數(shù)據(jù)庫(kù)mysql_test的表customers中查詢(xún)cust_id為916的客戶(hù)的姓名*/ ”);Mysql_select_db(“mysql_test”,$con)or die(“數(shù)據(jù)庫(kù)選擇失敗! .使用mysql_num_rows(data)讀取結(jié)果集中的記錄數(shù) /*在數(shù)據(jù)庫(kù)mysql_test的表customers中查詢(xún)女性客戶(hù)的人數(shù)*/ ”);Mysql_select_db(“mysql_test”,$con)or die(“數(shù)據(jù)庫(kù)選擇失敗! 查詢(xún)(R)Selec子句 書(shū)寫(xiě)順序 Select distinct:要返回的列或表達(dá)式 From:從中檢索數(shù)據(jù)的表 Where:行級(jí)過(guò)濾/分組前過(guò)濾 group by:分組說(shuō)明 having:組級(jí)過(guò)濾/分組后過(guò)濾 order by:輸出排序順序desc/asc limit start, count:要檢索的行數(shù) limit 3,4 從第3行開(kāi)始的連續(xù)4行 SELECT order_num,sum(quantity*item_price)ordertotal FROM orderitems WHEREorder_numBETWEEN20005AND 20009GROUP BY order_num HAVINGsum(quantity*item_price)>=50ORDER BYordertotalDESCLIMIT4;執(zhí)行順序 From表名->inner/left/right/ join on->where->group by->select distinct *->having->order by->limit start, count 插入Insert into(C)INSERT INTO students(name)values(‘楊過(guò)’),(‘小龍女’);更新(U)Update UPDATE customers SETcust_email = ‘elmer@fudd.com’WHEREcust_id = 10005;刪除(D)Delete DELETE FROM customers WHEREcust_id = 10006;Truncate刪除原來(lái)的表并重新創(chuàng)建一個(gè)表,刪除標(biāo)的全部?jī)?nèi)容時(shí)效率高。 操作表 創(chuàng)建CREATE TABLE customers(Cust_idint not null auto_increment primary key,Cust_name char(50)not null,Cust_addresschar(50)null)engine = innoDB;更新ALTER TABLE刪除DROP TABLEcustomers2;重命名RENAME TABLE customers2 TO customers;操作數(shù)據(jù)庫(kù) 創(chuàng)建CREATE DATABASE xxx charset = utf8;刪除DROP DATABASEXXX;切換USE XXX;查看SHOW DATABASES;關(guān)聯(lián)查詢(xún) INNER/LEFT/RIGHTJOIN ON SELECT students.name,subjects.title,scores.scoreFROM scores INNER JOIN students ONscores.stuid = students.id INNER JOIN subjects ONscores.subid = subjects.id; 1.數(shù)據(jù)庫(kù)創(chuàng)建 : Create database db_name; 數(shù)據(jù)庫(kù)刪除 : Drop database db_name;刪除時(shí)可先判斷是否存在,寫(xiě)成 : drop database if exits db_name.建表 : 創(chuàng)建數(shù)據(jù)表的語(yǔ)法 : create table table_name(字段1 數(shù)據(jù)類(lèi)型 , 字段2 數(shù)據(jù)類(lèi)型); 例 : create table mytable(id int , username char(20)); 刪表 : drop table table_name;例 : drop table mytable;.添加數(shù)據(jù) : Insert into 表名 [(字段1 , 字段2 , ….)] values(值1 , 值2 , …..); 如果向表中的每個(gè)字段都插入一個(gè)值,那么前面 [ ] 括號(hào)內(nèi)字段名可寫(xiě)也可不寫(xiě) 例 : insert into mytable(id,username)values(1,’zhangsan’);.查詢(xún) : 查詢(xún)所有數(shù)據(jù) : select * from table_name; 查詢(xún)指定字段的數(shù)據(jù) : select 字段1 , 字段2 from table_name; 例 : select id,username from mytable where id=1 order by desc;多表查詢(xún)語(yǔ)句------------參照第17條實(shí)例.更新指定數(shù)據(jù) , 更新某一個(gè)字段的數(shù)據(jù)(注意,不是更新字段的名字) Update table_name set 字段名=’新值’ [, 字段2 =’新值’ , …..][where id=id_num] [order by 字段 順序] 例 : update mytable set username=’lisi’ where id=1; Order語(yǔ)句是查詢(xún)的順序 , 如 : order by id desc(或asc), 順序有兩種 : desc倒序(100—1,即從最新數(shù)據(jù)往后查詢(xún)),asc(從1-100),Where和order語(yǔ)句也可用于查詢(xún)select 與刪除delete.刪除表中的信息 : 刪除整個(gè)表中的信息 : delete from table_name; 刪除表中指定條件的語(yǔ)句 : delete from table_name where 條件語(yǔ)句;條件語(yǔ)句如 : id=3;.創(chuàng)建數(shù)據(jù)庫(kù)用戶(hù) 一次可以創(chuàng)建多個(gè)數(shù)據(jù)庫(kù)用戶(hù)如: CREATE USER username1 identified BY ‘password’ , username2 IDENTIFIED BY ‘password’…..用戶(hù)的權(quán)限控制:grant 庫(kù),表級(jí)的權(quán)限控制 : 將某個(gè)庫(kù)中的某個(gè)表的控制權(quán)賦予某個(gè)用戶(hù) Grant all ON db_name.table_name TO user_name [ indentified by ‘password’ ];.表結(jié)構(gòu)的修改 (1)增加一個(gè)字段格式: alter table table_name add column(字段名 字段類(lèi)型);----此方法帶括號(hào) (2)指定字段插入的位置: alter table table_name add column 字段名 字段類(lèi)型 after 某字段; 刪除一個(gè)字段: alter table table_name drop字段名; (3)修改字段名稱(chēng)/類(lèi)型 alter table table_name change 舊字段名 新字段名 新字段的類(lèi)型; (4)改表的名字 alter table table_name rename to new_table_name; (5)一次性清空表中的所有數(shù)據(jù) truncate table table_name;此方法也會(huì)使表中的取號(hào)器(ID)從1開(kāi)始.增加主鍵,外鍵,約束,索引。。(使用方法見(jiàn)17實(shí)例) ① 約束(主鍵Primary key、唯一性Unique、非空Not Null) ② 自動(dòng)增張 auto_increment ③外鍵Foreign key-----與reference table_name(col_name列名)配合使用,建表時(shí)單獨(dú)使用 ④ 刪除多個(gè)表中有關(guān)聯(lián)的數(shù)據(jù)----設(shè)置foreign key 為set null---具體設(shè)置參考幫助文檔.查看數(shù)據(jù)庫(kù)當(dāng)前引擎 SHOW CREATE TABLE table_name; 修改數(shù)據(jù)庫(kù)引擎 ALTER TABLE table_name ENGINE=MyISAM | InnoDB;.SQL語(yǔ)句運(yùn)用實(shí)例: --1 建users表 create table users (id int primary key auto_increment, nikename varchar(20)not null unique, password varchar(100)not null, address varchar(200),reg_date timestamp not null default CURRENT_TIMESTAMP); --2 建articles表,在建表時(shí)設(shè)置外鍵 create table articles(id int primary key auto_increment,content longtext not null,userid int,constraint foreign key(userid)references users(id)on delete set null); --------- --2.1 建articles表,建表時(shí)不設(shè)置外鍵 create table articles(id int primary key auto_increment,content longtext not null,userid int); --2.2 給articles表設(shè)置外鍵 alter table articles add constraint foreign key(userid)references users(id)on delete set null; ---------- --3.向users表中插入數(shù)據(jù),同時(shí)插入多條 insert into users (id,nikename,password,address) values(1,'lyh1','1234',null),(10,'lyh22','4321','湖北武漢'),(null,'lyh333','5678', '北京海淀'); --4.向article中插入三條數(shù)據(jù) insert into articles (id,content,userid) values(2,'hahahahahaha',11),(null,'xixixixixix',10),(13,'aiaiaiaiaiaiaiaiaiaiaiaia',1),(14,'hohoahaoaoooooooooo',10); --5.進(jìn)行多表查詢(xún),選擇users表中ID=10的用戶(hù)發(fā)布的所有留言及該用戶(hù)的所有信息 select articles.id,articles.content,users.* from users,articles where users.id=10 and articles.userid=users.id order by articles.id desc; --6.查看數(shù)據(jù)庫(kù)引擎類(lèi)型 show create table users; --7.修改數(shù)據(jù)庫(kù)引擎類(lèi)型 alter table users engine=MyISAM;---因?yàn)閡sers表中ID被設(shè)置成外鍵,執(zhí)行此句會(huì)出錯(cuò) --8.同表查詢(xún),已知一個(gè)條件的情況下.查詢(xún)ID號(hào)大于用戶(hù)lyh1的ID號(hào)的所有用戶(hù) select a.id,a.nikename,a.address from users a,users b where b.nikename='lyh1' and a.id>b.id; ------也可寫(xiě)成 select id,nikename,address from users where id>(select id from users where nikename='lyh1'); 9.顯示年齡比領(lǐng)導(dǎo)還大的員工: select a.name from users a,users b where a.managerid=b.id and a.age>b.age; 查詢(xún)編號(hào)為2的發(fā)帖人: 先查articles表,得到發(fā)帖人的編號(hào),再根據(jù)編號(hào)查users得到的用戶(hù)名。 接著用關(guān)聯(lián)查詢(xún).select * from articles,users得到笛卡兒積,再加order by articles.id以便觀察 使用select * from articles,users where articles.id=2 篩選出2號(hào)帖子與每個(gè)用戶(hù)的組合記錄 再使用select * from articles,users where articles.id=2 and articles.userid=users.id選出users.id等于2號(hào)帖的發(fā)帖人id的記錄.只取用戶(hù)名:select user where user.id=(select userid from articles where article.id =2) 找出年齡比小王還大的人:假設(shè)小王是28歲,先想找出年齡大于28的人 select * from users where age>(select age from users where name='xiaowang'); *****要查詢(xún)的記錄需要參照表里面的其他記錄: select a.name from users a,users b where b.name='xiaowang' and a.age>b.age 表里的每個(gè)用戶(hù)都想pk一下.select a.nickname,b.nickname from users a,users b where a.id>b.id; 更保險(xiǎn)的語(yǔ)句:select a.nickname,b.nickname from(select * from users order by id)a,(se lect * from users order by id)b where a.id>b.id; 再查詢(xún)某個(gè)人發(fā)的所有帖子.select b.* from articles a , articles b where a.id=2 and a.userid=b.userid 說(shuō)明: 表之間存在著關(guān)系,ER概念的解釋?zhuān)胊ccess中的示例數(shù)據(jù)庫(kù)演示表之間的關(guān)系.只有innodb引擎才支持foreign key,mysql的任何引擎目前都不支持check約束。 Mysql數(shù)據(jù)庫(kù)學(xué)習(xí)心得(1) 由于工作中需要使用mysql,筆者通過(guò)網(wǎng)上學(xué)習(xí),動(dòng)手實(shí)踐,終于從一個(gè)“數(shù)據(jù)庫(kù)菜鳥(niǎo)”變成了能熟練操作mysql的“準(zhǔn)大蝦”了,:)。現(xiàn)將學(xué)習(xí)心得整理如下。 MySQL是完全網(wǎng)絡(luò)化的跨平臺(tái)關(guān)系型數(shù)據(jù)庫(kù)系統(tǒng),一個(gè)真正的多用戶(hù)、多線程SQL數(shù)據(jù)庫(kù)服務(wù)器,同時(shí)是具有客戶(hù)機(jī)/服務(wù)器體系結(jié)構(gòu)的分布式數(shù)據(jù)庫(kù)管理系統(tǒng)。它具有功能強(qiáng)、使 用簡(jiǎn)便、管理方便、運(yùn)行速度快、安全可靠性強(qiáng)等優(yōu)點(diǎn),用戶(hù)可利用許多語(yǔ)言編寫(xiě)訪問(wèn)MySQL數(shù)據(jù)庫(kù)的程序,對(duì)于中、小型應(yīng)用系統(tǒng)是非常理想的。除了支持標(biāo)準(zhǔn)的ANSI SQL語(yǔ)句,更重要的是,它還支持多種平臺(tái),而在Unix系統(tǒng)上該軟件支持多線程運(yùn)行方式,從而能獲得相當(dāng)好的性能。對(duì)于不使用Unix的用戶(hù),它可以在Windows NT系統(tǒng)上以系統(tǒng)服務(wù)方式運(yùn)行,或者在Windows 95/98系統(tǒng)上以普通進(jìn)程方式運(yùn)行。而在Unix/Linux系統(tǒng)上,MySQL支持多線程運(yùn)行方式,從而能獲得相當(dāng)好的性能,而且它是屬于開(kāi)放源代碼軟。 MySQL是以一個(gè)客戶(hù)機(jī)/服務(wù)器結(jié)構(gòu)的實(shí)現(xiàn),它由一個(gè)服務(wù)器守護(hù)程序mysqld和很多不同的客戶(hù)程序和庫(kù)組成,MySQL的執(zhí)行性能非常高,運(yùn)行速度非常快,并非常容易使用,是一個(gè)非常棒的數(shù)據(jù)庫(kù)。MySQL的官方發(fā)音是“My Ess Que Ell”(不是MY-SEQUEL)。 一.獲得MySQL 首先必須下載MySQL。Mysql的官方網(wǎng)站是:www.tmdps.cnf copy為c:my.cnf,并把c:mysqlibgwinb19.dll copy到winntsystem32。 3。啟動(dòng)mysql的方法是: c:mysqlinmysqld-shareware--install net start mysql 這么簡(jiǎn)單,就可以啟動(dòng)了。 4。更改超級(jí)用戶(hù)(root)的密碼: C:mysqlinmysql mysql mysql> UPDATE user SET password=PASSWORD('your password')swheresuser='root'; mysql> QUIT C:mysqlinmysqladmin reload 使用命令C:mysqlinmysqlshow去看看檢測(cè)一下。在這里應(yīng)該顯示: +-----------+ | Databases | +-----------+ | mysql | | test | +-----------+ 看到這些信息這一步就證明沒(méi)問(wèn)題了 再來(lái): C:mysqlinmysqlshow--user=root--password=your password mysql 在這里應(yīng)該顯示: Database: mysql +--------------+ | Tables | +--------------+ | columns_priv | | db | | host | | tables_priv | | user | +--------------+ 一切搞定! 5。C:mysqlinmysqladmin version status proc 應(yīng)該可以看到這些版本信息的: mysqladmin Ver 8.0 Distrib 3.22.32, for Win95/Win98 on i586 TCX Datakonsult AB, by Monty Server version 3.22.32-shareware-debug Protocol version 10 Connection localhost via TCP/IP TCP port 3306 Uptime: 1 hour 29 min 30 sec Threads: 1 Questions: 72 Slow queries: 0 Opens: 16 Flush tables: 1 Open tables: 0 Memory in use: 16423K Max memory used: 16490K Uptime: 5370 Threads: 1 Questions: 72 Slow queries: 0 Opens: 16 Flush tables: 1 Open tables: 0 Memory in use: 16423K Max memory used: 16490K +----+------+-----------+----+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+----+---------+------+-------+------------------+ | 35 | ODBC | localhost | | Query | 0 | | show processlist | +----+------+-----------+----+---------+------+-------+------------------+ 接著,用show databases命令可以將安裝的數(shù)據(jù)庫(kù)列出來(lái): mysql> show databases; 你就可以看到: +----------+ | Database | +----------+ | mysql | | test | +----------+ rows in set(0.00 sec) 如果一切正常的話,那說(shuō)明MySQL可以完全工作了!如果要退出程序,輸入:exit mysql> exit; Bye Mysql數(shù)據(jù)庫(kù)學(xué)習(xí)心得(3)------------------ 三.Mysql常識(shí) (一)字段類(lèi)型 1.INT[(M)] 正常大小整數(shù)類(lèi)型 2.DOUBLE[(M,D)] [ZEROFILL] 正常大小(雙精密)浮點(diǎn)數(shù)字類(lèi)型 3.DATE 日期類(lèi)型。支持的范圍是'1000-01-01'到'9999-12-31'。MySQL以'YYYY-MM-DD'格式來(lái)顯示DATE值,但是允許你使用字符串或數(shù)字把值賦給 DATE列 4.CHAR(M) 定長(zhǎng)字符串類(lèi)型,當(dāng)存儲(chǔ)時(shí),總是是用空格填滿(mǎn)右邊到指定的長(zhǎng)度 5.BLOB TEXT BLOB或TEXT類(lèi)型,最大長(zhǎng)度為65535(2^16-1)個(gè)字符。 6.VARCHAR 變長(zhǎng)字符串類(lèi)型,最常用的類(lèi)型。 (二)基本操作 1:顯示數(shù)據(jù)庫(kù) mysql>SHOW DATABASES; 2:當(dāng)前選擇的數(shù)據(jù)庫(kù),mysql> SELECT DATABASE(); +------------+ | DATABASE()| +------------+ | test | +------------+ 3.當(dāng)前數(shù)據(jù)庫(kù)包含的表信息: mysql> SHOW TABLES; +---------------------+ | Tables in test | +---------------------+ | mytable1 | | mytable2 | +---------------------+ 4.獲取表結(jié)構(gòu) mysql> desc mytable1; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | s1 | varchar(20)| YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 5.創(chuàng)建表 表是數(shù)據(jù)庫(kù)的最基本元素之一,表與表之間可以相互獨(dú)立,也可以相互關(guān)聯(lián)。創(chuàng)建表的基本語(yǔ)法如下: create table table_name (column_name datatype {identity |null|not null},…) 其中參數(shù)table_name和column_name必須滿(mǎn)足用戶(hù)數(shù)據(jù)庫(kù)中的識(shí)別器(identifier)的要求,參數(shù)datatype是一個(gè)標(biāo)準(zhǔn)的SQL類(lèi)型或由用戶(hù)數(shù) 據(jù)庫(kù)提供的類(lèi)型。用戶(hù)要使用non-null從句為各字段輸入數(shù)據(jù)。 create table還有一些其他選項(xiàng),如創(chuàng)建臨時(shí)表和使用select子句從其他的表中讀取某些字段組成新表等。還有,在創(chuàng)建表是可用PRIMARY KEY、KEY、INDEX等標(biāo)識(shí)符設(shè)定某些字段為主鍵或索引等。書(shū)寫(xiě)上要注意:在一對(duì)圓括號(hào)里的列出完整的字段清單。字段名間用逗號(hào)隔開(kāi) 。字段名間的逗號(hào)后要加一個(gè)空格。最后一個(gè)字段名后不用逗號(hào)。所有的SQL陳述都以分號(hào)“;”結(jié)束。 例: mysql>CREATE TABLE guest(name varchar(10),sex varchar(2),age int(3),career varchar(10)); 6.創(chuàng)建索引 索引用于對(duì)數(shù)據(jù)庫(kù)的查詢(xún)。一般數(shù)據(jù)庫(kù)建有多種索引方案,每種方案都精于某一特定的查詢(xún)類(lèi)。索引可以加速對(duì)數(shù)據(jù)庫(kù)的查詢(xún)過(guò)程。創(chuàng)建 索引的基本語(yǔ)法如下: create index index_name on table_name(col_name[(length)],...) 例: mysql> CREATE INDEX number ON guest(number(10)); 7.執(zhí)行查詢(xún) 查詢(xún)是使用最多的SQL命令。查詢(xún)數(shù)據(jù)庫(kù)需要憑借結(jié)構(gòu)、索引和字段類(lèi)型等因素。大多數(shù)數(shù)據(jù)庫(kù)含有一個(gè)優(yōu)化器(optimizer),把用戶(hù)的查 詢(xún)語(yǔ)句轉(zhuǎn)換成可選的形式,以提高查詢(xún)效率。 值得注意的是MySQL不支持SQL92標(biāo)準(zhǔn)的嵌套的where子句,即它只支持一個(gè)where子句。其基本語(yǔ)法如下: SELECT [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [HIGH_PRIORITY] [DISTINCT | DISTINCTROW | ALL] select_expression,...[INTO {OUTFILE | DUMPFILE} 'file_name' export_options] [FROM table_references [WHERE where_definition] [GROUP BY col_name,...] [HAVING where_definition] [ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC] ,...] [LIMIT [offset,] rows] [PROCEDURE procedure_name] ] 其中where從句是定義選擇標(biāo)準(zhǔn)的地方,where_definition可以有不同的格式,但都遵循下面的形式: 字段名操作表達(dá)式 字段名操作字段名 在第一種形式下,標(biāo)準(zhǔn)把字段的值與表達(dá)式進(jìn)行比較;在第二種形式下,把兩個(gè)字段的值進(jìn)行比較。根據(jù)所比較的數(shù)據(jù)類(lèi)型,search_condition中的操作可能選以下幾種: =檢查是否相等 !=檢查是否不等 >(或>=)檢查左邊值是否大于(或大于等于)右邊值 <(或<=)檢查左邊值是否小于(或小于等于)右邊值 [not] between檢查左邊值是否在某個(gè)范圍內(nèi) [not] in檢查左邊是否某個(gè)特定集的成員 [not] like檢查左邊是否為右邊的子串 is [not] null檢查左邊是否為空值 在這里,可以用通配符_代表任何一個(gè)字符,%代表任何字符串。使用關(guān)鍵字、和可以生成復(fù)雜的詞,它們運(yùn)行檢查時(shí)使用 布爾表達(dá)式的多重標(biāo)準(zhǔn)集。 例: mysql> select t1.name, t2.salary from employee AS t1, info AS t2swherest1.name = t2.name; mysql> select college, region, seed from tournament ORDER BY region, seed; mysql> select col_name from tbl_nameswherescol_name > 0; 8.改變表結(jié)構(gòu) 在數(shù)據(jù)庫(kù)的使用過(guò)程中,有時(shí)需要改變它的表結(jié)構(gòu),包括改變字段名,甚至改變不同數(shù)據(jù)庫(kù)字段間的關(guān)系。可以實(shí)現(xiàn)上述改變的命令是 alter,其基本語(yǔ)法如下: alter table table_name alter_spec [, alter_spec...] 例: mysql> alter table dbname add column userid int(11)not null primary key auto_increment; 這樣,就在表dbname中添加了一個(gè)字段userid,類(lèi)型為int(11)。 9.修改表中數(shù)據(jù) 在使用數(shù)據(jù)庫(kù)過(guò)程中,往往要修改其表中的數(shù)據(jù),比如往表中添加新數(shù)據(jù),刪除表中原有數(shù)據(jù),或?qū)Ρ碇性袛?shù)據(jù)進(jìn)行更改。它們的基本 語(yǔ)法如下: 數(shù)據(jù)添加: insert [into] table_name [(column(s))] values(expression(s)) 例: mysql>insertsintosmydatabase values('php','mysql','asp','sqlserver','jsp','oracle');Mysql數(shù)據(jù)庫(kù)學(xué)習(xí)心得(4)------------------ 10.表的數(shù)據(jù)更新 (1)一次修改一個(gè)字段,再次注意語(yǔ)法。文本需要加引號(hào)但數(shù)字不要。 mysql>update table01 set field03='new info'swheresfield01=1; Query OK, 1 row affected(0.00 sec) (2)一次改變多個(gè)字段,記住在每一個(gè)更新的字段間用逗號(hào)隔開(kāi)。 mysql>update table01 set field04=19991022, field05=062218swheresfield01=1; Query OK, 1 row affected(0.00 sec) (3)一次更新多個(gè)數(shù)據(jù) mysql>update table01 set field05=152901swheresfield04>19990101; Query OK, 3 rows affected(0.00 sec) 11.刪除數(shù)據(jù) mysql>delete from table01swheresfield01=3; Query OK, 1 row affected(0.00 sec) 12.導(dǎo)入數(shù)據(jù)庫(kù)表 (1)創(chuàng)建.sql文件 (2)先產(chǎn)生一個(gè)庫(kù)如auction.c:mysqlin>mysqladmin-u root-p creat auction,會(huì)提示輸入密碼,然后成功創(chuàng)建。 (3)導(dǎo)入auction.sql文件 c:mysqlin>mysql-u root-p auction < auction.sql。 通過(guò)以上操作,就可以創(chuàng)建了一個(gè)數(shù)據(jù)庫(kù)auction以及其中的一個(gè)表auction。 13.mysql數(shù)據(jù)庫(kù)的授權(quán) mysql>grant select,insert,delete,create,drop on *.*(或test.*/user.*/..) to用戶(hù)名@localhost identified by '密碼'; 如:新建一個(gè)用戶(hù)帳號(hào)以便可以訪問(wèn)數(shù)據(jù)庫(kù),需要進(jìn)行如下操作: mysql> grant usage -> ON test.* -> TO testuser@localhost; Query OK, 0 rows affected(0.15 sec) 此后就創(chuàng)建了一個(gè)新用戶(hù)叫:testuser,這個(gè)用戶(hù)只能從localhost連接到數(shù)據(jù)庫(kù)并可以連接到test數(shù)據(jù)庫(kù)。下一步,我們必須指定 testuser這個(gè)用戶(hù)可以執(zhí)行哪些操作: mysql> GRANT select, insert, delete,update -> ON test.* -> TO testuser@localhost; Query OK, 0 rows affected(0.00 sec) 此操作使testuser能夠在每一個(gè)test數(shù)據(jù)庫(kù)中的表執(zhí)行SELECT,INSERT和DELETE以及UPDATE查詢(xún)操作。現(xiàn)在我們結(jié)束操作并退出MySQL客戶(hù) 程序: mysql> exit Bye 14.授權(quán)MySQL用戶(hù)密碼 MySQL數(shù)據(jù)庫(kù)的默認(rèn)用戶(hù)名為“root”(MS SQL Server的sa相似),密碼默認(rèn)為空。在DOS提示符(注 意,不是mysql提示符)下輸入 c:mysqlin>“mysqladmin-u root-p password newpassword 回車(chē)后會(huì)提示你輸入原來(lái)的密碼,由于原來(lái)密碼為空,直接回車(chē),root用戶(hù)的密碼便改為”newpassword“了。 Mysql數(shù)據(jù)庫(kù)學(xué)習(xí)心得(5)------------------ 四.安裝phpMyAdmin MySQL圖形界面管理器phpMyAdmin是一套以php3寫(xiě)成,針對(duì)MySQL數(shù)據(jù)庫(kù)系統(tǒng)的Web管理界面。它可以很方便地以圖形化界面,來(lái)對(duì)MySQL數(shù)據(jù)庫(kù)里的字段、數(shù)據(jù)進(jìn)行增加、刪除等的動(dòng)作,更可以做數(shù)據(jù)庫(kù)本身的增刪管理;phpMyAdmin可以管理整個(gè)MySQL服務(wù)器(需要超級(jí)用戶(hù)),也可以管理單個(gè)數(shù)據(jù)庫(kù)。另外,您也可以通過(guò)使用這個(gè)圖形化界面來(lái)學(xué)習(xí)SQL正確的語(yǔ)法,直至熟練掌握。那要等到你看過(guò)MySQL手冊(cè)中相關(guān)的部分。你可以到http://www.phpwizard.net/phpMyAdmin/去下載最新的版本。 首先,將phpMyAdmin軟件包解到一個(gè)目錄下。 1.修改文檔config.inc.php3。 將原來(lái)的設(shè)定: §cfgServers[1]['host'] = '';// MySQL hostname §cfgServers[1]['port'] = '';// MySQL port-leave blank fordefault port §cfgServers[1]['adv_auth'] = false;// Use advanced authentication? §cfgServers[1]['stduser'] = '';// MySQL standard user(only needed with advanced auth) §cfgServers[1]['stdpass'] = '';// MySQL standard password(only needed with advanced auth) §cfgServers[1]['user'] = '';// MySQL user(only needed withbasic auth) §cfgServers[1]['password'] = '';// MySQL password(only needed with basic auth) §cfgServers[1]['only_db'] = '';// If set to a db-name, only this db is accessible §cfgServers[1]['verbose'] = '';// Verbose name for this host-leave blank to show the hostname : : require(”english.inc.php3“); 修改成: §cfgServers[1]['host'] = 'MySQL Server的hostname';//填入您的MySQL Server的主機(jī)名稱(chēng) §cfgServers[1]['port'] = '';//填入連結(jié)MySQL的port,不填則以預(yù)設(shè)的port進(jìn)行連結(jié) §cfgServers[1]['adv_auth'] = true;//改成true則進(jìn)入phpMyAdmin必須先身份認(rèn)證 §cfgServers[1]['stduser'] = 'root';// MySQL使用者的帳號(hào) §cfgServers[1]['stdpass'] = '密碼';// MySQL使用者的密碼 §cfgServers[1]['user'] = 'root';// MySQL管理帳號(hào) §cfgServers[1]['password'] = '密碼';// MySQL管理密碼 §cfgServers[1]['only_db'] = '';//指定管理的資庫(kù)名稱(chēng),不填則可以管理整個(gè)Server §cfgServers[1]['verbose'] = '';//指定MySQL的名稱(chēng),不填則使用系統(tǒng)本身的hostname : : require(”chinese_gb.inc.php3");//將語(yǔ)言改成中文 說(shuō)明: (1)因本管理接口,可以以一個(gè)接口,管理多個(gè)MySQL Server,所以可以在config.inc.php3中找到 §cfgServers[1]...§cfgServers[1]...§cfgServers[1]...: §cfgServers[2]...§cfgServers[2]...§cfgServers[2]...其中[1]代表第一個(gè)MySQL Server,[2]代表第二個(gè)MySQL Server,您要管理的MySQL Server超過(guò)三臺(tái)以上,您可以依照同樣的?述,增加[4].....下去! (2)若您的MySQL Server與http Server是同一臺(tái),則§cfgServers[1]['host'] =可直接填入localhost。 最后,打開(kāi)流覽器,輸入你的網(wǎng)址/phpMyAdmin之后您會(huì)看到一個(gè)密碼驗(yàn)證的小窗口,輸入您的MySQL管理帳號(hào)及密碼,即可成功地看到phpMyAdmin的管理畫(huà)面。通過(guò)phpmyadmin,你就可以圖形化的、方便的管理你的所有數(shù)據(jù)庫(kù)了。 五.小結(jié) 通過(guò)以上的學(xué)習(xí)和操作,終于可以自由操作mysql數(shù)據(jù)庫(kù)了,也可以輕車(chē)熟路的使用php或者asp調(diào)用mysql了。最后,筆者要特別感謝開(kāi)發(fā)了mysql并且免費(fèi)貢獻(xiàn)的天才以及在網(wǎng)上提供mysql使用說(shuō)明的大蝦們。 mysql數(shù)據(jù)庫(kù)常用語(yǔ)句 SQL分類(lèi): DDL—數(shù)據(jù)定義語(yǔ)言(CREATE,ALTER,DROP,DECLARE)DML—數(shù)據(jù)操縱語(yǔ)言(SELECT,DELETE,UPDATE,INSERT)DCL—數(shù)據(jù)控制語(yǔ)言(GRANT,REVOKE,COMMIT,ROLLBACK) 首先,簡(jiǎn)要介紹基礎(chǔ)語(yǔ)句: 1、說(shuō)明:創(chuàng)建數(shù)據(jù)庫(kù) CREATE DATABASE database-name 2、說(shuō)明:刪除數(shù)據(jù)庫(kù) drop database dbname 3、說(shuō)明:備份sql server---創(chuàng)建 備份數(shù)據(jù)的 device USE master EXEC sp_addumpdevice ?disk?, ?testBack?, ?c:mssql7backupMyNwind_1.dat?---開(kāi)始 備份 BACKUP DATABASE pubs TO testBack 4、說(shuō)明:創(chuàng)建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)根據(jù)已有的表創(chuàng)建新表: A:create table tab_new like tab_old(使用舊表創(chuàng)建新表)B:create table tab_new as select col1,col2… from tab_old definition only 5、說(shuō)明: 刪除新表:drop table tabname 6、說(shuō)明: 增加一個(gè)列:Alter table tabname add column col type 注:列增加后將不能刪除。DB2中列加上后數(shù)據(jù)類(lèi)型也不能改變,唯一能改變的是增加varchar類(lèi)型的長(zhǎng)度。 7、說(shuō)明: 添加主鍵:Alter table tabname add primary key(col)說(shuō)明: 刪除主鍵:Alter table tabname drop primary key(col) 8、說(shuō)明: 創(chuàng)建索引:create [unique] index idxname on tabname(col….)刪除索引:drop index idxname 注:索引是不可更改的,想更改必須刪除重新建。 9、說(shuō)明: 創(chuàng)建視圖:create view viewname as select statement 刪除視圖:drop view viewname 10、說(shuō)明:幾個(gè)簡(jiǎn)單的基本的sql語(yǔ)句 選擇:select * from table1 where 范圍 插入:insert into table1(field1,field2)values(value1,value2)刪除:delete from table1 where 范圍 更新:update table1 set field1=value1 where 范圍 查找:select * from table1 where field1 like ?%value1%?---like的語(yǔ)法很精妙,查資料!排序:select * from table1 order by field1,field2 [desc] 總數(shù):select count * as totalcount from table1 求和:select sum(field1)as sumvalue from table1平均:select avg(field1)as avgvalue from table1 最大:select max(field1)as maxvalue from table1 最小:select min(field1)as minvalue from table1 11、說(shuō)明:幾個(gè)高級(jí)查詢(xún)運(yùn)算詞 A: UNION 運(yùn)算符 UNION 運(yùn)算符通過(guò)組合其他兩個(gè)結(jié)果表(例如 TABLE1 和 TABLE2)并消去表中任何重復(fù)行而派生出一個(gè)結(jié)果表。當(dāng) ALL 隨 UNION 一起使用時(shí)(即 UNION ALL),不消除重復(fù)行。兩種情況下,派生表的每一行不是來(lái)自 TABLE1 就是來(lái)自 TABLE2。B: EXCEPT 運(yùn)算符 EXCEPT 運(yùn)算符通過(guò)包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重復(fù)行而派生出一個(gè)結(jié)果表。當(dāng) ALL 隨 EXCEPT 一起使用時(shí)(EXCEPT ALL),不消除重復(fù)行。C: INTERSECT 運(yùn)算符 INTERSECT 運(yùn)算符通過(guò)只包括 TABLE1 和 TABLE2 中都有的行并消除所有重復(fù)行而派生出一個(gè)結(jié)果表。當(dāng) ALL 隨 INTERSECT 一起使用時(shí)(INTERSECT ALL),不消除重復(fù)行。 注:使用運(yùn)算詞的幾個(gè)查詢(xún)結(jié)果行必須是一致的。 12、說(shuō)明:使用外連接 A、left outer join: 左外連接(左連接):結(jié)果集幾包括連接表的匹配行,也包括左連接表的所有行。SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c B:right outer join: 右外連接(右連接):結(jié)果集既包括連接表的匹配連接行,也包括右連接表的所有行。C:full outer join: 全外連接:不僅包括符號(hào)連接表的匹配行,還包括兩個(gè)連接表中的所有記錄。 其次,大家來(lái)看一些不錯(cuò)的sql語(yǔ)句 1、說(shuō)明:復(fù)制表(只復(fù)制結(jié)構(gòu),源表名:a 新表名:b)(Access可用) 法一:select * into b from a where 1<>1 法二:select top 0 * into b from a 2、說(shuō)明:拷貝表(拷貝數(shù)據(jù),源表名:a 目標(biāo)表名:b)(Access可用)insert into b(a, b, c)select d,e,f from b; 3、說(shuō)明:跨數(shù)據(jù)庫(kù)之間表的拷貝(具體數(shù)據(jù)使用絕對(duì)路徑)(Access可用)insert into b(a, b, c)select d,e,f from b in ?具體數(shù)據(jù)庫(kù)? where 條件 例子:..from b in ?“&Server.MapPath(”.“)&”data.mdb“ &”? where..4、說(shuō)明:子查詢(xún)(表名1:a 表名2:b)select a,b,c from a where a IN(select d from b)或者: select a,b,c from a where a IN(1,2,3) 5、說(shuō)明:顯示文章、提交人和最后回復(fù)時(shí)間 select a.title,a.username,b.adddate from table a,(select max(adddate)adddate from table where table.title=a.title)b 6、說(shuō)明:外連接查詢(xún)(表名1:a 表名2:b)select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c 7、說(shuō)明:在線視圖查詢(xún)(表名1:a)select * from(SELECT a,b,c FROM a)T where t.a > 1; 8、說(shuō)明:between的用法,between限制查詢(xún)數(shù)據(jù)范圍時(shí)包括了邊界值,not between不包括 select * from table1 where time between time1 and time2 select a,b,c, from table1 where a not between 數(shù)值1 and 數(shù)值2 9、說(shuō)明:in 的使用方法 select * from table1 where a [not] in(?值1?,?值2?,?值4?,?值6?) 10、說(shuō)明:兩張關(guān)聯(lián)表,刪除主表中已經(jīng)在副表中沒(méi)有的信息 delete from table1 where not exists(select * from table2 where table1.field1=table2.field1) 11、說(shuō)明:四表聯(lián)查問(wèn)題: select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where.....12、說(shuō)明:日程安排提前五分鐘提醒 SQL: select * from 日程安排 where datediff(?minute?,f開(kāi)始時(shí)間,getdate())>5 13、說(shuō)明:一條sql 語(yǔ)句搞定數(shù)據(jù)庫(kù)分頁(yè) select top 10 b.* from(select top 20 主鍵字段,排序字段 from 表名 order by 排序字段 desc)a,表名 b where b.主鍵字段 = a.主鍵字段 order by a.排序字段 14、說(shuō)明:前10條記錄 select top 10 * form table1 where 范圍 15、說(shuō)明:選擇在每一組b值相同的數(shù)據(jù)中對(duì)應(yīng)的a最大的記錄的所有信息(類(lèi)似這樣的用法可以用于論壇每月排行榜,每月熱銷(xiāo)產(chǎn)品分析,按科目成績(jī)排名,等等.)select a,b,c from tablename ta where a=(select max(a)from tablename tb where tb.b=ta.b) 16、說(shuō)明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重復(fù)行而派生出一個(gè)結(jié)果表 (select a from tableA)except(select a from tableB)except(select a from tableC) 17、說(shuō)明:隨機(jī)取出10條數(shù)據(jù) select top 10 * from tablename order by newid() 18、說(shuō)明:隨機(jī)選擇記錄 select newid() 19、說(shuō)明:刪除重復(fù)記錄 Delete from tablename where id not in(select max(id)from tablename group by col1,col2,...)20、說(shuō)明:列出數(shù)據(jù)庫(kù)里所有的表名 select name from sysobjects where type=?U? 21、說(shuō)明:列出表里的所有的 select name from syscolumns where id=object_id(?TableName?) 22、說(shuō)明:列示type、vender、pcs字段,以type字段排列,case可以方便地實(shí)現(xiàn)多重選擇,類(lèi)似select 中的case。 select type,sum(case vender when ?A? then pcs else 0 end),sum(case vender when ?C? then pcs else 0 end),sum(case vender when ?B? then pcs else 0 end)FROM tablename group by type 顯示結(jié)果: type vender pcs 電腦 A 電腦 A 光盤(pán) B 光盤(pán) A 手機(jī) B 手機(jī) C 23、說(shuō)明:初始化表table1 TRUNCATE TABLE table1 24、說(shuō)明:選擇從10到15的記錄 select top 5 * from(select top 15 * from table order by id asc)table_別名 order by id desc 隨機(jī)選擇數(shù)據(jù)庫(kù)記錄的方法(使用Randomize函數(shù),通過(guò)SQL語(yǔ)句實(shí)現(xiàn)) 對(duì)存儲(chǔ)在數(shù)據(jù)庫(kù)中的數(shù)據(jù)來(lái)說(shuō),隨機(jī)數(shù)特性能給出上面的效果,但它們可能太慢了些。你不能要求ASP“找個(gè)隨機(jī)數(shù)”然后打印出來(lái)。實(shí)際上常見(jiàn)的解決方案是建立如下所示的循環(huán): Randomize RNumber = Int(Rnd*499)+1 While Not objRec.EOF If objRec(“ID”)= RNumber THEN...這里是執(zhí)行腳本...end if objRec.MoveNext Wend 這很容易理解。首先,你取出1到500范圍之內(nèi)的一個(gè)隨機(jī)數(shù)(假設(shè)500就是數(shù)據(jù)庫(kù)內(nèi)記錄的總數(shù))。然后,你遍歷每一記錄來(lái)測(cè)試ID 的值、檢查其是否匹配RNumber。滿(mǎn)足條件的話就執(zhí)行由THEN 關(guān)鍵字開(kāi)始的那一塊代碼。假如你的RNumber 等于495,那么要循環(huán)一遍數(shù)據(jù)庫(kù)花的時(shí)間可就長(zhǎng)了。雖然500這個(gè)數(shù)字看起來(lái)大了些,但相比更為穩(wěn)固的企業(yè)解決方案這還是個(gè)小型數(shù)據(jù)庫(kù)了,后者通常在一 個(gè)數(shù)據(jù)庫(kù)內(nèi)就包含了成千上萬(wàn)條記錄。這時(shí)候不就死定了? 采用SQL,你就可以很快地找出準(zhǔn)確的記錄并且打開(kāi)一個(gè)只包含該記錄的recordset,如下所示: Randomize RNumber = Int(Rnd*499)+ 1 SQL = “SELECT * FROM Customers WHERE ID = ” & RNumber set objRec = ObjConn.Execute(SQL)Response.WriteRNumber & “ = ” & objRec(“ID”)& “ ” & objRec(“c_email”) 不必寫(xiě)出RNumber 和ID,你只需要檢查匹配情況即可。只要你對(duì)以上代碼的工作滿(mǎn)意,你自可按需操作“隨機(jī)”記錄。Recordset沒(méi)有包含其他內(nèi)容,因此你很快就能找到你需要的記錄這樣就大大降低了處理時(shí)間。 再談隨機(jī)數(shù) 現(xiàn)在你下定決心要榨干Random 函數(shù)的最后一滴油,那么你可能會(huì)一次取出多條隨機(jī)記錄或者想采用一定隨機(jī)范圍內(nèi)的記錄。把上面的標(biāo)準(zhǔn)Random 示例擴(kuò)展一下就可以用SQL應(yīng)對(duì)上面兩種情況了。 為了取出幾條隨機(jī)選擇的記錄并存放在同一recordset內(nèi),你可以存儲(chǔ)三個(gè)隨機(jī)數(shù),然后查詢(xún)數(shù)據(jù)庫(kù)獲得匹配這些數(shù)字的記錄: SQL = “SELECT * FROM Customers WHERE ID = ” & RNumber & “ OR ID = ” & RNumber2 & “ OR ID = ” & RNumber3 假如你想選出10條記錄(也許是每次頁(yè)面裝載時(shí)的10條鏈接的列表),你可以用BETWEEN 或者數(shù)學(xué)等式選出第一條記錄和適當(dāng)數(shù)量的遞增記錄。這一操作可以通過(guò)好幾種方式來(lái)完成,但是 SELECT 語(yǔ)句只顯示一種可能(這里的ID 是自動(dòng)生成的號(hào)碼): SQL = “SELECT * FROM Customers WHERE ID BETWEEN ” & RNumber & “ AND ” & RNumber & “+ 9” 注意:以上代碼的執(zhí)行目的不是檢查數(shù)據(jù)庫(kù)內(nèi)是否有9條并發(fā)記錄。 隨機(jī)讀取若干條記錄,測(cè)試過(guò) Access語(yǔ)法:SELECT top 10 * From 表名 ORDER BY Rnd(id)Sql server:select top n * from 表名 order by newid()mysql select * From 表名 Order By rand()Limit n Access左連接語(yǔ)法(最近開(kāi)發(fā)要用左連接,Access幫助什么都沒(méi)有,網(wǎng)上沒(méi)有Access的SQL說(shuō)明,只有自己測(cè)試, 現(xiàn)在記下以備后查) 語(yǔ)法 select table1.fd1,table1,fd2,table2.fd2 From table1 left join table2 on table1.fd1,table2.fd1 where...使用SQL語(yǔ)句 用...代替過(guò)長(zhǎng)的字符串顯示 語(yǔ)法: SQL數(shù)據(jù)庫(kù):select case when len(field)>10 then left(field,10)+?...? else field end as news_name,news_id from tablename Access數(shù)據(jù)庫(kù):SELECT iif(len(field)>2,left(field,2)+?...?,field)FROM tablename; Conn.Execute說(shuō)明 Execute方法 該方法用于執(zhí)行SQL語(yǔ)句。根據(jù)SQL語(yǔ)句執(zhí)行后是否返回記錄集,該方法的使用格式分為以下兩種: 1.執(zhí)行SQL查詢(xún)語(yǔ)句時(shí),將返回查詢(xún)得到的記錄集。用法為: Set 對(duì)象變量名=連接對(duì)象.Execute(“SQL 查詢(xún)語(yǔ)言”) Execute方法調(diào)用后,會(huì)自動(dòng)創(chuàng)建記錄集對(duì)象,并將查詢(xún)結(jié)果存儲(chǔ)在該記錄對(duì)象中,通過(guò)Set方法,將記錄集賦給指定的對(duì)象保存,以后對(duì)象變量就代表了該記錄集對(duì)象。 2.執(zhí)行SQL的操作性語(yǔ)言時(shí),沒(méi)有記錄集的返回。此時(shí)用法為: 連接對(duì)象.Execute “SQL 操作性語(yǔ)句” [, RecordAffected][, Option] ·RecordAffected 為可選項(xiàng),此出可放置一個(gè)變量,SQL語(yǔ)句執(zhí)行后,所生效的記錄數(shù)會(huì)自動(dòng)保存到該變量中。通過(guò)訪問(wèn)該變量,就可知道SQL語(yǔ)句隊(duì)多少條記錄進(jìn)行了操作。 ·Option 可選項(xiàng),該參數(shù)的取值通常為adCMDText,它用于告訴ADO,應(yīng)該將Execute方法之后的第一個(gè)字符解釋為命令文本。通過(guò)指定該參數(shù),可使執(zhí)行更高效。 ·BeginTrans、RollbackTrans、CommitTrans方法 這三個(gè)方法是連接對(duì)象提供的用于事務(wù)處理的方法。BeginTrans用于開(kāi)始一個(gè)事物;RollbackTrans用于回滾事務(wù);CommitTrans用于提交所有的事務(wù)處理結(jié)果,即確認(rèn)事務(wù)的處理。 事務(wù)處理可以將一組操作視為一個(gè)整體,只有全部語(yǔ)句都成功執(zhí)行后,事務(wù)處理才算成功;若其中有一個(gè)語(yǔ)句執(zhí)行失敗,則整個(gè)處理就算失敗,并恢復(fù)到處里前的狀態(tài)。 BeginTrans和CommitTrans用于標(biāo)記事務(wù)的開(kāi)始和結(jié)束,在這兩個(gè)之間的語(yǔ)句,就是作為事務(wù)處理的語(yǔ)句。判斷事務(wù)處理是否成功,可通過(guò) 連接對(duì)象的Error集合來(lái)實(shí)現(xiàn),若Error集合的成員個(gè)數(shù)不為0,則說(shuō)明有錯(cuò)誤發(fā)生,事務(wù)處理失敗。Error集合中的每一個(gè)Error對(duì)象,代表一 個(gè)錯(cuò)誤信息。
”;die();} echo “數(shù)據(jù)庫(kù)服務(wù)器連接成功!
”;?> //將connect.php部署在已開(kāi)啟的WAMP平臺(tái)環(huán)境中,并在瀏覽器地址中輸入“http://localhost/connect.php”
”;echo “使用函數(shù)mysql_pconnect()永久連接數(shù)據(jù)庫(kù)。
”;?>
”;die();} echo “數(shù)據(jù)庫(kù)選擇成功!
” ?>(2)數(shù)據(jù)的添加、更新和刪除操作,mysql_query(SQL語(yǔ)句[,connection]),insert、update、delete語(yǔ)句可置于函數(shù)mysql_query()中從而實(shí)現(xiàn)數(shù)據(jù)的添加、更新和刪除操作.數(shù)據(jù)的添加
”);Mysql_query(“set names ’gbk’”);//設(shè)置中文字符集
”;else echo “客戶(hù)信息添加失敗!
”;?>
”);Mysql_query(“set names ’gbk’”);$sql=“update customers set cust_address=’廣州市’”;$sql=$sql.“where cust_name=’李中華’”;if(mysql_query($sql,$con))echo “客戶(hù)地址修改成功!
”;else echo “客戶(hù)地址修改失敗!
”;?>
”);Mysql_query(“set names ’gbk’”);$sql=“delete from customers”;$sql=$sql.“where cust_name=’李中華’”;if(mysql_query($sql,$con))echo(“客戶(hù)信息刪除成功!
”);else echo(“客戶(hù)信息刪除失敗!
”);?>
”);Mysql_query(“set names ’gbk’”);$sql=“select cust_name from customers”;$sql=$sql.“where cust_id=916”;$result=mysql_query($sql,$con);if($result){ echo “客戶(hù)查詢(xún)成功!
”;$array=mysql_fetch_array($result,MYSQL_NUM);if($array){ echo “讀取到客戶(hù)信息!
”;echo “所要查詢(xún)客戶(hù)的姓名為:”.$array[0].“
”;} else echo “未讀取到客戶(hù)信息!
”;} else echo “客戶(hù)查詢(xún)失敗!
”;?>
”);Mysql_query(“set names ’gbk’”);$sql=“select * from customers”;$sql=$sql.“where cust_sex=’F’”;$result=mysql_query($sql,$con);if($result){ echo “查詢(xún)成功!
”;$num=mysql_num_rows($result);//如果結(jié)果為空,則為0行 echo “所要查詢(xún)的女性客戶(hù)人數(shù)為:”.$num.“位
”;} else echo “查詢(xún)失敗!
”;?>第二篇:mysql數(shù)據(jù)庫(kù)要點(diǎn)總結(jié)
第三篇:MySql知識(shí)點(diǎn)總結(jié)
第四篇:Mysql數(shù)據(jù)庫(kù)學(xué)習(xí)心得
第五篇:mysql數(shù)據(jù)庫(kù)常用語(yǔ)句