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甯哥敤鍩虹鎸囦护澶у叏的主要内容,如果未能解决你的问题,请参考以下文章