鏁版嵁搴撲箣MySQL鐨勪粙缁嶄笌浣跨敤20180703

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鏁版嵁搴撲箣MySQL鐨勪粙缁嶄笌浣跨敤20180703相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/mysql%e6%9c%8d%e5%8a%a1%e5%99%a8' title='mysql鏈嶅姟鍣?>mysql鏈嶅姟鍣?/a>   涓嶇敤   cat   rman   form   with   node   oracle   瀹屽叏   

 

/*******************************************************************************************/

涓€銆乵ysql鐨勫畨瑁?/p>

0.涓嬭浇

MySQL鏁版嵁搴撶増鏈浉瀵规瘮杈冪箒鏉傘€傚父瑙佺殑鏈夛細Community绀惧尯鐗堛€丒nterprise浼佷笟鐗堛€?/p>

Community鐗堟槸寮€婧愬厤璐圭殑锛岃繖涔熸槸鎴戜滑閫氬父鐢ㄧ殑MySQL鐨勭増鏈€傚彲浠ユ弧瓒崇粷澶у鏁扮敤鎴烽渶姹傘€?/p>

Enterprise鐗堬紝瀹樻柟鎸囧嚭鎻愪緵30澶╁厤璐硅瘯鐢ㄦ湡銆傚彲杩涗竴姝ュ垝鍒嗕负MySQL鏍囧噯鐗堛€丮ySQL浼佷笟鐗堛€丮ySQL闆嗙兢鐗堛€傚畼鏂规彁渚涗粯璐规湇鍔°€?/p>

鍏朵腑Community Server 鍙互鐩存帴浠巑ysql 鐨勫畼缃戜笅杞姐€備絾Enterprice Edition鍙兘浠嶰racle edelivery涓婁笅杞斤紝鑰孍delivery鏈夋椂浼氬睆钄戒腑鍥絀P銆?/p>

mysql鍚勭増鏈尯鍒弬瑙侊細

http://www.admin10000.com/Document/62.html

涓嬭浇mysql鏃舵敞鎰忓尯鍒嗙増鏈粏鑺傚強鎵€搴旂敤鐨勭郴缁熷钩鍙帮細linux(32/64) 銆亀in(32/64)

涓句緥锛歁ySQL Community Server 5.6.20 win鐗堟湰

         GA 鏄寚杞欢鐨勯€氱敤鐗堟湰锛屼竴鑸寚姝e紡鍙戝竷鐨勭増鏈?(Generally Available (GA) Release)

         mysql-essential-5.1.60-win32.msi          鏄簿绠€鐗堬紝濡傛灉鍙渶瑕乵ysql鏈嶅姟锛屽氨閫夋嫨姝ょ増鏈€?/p>

         mysql-5.1.60-win32.msi                            鏄畬鏁寸増锛屽寘鍚畨瑁呯▼搴忓拰閰嶇疆鍚戝锛屾湁MySQL鏂囨。銆?nbsp; 

         mysql-noinstall-5.1.60-win32.zip 鏄潪瀹夎鐨剒ip鍘嬬缉鍖咃紝娌℃湁鑷姩瀹夎绋嬪簭鍜岄厤缃悜瀵硷紝鏃犲畨瑁呭悜瀵?/p>

         mysql-5.1.60.zip 鏄敤浜巜indows鐨凪ysql婧愮爜鍘嬬缉鍖?/p>

linux鐗堟湰

         鍦╤ttp://www.mysql.com/downloads/缃戠珯涓婁笅杞戒笉浜?/p>

         鍦?www.oracle.com/downloads 鎵緈ysql 娉ㄥ唽鐢ㄦ埛, 閫夋嫨鎿嶄綔绯荤粺骞冲彴鍜宮ysql鐗堟湰 杩涜涓嬭浇

瀹樻柟鏂囨。涓婃湁鍏矼ySQL瀹夎锛屼粙缁嶄簡3绉嶇被鍨嬪強鍏跺搴斿畨瑁呮柟寮忔潵瀹夎MySQL鏁版嵁搴擄細

銆€銆€Linux supports anumber of different solutions for installing MySQL. The recommended method isto use one of the distributions from Oracle. If you choose this method, thereare three options available:

(1)  Installingfrom a generic binary package in .tar.gz format. See Section 2.2,鈥淚nstalling MySQL from Generic Binaries on Unix/Linux鈥?for moreinformation.

(2)    Extractingand compiling MySQL from a source distribution. For detailed instructions,see Section 2.9, 鈥淚nstallingMySQL from Source鈥?

(3)    Installingusing a pre-compiled RPM package. For more information on using the RPMsolution, see Section 2.5.1,鈥淚nstalling MySQL from RPM Packages on Linux鈥?/p>

鎴戜滑閫夌敤杈冪畝鍗曠殑RPM 鍖呮潵瀹夎

 

1.鏌ヨ

鏌ヨ鏈嶅姟鍣ㄤ笂宸茬粡瀹夎鐨刴ysql

         鍦ㄧ粓绔彁绀虹杈撳叆锛歳pm -aq | grep MySQL 鍛戒护

2.鍗歌浇

rpm -e 杞欢鍖呭悕--nodeps --allmatches (涓嶇悊浼氫緷璧栧叧绯伙紝鍒犻櫎鎵€鏈変笂涓€姝ユ煡鍑烘潵鐨勭浉鍚岀殑mysql)

 

3.瀹夎

瑙e帇.zip瀹夎鍖?/p>

unzip V46610-01-MySQL Database 5.6.20 RPM for Oracle Linux  RHEL 6 x86 (64bit).zip

瀹夎濡備笅杞欢鍖咃細

         MySQL-client-advanced-5.6.20-1.el6.x86_64.rpm

         MySQL-devel-advanced-5.6.20-1.el6.x86_64.rpm

         MySQL-server-advanced-5.6.20-1.el6.x86_64.rpm

 

--#琛ㄧず闇€瑕佸湪root涓嬫墽琛?/p>

#rpm -ivh MySQL-server-advanced-5.6.****-1.el6.x86_64.rpm

 

mysql瀹夎鐨勬椂鍊欒嚜鍔ㄥ垵濮嬪寲涓€涓猺oot鐢ㄦ埛(mysql鏁版嵁搴撶敤鎴?涓嶆槸鎿嶄綔绯荤粺鐢ㄦ埛)

鐢ㄦ埛鍚嶅瘑鐮佸湪鍦╮oot鐩綍涓嬶紝閫氳繃cat鐩綍涓嬬殑 .mysql_secret鏂囦欢鍙煡鐪嬪綋鍓嶇殑瀵嗙爜

[[email protected] oracle]# cat /root/.mysql_secret

 

#rpm -ivh MySQL-client-advanced-5.6.****-1.el6.x86_64.rpm

                   璇存槑:涓嶅畨瑁卪ysql-client鏄笉鑳戒娇鐢╩ysql宸ュ叿鐧婚檰鍒癿ysql鏁版嵁搴?/p>

鍏朵粬杞欢鍖呴€夋嫨鎬у畨瑁咃紝杩欎釜鏄紑鍙戦渶瑕佸畨瑁呯殑锛屽鏋滄病鏈夊畨瑁呭垯娌℃湁搴撴枃浠朵互鍙婂ご鏂囦欢锛屾棤娉曡繘琛岀紪绋嬶細

#rpm -ivh MySQL-devel-advanced-5.6.20-1.el6.x86_64.rpm

 

4.淇敼瀵嗙爜

涓€鑸嚜宸卞畨瑁呬箣鍚庤淇敼瀵嗙爜.

 

--鐧婚檰

mysql -uroot -p123456

 

鏀瑰瘑鐮侊紝鍦ㄧ櫥褰曞悗鐨勬儏鍐典笅

set password=password(鈥?23鈥?;

 

 

 

鍏蜂綋瀹夎鍙互鍙傝€冦€奟edhat涓嬪畨瑁卪ysql 5.6.20.docx銆?/p>

 

 

5.ubuntu涓嬪畨瑁卪ysql.txt

1). sudo apt-get install mysql-server

2). sudo apt-get isntall mysql-client

3). sudo apt-get install libmysqlclient-dev

 

鎻愮ず:瀹夎杩囩▼涓細鎻愮ず璁剧疆瀵嗙爜浠€涔堢殑锛屾敞鎰忚缃簡涓嶈蹇樹簡锛屽畨瑁呭畬鎴愪箣鍚庡彲浠ヤ娇鐢ㄥ涓嬪懡浠ゆ潵妫€鏌ユ槸鍚﹀畨瑁呮垚鍔燂細

 

 

6.杩滅▼杩炴帴妯″紡璇存槑

 

vmvare 缃戝崱

 

妗ユ帴妯″紡 : 鐗╃悊鏈?鍜岃櫄鎷熸満 鍚屼竴缃戞

NAT妯″紡 : VMNET8 涓?铏氭嫙鏈?鍚屼竴缃戞

涓绘満妯″紡: VMNET1 涓?铏氭嫙鏈?鍚屼竴缃戞銆備粎闄愪簬鑷繁鑳借繛锛屽埆浜鸿繛涓嶄簡

 

/*******************************************************************************************/

浜屻€乵ysql鐨勭畝浠?/p>

 

mysql 鏄憺鍏?mysqlAB鍏徃 ,鍚庢潵琚玸un鏀惰喘,涔嬪悗琚玱racle 鏀惰喘.

 

绀惧尯鐗堝拰浼佷笟鐗?/p>

 

寮€婧愮殑 ,鍏嶈垂鐨?/p>

 

mysql浣撶郴缁撴瀯锛?/p>

mysql棣栧厛鏄敤鎴凤紝鐒跺悗鎺ヤ笅鏉ユ槸鏁版嵁搴擄紝鏈€鍚庢墠鏄〃銆?/p>

鎵€浠ュ涔犱粠搴撳紑濮嬶紝

鍏堝簱鐨勫鍒犳敼鏌ワ紝鐒跺悗琛ㄧ殑澧炲垹鏀规煡锛屾渶鍚庢槸鏁版嵁鐨勫鍒犳敼鏌ワ紝

鐢变簬mysql姣旇緝灏忥紝涓嶅儚oracle閭d箞澶?鏈変釜dba鏉ユ搷浣滄暟鎹簱锛屾墍浠ysql闇€瑕侀兘寰椾細

 

鍏蜂綋瑙佸浘銆妋ysql浣撶郴缁撴瀯涓巓racle浣撶郴缁撴瀯鐨勪笉鍚?png銆嬶細

 鎶€鏈垎浜浘鐗? src=

 

/*******************************************************************************************/

涓夈€乵ysql搴撶殑鎿嶄綔

1.mysql鍚姩涓嶄簡鐨勬椂鍊欎娇鐢?

1).鍏堟潃鎺夎繘绋?mysqld锛屾病鏈夎繖涓繘绋嬶紝鍒欐棤娉曠櫥褰?/p>

 

2).閲嶅惎mysql鏈嶅姟

         [[email protected] oracle]# service mysql restart

 

2.搴撶殑澧炲垹鏀规煡

 

1).鏌ョ湅褰撳墠鏈夌殑鏁版嵁搴?/p>

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

+--------------------+

杩欎笁涓槸mysql鑷甫鐨勶紝杩欎笁涓笉鑳藉垹锛屽垹浜嗕細鏈夐棶棰?/p>

 

2).鍒涘缓涓€涓獃ekai鐨勬暟鎹簱

create database yekai;

 

3).鏌ョ湅yekai鏁版嵁搴撴槸鎬庝箞鍒涘缓鐨勶紝鏌ョ湅鏁版嵁搴?鍒涘缓鐨勭浉鍏?淇℃伅

mysql> show create database yekai;

+----------+------------------------------------------------------------------+

| Database | Create Database |

+----------+------------------------------------------------------------------+

| yekai | CREATE DATABASE `yekai` /*!40100 DEFAULT CHARACTERSET latin1 */ |

