棣栧厛鎵嬪姩鍚姩Mysql搴旂敤绋嬪簭
鎵撳紑缁堢杈撳叆濡備笅鍛戒护锛� 聽/usr/local/mysql/bin/mysql -u root -p
锛堟敞鎰忥細Windows涓嬬殑鏄細 聽mysql -u root -p锛�
鍏朵腑root涓虹敤鎴峰悕銆傝繖鏃朵細鍑虹幇濡備笅鍛戒护锛欵nter password: 聽123456
杩欐牱灏卞彲浠ヨ闂綘鐨�鏁版嵁搴�鏈嶅姟鍣ㄤ簡銆�
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽鏁版嵁搴撴搷浣�
涓嬮潰浠嬬粛涓�浜涘叧浜庝粠鏁版嵁搴撳垱寤哄埌浣跨敤鐨勪竴浜涚畝鍗曠殑鏂规硶锛�
鍒涘缓涓�涓悕瀛椾负mydatabase鏁版嵁搴擄細聽create database mydatabase ;
鍙互鐢ㄤ互涓嬪湴鍛戒护鏉ユ煡鐪嬪垱寤虹殑鏁版嵁搴撴槸鍚︽垚鍔燂細聽show databases ;
鏇存敼鏁版嵁搴撳悕瀛� 锛毬燼lter databases 聽Hdatabase ;
鏇存敼鏁版嵁搴搈ydatabase鐨勫瓧绗﹂泦 锛毬燼lter database mydatabase charset GBK ;
杩涘叆鏁版嵁搴擄細聽use mydatabase ;
鐢ㄤ笅闈㈢殑鍛戒护鏉ユ煡鐪嬭鏁版嵁搴撲腑鐨勮〃锛毬爏how tables ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 琛ㄦ搷浣�
鐢ㄤ笅闈㈢殑鍛戒护鏉ュ垱寤鸿〃锛歝reate table student (
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 name varchar(10) ,聽
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽gendervarchar(10) ,
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽sno 聽 聽int 聽 聽primary key(id)
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 )charset utf8;
鐢ㄤ笅闈㈢殑鍛戒护鏉ユ鏌ヨ〃鐨勫垱寤烘槸鍚︽垚鍔燂細聽show tables;
鏌ョ湅璇ヨ〃缁撴瀯锛岃〃涓瓧娈电殑淇℃伅锛毬燿esc 琛ㄥ悕 ; 聽 聽 聽 聽describe 琛ㄥ悕; 聽 聽 聽 show columns from 聽琛ㄥ悕;
淇敼琛ㄥ悕锛毬爎ename鍛戒护鐢ㄤ簬淇敼琛ㄥ悕銆� 聽rename鍛戒护鏍煎紡锛歳ename table 鍘熻〃鍚� to 鏂拌〃鍚�;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽alter 聽table 聽鏃ц〃鍚� 聽rename 聽鏂拌〃鍚�; 聽 聽 聽 聽 alter table student 聽rename 聽my_student;
淇敼瀛楁锛屽瓧娈垫搷浣滃緢澶氾細鏂板(add)銆佷慨鏀�(modify)銆侀噸鍚�(change)銆佸垹闄�(drop)
--缁欏鐢熻〃澧炲姞ID鏀惧埌绗竴涓綅缃�� 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽altertable 聽琛ㄥ悕 聽add 聽瀛楁鍚� 聽鏁版嵁绫诲瀷[浣嶇疆];
alter 聽table my_student add 聽id int first;
---灏嗗鐢熻〃涓殑number瀛﹀彿瀛楁鍙樻垚鍥哄畾闀垮害锛屼笖鏀惧埌绗簩浣嶃�� 聽 alter table 琛ㄥ悕 聽modify 聽瀛楁鍚� 聽鏁版嵁绫诲瀷 聽[浣嶇疆];聽
alter 聽table 聽my_student 聽modify 聽number 聽char(10) 聽after id;
---淇敼瀛︾敓琛ㄤ腑鐨刧ender瀛楁涓簊ex銆俛lter table 琛ㄥ悕 聽change 聽鏃у瓧娈靛悕 聽鏂板瓧娈靛悕 聽鏁版嵁绫诲瀷 ;alter table my_student 聽change 聽gender 聽sex 聽varchar(10);
---鍒犻櫎瀛︾敓琛ㄤ腑鐨勫瓧娈礱ge銆� 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽alter 聽table 聽琛ㄥ悕 聽 drop 聽 瀛楁鍚�; 聽 聽
alter 聽table 聽my_student 聽drop 聽age; 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽聽
鍒犻櫎鏁版嵁琛細 drop 聽table 聽琛ㄥ悕1锛岃〃鍚�2锛屸�︹��; 聽 聽 聽 聽 涓�娆℃�у垹闄ゅ涓〃
鏁版嵁鎿嶄綔
---鏂板鏁版嵁锛堟彃鍏ュ叏琛ㄥ瓧娈垫暟鎹級 insert into my_student values('Jim','female',1106101);
---鏂板鏁版嵁锛堟寚瀹氬瓧娈靛垪琛級 聽 insert into 聽my_student(number,sex) 聽values(1106,'male');
---鏌ョ湅鎵�鏈夌殑鏁版嵁 聽select 聽*聽
聽 聽 聽 聽 聽 聽 聽 聽 聽 from 聽琛ㄥ悕
聽 聽 聽 聽 聽 聽 聽 聽 聽 [where 鏉′欢];
---鏇存柊鏁版嵁 聽 聽alter鏄搷浣滄暟鎹簱锛岃〃锛屽瓧娈点�� update鏇存柊鏁版嵁
--鏇存柊鍚嶅瓧鍙獼im鐨勬�у埆銆� 聽 聽 聽update 聽琛ㄥ悕 聽set 聽瀛楁 = 鍊糩where 鏉′欢]锛�
update my_student set sex = 'female' where name = 'Jim';
---鍒犻櫎鏁版嵁聽
--鍒犻櫎琛ㄤ腑鎬у埆鐢风殑 聽 聽 聽delete 聽from 聽琛ㄥ悕 聽[where 聽鏉′欢];
delete 聽from my_student 聽where 聽sex = 'male';
涓枃鏁版嵁闂鏈川鏄瓧绗﹂泦闂锛宻et names 聽gbk; 聽 涓�瀹氳璁剧疆瀛楃闆嗐��
澶栭敭锛氬鏋滀竴寮犺〃涓湁涓�涓瓧閿锛堥潪涓婚敭锛夋寚鍚戝彟澶栦竴寮犺〃鐨勪富閿紝閭d箞灏嗚瀛楁绉颁箣涓哄閿��
涓婚敭闂
涓婚敭鐩存帴鍦ㄥ瓧娈典箣鍚庯紝鍙互鏈夊涓瓧娈典綔涓轰富閿紝杩欐牱鐨勪富閿О涓哄鍚堜富閿��
娌℃湁涓婚敭锛岃拷鍔犱富閿� 聽 聽alter 聽table 聽my_database 聽modify 聽course 聽char(10) 聽primary key;
娌℃湁涓婚敭锛岃拷鍔犲鍚堜富閿� 聽alter table 聽my_student 聽add 聽primary key (sno,cno);
鏇存柊涓婚敭 & 鍒犻櫎涓婚敭 聽alter 聽table 聽琛ㄥ悕 drop primary 聽key;
鑼冨紡锛氱粓鏋佺洰鏍囦负浜嗗噺灏戞暟鎹殑鍐椾綑锛屽嚒鏄�氳繃鍏崇郴瀵绘壘鍑烘潵鐨勬暟鎹紝鍧氬喅涓嶅啀閲嶅瀛樺偍銆�
绗竴鑼冨紡锛氬湪璁捐琛ㄥ瓨鍌ㄦ暟鎹椂锛屾暟鎹叿鏈夊師瀛愭�с��
濡傝〃锛堝鍚嶏紝鎬у埆锛屾暀璇炬椂闂达紙寮�濮嬶紝缁撴潫锛夛級 聽姝よ〃鐨勬暀璇炬椂闂翠粛鍙媶鍒嗭紝涓嶅叿澶囧師瀛愭�с��
瑙e喅鍔炴硶锛堝鍚嶏紝鎬у埆锛屽紑濮嬶紝缁撴潫锛�
绗簩鑼冨紡锛氳В鍐宠〃璁捐鍑虹幇鐨勯儴鍒嗕緷璧栵紝閮ㄥ垎渚濊禆灏辨槸瀛樺湪瀛楁渚濊禆涓婚敭涓煇涓瓧娈碉紙涓婚敭鐨勯儴鍒嗭級銆傚彧瑕佷笉瀛樺湪澶嶅悎涓婚敭锛岃〃鐨勮璁″氨涓�瀹氭弧瓒�2NF
濡備竴寮犺〃涓湁(璁插笀锛屾�у埆锛岀彮绾э紝鏁欏锛屽甫璇炬椂闂达紝寮�濮嬫椂闂达紝缁撴潫鏃堕棿锛� 聽 鍥犱负璁插笀娌″姙娉曚綔涓轰富閿紝闇�瑕佺粨鍚堣甯堢彮绾ф墠鑳戒綔涓轰富閿紙涓�涓�佸笀鍦ㄤ竴涓彮鍙甫涓�涓樁娈电殑璇撅紝鎬у埆骞朵笉渚濊禆鐝骇锛屽彧渚濊禆璁插笀锛涙暀瀹や笉渚濊禆璁插笀锛屽彧渚濊禆鐝骇锛屽洜姝ゅ嚭鐜颁簡鎬у埆鍜屾暀瀹や緷璧栦富閿腑鐨勪竴閮ㄥ垎锛�
瑙e喅鏂规锛� 鍙互灏嗘�у埆涓庤甯堝崟鐙垚琛紝鐝骇涓庢暀瀹や篃鍗曠嫭鎴愯〃锛屼互姝ゅ彇娑堝鍚堜富閿��
鏈変汉涔熻涓哄彲浠ヤ娇鐢ㄩ�昏緫涓婚敭锛屼緥濡傚鍔營DP銆傝繖鏍风湡鐨勫彲浠ュ悧锛� 聽涓嶅彲浠ワ紝濡備笅锛屼細鍑虹幇浼犻�掍緷璧栥��
绗笁鑼冨紡锛氱悊璁轰笂璁诧紝搴旇涓�寮犺〃涓殑鎵�鏈夊瓧娈甸兘鐩存帴渚濊禆涓婚敭锛屽鏋滆〃璁捐涓瓨鍦ㄤ竴涓瓧娈碉紝骞朵笉鐩存帴渚濊禆涓婚敭锛岃�屾槸閫氳繃鏌愪釜闈炰富閿瓧娈典緷璧栵紝鏈�缁堝疄鐜颁緷璧栦富閿紝濡備竴寮犺〃涓湁(涓婚敭IDP锛岃甯堬紝鎬у埆锛岀彮绾э紝鏁欏锛屽甫璇炬椂闂达紝寮�濮嬫椂闂达紝缁撴潫鏃堕棿锛� 聽鎬у埆渚濊禆浜庤甯堬紝璁插笀渚濊禆浜庝富閿甀DP锛涙暀瀹や緷璧栫彮绾э紝鐝骇渚濊禆涓婚敭IDP銆傛�у埆鍜屾暀甯堥兘瀛樺湪浼犻�掍緷璧栥��
瑙e喅鏂规锛氬皢瀛樺湪浼犻�掍緷璧栫殑瀛楁锛屼互鍙婁緷璧栫殑瀛楁鏈韩鍗曠嫭鍙栧嚭锛屽舰鎴愪竴涓崟鐙殑琛紝鐒跺悗鍦ㄩ渶瑕佸搴旂殑淇℃伅鐨勬椂鍊欙紝浣跨敤瀵瑰簲鐨勫疄浣撹〃鐨勪富閿姞杩涙潵銆備竴鍙ヨ瘽灏辨槸瀹炰綋鍗曠嫭寤鸿〃銆�
鏌ヨ鏁版嵁瀹屾暣璇硶
select 瀛楁鍚�/*
from 聽鏁版嵁婧�
[where 鏉′欢瀛愬彞]
[group by 瀛愬彞]
[having 瀛愬彞]
[order by 瀛愬彞]
[limit 瀛愬彞];
select * 聽from my_student;聽
---鍘婚噸
select distinct * 聽from 聽my_student;
--鎻掑叆鏁版嵁
insert into my_student values(null,'itcast01','寮犱笁','鐢�'),(null,'itcast02','鏉庡洓','鐢�'),(null,'itcast03','鐜嬩簲','濂�'),(null,'itcast03','鐢�');
--瀛楁鍒悕
select id,number as 瀛﹀彿锛宯ame as 濮撳悕, sex 鎬у埆聽
from my_student;
where瀛愬彞
鍘熺悊锛歸here鏄敮涓�涓�涓洿鎺ヤ粠纾佺洏鑾峰彇鏁版嵁鐨勬椂鍊欏氨寮�濮嬪垽鏂殑鏉′欢锛氫粠纾佺洏鍙栧嚭涓�鏉¤褰曪紝寮�濮嬭繘琛寃here鍒ゆ柇锛屽垽鏂殑缁撴灉濡傛灉鎴愮珛淇濆瓨鍒板唴瀛橈紝鍙嶄箣鏀惧純銆�
---鎵惧鐢焛d涓�1锛�3锛�5鐨勫鐢�
select 聽* 聽from 聽my_student 聽where id =1 || id =3 || id =5;
鎴栬�卻elect * from my_student where id in(1,3,5);
group by 聽瀛楁 [asc/desc]; 聽 鍒嗙粍 聽鏍规嵁鏌愪釜瀛楁杩涜鍒嗙粍锛岀浉鍚岀殑鏀句竴缁勶紝涓嶅悓鐨勫垎鍒颁笉鍚岀殑缁勩�傜粺璁$殑鏄暟鎹紝涓嶆槸璁板綍
--鎸夋�у埆鍒嗙粍
select * from my_student group by sex; 杩欐槸缁濆閿欒鐨勶紝鍒嗙粍鏄负浜嗙粺璁℃暟鎹紝鎸夊垎缁勫瓧娈佃繘琛屾暟鎹粺璁�
SQL鎻愪緵浜嗕竴绯诲垪缁熻鍑芥暟
Count(); 聽缁熻鍒嗙粍鍚庣殑璁板綍鏁帮紝姣忎竴鐩稿悓鐨勭粍鏈夊灏戣璁板綍
Max(); 聽 缁熻姣忕粍鏈�澶х殑鍊�
Min(); 聽 缁熻姣忕粍鏈�灏忕殑鍊�
Avg(); 聽 缁熻姣忕粍鐨勫钩鍧囧��
Sum(); 聽 缁熻姣忕粍鐨勫拰
---韬珮楂樼煯锛屽钩鍧囧勾榫勶紝鍜屾�诲勾榫�
select sex,count(*),max(height),min(height),avg(age),sun(age) 聽 from my_student 聽group by sex;
娉ㄦ剰浣跨敤group by鍗翠笉浣跨敤缁熻鍑芥暟锛屾病鎰忎箟!
澶氬瓧娈靛垎缁勶細鍏堟牴鎹竴涓瓧娈佃繘琛屽垎缁勶紝鐒跺悗瀵瑰垎缁勫悗鐨勭粨鏋滃啀娆℃寜鐓у叾浠栧瓧娈靛垎缁勩��
having瀛愬彞
涓巜here瀛愬彞涓�鏍疯繘琛屾潯浠跺垽鏂紝鎬濊�冿細涓轰粈涔堟槸group by鈥︹�aving鈥︹�� 鑰屼笉鏄痝roup by鈥︹�here???
杩涜鏁版嵁缁熻鏃讹紝鏄暟鎹繘鍏ュ唴瀛樹箣鍚庯紝浼氳繘琛屽垎缁�-銆嬬粺璁�-銆媓aving鏉′欢鍒ゆ柇銆傝�寃here鏄拡瀵逛粠纾佺洏璇诲彇鏁版嵁鏃惰繘琛屽垽鏂紝姝ゅ埢杩樻病鍒嗙粍锛岃繘琛岀粺璁″氨鍒ゆ柇浜嗐�傛墍浠ラ敊璇紒锛侊紒
---姹傚嚭鎵�鏈夌彮绾т汉鏁板ぇ浜庣瓑浜�2鐨勫鐢熶汉鏁�
order by 瀛愬彞
order by鏍规嵁鏌愪釜瀛楁杩涜鍗囧簭鎴栭檷搴忔帓搴忥紝渚濊禆鏍″闆�
鍩烘湰璇硶锛� order by 瀛楁鍚� [asc/desc]; 聽asc鍗囧簭 鍙笉鍐� 聽 聽 聽desc闄嶅簭
聽
limit瀛愬彞鏄竴绉嶉檺鍒剁粨鏋滅殑璇彞
1锛夌敤鏉ラ檺鍒舵暟閲�
--鏌ヨ瀛︾敓鍓嶄袱涓� 聽 select * from my_student limit 2;
2)闄愬埗璧峰浣嶇疆 聽limit 璧峰浣嶇疆锛岄暱搴�
--鏌ヤ粠缂栧彿4寮�濮嬶紝璇㈡壘涓や釜瀛︾敓
select * from my_student limit 4,2;
鍐呰繛鎺ワ紝澶栬繛鎺ワ紝鑷劧杩炴帴锛屼氦鍙夎繛鎺�
鍐呰繛鎺ワ紝浠庡乏琛ㄤ腑鍙栧嚭姣忎竴鏉¤褰曪紝鍜屽彸琛ㄤ腑鎵�鏈夌殑璁板綍杩涜鍖归厤锛屽尮閰嶅繀椤绘槸宸﹁〃涓笌鍙宠〃涓煇涓潯浠剁浉鍚岋紝鏈�缁堜細淇濈暀缁撴灉锛屽惁鍒欎笉淇濈暀銆�
鍩烘湰璇硶锛� 聽宸﹁〃 [inner] join 鍙宠〃 聽on 宸﹁〃.瀛楁 = 鍙宠〃.瀛楁;
select * from my_student inner join my_class on my_student.C_id = mu_class.id;
宸﹀杩炴帴锛氬乏琛ㄤ负涓伙紝鐒跺悗姣忔潯璁板綍涓庡彸琛ㄨ繘琛岃繛鎺ワ紝涓嶇鑳戒笉鑳藉尮閰嶇殑涓婏紝宸﹁〃閮戒細淇濈暀銆傝兘鍖归厤锛屽彸琛ㄦ煇鏉¤褰曚繚鐣欙紝涓嶈兘鍖归厤锛屾煇鏉¤褰曠疆涓篘ULL锛屾渶缁堣褰曟暟鑷冲皯涓嶅皯浜庡乏琛ㄥ凡鏈夌殑璁板綍銆�
鍩烘湰璇硶锛� 聽宸﹁〃 聽left join 聽鍙宠〃 聽on 聽宸﹁〃.瀛楁 = 鍙宠〃.瀛楁;
a琛� 聽 聽 id 聽 name 聽 聽 聽b琛� 聽 聽 id 聽 job 聽 parent_id
1 聽 寮�3 聽 聽 聽 聽 聽 聽 聽 聽 聽 1 聽 聽 23 聽 聽 1
2 聽 鏉庡洓 聽 聽 聽 聽 聽 聽 聽 聽 2 聽 聽 34 聽 聽 2
3 聽 鐜嬫 聽 聽 聽 聽 聽 聽 聽 聽 3 聽 聽 34 聽 聽 4
a.id鍚宲arent_id 聽 瀛樺湪鍏崇郴
--------------------------------------------------
聽1锛� 鍐呰繛鎺ヂ�
select 聽 a.*,b.* 聽 from 聽 a 聽 inner 聽 join 聽 b 聽 聽 on 聽 a.id=b.parent_id
缁撴灉鏄�
1 聽 寮�3 聽 聽 聽 聽 聽 聽 聽 聽 聽 1 聽 聽 23 聽 聽 1
2 聽 鏉庡洓 聽 聽 聽 聽 聽 聽 聽 聽 聽2 聽 聽 34 聽 聽 2
聽2锛夊乏杩炴帴聽聽
select 聽 a.*,b.* 聽 from 聽 a 聽 left 聽 join 聽 b 聽 聽 on 聽 a.id=b.parent_id
缁撴灉鏄�
1 聽 寮�3 聽 聽 聽 聽 聽 聽 聽 聽 聽 1 聽 聽 23 聽 聽 1
2 聽 鏉庡洓 聽 聽 聽 聽 聽 聽 聽 聽 聽2 聽 聽 34 聽 聽 2
3 聽 鐜嬫 聽 聽 聽 聽 聽 聽 聽 聽 聽null
3锛壜犲彸杩炴帴 聽聽
select 聽 a.*,b.* 聽 from 聽 a 聽 right 聽 join 聽 b 聽 聽 on 聽 a.id=b.parent_id
缁撴灉鏄�
1 聽 寮�3 聽 聽 聽 聽 聽 聽 聽 聽 聽 1 聽 聽 23 聽 聽 1
2 聽 鏉庡洓 聽 聽 聽 聽 聽 聽 聽 聽 聽2 聽 聽 34 聽 聽 2
null 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽3 聽 聽 34 聽 聽 4
4锛壜犲畬鍏ㄨ繛鎺ヂ�
select 聽 a.*,b.* 聽 from 聽 a 聽 full 聽 join 聽 b 聽 聽 on 聽 a.id=b.parent_id
缁撴灉鏄�
1 聽 寮�3 聽 聽 聽 聽 聽 聽 聽 聽 聽1 聽 聽 23 聽 聽 1
2 聽 鏉庡洓 聽 聽 聽 聽 聽 聽 聽 聽聽2 聽 聽 34 聽 聽 2
null 聽 聽 聽 聽 聽 聽 聽 銆�銆�聽 3 聽 聽 34 聽 聽 4
3 聽 鐜嬫 聽 聽 聽 聽 聽 聽 聽 聽 null
闇�瑕佹墜鍔ㄥ鍔犲彲浠ヨ繙绋嬭闂暟鎹簱鐨勭敤鎴枫��
鏂规硶涓�銆佹湰鍦扮櫥鍏ysql锛屾洿鏀� "mysql" 鏁版嵁搴撻噷鐨� "user" 琛ㄩ噷鐨� "host" 椤癸紝灏�"localhost"鏀逛负"%"
#mysql -u root -proot
mysql>use mysql;
mysql>update user set host = '%' where user = 'root';
mysql>select host, user from user;
鏂规硶浜屻�佺洿鎺ユ巿鏉�(鎺ㄨ崘)
銆�銆�浠庝换浣曚富鏈轰笂浣跨敤root鐢ㄦ埛锛屽瘑鐮侊細youpassword锛堜綘鐨剅oot瀵嗙爜锛夎繛鎺ュ埌mysql鏈嶅姟鍣細
# mysql -u root -proot
mysql>GRANT ALL PRIVILEGES ON *.* TO'root'@'%'IDENTIFIED BY 'youpassword' WITH GRANT OPTION;
鎿嶄綔瀹屽悗鍒囪鎵ц浠ヤ笅鍛戒护鍒锋柊鏉冮檺聽
FLUSH PRIVILEGES聽