鍒涘缓绱㈠紩銆佷慨鏀圭储寮曘€佸垹闄ょ储寮曠殑鍛戒护璇彞
鍒涘缓绱㈠紩銆佷慨鏀圭储寮曘€佸垹闄ょ储寮曠殑鍛戒护璇彞
2012-05-07 11:53:09 | 1671娆¢槄璇?| 璇勮锛? 鏉?| itokit mysql绱㈠紩瀛︿範----2----鍒涘缓绱㈠紩銆佷慨鏀圭储寮曘€佸垹闄ょ储寮曠殑鍛戒护璇彞
鏌ョ湅琛ㄤ腑宸茬粡瀛樺湪 index锛歴how index from table_name;
鍒涘缓鍜屽垹闄ょ储寮?
绱㈠紩鐨勫垱寤哄彲浠ュ湪CREATE TABLE璇彞涓繘琛岋紝涔熷彲浠ュ崟鐙敤CREATE INDEX鎴朅LTER TABLE鏉ョ粰琛ㄥ鍔犵储寮曘€傚垹闄ょ储寮曞彲浠ュ埄鐢ˋLTER TABLE鎴朌ROP INDEX璇彞鏉ュ疄鐜般€?
锛?锛変娇鐢ˋLTER TABLE璇彞鍒涘缓绱㈠紩銆?
璇硶濡備笅锛?
alter table table_name add index index_name (column_list) ;
alter table table_name add unique (column_list) ;
alter table table_name add primary key (column_list) ;
鍏朵腑鍖呮嫭鏅€氱储寮曘€乁NIQUE绱㈠紩鍜孭RIMARY KEY绱㈠紩3绉嶅垱寤虹储寮曠殑鏍煎紡锛宼able_name鏄澧炲姞绱㈠紩鐨勮〃鍚嶏紝column_list鎸囧嚭瀵瑰摢浜涘垪杩涜绱㈠紩锛屽鍒楁椂鍚勫垪涔嬮棿鐢ㄩ€楀彿鍒嗛殧銆傜储寮曞悕index_name鍙€夛紝缂虹渷鏃讹紝MySQL灏嗘牴鎹涓€涓储寮曞垪璧嬩竴涓悕绉般€傚彟澶栵紝ALTER TABLE鍏佽鍦ㄥ崟涓鍙ヤ腑鏇存敼澶氫釜琛紝鍥犳鍙互鍚屾椂鍒涘缓澶氫釜绱㈠紩銆?
鍒涘缓绱㈠紩鐨勭ず渚嬪涓嬶細
mysql> use tpsc
Database changed
mysql> alter table tpsc add index shili (tpmc ) ;
Query OK, 2 rows affected (0.08 sec)
Records: 2 Duplicates: 0 Warnings: 0
锛?锛変娇鐢–REATE INDEX璇彞瀵硅〃澧炲姞绱㈠紩銆?
鑳藉澧炲姞鏅€氱储寮曞拰UNIQUE绱㈠紩涓ょ銆傚叾鏍煎紡濡備笅锛?
create index index_name on table_name (column_list) ;
create unique index index_name on table_name (column_list) ;
璇存槑锛歵able_name銆乮ndex_name鍜宑olumn_list鍏锋湁涓嶢LTER TABLE璇彞涓浉鍚岀殑鍚箟锛岀储寮曞悕涓嶅彲閫夈€傚彟澶栵紝涓嶈兘鐢–REATE INDEX璇彞鍒涘缓PRIMARY KEY绱㈠紩銆?
锛?锛夊垹闄ょ储寮曘€?
鍒犻櫎绱㈠紩鍙互浣跨敤ALTER TABLE鎴朌ROP INDEX璇彞鏉ュ疄鐜般€侱ROP INDEX鍙互鍦ˋLTER TABLE鍐呴儴浣滀负涓€鏉¤鍙ュ鐞嗭紝鍏舵牸寮忓涓嬶細
drop index index_name on table_name ;
alter table table_name drop index index_name ;
alter table table_name drop primary key ;
鍏朵腑锛屽湪鍓嶉潰鐨勪袱鏉¤鍙ヤ腑锛岄兘鍒犻櫎浜唗able_name涓殑绱㈠紩index_name銆傝€屽湪鏈€鍚庝竴鏉¤鍙ヤ腑锛屽彧鍦ㄥ垹闄RIMARY KEY绱㈠紩涓娇鐢紝鍥犱负涓€涓〃鍙彲鑳芥湁涓€涓狿RIMARY KEY绱㈠紩锛屽洜姝や笉闇€瑕佹寚瀹氱储寮曞悕銆傚鏋滄病鏈夊垱寤篜RIMARY KEY绱㈠紩锛屼絾琛ㄥ叿鏈変竴涓垨澶氫釜UNIQUE绱㈠紩锛屽垯MySQL灏嗗垹闄ょ涓€涓猆NIQUE绱㈠紩銆?
濡傛灉浠庤〃涓垹闄ゆ煇鍒楋紝鍒欑储寮曚細鍙楀奖鍝嶃€傚浜庡鍒楃粍鍚堢殑绱㈠紩锛屽鏋滃垹闄ゅ叾涓殑鏌愬垪锛屽垯璇ュ垪涔熶細浠庣储寮曚腑鍒犻櫎銆傚鏋滃垹闄ょ粍鎴愮储寮曠殑鎵€鏈夊垪锛屽垯鏁翠釜绱㈠紩灏嗚鍒犻櫎銆?
鍒犻櫎绱㈠紩鐨勬搷浣滐紝濡備笅闈㈢殑浠g爜锛?
mysql> drop index shili on tpsc ;
Query OK, 2 rows affected (0.08 sec)
Records: 2 Duplicates: 0 Warnings: 0
璇ヨ鍙ュ垹闄や簡鍓嶉潰鍒涘缓鐨勫悕绉颁负鈥渟hili鈥濈殑绱㈠紩銆?
===========================================
MYSQL绱㈠紩鏃犳晥鍜岀储寮曟湁鏁堢殑璇︾粏浠嬬粛锛岄渶瑕佺殑鏈嬪弸鍙互鍙傝€冧竴涓?
1銆乄HERE瀛楀彞鐨勬煡璇㈡潯浠堕噷鏈変笉绛変簬鍙凤紙WHERE column!=...锛夛紝MYSQL灏嗘棤娉曚娇鐢ㄧ储寮?
2銆佺被浼煎湴锛屽鏋淲HERE瀛楀彞鐨勬煡璇㈡潯浠堕噷浣跨敤浜嗗嚱鏁帮紙濡傦細WHERE DAY(column)=...锛夛紝MYSQL灏嗘棤娉曚娇鐢ㄧ储寮?
3銆佸湪JOIN鎿嶄綔涓紙闇€瑕佷粠澶氫釜鏁版嵁琛ㄦ彁鍙栨暟鎹椂锛夛紝MYSQL鍙湁鍦ㄤ富閿拰澶栭敭鐨勬暟鎹被鍨嬬浉鍚屾椂鎵嶈兘浣跨敤绱㈠紩锛屽惁鍒欏嵆浣垮缓绔嬩簡
绱㈠紩涔熶笉浼氫娇鐢?
4銆佸鏋淲HERE瀛愬彞鐨勬煡璇㈡潯浠堕噷浣跨敤浜嗘瘮杈冩搷浣滅LIKE鍜孯EGEXP锛孧YSQL鍙湁鍦ㄦ悳绱㈡ā鏉跨殑绗竴涓瓧绗︿笉鏄€氶厤绗︾殑鎯呭喌涓嬫墠鑳?
浣跨敤绱㈠紩銆傛瘮濡傝锛屽鏋滄煡璇㈡潯浠舵槸LIKE 'abc%',MYSQL灏嗕娇鐢ㄧ储寮曪紱濡傛灉鏉′欢鏄疞IKE '%abc'锛孧YSQL灏嗕笉浣跨敤绱㈠紩銆?
5銆佸湪ORDER BY鎿嶄綔涓紝MYSQL鍙湁鍦ㄦ帓搴忔潯浠朵笉鏄竴涓煡璇㈡潯浠惰〃杈惧紡鐨勬儏鍐典笅鎵嶄娇鐢ㄧ储寮曘€傚敖绠″姝わ紝鍦ㄦ秹鍙婂涓暟鎹〃鐨勬煡
璇㈤噷锛屽嵆浣挎湁绱㈠紩鍙敤锛岄偅浜涚储寮曞湪鍔犲揩ORDER BY鎿嶄綔鏂归潰涔熸病浠€涔堜綔鐢ㄣ€?
6銆佸鏋滄煇涓暟鎹垪閲屽寘鍚潃璁稿閲嶅鐨勫€硷紝灏辩畻涓哄畠寤虹珛浜嗙储寮曚篃涓嶄細鏈夊緢濂界殑鏁堟灉銆傛瘮濡傝锛屽鏋滄煇涓暟鎹垪閲屽寘鍚簡鍑€鏄?
浜涜濡傗€?/1鈥濇垨鈥淵/N鈥濈瓑鍊硷紝灏辨病鏈夊繀瑕佷负瀹冨垱寤轰竴涓储寮曘€?
7銆佺储寮曟湁鐢ㄧ殑鎯呭喌涓嬪氨澶浜嗐€傚熀鏈彧瑕佸缓绔嬩簡绱㈠紩锛岄櫎浜嗕笂闈㈡彁鍒扮殑绱㈠紩涓嶄細浣跨敤鐨勬儏鍐典笅涔嬪锛屽叾浠栨儏鍐靛彧瑕佹槸浣跨敤鍦?
WHERE鏉′欢閲岋紝ORDER BY 瀛楁锛岃仈琛ㄥ瓧娈碉紝涓€鑸兘鏄湁鏁堢殑銆?寤虹珛绱㈠紩瑕佺殑灏辨槸鏈夋晥鏋溿€?涓嶇劧杩樼敤瀹冨共鍚楋紵 濡傛灉涓嶈兘纭畾鍦?
=================================
MySQL绱㈠紩绫诲瀷涓€瑙?璁㎝ySQL楂樻晥杩愯璧锋潵
2009-10-15 17:51 浣氬悕 缃戠粶杞浇 瀛楀彿锛歍 | T
涓€閿敹钘忥紝闅忔椂鏌ョ湅锛屽垎浜ソ鍙嬶紒
鏈枃浠嬬粛浜嗕竷绉峂ySQL绱㈠紩绫诲瀷銆傚湪鏁版嵁搴撹〃涓紝瀵瑰瓧娈靛缓绔嬬储寮曞彲浠ュぇ澶ф彁楂樻煡璇㈤€熷害銆傞€氳繃鍠勭敤杩欎簺绱㈠紩锛屽彲浠ヤ护MySQL鐨勬煡璇㈠拰杩愯鏇村姞楂樻晥銆?
AD锛?1CTO瀛﹂櫌锛欼T绮惧搧璇剧▼鍦ㄧ嚎鐪嬶紒
绱㈠紩鏄揩閫熸悳绱㈢殑鍏抽敭銆侻ySQL绱㈠紩鐨勫缓绔嬪浜嶮ySQL鐨勯珮鏁堣繍琛屾槸寰堥噸瑕佺殑銆備笅闈粙缁嶅嚑绉嶅父瑙佺殑MySQL绱㈠紩绫诲瀷銆?
鍦ㄦ暟鎹簱琛ㄤ腑锛屽瀛楁寤虹珛绱㈠紩鍙互澶уぇ鎻愰珮鏌ヨ閫熷害銆傚亣濡傛垜浠垱寤轰簡涓€涓?mytable琛細
CREATE TABLE mytable(
ID INT NOT NULL,
username VARCHAR(16) NOT NULL
);
鎴戜滑闅忔満鍚戦噷闈㈡彃鍏ヤ簡10000鏉¤褰曪紝鍏朵腑鏈変竴鏉★細5555, admin銆?
鍦ㄦ煡鎵緐sername="admin"鐨勮褰?SELECT * FROM mytable