+----------+------------------------------------------------------------------+

1 row in set (0.00 sec)

 

 

4).鍒涘缓浜唘tf8瀛楃闆嗙殑鏁版嵁搴擄紝杩欐牱鍙互鏀寔涓枃

create database mydb1 character set utf8;

 

5).灏嗘暟鎹簱鐨勫瓧绗﹂泦淇敼涓簎tf8

alter database yekai character set utf8;

 

6).鍒犻櫎yekai杩欎釜鏁版嵁搴?/p>

drop database yekai;

 

7).鍒涘缓涓€涓猽tf8鐨勬暟鎹簱骞跺杈撳叆鏁版嵁杩涜鏍″ 锛屽垱寤烘暟鎹簱 瀵规暟鎹簱瀛樺叆鐨勬暟鎹寜鐓tf8鏍煎紡杩涜涓€涓鏌?/p>

create database mydb3 character set utf8 collate utf8_general_ci;

鍚庣画鎿嶄綔杩欎釜鏁版嵁搴撶殑鏃跺€欏氨浼氭鏌ユ槸鍚︽槸utf8鏍煎紡鐨勩€?/p>

 

 

 

/*******************************************************************************************/

鍥涖€乵ysql琛ㄧ殑绠$悊

 

琛ㄧ殑绠$悊:澧炲垹鏀规煡

1.琛ㄧ殑澧炲姞

鎴戜滑瑕佸垱寤鸿〃鍏堣閫夋嫨鏁版嵁搴?/p>

--鍏堥€夊彇搴?/p>

use mydb1 //杩欎釜鎸囦护涓嶉渶瑕佹暡鍒嗗彿锛屽叾浠栫殑閮借鏁?/p>

--鍐嶅垱寤鸿〃锛岀敱浜巑ysql鐨勬暟鎹被鍨嬩笌oracle鐨勪笉涓€鏍凤紝鎵€浠ュ垪鍚嶅悗闈㈢殑鏁版嵁绫诲瀷瑕佺敤mysql鑷繁鐨勶紝鐩稿浜巓racle瑕佸仛淇敼

create table t1(id int,name varchar(30));

//鍏蜂綋鏁版嵁绫诲瀷鐨勮鏄庯紝浠ュ強鏁版嵁绫诲瀷琛ㄧず鐨勮鏄庯紝

timeStamp: 鏃堕棿鎴筹紝鑷姩璧嬪€间负褰撳墠鏃ユ湡鏃堕棿

 

娉ㄦ剰锛宮ysql涓殑鏁版嵁绫诲瀷锛?/p>

鍏蜂綋瑙佸浘1锛?/p>

 鎶€鏈垎浜浘鐗? src=

 

2.琛ㄧ殑淇敼

--淇敼琛ㄧ殑灞炴€э紝鍙互鍔燾olumn鍏抽敭瀛楋紝oracle涓嶈兘鍔?/p>

 

--澧炲姞email鍒?/p>

alter table t1 add email varchar(30);

--澧炲姞sal鍒?/p>

alter table t1 add column sal float;

 

--淇敼鍒楀睘鎬э紝鍙互鍔燾olumn鍏抽敭瀛楋紝oracle涓嶈兘鍔?/p>

alter table t1 modify email varchar(40);

--change鍙互淇敼鍒楀悕鍜屽垪鐨勫睘鎬э紝濡備笅鏄妸鍒楀悕email鏀逛负 address锛屽苟淇敼鏁版嵁绫诲瀷涓簐archar(20)

alter table t1 change column email address varchar(20);

desc t1;//鏌ョ湅琛ㄧ粨鏋勩€?/p>

 

--鍒犻櫎鍒?/p>

alter table t1 drop column address;

 

--閲嶅懡鍚嶈〃闇€瑕佹湁table鍏抽敭瀛楋紝oracle鍒欎笉闇€瑕?/p>

rename table t1 to t2;

 

create table t1 as select * from t2;

 

3.琛ㄧ殑鏌ョ湅

--鏌ョ湅褰撳墠搴撲笅鏈夊摢浜涜〃锛屼唬鏇夸簡oracle涓殑select * from tab;

show tables;

 

--鏌ョ湅琛ㄧ殑鍒涘缓鏂瑰紡锛?/p>

show create table t1;

鍏蜂綋瑙佸浘2:

 鎶€鏈垎浜浘鐗? src=

鍏朵腑锛孍NGINE(寮曟搸)=InnoDB锛?/p>

杩欎釜寮曟搸鏄?InnoDB 

InnoDB 锛氬紩鎿庣殑涓€绉嶆柟寮忥紝杩欑鏂瑰紡鏀寔浜嬪姟,榛樿鎯呭喌涓嬩簨鍔′笉寮€鍚?鑷姩鎻愪氦锛岃繖鐐瑰拰oracle涓嶄竴鏍?.

榛樿鐨勮繖绉嶅紩鎿庨€熷害姣旇緝蹇€?/p>

鍙﹀鍏朵腑鍒楀悕杩樻湁琛ㄥ悕鍔犱簡鍗曞紩鍙凤紝琛ㄧず杩欎釜鍚嶇О鏄尯鍒嗗ぇ灏忓啓鐨勶紝

mysql> use Mydb1

ERROR 1049 (42000): Unknown database 鈥楳ydb1鈥?/p>

搴撳悕鍖哄垎澶у皬鍐?/p>

瀛楁涓嶅尯鍒嗗ぇ灏忓啓

 

4.琛ㄧ殑鍒犻櫎

--鍒犻櫎琛?涓嶆敮鎸乸urge鍏抽敭瀛?锛屼篃灏辨槸mysql涓嶆敮鎸佸洖鏀剁珯锛堝洜涓簃ysql鏄皬鏁版嵁搴擄級

drop table t1;

 

娓呯┖琛細truncate鍏堝垹闄よ〃锛屽啀鍒涘缓涓┖琛?/p>

 

/*******************************************************************************************/

浜斻€乵ysql鏁版嵁澶勭悊

mysql瀵规暟鎹殑澧炲垹鏀规煡鍜宱racle澶ц嚧鏄竴鏍风殑銆?/p>

琛ㄦ暟鎹殑澶勭悊锛屼篃灏辨槸澧炲垹鏀规煡

create table employee(id int,

                name varchar(20),

                sex int,

                birthday date,

                salary double,

                entry_date date,

                resume text

                );//杩欓噷闈㈡槸绌烘牸锛屼笉瑕佹湁tab鍋ワ紝tab閿細瀵艰嚧鏁版嵁閿欒锛岀己灏戣

