mysql甯哥敤鍩虹鎸囦护澶у叏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql甯哥敤鍩虹鎸囦护澶у叏相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e7%ad%89%e5%80%bc%e8%bf%9e%e6%8e%a5' title='绛夊€艰繛鎺?>绛夊€艰繛鎺?/a>   bsp   change   鍏佽   substr   ediff   鍊掑簭   鍥炴粴   

mysql鎸囦护

鍚姩 net start mysql
閫€鍑簃ysql quit
鐧诲綍 mysql -uroot -p
閫昏緫闈?not ! 閫昏緫涓?and && 鎴栬€? or || 閫昏緫寮傛垨 xor
鑼冨洿鏌ヨ where 瀛楁 between 寮€濮嬪€?and缁撴潫鍊?br />娑堥櫎閲嶅鍊?distinct select distinct 瀛楁鍚?from 琛ㄥ悕
涔?鐮?set names gbk
鍒涘缓 create database 鏁版嵁搴撳悕锛?br />鏌ョ湅鏁版嵁搴?show databases;
閫夋嫨 use 鏁版嵁搴撳悕锛?br />鍒犻櫎 drop database 鏁版嵁搴撳悕锛?br />鍒涘缓琛? create table [if not exists] 琛ㄥ悕(瀛楁1 鏁版嵁绫诲瀷 [瀛楁灞炴€绾︽潫][绱㈠紩][娉ㄩ噴],...)[琛ㄧ被鍨媇[琛ㄥ瓧绗﹂泦][娉ㄩ噴];
涓婚敭 primary key
娉ㄩ噴 comment
鏌ョ湅琛?select * from 琛ㄥ悕
鏌ョ湅寤鸿〃璇彞 show create table 琛ㄥ悕;
瀛楁绾︽潫
闈炵┖绾︽潫 not null 瀛楁涓嶅彲浠ヤ负绌?蹇呭~
榛樿绾︽潫 default 璧嬩簣鏌愬瓧娈甸粯璁ゅ€?br />鍞竴绾︽潫 unique(uk)璁剧疆瀛楁鐨勫€兼槸鍞竴鐨?鍏佽涓虹┖锛屼絾鍙兘鏈変竴涓┖鍊?br />涓婚敭绾︽潫 peimary key(pk)璁剧疆璇ュ瓧娈典负琛ㄧ殑涓婚敭鍙敮涓€鏍囪瘑璇ヨ〃璁板綍
澶栭敭绾︽潫 foreign key(fk)鐢ㄤ簬鍦ㄤ袱琛ㄤ箣闂村缓绔嬪叧绯伙紝闇€瑕佹寚瀹氬紩鐢ㄤ富琛ㄧ殑鍝竴瀛楁
鑷姩澧為暱 auto_increment璁剧疆璇ュ垪涓鸿嚜澧炲瓧娈甸粯璁ゆ瘡鏉¤嚜澧?閫氬父鐢ㄤ簬璁剧疆涓婚敭
鏌ョ湅琛?show tables describe 琛ㄥ悕 desc 琛ㄥ悕
鍒犻櫎琛?drop table if exists 琛ㄥ悕锛?br />璁剧疆琛ㄧ殑寮曟搸 engine=myisam;
鏌ョ湅瀛樺偍寮曟搸 show engines g;
澹版槑鏃犵鍙锋暟 unsigned
浣嶆暟涓嶈冻浠?琛ヤ綅 zerofill
瀛楁瀹氫箟
淇敼琛ㄥ悕 alter table 鏃ц〃鍚?rename[to] 鏂拌〃鍚? 淇敼琛ㄧ殑瀹氫箟
娣诲姞瀛楁 alter table 琛ㄥ悕 add 瀛楁鍚? 鏁版嵁绫诲瀷 [灞炴€
淇敼瀛楁鐨勬暟鎹被鍨?alter table 琛ㄥ悕 modify 瀛楁鍚?鏁版嵁绫诲瀷
淇敼瀛楁鍚?alter table 琛ㄥ悕 change 鍘熷瓧娈靛悕 鏂板瓧娈靛悕 鏁版嵁绫诲瀷[灞炴€
淇敼瀛楁浣嶇疆 alter table 琛ㄥ悕 modify 瀛楁1 鏁版嵁绫诲瀷 first (瀛楁1涓虹涓€涓?||after 瀛楁2 (灏嗗瓧娈?鎻掔殑瀛楁2鍚庨潰)
鍒犻櫎瀛楁 alter table 琛ㄥ悕 drop 瀛楁鍚?br />鍒犻櫎琛ㄧ殑澶栭敭绾︽潫 alter table 琛ㄥ悕 drop foreign key 澶栭敭绾︽潫鍚?br />娣诲姞涓婚敭 alter table 琛ㄥ悕 add constraint 绾︽潫鍚嶏紙涓婚敭锛?primary key 琛ㄥ悕(涓婚敭瀛楁)
娣诲姞澶栭敭 alter table 琛ㄥ悕(浠庤〃) add constraint 澶栭敭鍚?foreign key(澶栭敭瀛楁) references 鍏宠仈琛ㄥ悕:涓昏〃(鍏宠仈瀛楁):涓昏〃瀛楁
娣诲姞澶栭敭锛歝onstraint 澶栭敭鍚?foreign key(澶栭敭瀛楁) references 鍏宠仈琛ㄥ悕(鍏宠仈瀛楁);
ALTER TABLE t_student ADD CONSTRAINT t_cname1 FOREIGN KEY(cname) REFERENCES t_class(NAME);
t_comment 鏄瘎璁鸿〃锛宯_id 鏄叾瀛楁锛屼负n_id 娣诲姞澶栭敭锛屽搴旂殑鍏宠仈琛ㄦ槸t_news.
ALTER TABLE t_comment ADD CONSTRAINT n_id FOREIGN KEY(n_id) REFERENCES t_news(id)

鏁版嵁鎿嶄綔
鎻掑叆鏁版嵁(鍗? insert into 琛ㄥ悕[(瀛楁鍚嶅垪琛?] values(鍊煎垪琛?;
鎻掑叆鏁版嵁(澶? insert into 琛ㄥ悕[(瀛楁鍚嶅垪琛?](鍙笉鍐? values(鍊煎垪琛?),(鍊煎垪琛?),(鍊煎垪琛?)....;
鏁版嵁鏇存柊 update set 瀛楁1=鍊?,瀛楁2=鍊?...[where鏉′欢]
寤轰竴涓柊琛ㄥ苟澶嶅埗鏁版嵁 create table 鏂拌〃鍚?as select * from 琚鍒惰〃鍚?where;
閫氳繃缁撴灉闆嗘柟寮忔彃鍏?insert into 闇€瑕佸鍒舵暟鎹殑琛ㄥ悕 select*from 琚鍒惰〃鍚?br />娓呯┖琛╠elete from 琛ㄥ悕(鏄€愯鍒犻櫎锛屼笉浼氬垹闄よ〃缁撴瀯锛屼絾鏄篃涓嶄細閲嶇疆鑷);truncate table 琛ㄥ悕(娓呯┖琛紝,蹇紝浼氶噸缃嚜澧?;drop table 琛ㄥ悕(鐩存帴鍒犻櫎琛ㄧ粨鏋?
鍒犻櫎鏁版嵁(璁板綍) delete from 琛ㄥ悕 [where 鏉′欢]
淇敼鏁版嵁(璁板綍) update 琛ㄥ悕 set sss=ssss where sss;
鏁版嵁鏌ヨ 
select 瀛楁鍚?from 琛ㄥ悕 where 鏉′欢 group by 鍒嗙粍鐨勫瓧娈靛悕 having 锛堝垎缁勭瓫閫夋潯浠讹級鏉′欢 order by 鎺掑簭鐨勫瓧娈靛悕 姝e簭asc 鍊掑簭desc
select 瀛楁鍚嶏紙鐢紝鍒嗗壊瀛楁鍚嶏級 from 琛ㄥ悕 where 鏉′欢 group by 鍒嗙粍鐨勫瓧娈靛悕 having 锛堝垎缁勭瓫閫夋潯浠讹級鏉′欢 order by 鎺掑簭鐨勫瓧娈靛悕
姝e簭asc 鍊掑簭desc limit 浣嶇疆鍋忕Щ閲忥紙榛樿鍙互涓嶅啓锛屼粠0寮€濮嬶級 琛屾暟锛?br />鍑芥暟
鑱氬悎鍑芥暟
select 鍑芥暟锛堝瓧娈碉級 as 涓枃 from 琛ㄥ悕锛?br />avg() 杩斿洖鏌愬瓧娈电殑骞冲潎鍊?br />count() 杩斿洖鏌愬瓧娈电殑琛屾暟
max() 杩斿洖鏌愬瓧娈电殑鏈€澶у€?br />min() 杩斿洖鏌愬瓧娈电殑鏈€灏忓€?br />sum() 杩斿洖鏌愬瓧娈电殑鍜?br />瀛楃涓插嚱鏁?/em>
concat(鈥榮tr1鈥?鈥榮tr2鈥?..) 瀛楃涓茶繛鎺?br />insert(鈥樺師瀛楃鈥?璧峰浣嶇疆,缁堟浣嶇疆,鈥樻浛鎹㈠瓧绗︹€? 瀛楃涓叉浛鎹?br />lower(鈥榮tr鈥? 灏嗗瓧绗︿覆杞负灏忓啓
upper(鈥榮tr鈥? 灏嗗瓧绗︿覆杞负澶у啓
substring(瀛楃,璧峰浣嶇疆,缁堟浣嶇疆) 瀛楃涓叉埅鍙?br />鏃ユ湡鏃堕棿鍑芥暟
select 鍑芥暟锛堝璞★級锛?br />curdate() 鑾峰彇褰撳墠鏃堕棿骞存湀鏃?br />curtime() 鑾峰彇褰撳墠鏃堕棿鏃跺垎绉?br />now() 鑾峰彇褰撳墠鏃ユ湡鍜屾椂闂?br />week(date) 杩斿洖鏃ユ湡date涓轰竴骞寸殑绗嚑鍛?br />year(date) 杩斿洖鏃ユ湡date鐨勫勾浠?br />month锛坉ate锛? 杩斿洖鏃ユ湡鐨勫ぉ鏁?br />hour(time) 杩斿洖鏃堕棿time鐨勫皬鏃跺€?br />minute(time) 杩斿洖鏃堕棿time鐨勫垎閽熷€?br />datediff(date1,date2) 杩斿洖鏃ユ湡鍙傛暟1鍜?涔嬮棿鐨勫ぉ鏁?br />adddate(date,n) 璁$畻鏃ユ湡鍙傛暟date鍔犱笂n澶╁悗鐨勬棩鏈?br />鏁板鍑芥暟
ceil(x) 杩斿洖澶т簬鎴栫瓑浜庢暟鍊紉鐨勬渶灏忔暣鏁?br />floor(x) 杩斿洖灏忎簬鎴栫瓑浜庢暟鍊紉鐨勬渶澶ф暣鏁?br />rand() 杩斿洖0~1涔嬮棿鐨勯殢鏈烘暟
瀛愭煡璇?/strong>
select ...from 琛? where 瀛楁1 姣旇緝杩愮畻绗?瀛愭煡璇?:鐢ㄤ簬宓屽鍦╯elect,insert,update,delete 璇彞涓?br />鍐呰繛鎺?绛夊€艰繛鎺?
inner join 杩炴帴鐨勮〃鍚?on 杩炴帴鏉′欢 锛堢瓑鍊艰繛鎺ワ級
in瀛愭煡璇?鏌ヨ缁撴灉涓哄澶存槸鍙互鐢╥n浠f浛= 姣旇緝杩愮畻绗?鍙兘杩斿洖鍗曚釜鏁板€?br />not in 鍙嶈浆 鏌ヨ缁撴灉涓嶄负澶氭潯鐨?br />exists瀛愭煡璇?select ...from 琛ㄥ悕 where exists(瀛愭煡璇㈣鍙?瀛愭煡璇㈢粨鏋滀负true鏃讹紝鎵ц澶栧眰浠g爜 涓篺alse鏃跺灞傛煡璇笉鎵ц
not exists 瀛愭煡璇㈢粨鏋滀笉涓簍rue鏃舵墽琛?br />宓屽瀛愭煡璇㈠寘鎷瑂elect from 锛坵here group having 鍙€夊瓙鍙?锛夌瓑瀛愬彞
涓存椂琛?create temporary table 琛ㄥ悕锛堟煡璇㈣鍙ワ級

浜嬪姟
浜嬪姟鏄綔涓哄崟涓€昏緫宸ヤ綔鍗曞厓鎵ц鐨勪竴绯诲垪鎿嶄綔
澶氫釜鎿嶄綔浣滀负涓€涓暣浣撳悜绯荤粺鎻愪氦锛岃涔堥兘鎵ц锛岃涔堥兘涓嶆墽琛?br />浜嬪姟鏄笉鍙垎鍓茬殑宸ヤ綔閫昏緫鍗曞厓
浜嬪姟鍏锋湁鍥涗釜灞炴€cid
鍘熷瓙鎬э細涓€涓畬鏁寸殑鎿嶄綔锛岃涔堝叏閮ㄦ墽琛屽畬姣曪紝瑕佷箞閮戒笉鎵ц
涓€鑷存€э細浜嬪姟瀹屾垚鍓嶅悗锛屾暟鎹淇濇寔涓€鑷寸姸鎬?br />闅旂鎬э細浜嬪姟涓庝簨鍔″湪鍚屾椂杩愯鏃讹紝涓嶄緷璧栧奖鍝嶅叾浠栦簨鍔?br />鎸佷箙鎬э細浜嬪姟缁撴潫鍚庯紝鎻愪氦鍒版暟鎹簱涓殑淇敼浼氳姘镐箙淇濆瓨
寮€濮嬩簨鍔egin 鎴杝tart transaction 鎻愪氦浜嬪姟 commit 鍥炴粴浜嬪姟 rollback
浜嬪姟榛樿鐘舵€佹槸鑷姩鎻愪氦锛屽叧闂瓅寮€鍚痵et autocommit=0|1,鍏抽棴鍚庨渶瑕佹墜鍔ㄦ彁浜?/p>

瑙嗗浘
瑙嗗浘鏄竴寮犺櫄鎷熻〃锛岃〃绀轰竴寮犺〃鐨勯儴鍒嗘暟鎹垨澶氬紶琛ㄧ殑缁煎悎鏁版嵁锛岀粨鏋勫拰鏁版嵁鏄缓绔嬪湪瀵硅〃鐨勬煡璇㈠熀纭€涓?br />锛岃鍥句笉瀛樻斁鏁版嵁锛屼粎浠呮槸寮曠敤鍘熷琛ㄤ腑鐨勬暟鎹紝涓€涓〃鍙互鍒涘缓澶氫釜瑙嗗浘
瑙嗗浘鍙互鐢ㄦ潵绛涢€夎〃涓殑琛岋紝闃叉鏈巿鏉冪敤鎴疯闂暟鎹紝闄嶄綆鏁版嵁搴撶殑澶嶆潅绋嬪害锛屾妸澶氫釜鐗╃悊鏁版嵁搴撴娊璞′负涓€涓€昏緫鏁版嵁搴?br />鍒涘缓瑙嗗浘 create view view_name(瀛楁鍒悕) as select 瀛楁鍚?from 琛ㄥ悕(鍙互鐢ㄦ潵淇敼瑙嗗浘)
淇敼瑙嗗浘 alter view 瑙嗗浘 select 瀛楁鍚?from 琛ㄥ悕
鏇存柊瑙嗗浘 update 瑙嗗浘鍚?set 瀛楁閲嶆柊璧嬪€?br />鏌ョ湅瀹氫箟 desc 瑙嗗浘鍚?
鍒犻櫎瑙嗗浘 drop view if exists view_name
鏌ョ湅瑙嗗浘 select 瀛楁1锛屽瓧娈?... from view_name 鏌ョ湅瑙嗗浘淇℃伅 show table status like鈥樿鍥惧悕鈥榎g鏌ョ湅璇︾粏淇℃伅show create view 瑙嗗浘鍚峔g 鏌ョ湅鎵€鏈夎鍥?select*from information_schema.views;
鏌ョ湅鎵€鏈夎鍥?use information_schema select*from viewsg;

绱㈠紩
鍒涘缓绱㈠紩 create [unique 鍞竴绱㈠紩| fulltext鍏ㄦ枃绱㈠紩| spatial 绌洪棿绱㈠紩] index_name on table_name(columm_name[length]..);
鍒犻櫎绱㈠紩 drop index index_name on table_name 璇ヨ〃鐨勬墍鏈夌储寮曢兘鍚屾椂琚垹闄?br />鏌ョ湅绱㈠紩 show index from table_name
鏌ョ湅搴撲腑鐨勫叏閮ㄧ储寮?use 鏁版嵁搴?show index from studentg

鏁版嵁搴撳浠芥仮澶?/strong>
mysqldump鍛戒护 mysql澶囦唤宸ュ叿灏哻reate鍜宨nsert into璇彞淇濆瓨鍒版枃鏈枃浠?灞炰簬dos鍛戒护
mysqldump [options 閫夐」鍙傛暟] -u username 鐢ㄦ埛鍚?-h host 鐧诲綍涓绘満鍚?-p password瀵嗙爜
dbname澶囦唤鐨勬暟鎹簱鍚?[tbname1澶囦唤鐨勮〃鍚?涓嶅啓鏄叏閮╙>filen.sql澶囦唤鍚庣殑鏂囦欢鍚嶇О
鎭㈠鏁版嵁搴?mysql -u root -p 鏁版嵁搴撳悕 <涔嬪墠澶囦唤鐨勬枃浠跺悕 (DOS鍛戒护,闇€瑕佸垱寤烘柊鐨勬暟鎹簱鍚庝娇鐢?
鎭㈠鏁版嵁搴?source filename 鍦╩ysql鏈嶅姟涓娇鐢ㄩ渶鍒涘缓鏂版暟鎹簱骞堕€夋嫨鍚庝娇鐢?br />瀵煎嚭浠呮暟鎹俊鎭殑鐩爣鏂囦欢select*from 琛ㄥ悕 where 鏉′欢 into outfile 纭洏璺緞鏂囨湰鏂囦欢
瀵煎叆鏁版嵁鏂囦欢 load data infile 涔嬪墠瀵煎嚭鐨勬枃鏈枃浠?into table 琛ㄥ悕
甯哥敤鍙傛暟閫夐」
mysql鏁版嵁绫诲瀷

鏁板瓧
tinyint 闈炲父灏忕殑鏁? 鏈夌鍊?-128锛?27) 鏃犵鍊?0锛?55) 1瀛楄妭
smallint 杈冨皬鐨勬暟鎹? 鏈夌鍊?-32 768锛?2 767) 鏃犵鍊?(0锛?5 535) 2瀛楄妭
mediumint 涓瓑澶у皬鐨勬暟鎹? 鏈夌鍊?(-8 388 608锛? 388 607) 鏃犵鍊?(0锛?6 777 215) 3瀛楄妭
int 鏍囧噯鏁存暟 鏈夌鍊?-2 147 483 648锛? 147 483 647) 鏃犵鍊?0锛? 294 967 295) 4瀛楄妭
bigint 杈冨ぇ鐨勬暣鏁? 鏈夌鍊?-9,223,372,036,854,775,808锛? 223 372 036 854 775 807) 鏃犵鍊?0锛?8 446 744 073 709 551 615) 8瀛楄妭
float 鍗曠簿搴︽诞鐐瑰瀷 鏈夌鍊?-3.402 823 466 E+38锛?1.175 494 351 E-38)锛?锛?1.175 494 351 E-38锛?.402 823 466 351 E+38) 鏃犵鍊?锛?1.175 494 351 E-38锛?.402 823 466 E+38) 4瀛楄妭
double 鍙岀簿搴︽诞鐐瑰瀷 鏈夌鍊?-1.797 693 134 862 315 7 E+308锛?2.225 073 858 507 201 4 E-308)锛?锛?2.225 073 858 507 201 4 E-308锛?.797 693 134 862 315 7 E+308) 鏃犵鍊?0锛?2.225 073 858 507 201 4 E-308锛?.797 693 134 862 315 7 E+308) 8瀛楄妭
decimal(m,d) 瀛楃涓插瀷娴偣鏁? 鏈夌鍊间緷璧栦簬M鍜孌鐨勫€? 鏃犵鍊? m+2瀛楄妭 濡傛灉M>D锛屼负M+2鍚﹀垯涓篋+2
瀛楃
char[(m)] 瀹氶暱瀛楃涓? m瀛楄妭0-255
varchar[(m)]鍙彉瀛楃涓? 鍙敼鍙橀暱搴?-65535
tinytext 寰瀷鏂囨湰涓? 0-255
text 鏂囨湰涓? 0-65 535
mediumtext 0-16 777 215 bytes 涓瓑闀垮害鏂囨湰鏁版嵁
longtext 0-4 294 967 295 bytes 鏋佸ぇ鏂囨湰鏁版嵁
鏃ユ湡
date yyyy-mm-dd 1000-01-01~ 9999-12-31
datetime yy-mm-dd hh:mm:ss 1000-01-01 00锛?0锛?0 ~9999-12-31 23锛?9锛?9
time hh:mm:ss -835:59:59 ~ 838:59:59
*timestamp yyyymmddhhmmss 1970骞存煇鏃跺埢~2038骞存煇鏃跺埢锛岀簿搴︿负1绉?br />year yyyy 1901~2155

以上是关于mysql甯哥敤鍩虹鎸囦护澶у叏的主要内容,如果未能解决你的问题,请参考以下文章

Windows CMD鍛戒护澶у叏(鍊煎緱鏀惰棌)

Linux鍩虹

MySql 甯哥敤璇硶鎬荤粨

MySQL 甯哥敤鍛戒护鍜屽熀纭€璇硶

Linux甯哥敤鍛戒护

jvm鎸囦护