Mac缁堢鎿嶄綔Mysql锛屼互鍙奙ysql鐨勬搷浣�

棣栧厛鎵嬪姩鍚姩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聽

你可能感兴趣的:(Mac缁堢鎿嶄綔Mysql锛屼互鍙奙ysql鐨勬搷浣�)