insert into employee values(1,鈥樺彾寮€鈥?1,鈥?983-04-27鈥?15000,鈥?012-06-24鈥?鈥樹竴涓ぇ鐗涒€?;

insert into employee(id,name,sex,birthday,salary,entry_date,resume) values(2,鈥樺倕绾㈤洩鈥?1,鈥?984-02-22鈥?10000,鈥?012-07-24鈥?鈥樹竴涓腑鐗涒€?;

insert into employee(id,name,sex,birthday,salary,entry_date,resume) values(3,鈥橀檰灏忎匠鈥?0,鈥?985-08-28鈥?7000,鈥?012-08-24鈥?鈥樹竴涓皬铏锯€?;

        

update emploee set resume=鈥?23鈥?where id =3;

delete from emploee where id =3;

 

浜嬪姟鐨勫鐞嗕笂涓嶄竴鏍?mysql鏄嚜鍔ㄦ彁浜ょ殑銆?/p>

 

 

 

/*******************************************************************************************/

鍏€乵ysql鐨勫嚱鏁?/p>

 

1.鏃ユ湡鏃堕棿鍑芥暟

MySQL閲岄潰鏃堕棿鍒嗕负涓夌被锛氭椂闂淬€佹棩鏈熴€佹椂闂存埑(鍚湁鏃跺垎绉掔殑sysdate),鍏蜂綋瑙佸浘3锛?/p>

 鎶€鏈垎浜浘鐗? src=

 

 

sysdate浼垪 mysql涓嶅瓨鍦?浣跨敤now()

鍗筹細select now() from dual;

 

--鏄ㄥぉ,浠婂ぉ,鏄庡ぉ

select now()-1,now(),now()+1 from dual;//杩欐牱绫讳技oracle鐨勫仛娉曚笉瀵癸紝瀹冩妸鏃ユ湡鍙樹负鏁板瓧鐒跺悗鍔犲噺1浜?/p>

姝g‘鐨勫仛娉曪紝浣跨敤date_add鎴栬€卍ate_sub锛?/p>

mysql> select date_add(now(),interval -1 day),now(),date_add(now(),interval 1 day) from dual;

//interval鏄叧閿瓧锛?1 鏁版嵁鏄灏戯紝day鏄暟鎹被鍨?/p>

 

--鏄庡勾鐨勪粖澶?/p>

mysql> select date_add(now(),interval 1 year) from dual;

 

--addtime鍑芥暟澧炲姞绉掓暟锛屾敞鎰忚繖涓鏁拌灏忎簬60

mysql> select addtime(now(),10),now() from dual;

--娉ㄦ剰杩欎釜绉掓暟瑕佸皬浜?0锛屽鏋滆鍔犱竴鍒嗛挓锛屽垯锛?/p>

mysql> select addtime(now(),鈥?:1:0鈥?,now() from dual;

 

 

2.瀛楃涓茬浉鍏冲嚱鏁?/p>

瀛楃涓茬浉鍏冲嚱鏁帮紝鍏蜂綋鍙鍥?锛?/p>

 鎶€鏈垎浜浘鐗? src=

--concat鍑芥暟鏀寔澶氫釜鍙傛暟,oracle涓嶆敮鎸佽繖涔堝涓?|| 璇硶mysql淇濈暀鍗充笉鎶ラ敊,浣嗘槸mysql涓姛鑳戒笉瀛樺湪

mysql> select concat(鈥榟ello鈥?鈥榳orld鈥?鈥?11鈥?,鈥榟ello鈥榺|鈥榳orld鈥?from dual;

 

--鏌ョ湅瀛楃闆?缂栫爜)

mysql> select charset(鈥榟llo鈥?,charset(鈥樺彾寮€鈥? from dual;

+-----------------+-------------------+

| charset(鈥榟llo鈥? | charset(鈥樺彾寮€鈥?   |

+-----------------+-------------------+

| utf8            | utf8              |

+-----------------+-------------------+

 

-- 鏌ョ湅瀛楃涓查暱搴︼紝涓€涓眽瀛椾笁涓瓧鑺?杩欐椂鍥犱负鏍煎紡鏄痷tf8锛夛紝oracle鏄痝bk鏍煎紡锛屾墍浠racle鏄袱涓瓧鑺?/p>

mysql> select length(鈥榟ello涓浗鈥? from dual;

+-----------------------+

| length(鈥榟ello涓浗鈥?   |

+-----------------------+

|                    11 |

+-----------------------+

 

 

3.鏃ユ湡杞崲鍑芥暟

鍦∕ySQL涓病鏈塼o_date鍑芥暟锛岃繘琛屾棩鏈熻浆鎹㈤渶浣跨敤date_format()鏉ヤ唬鏇?/p>

 

1).鏃ユ湡杞瓧绗︿覆锛?/p>

杞崲鍑芥暟  yyyy-mm-dd 璇硶鏀寔,鍔熻兘娌℃湁

mysql> select date_format(鈥?013-5-11鈥? 鈥榶yyy-mm-dd鈥? from dual;

 

mysql> select date_format(now(), 鈥?Y-%m-%d鈥? from dual;         

y(娌℃湁20鍙湁17)鍜孻(2017)涓嶄竴鏍枫€?/p>

 

mysql> select date_format(now(), 鈥?Y-%c-%d %h:%i:%s鈥? from dual;  

c(02鍙樻垚2锛屼細鍘绘帀0)鍜宮(02)銆丮(鐢ㄨ嫳璇功鍐欑殑鏈堜唤)涓嶄竴鏍?/p>

 

2).瀛楃涓茶浆鏃ユ湡锛?/p>

mysql> select str_to_date(鈥?013-6-04 05:14:15鈥?, 鈥?Y-%c-%d %h:%i:%s鈥? from dual;

         //鏈€濂藉瓧绗︿覆瑕佸拰鏍煎紡涓蹭竴鑷达紝鍗冲搴旇捣鏉ワ紝褰撶劧涓嶄竴鑷翠篃鑳借浆鎹?/p>

mysql> select str_to_date(鈥?013-06-04 05:14:15鈥?, 鈥?Y-%m-%d %h:%i:%s鈥? from dual;

 

3).鏍煎紡寰堝儚Linux涓嬬殑date鍛戒护锛宒ate鍛戒护鍚庡彲浠ュ姞鏍煎紡涓叉潵寰楀埌鎯宠鐨勬棩鏈熸牸寮忥細

[[email protected] ~]$ date +鈥?y-%m-%d鈥?/p>

17-02-16

[[email protected] ~]$ date +鈥?Y-%m-%d鈥?/p>

2017-02-16

 

 

4.鏁板鐩稿叧鍑芥暟

鍏蜂綋鍙鍥?锛?/p>

 鎶€鏈垎浜浘鐗? src=

 

5.group by璇硶

-- 姹傚悇涓彮鑻辫鐨勫钩鍧囧垎

select avg(english),class_id,id from student group by class_id;

//杩欐牱鍦╫racle涓槸涓嶅厑璁哥殑(select鍚庨潰鍑虹幇鐨勯潪涓诲嚱鏁扮殑鍒楀湪group by鍚庨潰蹇呴』鍑虹幇)锛屼絾鏄湪mysql涓槸鍙互锛?/p>

杩欎釜id鏄瘡涓垎缁勭涓€鏉¤褰曞搴旂殑id锛屼絾鏄繖鏍锋病鏈夋剰涔?

 

mysql group by璇硶妫€鏌ヤ笉涓ユ牸,鎴戜滑浠嶇劧鐢╫racle鐨勮姹?

 

/*******************************************************************************************/

涓冦€佸琛ㄦ暟鎹?/p>

create database if not exists mydb1 character set utf8;

//if not exists 濡傛灉涓嶅瓨鍦ㄦ墠鍘绘墽琛岋紝杩欐牱鍙嶅鎵ц杩欎釜鍛戒护灏变笉浼氭姤閿?/p>

 

sql鏂囦欢閲岄潰鐨勮繖浜泂ql璇彞闄や簡澶嶅埗绮樿创鏉ョ洿鎺ュ锛屽彲浠ョ洿鎺ヤ娇鐢?/p>

source scott_data.sql

杩欐牱鏉ユ墽琛屻€?/p>

 

Oracle涓繛鎺ユ柟娉曪細

                   绛夊€艰繛鎺?/p>

                   涓嶇瓑鍊艰繛鎺?nbsp;   

                   澶栬繛鎺?/p>

                   鑷繛鎺?/p>

MySQL 浣跨敤SQL99鏍囧噯鐨勮繛鎺ユ煡璇紙JOIN..ON..锛?/p>

 

1.鍐呰繛鎺?/p>

瀵瑰簲浜巓racle鐨勭瓑鍊艰繛鎺?/p>

(鍦╩ysql涓娇鐢╡dit鏉ヤ功鍐欙紝鍏跺疄鏄敤vi鎵撳紑浜嗭紝鑰屽湪oracle涓璭d灏卞彲浠ヤ簡銆?/p>

edit涓功鍐欏畬姣曞悗锛岀敤wq淇濆瓨锛岀劧鍚庡啀杈撳叆;鍙锋墽琛屻€?

 

1).姹傚憳宸ュ彿,濮撳悕,鏈堣柂,閮ㄩ棬鍚嶇О

--oracle鍐欐硶

select e.empno,e.ename,e.sal,d.dname

  from emp e,dept d

 where e.deptno=d.deptno;

 

--sql99(mysql)鍐欐硶

select e.empno,e.ename,e.sal,d.dname

  from emp e inner join dept d

 on e.deptno=d.deptno;

鍙h瘈: 1. , --> inner join 2. where ---> on

 

涓ょ鍐欐硶鍦╩ysql涓兘鑳戒娇鐢?/p>

 

 

2.澶栬繛鎺?/p>

1).姹傚憳宸ユ€讳汉鏁?鏄剧ず閮ㄩ棬缂栧彿,鍚嶇О,浜烘暟

--oracle鍐欐硶

select d.deptno,d.dname,count(e.empno)

  from emp e , dept d

 where e.deptno=d.deptno;

鏌ヨ缁撴灉鏈夐棶棰橈紝杩欐槸鐢变簬鏉′欢涓嶆槸瀹屽叏鐩哥瓑锛岄渶瑕佺敤鍒板乏澶?鍙冲鎵嶈锛?涔熸湁鍙兘鏄病鏈塯roup by鐨勫師鍥?

 

>>>>>>oracle鐨勫杩炴帴鍦╩ysql涓笉鑳戒娇鐢紝濡備笅鏄痮racle鐨勫杩炴帴鍦╩ysql涓笉鑳戒娇鐢?/p>

select d.deptno,d.dname,count(e.empno)

  from emp e , dept d

 where e.deptno(+)=d.deptno;

 

--sql99鐨勫啓娉?/p>

select d.deptno,d.dname,count(e.empno)

  from emp e right outer join dept d

 on e.deptno=d.deptno

group by d.deptno,d.dname;

鍙冲杩炴帴鍙h瘈: 1  , --- > right outer join(鍗充繚鐣檍oin鍙宠竟鐨勯儴鍒嗭紝涔熷氨鏄痙ept d) 2 where --> on

 

select d.deptno,d.dname,count(e.empno)

  from dept d left outer join emp e

 on e.deptno=d.deptno

group by d.deptno,d.dname;

宸﹀杩炴帴鍙h瘈: 1  , --- > left outer join 2 where --> on

 

宸﹀鎴栬€呭彸澶栫殑鍙栧喅鏉′欢鏄?琛ㄥ湪(閫楀彿(outer join))鐨勪綅缃?涓庣瓑鍙锋棤鍏?/p>

 

 

3.鑷繛鎺?鐗规畩鐨勫杩炴帴)

鏁版嵁閮藉湪鍚屼竴寮犺〃锛屼笉鍦ㄥ悓涓€琛?/p>

//鎶奺mp琛ㄥ垎鍒綋鎴愯€佹澘琛ㄥ拰鍛樺伐琛?/p>

鍛樺伐琛ㄧ殑鑰佹澘鏄€佹澘琛ㄧ殑鍛樺伐(杩炴帴(绛涢€夎褰曠殑)鏉′欢锛?绛涢€?鎵惧嚭)涓ゅ紶琛ㄧ粍鍚堣捣鏉ョ殑闆嗗悎涓弧瓒?杩炴帴)鏉′欢鐨勮褰?).

 

--oracle鍐欐硶(涓嶅甫澶栬繛鎺?

select concat(e.ename,鈥樷€樷€榮 boss is 鈥?b.ename)

  from emp e,emp b

 where e.mgr = b.empno;

 

--sql99鐨勫啓娉?/p>

select concat(e.ename,鈥樷€樷€榮 boss is 鈥?b.ename)

  from emp e left outer join  emp b

 on e.mgr = b.empno;

杩斿洖鐨勭粨鏋滀腑鏈変竴涓褰曟槸null锛岃繖鏃跺洜涓烘湁涓€涓?b.ename鏄痭ull锛岃繖鏍峰瓧绗︿覆杩炴帴鍚庝篃鏄痭ull

 

婊ょ┖鍑芥暟锛?nbsp;          

--nvl(婊ょ┖)鍑芥暟mysql涓嶆敮鎸?/p>

select concat(e.ename,鈥樷€樷€榮 boss is 鈥?nvl(b.ename,鈥榟imself鈥?)

  from emp e left outer join  emp b

 on e.mgr = b.empno;

 

--nvl(婊ょ┖)鍑芥暟鐢╥fnull鏇挎崲

select concat(e.ename,鈥樷€樷€榮 boss is 鈥?ifnull(b.ename,鈥榟imself鈥?)

  from emp e left outer join  emp b

 on e.mgr = b.empno;

 

 

4.婊″鑱旀帴

         浠讳竴杈规湁鍊煎氨浼氭樉绀恒€?/p>

                   select e.*, d.*

                   from emp e full outer join dept d             

                   on e.deptno=d.deptno

         涔熷彲浠ョ渷鐣uter鍏抽敭瀛?/p>

 

        

5.浜ゅ弶杩炴帴锛?/p>

         鍙夐泦锛屽嵆绗涘崱灏旈泦

                   select e.*, d.*

                   from emp e cross join dept d

         鏃犺繛鎺ユ潯浠?/p>

 

 

 

6.inner 鍜?outer 鍙互鐪?

 

 

7.

鍚庣画瀹為檯椤圭洰涓敤鐨勬槸redhat绯荤粺涓婅鏁版嵁搴?/p>

 

select 鈥榗reate synonym鈥榺|TNAME||鈥榝or scott.鈥榺|TNAME from tab where tabtype=鈥楾ABLE鈥?//杩欐牱鍙互鎷垮埌寤烘墍鏈夊悓涔夎瘝鐨勮鍙?/p>

 

搴忓垪鐨勪綔鐢ㄧ粰琛ㄧ殑涓婚敭浣跨敤,涓轰簡閬垮厤涓婚敭鍐茬獊.

nextval  榛樿鍔犺浇鍒板唴瀛樹腑 20涓?濡傛灉娌$敤瀹屽氨鎺夌數浜嗭紝鐢变簬鍦ㄥ唴瀛樹腑璁颁笉涓嬫潵锛屾墍浠ラ噸鏂颁笂鐢靛悗鏄粠涓嬩竴娈靛嵆40寮€濮?/p>

 

 

/*******************************************************************************************/

鍏€乵ysql鐨勭害鏉?/p>

1.绾︽潫鐨勫畾涔?/p>

         *瀹氫箟涓婚敭绾︽潫銆€primary key:       涓嶅厑璁镐负绌猴紝涓嶅厑璁搁噸澶?/p>

         *瀹氫箟涓婚敭鑷姩澧為暱銆€auto_increment

         *瀹氫箟鍞竴绾︽潫銆€unique

         *瀹氫箟闈炵┖绾︽潫銆€not null

         *瀹氫箟澶栭敭绾︽潫銆€constraint ordersid_FK foreign key(ordersid) references orders(id)

         *鍒犻櫎涓婚敭锛歛lter table tablename drop primary key ;

鍙互閫氳繃SELECT * FROM information_schema.`TABLE_CONSTRAINTS`;鏌ョ湅琛ㄧ殑绾︽潫

 

2.涓嶰racle鐨勫尯鍒?/p>

mysql鐨勭害鏉熶笌oracle鐨勫尯鍒湪浜庢病鏈夋鏌ヤ簡锛屽嵆璇硶鏀寔浣嗘槸鍔熻兘娌℃湁浜?/p>

 

mysql涓婚敭涓嶉渶瑕佸簭鍒楋紝鍙渶瑕佽嚜鍔ㄥ闀垮叧閿瓧鍗冲彲

 

mysql瀹氫箟澶栭敭璇硶涓巓racle鐣ユ湁涓嶅悓銆?/p>

瀹氫箟澶栭敭绾︽潫銆€constraint ordersid_FK foreign key(ordersid) references orders(id)

//ordersid_FK 鍒悕 锛宱rdersid  鏈〃閲岀敤澶栭敭鐨勫瓧娈碉紝orders(id) 寮曠敤rders琛ㄧ殑id瀛楁

 

3.绀轰緥锛?/p>

mysql> drop table myclass;

mysql> create table myclass(

                    class_id int primary key auto_increment,

                    class_name varchar(20) not null,

                    create_date timestamp);

 

mysql> insert into myclass(class_id,class_name) values(5,鈥?鐝€?; //鍗充娇鏄痑uto_increment锛屽彧瑕佹弧瓒充富閿殑绾︽潫涔熻兘杩欐牱鎵嬪姩澧炲姞

 

mysql> insert into myclass(class_name) values(鈥樹粈涔堢彮鈥?; //鑷姩澧為暱鐨勬満鍒舵槸鍙栧墠闈㈢殑鏈€澶у€硷紝鐒跺悗鍔?锛屼竴寮€濮嬪暐閮芥病鏈夊垯绗竴涓槸1

 

mysql> create table mystudent(

        student_id int primary key auto_increment,

        student_name varchar(20) not null,

        hiredate timestamp,

        class_id int,

        constraint stu_classid_FK foreign key(class_id) references myclass(class_id));

//stu_classid_FK 澶栭敭绾︽潫鍚嶏紝class_id 鏄閿紝渚濊禆浜巑yclass琛ㄧ殑class_id(涓婁竴涓〃鐨勪富閿槸杩欎釜琛ㄧ殑澶栭敭(鐨勪緷璧?)

 

--闈炵┖绾︽潫

mysql> insert into mystudent(student_name,class_id) values(null,1);

ERROR 1048 (23000): Column 鈥榮tudent_name鈥?cannot be null

 

--杩濆弽澶栭敭绾︽潫//褰撳墠class_id渚濊禆浜庡叾浠栬〃鐨勪富閿紝鍗冲綋鍓嶇殑class_id鐨勫€?瑕佹槸鍏朵粬琛ㄧ殑涓婚敭涓凡缁忓瓨鍦ㄧ殑

mysql> insert into mystudent(student_name,class_id) values(鈥榝uhongxue2鈥?8);//涓婁竴涓〃鐨刢lass_id涓病鏈夊€间负8鐨?/p>

 

--鏈夊閿椂鍒犻櫎琛?/p>

-杩濆弽澶栭敭绾︽潫,闇€瑕佸厛鍒犻櫎瀛︾敓,鍐嶅垹闄ょ彮绾?/p>

mysql> delete from myclass where class_id=5;

mysql> delete from mystudent where class_id=5;

Query OK, 1 row affected (0.00 sec)

 

mysql> delete from myclass where class_id=5;      

Query OK, 1 row affected (0.01 sec)

 

 

/*******************************************************************************************/

涔濄€乵ysql涓枃涔辩爜闂

涓枃涔辩爜闂:瑕侀噸瑙?/p>

 

涓夊眰鍥犵礌锛?/p>

鍥犵礌1锛?MySQL鑷韩鐨勮璁?/p>

銆愬疄楠屾楠?銆戯細   

mysql> show variables like 鈥榗haracter%鈥?        鏌ョ湅鎵€鏈夊簲鐢ㄧ殑瀛楃闆?/p>

缁撴灉鏈夊ソ鍑犵瀛楃闆嗭紝鍏蜂綋鍝瀛楃闆嗘槸灞炰簬鍝竴绔殑瑙佸浘銆婂瓧绗﹂泦褰掔被.png銆嬶細

 鎶€鏈垎浜浘鐗? src=

 

銆愬疄楠屾楠?銆戯細    鎸囧畾瀛楃闆嗙櫥褰曟暟鎹簱

$ mysql -uroot -p123456 --default_character_set=gbk 鎸囧畾瀛楃闆嗙櫥褰曟暟鎹簱

         mysql> show variables like 鈥榗haracter%鈥?

         褰卞搷浜嗕笌瀹㈡埛绔浉鍏宠仈鐨?3澶?(鏈€澶栧眰)

鍦ㄨ繖绉嶇姸鎬佷笅鎵цuse mydb2;

         mysql> select * from employee;                       

         鏌ョ湅杈撳嚭锛屼細鍑虹幇涔辩爜銆?/p>

         鍘熸潵鐨勪笁鏉℃暟鎹紝鏄互utf8鐨勫舰寮忓瓨鍌ㄥ埌鏁版嵁搴撲腑锛屽綋浣跨敤gbk杩炴帴浠ュ悗锛屾暟鎹簱浠嶆寜鐓tf8鐨勫舰寮忓皢鏁版嵁杩斿洖锛屽嚭閿欍€?/p>

銆愬疄楠屾楠?銆戯細

鍦ㄨ鐜涓嬫彃鍏ュ甫鏈変腑鏂囩殑涓€琛屾暟鎹€?/p>

         mysql> insert into employee(id,name,sex,birthday,salary,entry_date,resume) values(10,鈥樺紶涓夌柉鈥?1,鈥?983-09-21鈥?15000,鈥?012-06-24鈥?鈥樹竴涓€佺墰鈥?;

         ERROR 1366 (HY000): Incorrect string value: 鈥榎x80xE4xB8xAAxE8x80...鈥?for column 鈥榬esume鈥?at row 1

 

鍥犵礌2锛氭搷浣滅郴缁熺殑璇█闆?/p>

linux鎿嶄綔绯荤粺 鏄竴涓?澶氱敤鎴风殑鎿嶄綔

[[email protected] ~]# cat /etc/sysconfig/i18n //鏌ョ湅褰撳墠璇█璁剧疆

         LANG="zh_CN.UTF-8"     //鏁版嵁瀛樺偍鎸夌収UTF8鏍煎紡,鏁版嵁灞曠ず鎸夌収绠€浣撲腑鏂?

         鎿嶄綔绯荤粺鐨勮彍鍗曟寜鐓h_CN鏄剧ず,  鏂囦欢瀛樺偍鎸夌収utf8

linux鎿嶄綔绯荤粺璇█鐜 鍜?鐢ㄦ埛鐨勯厤缃殑璇█鐜LANG 鐩镐簰褰卞搷(涓よ€?瑕?涓€鏍?

         [[email protected] ~]$ echo $LANG

         zh_CN.UTF-8

鏀逛簡浠绘剰涓€涓紝灏变細涔辩爜锛?/p>

銆愬疄楠屾楠?銆戯細

淇敼鐢ㄦ埛涓嬬殑.bash_profile 涓殑LANG锛屽睆钄芥搷浣滅郴缁熺殑LANG璁剧疆銆傚啀鏌ユ暟鎹簱

                   mysql> select * from employee;

缁撹锛?鐢ㄦ埛鐨凩ANG璁剧疆锛屽奖鍝嶄簡搴旂敤绋嬪簭鐨勮瑷€鐜,瀵艰嚧myql鐨勮瑷€鐜鍙戠敓浜嗘敼鍙橈細

         mysql> show variables like 鈥榗haracter%鈥?

         鍦ㄦ鐜涓嬶紝妫€绱腑鏂囦細鍑虹幇涔辩爜銆?/p>

銆愬疄楠屾楠?銆戯細鍦ㄤ笂杩扮幆澧冧箣涓嬶紝鍚戞暟鎹簱涓彃鍏ヤ腑鏂囥€?/p>

insert into employee(id,name,sex,birthday,salary,entry_date,resume) values(5,鈥樺紶涓夌柉鈥?1,鈥?987-05-21鈥?15000,鈥?014-06-24鈥?鈥樹竴涓€佺墰鈥?;

鏁版嵁鑳芥彃鍏ュ埌鏁版嵁搴撲腑锛屾病 鏈?鎶?浠?浣?閿?璇紒浣嗘樉绀轰笉姝g‘銆?/p>

 

鍥犵礌3锛氭枃浠跺瓨鍌ㄦ牸寮?/p>

鍛戒护杈撳叆瀛楃鐨勬牸寮忥紝

姣斿

杩滅▼杩炴帴宸ュ叿閲岄厤缃緭鍏ュ懡浠ょ殑瀛楃缂栫爜 涓嶄竴鑷存椂涔熶細鏈夐棶棰?/p>

 

鎵€浠ユ搷浣滃墠瑕佹煡璇竴涓嬫暟鎹紝骞朵笖鎿嶄綔鍚庡啀鏌ヤ竴涓嬶紝纭畾涓€涓槸鍚︽湁涔辩爜銆?/p>

 

 

/*******************************************************************************************/

鍗併€乵ysql鍒嗛〉闂澶勭悊

鍓嶉潰璇寸殑閭d袱涓猰ysql鏈潵灏辨湁锛屼笖涓嶈兘鍒犻櫎鐨勮〃锛岄噷闈㈤兘鏄竴浜沵ysql搴撶浉鍏崇殑淇℃伅锛屾瘮濡傝〃鐨勭害鏉熷暐鐨?/p>

鍙互鍒囨崲鍒伴偅涓や釜鏁版嵁搴擄紝鐒跺悗鏌ヨ鐪嬬湅閲岄潰鐨勫唴瀹广€?/p>

 

--鏄剧ず鏁版嵁

select * from emp G; 杩欑鏂瑰紡鏄惧嚭鍑烘潵鐨勭粨鏋滄槸涓€琛屼竴琛岀殑灞曠ず鏁版嵁锛屽氨鍜屾病鏈塡G鐨勬晥鏋滃畬鍏ㄤ笉鍚屼簡

 

--top-N闂瑙e喅(mysql瑙e喅top-N闂鐨勬柟娉曚笌oracle涓嶅悓锛屼富瑕佸氨鏄娇鐢ㄤ簡limit)

         -- limit 鍙栧墠3鍚嶏紝

                   select * from emp order by sal desc limit 3;

         -- limit鍙?-8鍚嶏紝limit 鍚庣殑涓や釜鏁板瓧,浠h〃璺宠繃鐨勮褰曟暟,鍜岀劧鍚庡彇鐨勮褰曟暟.

                   select * from emp order by sal desc limit 4,4;

 

                  

/*******************************************************************************************/

鍗佷竴銆乵ysql api缂栫▼

mysql API璇︾粏瑙併€奙ySQL 涓枃瀹屽叏鍙傝€冩墜鍐?.1.chm銆嬶紝涓昏鐮旂┒鐨勬槸c搴擄紝涔熷氨鏄痬ysql鎻愰珮缁檆鐨刟pi

(涓嬮潰鎽樺嚭鐨勬槸鏂囨。涓渶瑕佺壒鍒敞鎰忕殑鍦版柟锛屽苟浣滀簡鐩稿簲鐨勬敞閲?

 

1.渚濊禆

C API浠g爜鏄笌MySQL涓€璧锋彁渚涚殑銆傚畠鍖呭惈鍦╩ysqlclient搴撲腑锛屽苟鍏佽C绋嬪簭璁块棶鏁版嵁搴撱€?/p>

//涔熷氨鏄痗鎿嶄綔mysql鏃堕渶瑕佽繖涓簱鏂囦欢浠ュ強鐩稿叧鐨勫ご鏂囦欢

//渚濊禆鐨勫ご鏂囦欢鍜屽簱鏂囦欢

/usr/include/mysql/mysql.h

/usr/lib64/mysql/libmysqlclient.a

[[email protected] ~]$ locate mysql.h  //鎴栬€呬娇鐢╨ocate鍛戒护鎵惧埌澶存枃浠跺拰搴撴枃浠?杩欐椂鐢变簬linux骞冲彴瀵规枃浠朵俊鎭仛浜嗚褰曪紝鏁版嵁涔熶笉鏄疄鏃舵洿鏂扮殑鎵€浠ラ€熷害蹇紝浣跨敤locate鍙煡璇㈠埌

//姣斿鏂板浜嗕竴涓枃浠讹紝鐢╨ocate涓嶄竴瀹氭壘鐨勫嚭鏉ワ紝浣嗘槸鐢╢ind鍙互鎵惧嚭鏉?//鍚屾牱瀵逛簬libmysqlclient.a鏂囦欢锛屼篃鍙互鐢ㄨ繖涓懡浠ゆ壘鍒拌矾寰?/p>

 

2.浣跨敤娴佺▼

涓嶮ySQL浜や簰鏃讹紝搴旂敤绋嬪簭搴斾娇鐢ㄨ涓€鑸€у師鍒?娴佺▼)锛?/p>

 

1).閫氳繃璋冪敤mysql_library_init()锛屽垵濮嬪寲MySQL搴撱€傚簱鍙互鏄痬ysqlclient C瀹㈡埛绔簱锛屾垨mysqld宓屽叆寮忔湇鍔″櫒搴擄紝鍏蜂綋鎯呭喌鍙栧喅浜庡簲鐢ㄧ▼搴忔槸鍚︿笌鈥?libmysqlclient鈥濇垨鈥?libmysqld鈥濇爣蹇楅摼鎺ャ€?/p>

 

2).閫氳繃璋冪敤mysql_init()鍒濆鍖栬繛鎺ュ鐞嗙▼搴忥紝骞堕€氳繃璋冪敤mysql_real_connect()杩炴帴鍒版湇鍔″櫒銆?/p>

 

3).鍙戝嚭SQL璇彞骞跺鐞嗗叾缁撴灉銆傦紙鍦ㄤ笅闈㈢殑璁ㄨ涓紝璇︾粏浠嬬粛浜嗕娇鐢ㄥ畠鐨勬柟娉曪級銆?/p>

 

4).閫氳繃璋冪敤mysql_close()锛屽叧闂笌MySQL鏈嶅姟鍣ㄧ殑杩炴帴銆?/p>

 

5).閫氳繃璋冪敤mysql_library_end()锛岀粨鏉烳ySQL搴撶殑浣跨敤銆?/p>

 

//鍏朵腑绗竴姝ュ拰绗簲姝ュ彲浠ョ渷鐣?/p>

//mysql缂栫▼鐨勬祦绋?

mysql_init 鍒濆鍖?/p>

mysql_real_connect 杩炴帴

//鍋氭搷浣?/p>

mysql_close  鍏抽棴

 

3.绀轰緥

鐔熸倝mysql api缂栫▼鐨勬柟寮忥細缂栧啓涓€涓猰ysql瀹㈡埛绔?

         1).鎴愬姛杩炴帴鍒癿ysql

         2).鍋氫竴涓嬫煡璇㈠拰鏂板鏁版嵁鐨勬搷浣?/p>

         3).缂栧啓瀹㈡埛绔?/p>

        

1).鎴愬姛杩炴帴鍒癿ysql(閰嶇疆鐜)

(1).鍒濆鍖栧嚱鏁?/p>

         MYSQL *mysql_init(MYSQL *mysql)  鍒濆鍖栧嚱鏁?/p>

         MYSQL 浠h〃mysql杩炴帴鍙ユ焺 ,浼犲叆鍙傛暟鍙互涓篘ULL

         杩斿洖鎴愬姛 鏄?杩炴帴鍙ユ焺,澶辫触 NULL

        

(2).杩炴帴mysql鐨勫嚱鏁?/p>

         MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag)

        

         mysql 杩炴帴鍙ユ焺 ,瑕佺敤鍒濆鍚庣殑杩斿洖鍊?/p>

         host    涓绘満 鏈湴ip鍦板潃

         user  鐢ㄦ埛 mysql鐨勭敤鎴?/p>

         passwd 瀵嗙爜 mysql鐨勭敤鎴风殑瀵嗙爜

         db     搴撳悕

         port  绔彛 涓€鑸笉鐢?濉?

         unix_socket  涓€鑸笉鐢?濉玁ULL

         client_flag  鏍囧織浣?閫氬父涓?,鍏蜂綋瑙佹墜鍐岋紝閲岄潰鏈夎鏄?/p>

 

         杩斿洖鍊? 杩斿洖 杩炴帴鍙ユ焺,澶辫触杩斿洖 NULL

 

(3).鍏抽棴杩炴帴

         void mysql_close(MYSQL *mysql)

         mysql 杩炴帴鍙ユ焺 mysql_real_connect

 

浠g爜瑙併€奾ello.c銆嬶細

鎶€鏈垎浜浘鐗? id=
 1 //gcc 01_hello.c -o 01_hello -I/usr/include/mysql/ -L/usr/lib64/mysql/ -lmysqlclient -lstdc++ -ldl -lpthread -lrt
 2 #include "mysql.h"
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 
 6 #define _HOST_  "127.0.0.1"   //涓绘満
 7 #define _USER_  "root"   //鐢ㄦ埛 mysql鐨勭敤鎴?/span>
 8 #define _PASSWD_ "123"   //瀵嗙爜 mysql
 9 #define _DB_     "scott"  //搴?/span>
10 
11 
12 
13 int main()
14 {
15     MYSQL *mysql = NULL;
16     //1. mysql_init
17     mysql = mysql_init(NULL);
18     if(mysql == NULL){
19         printf("mysql init err
");
20         exit(1);
21     }
22     //2. mysql_real_connect
23     //MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag) 
24     mysql = mysql_real_connect(mysql,_HOST_,_USER_,_PASSWD_,_DB_,0,NULL,0);
25     if(mysql == NULL ){
26         printf("mysql connect err!
");
27         exit(1);
28     }
29     printf("welcome to mysql!
");
30     //3. mysql_close
31     mysql_close(mysql);
32     return 0;
33 }
hello.c

 

娉ㄦ剰缂栬瘧锛?/p>

//gcc 01_hello.c -o 01_hello -I/usr/include/mysql/ -L/usr/lib64/mysql/ -lmysqlclient

杩欐牱棣栧厛浼氭姤gxx鐨勯敊璇紝杩欐槸缂哄皯g++鐨勫簱锛岃В鍐虫柟娉?

//gcc 01_hello.c -o 01_hello -I/usr/include/mysql/ -L/usr/lib64/mysql/ -lmysqlclient -lstdc++

杩欐牱棣栧厛浼氭姤dlxx鐨勯敊璇紝man 鐩稿叧缂哄皯瀹氫箟鐨勫嚱鏁帮紝鐒跺悗鍔犱笂闇€瑕佺殑锛岃В鍐虫柟娉?

gcc 01_hello.c -o 01_hello -I/usr/include/mysql/ -L/usr/lib64/mysql/ -lmysqlclient -lstdc++ -ldl -lpthread -lrt

 

(4).makefile缂栧啓

 

make -n //鍋囪璧颁竴閬嶏紝骞朵笉鏄湡鐨勬墽琛岋紝鐩稿綋浜庢祴璇曟寚浠ゃ€?/p>

 

鍏蜂綋鍐呭瑙併€妋akefile銆嬶細

鎶€鏈垎浜浘鐗? id=
 1 #gcc 01_hello.c -o 01_hello -I/usr/include/mysql/ -L/usr/lib64/mysql/ -lmysqlclient -lstdc++ -ldl -lpthread -lrt
 2 
 3 GCC=gcc
 4 IncPath=/usr/include/mysql/
 5 LibPath=/usr/lib64/mysql/
 6 PubLib=-lmysqlclient -lstdc++ -ldl -lpthread -lrt
 7 
 8 SrcFiles=$(wildcard *.c)
 9 TargetFiles=$(patsubst %.c,%,$(SrcFiles))
10 
11 all:$(TargetFiles)
12 
13 
14 %:%.c
15     $(GCC) $? -o [email protected] -I$(IncPath) -L$(LibPath) $(PubLib)
16 
17 
18 clean:
19     rm -f $(TargetFiles)
makefile

 

2).mysql api鏂板鏁版嵁

瀹炵幇insert鍔熻兘锛?/p>

         -- mysql sql鎵ц鍑芥暟 鎵ц瀹屼簡灏卞畬浜嗭紝涓嶄細杩斿洖缁撴灉

         int mysql_query(MYSQL *mysql, const char *query)

         mysql 杩炴帴鍙ユ焺

         query   鎵ц鐨剆ql

         鎴愬姛杩斿洖0 ,澶辫触杩斿洖闈?

 

浠g爜瑙併€奿nsert.c銆嬶細

鎶€鏈垎浜浘鐗? id=
 1 //insert 
 2 #include "mysql.h"
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 
 6 #define _HOST_  "127.0.0.1"   //涓绘満
 7 #define _USER_  "root"   //鐢ㄦ埛 mysql鐨勭敤鎴?/span>
 8 #define _PASSWD_ "123"   //瀵嗙爜 mysql
 9 #define _DB_     "scott"  //搴?/span>
10 
11 
12 
13 int main()
14 {
15     MYSQL *mysql = NULL;
16     //1. mysql_init
17     mysql = mysql_init(NULL);
18     if(mysql == NULL){
19         printf("mysql init err
");
20         exit(1);
21     }
22     //2. mysql_real_connect
23     //MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag) 
24     mysql = mysql_real_connect(mysql,_HOST_,_USER_,_PASSWD_,_DB_,0,NULL,0);
25     if(mysql == NULL ){
26         printf("mysql connect err!
");
27         exit(1);
28     }
29     printf("welcome to mysql!
");
30     
31     // 鍦ㄦ澶勬坊鍔犱笟鍔′唬鐮?
32     char *rSql = "insert into dept(deptno,dname,loc) values(50,鈥榗aiwu鈥?鈥榖eijing鈥?";
33     //鎵ц
34     if(mysql_query(mysql,rSql) ){
35         printf("mysql query err:%s
",rSql);
36         mysql_close(mysql);
37         exit(1);
38     }
39     printf("insert ok!
");
40     //3. mysql_close
41     mysql_close(mysql);
42     return 0;
43 }
insert.c

 

3).瀹炵幇mysql鏌ヨ

闄や簡鐢ㄦ墽琛屽嚱鏁板锛岃繕闇€瑕佺煡閬撹繑鍥炵殑缁撴灉锛屼篃灏辨槸鏌ヨ缁撴灉闆嗭紝

杩欐椂闇€瑕佷娇鐢ㄧ殑鍑芥暟锛?/p>

MYSQL_RES *mysql_store_result(MYSQL *mysql)

 

鎻忚堪

瀵逛簬鎴愬姛妫€绱簡鏁版嵁鐨勬瘡涓煡璇紙SELECT銆丼HOW銆丏ESCRIBE銆丒XPLAIN銆丆HECK TABLE绛夛級锛屽繀椤昏皟鐢╩ysql_store_result()鎴杕ysql_use_result() 銆?/p>

瀵逛簬鍏朵粬鏌ヨ锛屼笉闇€瑕佽皟鐢╩ysql_store_result()鎴杕ysql_use_result()锛屼絾鏄鏋滃湪浠讳綍鎯呭喌涓嬪潎璋冪敤浜唌ysql_store_result()锛屽畠涔熶笉浼氬鑷翠换浣曚激瀹虫垨鎬ц兘闄嶄綆銆傞€氳繃妫€鏌ysql_store_result()鏄惁杩斿洖0锛屽彲妫€娴嬫煡璇㈡槸鍚︽病鏈夌粨鏋滈泦锛堜互鍚庝細鏇村锛夈€?/p>

 

MYSQL_RES鍏充簬璇ョ粨鏋勪綋鐨勬弿杩板彲浠ュ幓mysql.h閲岄潰鏌ユ壘

 

         mysql 杩炴帴鍙ユ焺

         杩斿洖 缁撴灉闆?MYSQL_RES ,澶辫触 杩斿洖 NULL

        

         --鍙栫粨鏋滈泦鐨勪笅涓€琛?寰幆璋冪敤杩欎釜鍑芥暟鍒欏彲浠ュ緱鍒扮粨鏋滈泦鐨勬墍鏈夎

         MYSQL_ROW mysql_fetch_row(MYSQL_RES *result) //鍙互璋冪敤mysql_fetch_row()鏉ヨ幏鍙栫粨鏋滈泦涓殑琛?/p>

         result 鍙栧洖鐨勭粨鏋滈泦

        

         MYSQL_ROW 杩斿洖涓嬩竴琛岀殑缁撴瀯 //MYSQL_ROW 鏄痗har**锛屾槸涓€涓簩缁存暟缁勶紝琛ㄧず涓€琛岀殑鍐呭

         char ** 鍙互灞曠ず.

         鍗充簩缁存暟缁勪腑鐨勬瘡涓€涓厓绱犳槸姣忎竴鍒楃殑鍐呭锛屾瘮濡俽ow[0]鏄琛岀涓€鍒楃殑鍐呭锛宺ow[1]鏄琛岀浜屽垪鐨勫唴瀹?/p>

        

         --閲婃斁缁撴灉闆?/涓€鏃﹀畬鎴愪簡瀵圭粨鏋滈泦鐨勬搷浣滐紝蹇呴』璋冪敤mysql_free_result()銆?/p>

         void mysql_free_result(MYSQL_RES *result)

 

         鑾峰彇鍒楃殑涓暟鐨勫嚱鏁帮細

         unsigned int mysql_num_fields(MYSQL_RES *result) //杩斿洖缁撴灉闆嗕腑鐨勮鏁般€?/p>

        

         鑾峰彇鍒楀悕浣跨敤鐨勫嚱鏁帮細

         MYSQL_FIELD *mysql_fetch_fields(MYSQL_RES *result) //杩斿洖鎵€鏈塎YSQL_FIELD缁撴瀯鐨勬暟缁勶紝鏁扮粍鐨勫厓绱犲氨鏄瘡涓€鍒楃殑鍒楀悕

 

浠g爜瑙併€妔elect.c銆嬶細

鎶€鏈垎浜浘鐗? id=
 1 //select 鏌ヨ鍔熻兘
 2 #include "mysql.h"
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 
 6 #define _HOST_  "127.0.0.1"   //涓绘満
 7 #define _USER_  "root"   //鐢ㄦ埛 mysql鐨勭敤鎴?/span>
 8 #define _PASSWD_ "123"   //瀵嗙爜 mysql
 9 #define _DB_     "scott"  //搴?/span>
10 
11 
12 
13 int main()
14 {
15     MYSQL *mysql = NULL;
16     //1. mysql_init
17     mysql = mysql_init(NULL);
18     if(mysql == NULL){
19         printf("mysql init err
");
20         exit(1);
21     }
22     //2. mysql_real_connect
23     //MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag) 
24     mysql = mysql_real_connect(mysql,_HOST_,_USER_,_PASSWD_,_DB_,0,NULL,0);
25     if(mysql == NULL ){
26         printf("mysql connect err!
");
27         exit(1);
28     }
29     printf("welcome to mysql!
");
30     
31     //鍦ㄦ澶勬坊鍔犱笟鍔′唬鐮?32     
33     //鎵ц鏌ヨ
34     if(mysql_query(mysql,"select * from emp") ){
35         printf("mysql query err
");
36         mysql_close(mysql);
37         exit(1);
38     }
39     //澶勭悊鏌ヨ缁撴灉闆?/span>
40     MYSQL_RES *result = mysql_store_result(mysql);
41     MYSQL_ROW row;
42     int i =0;
43     if(result != NULL)//浠h〃鏈夌粨鏋滈泦
44     {
45         //    MYSQL_ROW mysql_fetch_row(MYSQL_RES *result) 
46         while( (row=mysql_fetch_row(result) )) {
47             for(i = 0; i < 8 ; i ++)//閽堝 鍒楃殑寰幆,涓€鍏辨槸8鍒?
48             {
49                 printf("%s	",row[i]);
50             }
51             printf("
");//涓婇潰澶勭悊鏄竴琛?姝ゅ鎹㈣
52         }
53         mysql_free_result(result);
54     }
55     
56     
57     
58     //3. mysql_close
59     mysql_close(mysql);
60     return 0;
61 }
select.c

 

4).瀹㈡埛绔伐鍏风紪鍐?/p>

绫讳技杈撳叆mysql -uroot -p123杩涜鐧诲綍锛屽苟杈撳叆sql璇彞鍚庝細杩斿洖缁撴灉鐨勭▼搴忓伐鍏?/p>

 

缂栧啓瀹㈡埛绔?

         杩炴帴鍒癿ysql鏁版嵁搴?mysql_init mysql_real_connect

         鎵撳嵃绠$悊鍙颁俊鎭?MYSQL>

         绛夊緟杈撳叆sql

         鎵ц杈撳叆鐨剆ql,涓昏鏄袱绫?鏈夌粨鏋滈泦(鏌ヨ),娌℃湁缁撴灉闆嗙殑

         鏈夌粨鏋滈泦鐨勬墦鍗扮粨鏋滈泦

 

         鍏抽棴 mysql_close

 

娉ㄦ剰锛岃幏鍙栨帶鍒跺彴鐨勮緭鍏ユ暟鎹竴鑸篃鍙互浣跨敤fgets锛岃鍑芥暟鏄樆濉炵殑銆?/p>

 

my_ulonglong mysql_affected_rows(MYSQL *mysql) //杩斿洖鐨勬槸锛屽灏戜釜鏁板湪闆嗗悎涓?/p>

鎻忚堪

杩斿洖涓婃UPDATE鏇存敼鐨勮鏁帮紝涓婃DELETE鍒犻櫎鐨勮鏁帮紝鎴栦笂娆NSERT璇彞鎻掑叆鐨勮鏁般€?/p>

 

//杩炴帴涔嬪悗璁剧疆涓€涓嬪瓧绗﹂泦锛屾潵淇濊瘉涓嶅悓鍦版柟鐧诲綍鍒版湇鍔″櫒鍐欏叆鐨勬暟鎹殑缂栫爜鏍煎紡閮芥槸涓€鏍风殑銆?/p>

int mysql_set_character_set(MYSQL *mysql, char *csname)

鎻忚堪

璇ュ嚱鏁扮敤浜庝负褰撳墠杩炴帴璁剧疆榛樿鐨勫瓧绗﹂泦銆?/p>

mysql_set_character_set(mysql,"utf8"); //璁剧疆瀛楃闆?/p>

        

浠g爜瑙併€奵lient.c銆嬶細

鎶€鏈垎浜浘鐗? id=
 1 //select2 鏌ヨ鍔熻兘 瀹屽杽
 2 #include "mysql.h"
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 #include <string.h>
 6 
 7 #define _HOST_  "127.0.0.1"   //涓绘満
 8 #define _USER_  "root"   //鐢ㄦ埛 mysql鐨勭敤鎴?/span>
 9 #define _PASSWD_ "123"   //瀵嗙爜 mysql
10 #define _DB_     "scott"  //搴?/span>
11 
12 //鏄剧ず缁撴灉闆嗙殑鍑芥暟
13 void showresult(MYSQL_RES *result,MYSQL *mysql)
14 {
15     unsigned int num_fields;
16     unsigned int i;
17     MYSQL_FIELD *fields;
18      
19     num_fields = mysql_num_fields(result);//鑾峰緱瀛楁鏁?/span>
20     fields = mysql_fetch_fields(result);//鑾峰彇瀛楁鏁扮粍
21     for(i = 0; i < num_fields; i++)
22     {
23        printf("%s	", fields[i].name);//鎵撳嵃瀛楁,涔熷氨鏄〃澶?/span>
24     }
25     printf("
");
26     
27     printf("----------------------------------------------------------------
");
28     
29     MYSQL_ROW row;
30     if(result != NULL)//浠h〃鏈夌粨鏋滈泦
31     {
32         //    MYSQL_ROW mysql_fetch_row(MYSQL_RES *result) 
33         while( (row=mysql_fetch_row(result) )) {
34             for(i = 0; i < num_fields ; i ++)//num_fields 鍒楁暟 
35             {
36                 printf("%s	",row[i]);
37             }
38             printf("
");//涓婇潰澶勭悊鏄竴琛?姝ゅ鎹㈣
39         }
40         
41     }
42     printf("----------------------------------------------------------------
");
43     printf("%ld rows in set
",(long) mysql_affected_rows(mysql));
44 
45 }
46 
47 int main()
48 {
49     MYSQL *mysql = NULL;
50     //1. mysql_init
51     mysql = mysql_init(NULL);
52     if(mysql == NULL){
53         printf("mysql init err
");
54         exit(1);
55     }
56     //2. mysql_real_connect
57     //MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag) 
58     mysql = mysql_real_connect(mysql,_HOST_,_USER_,_PASSWD_,_DB_,0,NULL,0);
59     if(mysql == NULL ){
60         printf("mysql connect err!
");
61         exit(1);
62     }
63     printf("welcome to mysql!
");
64     
65     mysql_set_character_set(mysql,"utf8"); //璁剧疆瀛楃闆?66 
67 
68     //鍦ㄦ澶勬坊鍔犱笟鍔′唬鐮?/span>
69     char rSql[512];
70     
71     
72     while(1){
73         printf("yekaiSQL>");
74         memset(rSql,0x00,sizeof(rSql));
75         fgets(rSql, sizeof(rSql), stdin);
76         printf("rSql:%s
",rSql);
77         if(strncmp(rSql,"quit",4) == 0 || strncmp(rSql,"QUIT",4) == 0)
78         {
79             printf("bye bye
");
80             break;
81         }
82         //鎵цsql
83         if(mysql_query(mysql,rSql) ){
84             printf("mysql query err
");
85             //mysql_close(mysql);
86             //exit(1);
87         }
88         //澶勭悊鏌ヨ缁撴灉闆?/span>
89         MYSQL_RES *result = mysql_store_result(mysql);
90         if(result != NULL){
91             showresult(result,mysql);//鎵撳嵃缁撴灉闆?/span>
92             mysql_free_result(result);
93         }
94     }
95     
96     //3. mysql_close
97     mysql_close(mysql);
98     return 0;
99 }
client.c

 

 

/*******************************************************************************************/

鍗佷簩銆乵ysql棰勫鐞?/p>

1.棰勫鐞嗗嚱鏁颁富瑕佷綔鐢ㄥ氨鏄负浜嗘彁楂橀€熷害锛岄€傚悎sql璇彞澶氭鍙嶅鎵ц

 

2.MySQL瀹㈡埛绔紡鏈嶅姟鍣ㄥ崗璁彁渚涗簡棰勫鐞嗚鍙ャ€傝鍔熻兘閲囩敤浜嗙敱mysql_stmt_init()鍒濆鍖栧嚱鏁拌繑鍥炵殑MYSQL_STMT璇彞澶勭悊绋嬪簭鏁版嵁缁撴瀯銆?/p>

 

3.瀵逛簬澶氭鎵ц鐨勮鍙ワ紝棰勫鐞嗘墽琛屾槸涓€绉嶆湁鏁堢殑鏂瑰紡銆?/p>

棣栧厛瀵硅鍙ヨ繘琛岃В鏋愶紝$$$涓烘墽琛屼綔濂藉噯澶?$$銆?/p>

鎺ヤ笅鏉ワ紝鍦ㄤ互鍚庝娇鐢ㄥ垵濮嬪寲鍑芥暟杩斿洖鐨勮鍙ュ彞鏌勬墽琛屼竴娆℃垨澶氭銆?/p>

瀵逛簬澶氭鎵ц鐨勮鍙ワ紝棰勫鐞嗘墽琛屾瘮鐩存帴鎵ц蹇紝涓昏鍘熷洜鍦ㄤ簬锛?/p>

浠呭鏌ヨ鎵ц$$$涓€娆¤В鏋愭搷浣?$$銆傚湪鐩存帴鎵ц鐨勬儏鍐典笅锛屾瘡娆℃墽琛岃鍙ユ椂锛屽潎灏嗚繘琛屾煡璇€?/p>

姝ゅ锛岀敱浜庢瘡娆℃墽琛岄澶勭悊璇彞鏃朵粎闇€$$$鍙戦€佸弬鏁?$$鐨勬暟鎹紝浠庤€屽噺灏戜簡缃戠粶閫氫俊閲忋€?/p>

 

4.棰勫鐞嗚鍙ョ殑鍙︿竴涓紭鐐规槸锛屽畠閲囩敤浜嗕簩杩涘埗鍗忚锛屼粠鑰屼娇寰楀鎴风鍜屾湇鍔″櫒涔嬮棿鐨勬暟鎹紶杈撴洿鏈夋晥鐜囥€?/p>

 

5.涓嬭堪璇彞鍙敤浣滈澶勭悊璇彞锛?/p>

CREATE TABLE銆丏ELETE銆丏O銆両NSERT銆丷EPLACE銆丼ELECT銆丼ET銆乁PDATE銆?/p>

浠ュ強澶у鏁癝HOW璇彞銆傚湪MySQL 5.1涓紝涓嶆敮鎸佸叾浠栬鍙ャ€?/p>

 

6.缂虹偣锛氱敤鐨勬瘮杈冮毦涓€鐐癸紝涔熷氨鏄紪绋嬬殑鏃跺€欓毦涓€鐐广€?/p>

 

7.棰勫鐞嗘祦绋嬭鍥俱€婇澶勭悊鐨勬祦绋?png銆嬶細

 鎶€鏈垎浜浘鐗? src=

8.mysql棰勫鐞嗚瑙?浠g爜璇存槑)

#define INSERT_SAMPLE "INSERT INTO test_table(col1,col2,col3) VALUES(?,?,?)"

//鍏朵腑閭d笁涓棶鍙蜂唬琛ㄥ崰浣嶏紝鍏跺疄灏辨槸浠h〃瑕佷紶鍏ョ殑鍙傛暟

 

MYSQL_BIND    bind[3];//缁戝畾鍙橀噺 ,鍓嶉潰鏈変笁涓棶鍙凤紝鍗虫湁涓変釜鍙傛暟锛屾墍浠ヨ繖閲屾槸涓変釜

 

bind[0].buffer= (char *)&int_data;//鍐呭瓨鍦板潃鐨勬槧灏?/p>

//娉ㄦ剰锛岃繖涓槸鎸囬拡鐨勮祴鍊煎鑷寸殑缁撴灉鏄弻鍚戠殑锛屽嵆bind[0].buffer鎸囧悜鐨勫唴瀹规敼鍙樹簡锛屽垯int_data

閲岄潰鐨勫唴瀹逛篃璺熺潃鍙樺寲浜嗭紝鍚屾椂锛宨nt_data閲岄潰鐨勫唴瀹规敼鍙樹簡锛屽嵆bind[0].buffer鎵€鎸囧悜鐨勫唴瀹逛篃鍙樺寲浜?/p>

bind[0].is_null= 0;//鐢变簬鏄彇鍦板潃璧嬪€肩殑锛屾墍浠ユ敼涓虹瓑浜嶯ULL鏇村悎閫傦紝

bind[0].length= 0;//鐢变簬鏄彇鍦板潃璧嬪€肩殑锛屾墍浠ユ敼涓虹瓑浜嶯ULL鏇村悎閫傦紝

 

//濡傛灉缁戝畾鍙橀噺鍐呯殑鎴愬憳涓嶇瓑浜嶯ULL锛岄偅涔堢敱浜庡唴瀛樻槧灏勭殑鍏崇郴锛屼慨鏀逛簡璧嬪€肩殑鍙橀噺锛屽垯灏辨槸淇敼浜嗙粦瀹氬彉閲忔寚鍚戠殑

鍐呭瓨锛屽悓鏃朵篃浼氫紶閫掑埌sql璇彞(绫讳技浼犻€掍簡瀹炲弬)锛屽洜涓虹粦瀹氬彉閲忓凡缁忓拰sql璇彞寤虹珛浜嗚仈绯?/p>

绀轰緥锛?/p>

         int_data= 10;             /* integer */

         strncpy(str_data, "MySQL", STRING_SIZE); /* string  */

         str_length= strlen(str_data); 

         /* INSERT SMALLINT data as NULL */

         is_null= 1;//鎸囩ず鎻掑叆鐨勭涓変釜瀛楁鏄惁涓簄ull锛屽鏋滅瓑浜?琛ㄧず瑕佹彃鍏ョ殑鏄┖锛屽垯鏃犺鎸囧悜鐨勫唴瀛樻槸浠€涔堟儏鍐碉紝鎻掑叆鐨勬暟鎹兘鏄痭ull

          

         /* Execute the INSERT statement - 1*/

         if (mysql_stmt_execute(stmt)) //棰勫鐞嗙殑鎵ц,绗竴娆℃墽琛?/p>

        

琛ュ厖锛?nbsp;   

//int mysql_stmt_fetch(MYSQL_STMT *stmt)   娑夊強鍒版煡璇紝鍏蜂綋鐪嬫墜鍐?/p>

 

 

param_count= mysql_stmt_param_count(stmt);//鑾峰緱鍙傛暟涓暟 锛岃繖涓嚱鏁颁笉鏄澶勭悊蹇呴』鐨勬祦绋嬶紝

 

 

 

浠g爜瑙併€妏repare_insert.c銆嬶細

鎶€鏈垎浜浘鐗? id=
  1 #include <stdio.h>
  2 #include "mysql.h"
  3 #include <stdlib.h>
  4 #include <string.h>
  5 
  6 #define _HOST_ "localhost"  //涓绘満
  7 #define _USER_ "root"       //mysql鐢ㄦ埛,闈炰富鏈?/span>
  8 #define _PASSWD_ "123"   //瀵嗙爜
  9 #define _DBNAME_ "scott"    //搴撳悕
 10 
 11 #define STRING_SIZE 50
 12  
 13 #define DROP_SAMPLE_TABLE "DROP TABLE IF EXISTS test_table"
 14 #define CREATE_SAMPLE_TABLE "CREATE TABLE test_table(col1 INT, 15                                                  col2 VARCHAR(40), 16                                                  col3 SMALLINT, 17                                                  col4 TIMESTAMP)"
 18 #define INSERT_SAMPLE "INSERT INTO test_table(col1,col2,col3) VALUES(?,?,?)"
 19 void prepare_insert(MYSQL *mysql);
 20 
 21 int main()
 22 {
 23     //1.鍒濆鍖?/span>
 24     MYSQL * mysql = NULL;
 25     mysql = mysql_init(NULL) ;
 26     if(mysql == NULL )
 27     {
 28         printf("mysql init err
");
 29         exit(1);
 30     }
 31     //2.杩炴帴
 32     mysql = mysql_real_connect(mysql, _HOST_,_USER_, _PASSWD_,_DBNAME_, 0, NULL,0);
 33     if(mysql == NULL)
 34     {
 35         printf("mysql_real_connect connect err
");
 36         exit(1);
 37     }
 38     printf("welcome to mysql 
");
 39     prepare_insert(mysql);
 40     //3.鍏抽棴
 41     mysql_close(mysql);
 42     return 0;
 43 }
 44 
 45 
 46 
 47 
 48 void prepare_insert(MYSQL *mysql)
 49 {
 50     MYSQL_STMT    *stmt;//棰勫鐞嗙殑鍙ユ焺
 51     MYSQL_BIND    bind[3];//缁戝畾鍙橀噺 
 52     my_ulonglong  affected_rows;
 53     int           param_count;
 54     short         small_data;
 55     int           int_data;
 56     char          str_data[STRING_SIZE];
 57     unsigned long str_length;
 58     my_bool       is_null;
 59      
 60     if (mysql_query(mysql, DROP_SAMPLE_TABLE))//鍒犻櫎琛?/span>
 61     {
 62       fprintf(stderr, " DROP TABLE failed
");
 63       fprintf(stderr, " %s
", mysql_error(mysql));
 64       exit(0);
 65     }
 66      
 67     if (mysql_query(mysql, CREATE_SAMPLE_TABLE))//鍒涘缓琛?/span>
 68     {
 69       fprintf(stderr, " CREATE TABLE failed
");
 70       fprintf(stderr, " %s
", mysql_error(mysql));
 71       exit(0);
 72     }
 73      
 74     /* Prepare an INSERT query with 3 parameters */
 75     /* (the TIMESTAMP column is not named; the server */
 76     /*  sets it to the current date and time) */
 77     stmt = mysql_stmt_init(mysql); //棰勫鐞嗙殑鍒濆鍖?/span>
 78     if (!stmt)
 79     {
 80       fprintf(stderr, " mysql_stmt_init(), out of memory
");
 81       exit(0);
 82     }
 83     if (mysql_stmt_prepare(stmt, INSERT_SAMPLE, strlen(INSERT_SAMPLE))) //insert 璇彞 鐨勯澶勭悊 
 84     {
 85       fprintf(stderr, " mysql_stmt_prepare(), INSERT failed
");
 86       fprintf(stderr, " %s
", mysql_stmt_error(stmt));
 87       exit(0);
 88     }
 89     fprintf(stdout, " prepare, INSERT successful
");
 90      
 91     /* Get the parameter count from the statement */
 92     param_count= mysql_stmt_param_count(stmt);//鑾峰緱鍙傛暟涓暟 
 93     fprintf(stdout, " total parameters in INSERT: %d
", param_count);
 94      
 95     if (param_count != 3) /* validate parameter count */
 96     {
 97       fprintf(stderr, " invalid parameter count returned by MySQL
");
 98       exit(0);
 99     }
100      
101     /* Bind the data for all 3 parameters */
102      
103     memset(bind, 0, sizeof(bind));
104      
105     /* INTEGER PARAM */
106     /* This is a number type, so there is no need to specify buffer_length */
107     bind[0].buffer_type= MYSQL_TYPE_LONG;
108     bind[0].buffer= (char *)&int_data;//鍐呭瓨鍦板潃鐨勬槧灏?
109     bind[0].is_null= 0;
110     bind[0].length= 0;
111      
112     /* STRING PARAM */
113     bind[1].buffer_type= MYSQL_TYPE_STRING;
114     bind[1].buffer= (char *)str_data;//char 100 
115     bind[1].buffer_length= STRING_SIZE;
116     bind[1].is_null= 0;
117     bind[1].length= &str_length;
118      
119     /* SMALLINT PARAM */
120     bind[2].buffer_type= MYSQL_TYPE_SHORT;
121     bind[2].buffer= (char *)&small_data;
122     bind[2].is_null= &is_null;//鏄惁涓簄ull鐨勬寚绀哄櫒 
123     bind[2].length= 0;
124      
125     /* Bind the buffers */
126     if (mysql_stmt_bind_param(stmt, bind)) //缁戝畾鍙橀噺 鍙傛暟缁戝畾
127     {
128       fprintf(stderr, " mysql_stmt_bind_param() failed
");
129       fprintf(stderr, " %s
", mysql_stmt_error(stmt));
130       exit(0);
131     }
132      
133     //绗竴娉㈣祴鍊?
134     int_data= 10;             /* integer */
135     strncpy(str_data, "MySQL", STRING_SIZE); /* string  */
136     str_length= strlen(str_data);
137      
138     /* INSERT SMALLINT data as NULL */
139     is_null= 1;//鎸囩ず鎻掑叆鐨勭涓変釜瀛楁鏄惁涓簄ull 
140      
141     /* Execute the INSERT statement - 1*/
142     if (mysql_stmt_execute(stmt)) //棰勫鐞嗙殑鎵ц,绗竴娆℃墽琛?
143     {
144       fprintf(stderr, " mysql_stmt_execute(), 1 failed
");
145       fprintf(stderr, " %s
", mysql_stmt_error(stmt));
146       exit(0);
147     }
148      
149     /* Get the total number of affected rows */
150     affected_rows= mysql_stmt_affected_rows(stmt);//棰勫鐞嗙殑褰卞搷鏉℃暟
151     fprintf(stdout, " total affected rows(insert 1): %lu
",
152                     (unsigned long) affected_rows);
153      
154     if (affected_rows != 1) /* validate affected rows */
155     {
156       fprintf(stderr, " invalid affected rows by MySQL
");
157       exit(0);
158     }
159      
160     //绗簩娉㈣祴鍊?
161     int_data= 1000;
162     strncpy(str_data, "The most popular Open Source database", STRING_SIZE);
163     str_length= strlen(str_data);
164     small_data= 1000;         /* smallint */
165     is_null= 0;               /* reset */
166      
167     /* Execute the INSERT statement - 2*/
168     if (mysql_stmt_execute(stmt))//绗簩娆℃墽琛?/span>
169     {
170       fprintf(stderr, " mysql_stmt_execute, 2 failed
");
171       fprintf(stderr, " %s
", mysql_stmt_error(stmt));
172       exit(0);
173     }
174      
175     /* Get the total rows affected */
176     affected_rows= mysql_stmt_affected_rows(stmt);
177     fprintf(stdout, " total affected rows(insert 2): %lu
",
178                     (unsigned long) affected_rows);
179      
180     if (affected_rows != 1) /* validate affected rows */
181     {
182       fprintf(stderr, " invalid affected rows by MySQL
");
183       exit(0);
184     }
185      
186     /* Close the statement */
187     if (mysql_stmt_close(stmt))
188     {
189       fprintf(stderr, " failed while closing the statement
");
190       fprintf(stderr, " %s
", mysql_stmt_error(stmt));
191       exit(0);
192     }
193 
194 }
prepare_insert.c

 

 

/*******************************************************************************************/

鍗佷笁銆乵ysql浜嬪姟澶勭悊

mysql榛樿浜嬪姟涓嶅紑鍚紝浣嗘槸浜嬪姟鏄敮鎸佺殑锛屾槸鍙互寮€鍚殑銆?/p>

 

#define SET_TRAN   "SET AUTOCOMMIT=0"  //浠h〃鑷姩鎻愪氦鏄鍏抽棴浜嗙殑锛屼篃灏辨槸浜嬪姟寮€鍚簡銆傗€斺€斺€旀墜鍔╟ommit

#define UNSET_TRAN      "SET AUTOCOMMIT=1"  //鑷姩commit锛屾墽琛屽畬sql璇彞锛岀珛鍒诲氨鎻愪氦锛屼篃灏辨槸鎭㈠浜嗛粯璁?/p>

//鏃犺璁剧疆浜嬪姟涓烘墜鍔ㄦ彁浜よ繕鏄嚜鍔ㄦ彁浜わ紝閮借鍏堝紑鍚簨鍔★紝鍗虫墽琛?start transaction"

 

鍏蜂綋瑙佷唬鐮併€妕ran.c銆嬶細

鎶€鏈垎浜浘鐗? id=
  1 //mysql涓殑浜嬪姟
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 #include "mysql.h"
  6 
  7 #define SET_TRAN    "SET AUTOCOMMIT=0"          //鎵嬪姩commit    鈥斺€斺€斺€旀墜鍔╟ommit
  8 #define UNSET_TRAN    "SET AUTOCOMMIT=1"            //鑷姩commit
  9 
 10 #define _HOST_ "127.0.0.1"
 11 #define _USER_ "root"
 12 #define _PASSWD_ "123"
 13 #define _DBNAME_ "scott"
 14 
 15 //璁剧疆浜嬪姟涓烘墜鍔ㄦ彁浜?/span>
 16 int mysql_OperationTran(MYSQL *mysql)              
 17 {
 18     //--寮€鍚簨鍔?/span>
 19     int ret = mysql_query(mysql, "start transaction");  
 20     if (ret != 0) {
 21         printf("mysql_OperationTran query start err: %s
", mysql_error(mysql));
 22         return ret;
 23     }
 24 
 25     //--璁剧疆浜嬪姟涓烘墜鍔ㄦ彁浜?/span>
 26     ret = mysql_query(mysql, SET_TRAN);            //set銆€autocommmit = 0
 27     if (ret != 0) {
 28         printf("mysql_OperationTran query set err: %s
", mysql_error(mysql));
 29         return ret;
 30     }
 31 
 32     return ret;
 33 }
 34 
 35 //璁剧疆浜嬪姟涓鸿嚜鍔ㄦ彁浜?/span>
 36 int mysql_AutoTran(MYSQL *mysql)
 37 {
 38     //--寮€鍚簨鍔?/span>
 39     int ret = mysql_query(mysql, "start transaction");  
 40     if (ret != 0) {
 41         printf("mysql_AutoTran query start err: %s
", mysql_error(mysql));
 42         return ret;
 43     }
 44 
 45     //--璁剧疆浜嬪姟涓鸿嚜鍔ㄦ彁浜?/span>
 46     ret = mysql_query(mysql, UNSET_TRAN);  //"set autocommit = 1"
 47     if (ret != 0) {
 48         printf("mysql_AutoTran query set err: %s
", mysql_error(mysql));
 49         return ret;
 50     }
 51 
 52     return ret;        
 53 }
 54 
 55 //鎵цcommit锛屾墜鍔ㄦ彁浜や簨鍔?/span>
 56 int mysql_Commit(MYSQL *mysql)
 57 {
 58     int ret = mysql_query(mysql, "COMMIT"); //鎻愪氦
 59     if (ret != 0) {
 60         printf("commit err: %s
", mysql_error(mysql));
 61         return ret;
 62     }
 63     return ret;
 64 }
 65 
 66 //鎵цrollback锛屽洖婊氫簨鍔?       
 67 int mysql_Rollback(MYSQL *mysql)
 68 {
 69     int ret = mysql_query(mysql, "ROLLBACK");
 70     if (ret != 0) {
 71         printf("rollback err: %s
", mysql_error(mysql));
 72         return ret;
 73     }
 74     return ret;
 75     
 76 }
 77 
 78 #define DROP_SAMPLE_TABLE "DROP TABLE IF EXISTS test_table"
 79 #define CREATE_SAMPLE_TABLE "CREATE TABLE test_table(col1 INT, 80                                                  col2 VARCHAR(10), 81                                                  col3 VARCHAR(10))"
 82                                                                                        
 83 #define sql01 "INSERT INTO test_table(col1,col2,col3) VALUES(10, 鈥楢AA鈥? 鈥楢1鈥?"
 84 #define sql02 "INSERT INTO test_table(col1,col2,col3) VALUES(20, 鈥楤BB鈥? 鈥楤2鈥?"
 85 #define sql03 "INSERT INTO test_table(col1,col2,col3) VALUES(30, 鈥楥CC鈥? 鈥楥3鈥?"
 86 #define sql04 "INSERT INTO test_table(col1,col2,col3) VALUES(40, 鈥楧DD鈥? 鈥楧4鈥?"
 87 
 88 int main(void)
 89 {
 90     int ret = 0;
 91 
 92     MYSQL *mysql = mysql_init(NULL);
 93     
 94     mysql = mysql_real_connect(mysql, _HOST_, _USER_, _PASSWD_, _DBNAME_, 0, NULL, 0);
 95     if (mysql == NULL) {
 96         ret = mysql_errno(mysql);
 97         printf("func mysql_real_connect() err锛?d
", ret);
 98         return ret;
 99     }     
100     printf(" --- connect ok......
");    
101     //鎵ц鍒犻櫎琛?/span>
102     if (mysql_query(mysql, DROP_SAMPLE_TABLE)) {
103       fprintf(stderr, " DROP TABLE failed
");
104       fprintf(stderr, " %s
", mysql_error(mysql));
105       exit(0);
106     }
107     //鎵ц鍒涘缓琛?/span>
108     if (mysql_query(mysql, CREATE_SAMPLE_TABLE)) {
109       fprintf(stderr, " CREATE TABLE failed
");
110       fprintf(stderr, " %s
", mysql_error(mysql));
111       exit(0);
112     }    
113     
114     ret = mysql_OperationTran(mysql);     //寮€鍚簨鍔★紝骞朵慨鏀逛簨鍔″睘鎬т负鎵嬪姩commit 
115     if (ret != 0) {
116         printf("mysql_OperationTran() err:%d
", ret);
117         return ret;
118     }
119     
120     ret = mysql_query(mysql, sql01);    //鍚戣〃涓彃鍏ョ涓€琛屾暟鎹?鈥楢AA鈥?/span>
121     if (ret != 0) {
122         printf("mysql_query() err:%d
", ret);
123         return ret;
124     }
125     
126     ret = mysql_query(mysql, sql02);    //鍚戣〃涓彃鍏ョ浜岃鏁版嵁 鈥楤BB鈥?/span>
127     if (ret != 0) {
128         printf("mysql_query() err:%d
", ret);
129         return ret;
130     }
131     
132     ret = mysql_Commit(mysql);             //鎵嬪姩鎻愪氦浜嬪姟
133     if (ret != 0) {
134         printf("mysql_Commit() err:%d
", ret);
135         return ret;
136     }
137     //////////AAA BBB  杩涘幓浜嗐€?/span>
138     
139 #if 1
140     ret = mysql_AutoTran(mysql);         // =鍐嶆= 淇敼浜嬪姟灞炴€т负銆愯嚜鍔ㄣ€慶ommit
141     if (ret != 0) {
142         printf("mysql_OperationTran() err:%d
", ret);
143         return ret;
144     }
145 #else 
146     ret = mysql_OperationTran(mysql);     // =鍐嶆= 淇敼浜嬪姟灞炴€т负銆愭墜鍔ㄣ€慶ommit
147     if (ret != 0) {
148         printf("mysql_OperationTran() err:%d
", ret);
149         return ret;
150     }
151 #endif
152 
153     ret = mysql_query(mysql, sql03);    //鍚戣〃涓彃鍏ョ涓夎鏁版嵁 鈥楥CC鈥?/span>
154     if (ret != 0) {
155         printf("mysql_query() err:%d
", ret);
156         return ret;
157     }
158     
159     ret = mysql_query(mysql, sql04);    //鍚戣〃涓彃鍏ョ鍥涜鏁版嵁 鈥楧DD鈥?/span>
160     if (ret != 0) {
161         printf("mysql_query() err:%d
", ret);
162         return ret;
163     }
164     
165     ret = mysql_Rollback(mysql);        //鐩存帴rollback鎿嶄綔
166     if (ret != 0) {
167         printf("mysql_Rollback() err:%d
", ret);
168         return ret;
169     }
170     
171     //rollback鎿嶄綔鏄惁鑳藉洖閫€鎺塁CC銆丏DD鐨勫€硷紝鍙栧喅浜庝簨鍔″睘鎬с€?/span>
172     
173     mysql_close(mysql);
174     
175     return 0;    
176 }
tran.c

 

以上是关于鏁版嵁搴撲箣MySQL鐨勪粙缁嶄笌浣跨敤20180703的主要内容,如果未能解决你的问题,请参考以下文章

drogon鍏ラ棬鏁欑▼涓?鎺у埗鍣ㄥ拰鏁版嵁搴撳鎴风浣跨敤

浜戞爾骞茶揣鍥為 | 琛屼笟椤剁骇NoSQL鎴愬憳鍧愰晣锛孨oSQL鏁版嵁搴撲笓鍦洪噸鐐硅В鏋愶紒

鏁版嵁搴撲紭鍖栫殑鎬濊矾

涓€鏉℃洿鏂癝QL鍦∕ySQL鏁版嵁搴撲腑鏄浣曟墽琛岀殑

Greenplum鏁版嵁搴撲腑绯荤粺琛╬g_class璇﹁В