站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > MySQL 5.1参考手册中文版

Chapter聽11.聽Column Types - MySQL 5.1参考手册中文版

绗11绔狅細鍒楃被鍨

MySQL鏀寔澶氱鍒楃被鍨嬶細鏁板肩被鍨嬨佹棩鏈/鏃堕棿绫诲瀷鍜屽瓧绗︿覆(瀛楃)绫诲瀷銆傛湰绔犻鍏堝杩欎簺鍒楃被鍨嬭繘琛屼簡姒傝堪锛岀劧鍚庢洿鍔犺缁嗗湴鎻忚堪浜嗗悇绉嶅垪鐨勭被鍨嬶紝浠ュ強鍒楃被鍨嬪瓨鍌ㄩ渶姹傜殑鎬荤粨銆傛杩板緢绠鍗曘傚叧浜庡叿浣撳垪绫诲瀷鐨勮缁嗕俊鎭簲鏌ラ槄璇︾粏鐨勬弿杩帮紝渚嬪鎸囧畾鍊兼椂鍏佽浣跨敤鐨勬牸寮忋

MySQL鏀寔澶勭悊绌洪棿鏁版嵁鐨勬墿灞曞悕銆傚叧浜庣┖闂寸被鍨嬬殑淇℃伅鍙傝绗19绔狅細MySQL涓殑绌洪棿鎵╁睍

鍑犵鍒楃被鍨嬫弿杩颁娇鐢ㄤ簡涓嬭堪鎯緥锛

         M

琛ㄧず鏈澶ф樉绀哄搴︺傛渶澶ф湁鏁堟樉绀哄搴︽槸255

         D

閫傜敤浜庢诞鐐瑰拰瀹氱偣绫诲瀷锛屽苟琛ㄧず灏忔暟鐐瑰悗闈㈢殑浣嶆暟銆傛渶澶у彲鑳界殑鍊兼槸30锛屼絾涓嶅簲澶т簬M-2

         鏂规嫭鍙([鈥欏拰鈥])琛ㄧず鍙夐儴鍒嗐

11.1. 鍒楃被鍨嬫杩

11.1.1. 鏁板肩被鍨嬫杩

涓嬮潰涓烘暟鍊煎垪绫诲瀷鐨勬杩般傝缁嗕俊鎭弬瑙11.2鑺傦紝鈥滄暟鍊肩被鍨嬧銆傚垪瀛樺偍闇姹傚弬瑙11.5鑺傦紝鈥滃垪绫诲瀷瀛樺偍闇姹傗

M鎸囩ず鏈澶ф樉绀哄搴︺傛渶澶ф湁鏁堟樉绀哄搴︽槸255銆傛樉绀哄搴︿笌瀛樺偍澶у皬鎴栫被鍨嬪寘鍚殑鍊肩殑鑼冨洿鏃犲叧锛岀浉鍏虫弿杩拌11.2鑺傦紝鈥滄暟鍊肩被鍨嬧

濡傛灉涓轰竴涓暟鍊煎垪鎸囧畾ZEROFILLMySQL鑷姩涓鸿鍒楁坊鍔UNSIGNED灞炴с

SERIALBIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE鐨勪竴涓埆鍚嶃

鍦ㄦ暣鏁板垪瀹氫箟涓紝SERIAL DEFAULT VALUENOT NULL AUTO_INCREMENT UNIQUE鐨勪竴涓埆鍚嶃

璀﹀憡锛搴斿綋娓呮锛屽綋浣跨敤鍦ㄦ暣鏁板硷紙鍏朵腑涓涓槸UNSIGNED绫诲瀷锛変箣闂翠娇鐢ㄥ噺鍙锋椂锛岀粨鏋滄槸鏃犵鍙枫傚弬瑙12.8鑺傦紝鈥淐ast鍑芥暟鍜屾搷浣滅鈥

         BIT[(M)]

浣嶅瓧娈电被鍨嬨M琛ㄧず姣忎釜鍊肩殑浣嶆暟锛岃寖鍥翠负浠164銆傚鏋M琚渷鐣ワ紝 榛樿涓1

         TINYINT[(M)] [UNSIGNED] [ZEROFILL]

寰堝皬鐨勬暣鏁般傚甫绗﹀彿鐨勮寖鍥存槸-128127銆傛棤绗﹀彿鐨勮寖鍥存槸0255

         BOOLBOOLEAN

TINYINT(1)鐨勫悓涔夎瘝銆zero鍊艰瑙嗕负鍋囥傞潪zero鍊艰涓虹湡銆

鍦ㄥ皢鏉ワ紝灏嗘牴鎹爣鍑SQL寮曞叆瀹屽叏甯冨皵绫诲瀷鐨勫鐞嗐

         SMALLINT[(M)] [UNSIGNED] [ZEROFILL]

灏忕殑鏁存暟銆傚甫绗﹀彿鐨勮寖鍥存槸-3276832767銆傛棤绗﹀彿鐨勮寖鍥存槸065535

         MEDIUMINT[(M)] [UNSIGNED] [ZEROFILL]

涓瓑澶у皬鐨勬暣鏁般傚甫绗﹀彿鐨勮寖鍥存槸-83886088388607銆傛棤绗﹀彿鐨勮寖鍥存槸016777215

         INT[(M)] [UNSIGNED] [ZEROFILL]

鏅氬ぇ灏忕殑鏁存暟銆傚甫绗﹀彿鐨勮寖鍥存槸-21474836482147483647銆傛棤绗﹀彿鐨勮寖鍥存槸04294967295

         INTEGER[(M)] [UNSIGNED] [ZEROFILL]

杩欐槸INT鐨勫悓涔夎瘝銆

         BIGINT[(M)] [UNSIGNED] [ZEROFILL]

澶ф暣鏁般傚甫绗﹀彿鐨勮寖鍥存槸-92233720368547758089223372036854775807銆傛棤绗﹀彿鐨勮寖鍥存槸018446744073709551615

搴旀竻妤BIGINT鍒楃殑涓嬭堪鍐呭锛

o        浣跨敤甯︾鍙风殑BIGINTDOUBLE鍊艰繘琛屾墍鏈夌畻娉曪紝鍥犳闄や簡浣嶅嚱鏁帮紝涓嶅簲浣跨敤澶т簬9223372036854775807(63)鐨勬棤绗﹀彿鐨勫ぇ鏁存暟! 濡傛灉杩欐牱鍋氾紝缁撴灉涓殑鏈鍚庡嚑浣嶅彲鑳藉嚭閿欙紝杩欐槸鐢变簬灏BIGINT鍊艰浆鎹负DOUBLE杩涜鍥涜垗浜斿叆鏃堕犳垚鐨勯敊璇

MySQL鍙互鍦ㄤ互涓嬫儏鍐典笅澶勭悊BIGINT

         褰撲娇鐢ㄦ暣鏁板湪涓涓BIGINT鍒椾繚瀛樺ぇ鐨勬棤绗﹀彿鐨勫兼椂銆

         MIN(col_name)MAX(col_name)涓紝鍏朵腑col_nameBIGINT鍒椼

         浣跨敤鎿嶄綔绗(+-*绛夌瓑)骞朵笖涓や釜鎿嶄綔鏁板潎涓烘暣鏁版椂銆

o        鎬绘槸鍙互浣跨敤涓涓瓧绗︿覆鍦BIGINT鍒椾腑淇濆瓨涓ユ牸鏁存暟鍊笺傚湪杩欑鎯呭喌涓嬶紝MySQL鎵ц瀛楃涓-鏁板瓧杞崲锛屽叾闂翠笉瀛樺湪鍙岀簿搴﹁〃绀恒

o        褰撲袱涓搷浣滄暟鍧囦负鏁存暟鍊兼椂锛-+* 鎿嶄綔绗︿娇鐢BIGINT绠楁硶銆傝繖璇存槑濡傛灉涔樹袱涓ぇ鏁存暟(鎴栨潵鑷繑鍥炴暣鏁扮殑鍑芥暟)锛屽綋缁撴灉澶т簬9223372036854775807鏃讹紝浼氬緱鍒版剰鎯充笉鍒扮殑缁撴灉銆

         FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]

(鍗曠簿搴)娴偣鏁般傚厑璁哥殑鍊兼槸-3.402823466E+38-1.175494351E-3801.175494351E-383.402823466E+38銆傝繖浜涙槸鐞嗚闄愬埗锛屽熀浜IEEE鏍囧噯銆傚疄闄呯殑鑼冨洿鏍规嵁纭欢鎴栨搷浣滅郴缁熺殑涓嶅悓鍙兘绋嶅井灏忎簺銆

M鏄皬鏁扮旱浣嶆暟锛D鏄皬鏁扮偣鍚庨潰鐨勪綅鏁般傚鏋MD琚渷鐣ワ紝鏍规嵁纭欢鍏佽鐨勯檺鍒舵潵淇濆瓨鍊笺傚崟绮惧害娴偣鏁扮簿纭埌澶х害7浣嶅皬鏁颁綅銆

濡傛灉鎸囧畾UNSIGNED锛屼笉鍏佽璐熷笺

浣跨敤娴偣鏁板彲鑳戒細閬囧埌鎰忔兂涓嶅埌鐨勯棶棰橈紝鍥犱负鍦MySQL涓殑鎵鏈夎绠楃敤鍙岀簿搴﹀畬鎴愩傚弬瑙A.5.7鑺傦紝鈥滆В鍐充笌涓嶅尮閰嶈鏈夊叧鐨勯棶棰樷

         DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]

鏅氬ぇ灏(鍙岀簿搴)娴偣鏁般傚厑璁哥殑鍊兼槸-1.7976931348623157E+308-2.2250738585072014E-30802.2250738585072014E-3081.7976931348623157E+308銆傝繖浜涙槸鐞嗚闄愬埗锛屽熀浜IEEE鏍囧噯銆傚疄闄呯殑鑼冨洿鏍规嵁纭欢鎴栨搷浣滅郴缁熺殑涓嶅悓鍙兘绋嶅井灏忎簺銆

M鏄皬鏁版讳綅鏁帮紝D鏄皬鏁扮偣鍚庨潰鐨勪綅鏁般傚鏋MD琚渷鐣ワ紝鏍规嵁纭欢鍏佽鐨勯檺鍒舵潵淇濆瓨鍊笺傚弻绮惧害娴偣鏁扮簿纭埌澶х害15浣嶅皬鏁颁綅銆

濡傛灉鎸囧畾UNSIGNED锛屼笉鍏佽璐熷笺

         DOUBLE PRECISION[(M,D)] [UNSIGNED] [ZEROFILL], REAL[(M,D)] [UNSIGNED] [ZEROFILL]

DOUBLE鐨勫悓涔夎瘝銆傞櫎浜嗭細濡傛灉SQL鏈嶅姟鍣ㄦā寮忓寘鎷REAL_AS_FLOAT閫夐」锛REALFLOAT鐨勫悓涔夎瘝鑰屼笉鏄DOUBLE鐨勫悓涔夎瘝銆

         FLOAT(p) [UNSIGNED] [ZEROFILL]

娴偣鏁般p琛ㄧず绮惧害锛堜互浣嶆暟琛ㄧず锛夛紝浣MySQL鍙娇鐢ㄨ鍊兼潵纭畾鏄惁缁撴灉鍒楃殑鏁版嵁绫诲瀷涓FLOATDOUBLE銆傚鏋p涓轰粠024锛屾暟鎹被鍨嬪彉涓烘病鏈MD鍊肩殑FLOAT銆傚鏋p涓轰粠2553锛屾暟鎹被鍨嬪彉涓烘病鏈MD鍊肩殑DOUBLE銆傜粨鏋滃垪鑼冨洿涓庢湰鑺傚墠闈㈡弿杩扮殑鍗曠簿搴FLOAT鎴栧弻绮惧害DOUBLE鏁版嵁绫诲瀷鐩稿悓銆

FLOAT(p)璇硶涓ODBC鍏煎銆

         DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL]

鍘嬬缉鐨勨涓ユ牸鈥濆畾鐐规暟銆M鏄皬鏁颁綅鏁(绮惧害)鐨勬绘暟锛D鏄皬鏁扮偣(鏍囧害)鍚庨潰鐨勪綅鏁般傚皬鏁扮偣鍜(璐熸暟)鐨勨-鈥欑鍙蜂笉鍖呮嫭鍦M涓傚鏋D0锛屽垯鍊兼病鏈夊皬鏁扮偣鎴栧垎鏁伴儴鍒嗐DECIMAL鏁存暟鏈澶т綅鏁(M)65銆傛敮鎸佺殑鍗佽繘鍒舵暟鐨勬渶澶т綅鏁(D)30銆傚鏋D琚渷鐣ワ紝 榛樿鏄0銆傚鏋M琚渷鐣ワ紝 榛樿鏄10

濡傛灉鎸囧畾UNSIGNED锛屼笉鍏佽璐熷笺

鎵鏈DECIMAL鍒楃殑鍩烘湰璁$畻(+-*/)65浣嶇簿搴﹀畬鎴愩

         DEC[(M[,D])] [UNSIGNED] [ZEROFILL], NUMERIC[(M[,D])] [UNSIGNED] [ZEROFILL], FIXED[(M[,D])] [UNSIGNED] [ZEROFILL]

DECIMAL鐨勫悓涔夎瘝銆FIXED鍚屼箟璇嶉傜敤浜庝笌鍏跺畠鏈嶅姟鍣ㄧ殑鍏煎鎬с

11.1.2. 鏃ユ湡鍜屾椂闂寸被鍨嬫杩

鏈妭缁煎悎璁ㄨ浜嗕复鏃跺垪绫诲瀷銆傝缁嗕俊鎭紝鍙傝11.3鑺傦紝鈥滄棩鏈熷拰鏃堕棿绫诲瀷鈥銆傚垪瀛樺偍闇姹傚弬瑙11.5鑺傦紝鈥滃垪绫诲瀷瀛樺偍闇姹傗

         DATE

鏃ユ湡銆傛敮鎸佺殑鑼冨洿涓'1000-01-01''9999-12-31'MySQL'YYYY-MM-DD'鏍煎紡鏄剧ずDATE鍊硷紝浣嗗厑璁镐娇鐢ㄥ瓧绗︿覆鎴栨暟瀛椾负DATE鍒楀垎閰嶅笺

         DATETIME

鏃ユ湡鍜屾椂闂寸殑缁勫悎銆傛敮鎸佺殑鑼冨洿鏄'1000-01-01 00:00:00''9999-12-31 23:59:59'MySQL'YYYY-MM-DD HH:MM:SS'鏍煎紡鏄剧ずDATETIME鍊硷紝浣嗗厑璁镐娇鐢ㄥ瓧绗︿覆鎴栨暟瀛椾负DATETIME鍒楀垎閰嶅笺

         TIMESTAMP[(M)]

鏃堕棿鎴炽傝寖鍥存槸'1970-01-01 00:00:00'2037骞淬

TIMESTAMP鍒楃敤浜INSERTUPDATE鎿嶄綔鏃惰褰曟棩鏈熷拰鏃堕棿銆傚鏋滀綘涓嶅垎閰嶄竴涓硷紝琛ㄤ腑鐨勭涓涓TIMESTAMP鍒楄嚜鍔ㄨ缃负鏈杩戞搷浣滅殑鏃ユ湡鍜屾椂闂淬備篃鍙互閫氳繃鍒嗛厤涓涓NULL鍊硷紝灏TIMESTAMP鍒楄缃负褰撳墠鐨勬棩鏈熷拰鏃堕棿銆

TIMESTAMP鍊艰繑鍥炲悗鏄剧ず涓'YYYY-MM-DD HH:MM:SS'鏍煎紡鐨勫瓧绗︿覆锛屾樉绀哄搴﹀浐瀹氫负19涓瓧绗︺傚鏋滄兂瑕佽幏寰楁暟瀛楀硷紝搴斿湪TIMESTAMP 鍒楁坊鍔+0

娉ㄩ噴锛MySQL 4.1浠ュ墠浣跨敤鐨TIMESTAMP鏍煎紡鍦MySQL 5.1涓笉鏀寔锛涘叧浜庢棫鏍煎紡鐨勪俊鎭弬瑙MySQL 4.1 鍙傝冩墜鍐

         TIME

鏃堕棿銆傝寖鍥存槸'-838:59:59''838:59:59'MySQL'HH:MM:SS'鏍煎紡鏄剧ずTIME鍊硷紝浣嗗厑璁镐娇鐢ㄥ瓧绗︿覆鎴栨暟瀛椾负TIME鍒楀垎閰嶅笺

         YEAR[(2|4)]

涓や綅鎴栧洓浣嶆牸寮忕殑骞淬傞粯璁ゆ槸鍥涗綅鏍煎紡銆傚湪鍥涗綅鏍煎紡涓紝鍏佽鐨勫兼槸190121550000銆傚湪涓や綅鏍煎紡涓紝鍏佽鐨勫兼槸7069锛岃〃绀轰粠1970骞村埌2069骞淬MySQLYYYY 鏍煎紡鏄剧ずYEAR鍊硷紝浣嗗厑璁镐娇鐢ㄥ瓧绗︿覆鎴栨暟瀛椾负YEAR鍒楀垎閰嶅笺

11.1.3. 瀛楃涓茬被鍨嬫杩

鏈妭缁煎悎璁ㄨ浜嗗瓧绗︿覆鍒楃被鍨嬨傝缁嗕俊鎭弬瑙11.4鑺傦紝鈥淪tring绫诲瀷鈥銆傚垪瀛樺偍闇姹傚弬瑙11.5鑺傦紝鈥滃垪绫诲瀷瀛樺偍闇姹傗

鍦ㄦ煇浜涙儏鍐典腑锛MySQL鍙互灏嗕竴涓瓧绗︿覆鍒楁洿鏀逛负涓嶅悓浜CREATE TABLEALTER TABLE璇彞涓墍缁欏嚭鐨勭被鍨嬨傚弬瑙13.1.5.1鑺傦紝鈥滄矇瀵傜殑鍒楄鏍煎彉鏇粹

MySQL 5.1瀛楃涓叉暟鎹被鍨嬪寘鎷儴鍒嗗湪MySQL 4.1涔嬪墠鐨勭増鏈腑娌℃湁鐨勭壒鎬э細

         璁稿瀛楃涓叉暟鎹被鍨嬬殑鍒楀畾涔夊彲浠ュ寘鎷寚瀹氬瓧绗﹂泦鐨CHARACTER SET灞炴э紝涔熷彲鑳藉寘鎷牎瀵硅鍒欍(CHARSETCHARACTER SET鐨勪竴涓悓涔夎瘝锛杩欎簺灞炴ч傜敤浜CHARVARCHARTEXT绫诲瀷銆ENUMSET銆備緥濡傦細

                CREATE TABLE t
                (
                    c1 CHAR(20) CHARACTER SET utf8,
                    c2 CHAR(20) CHARACTER SET latin1 COLLATE latin1_bin
                );

璇ヨ〃瀹氫箟鍒涘缓浜嗕竴涓悕涓c1鐨勫垪锛屽叿鏈変竴涓utf8瀛楃闆嗗拰璇ュ瓧绗﹂泦鐨勯粯璁 鏍″瑙勫垯锛屽拰涓涓悕涓c2鐨勫垪浠ュ強latin1瀛楃闆嗗拰璇ュ瓧绗﹂泦鐨勪簩鍏 鏍″瑙勫垯銆備簩鍏冩牎瀵硅鍒欏澶у皬鍐欎笉鏁忔劅銆

         MySQL 5.1鐢ㄥ瓧绗﹀崟浣嶈В閲婂湪瀛楃鍒楀畾涔変腑鐨勯暱搴﹁鑼冦(浠ュ墠鐨勪竴浜MySQL鐗堟湰浠ュ瓧鑺傝В閲婇暱搴︼級

         瀵逛簬CHARVARCHARTEXT绫诲瀷锛BINARY灞炴у彲浠ヤ负鍒楀垎閰嶈鍒楀瓧绗﹂泦鐨 鏍″瑙勫垯銆

         瀛楃鍒楃殑鎺掑簭鍜屾瘮杈冨熀浜庡垎閰嶇粰鍒楃殑瀛楃闆嗐傚湪浠ュ墠鐨勭増鏈腑锛屾帓搴忓拰姣旇緝鍩轰簬鏈嶅姟鍣ㄥ瓧绗﹂泦鐨勬牎瀵硅鍒欍傚浜CHARVARCHAR 鍒楋紝浣犲彲浠ョ敤BINARY灞炴у0鏄庡垪璁╂帓搴忓拰 鏍″瑙勫垯浣跨敤褰撳墠鐨勫瓧绗︿唬鐮佸艰屼笉鏄瘝姹囬『搴忋

鍏充簬MySQL 5.1涓瓧绗﹂泦鐨勬敮鎸侊紝鍙傝绗10绔狅細瀛楃闆嗘敮鎸

         [NATIONAL] CHAR(M) [BINARY| ASCII | UNICODE]

鍥哄畾闀垮害瀛楃涓诧紝褰撲繚瀛樻椂鍦ㄥ彸渚у~鍏呯┖鏍间互杈惧埌鎸囧畾鐨勯暱搴︺M琛ㄧず鍒楅暱搴︺M鐨勮寖鍥存槸0255涓瓧绗︺

娉ㄩ噴锛褰撴绱CHAR鍊兼椂灏鹃儴绌烘牸琚垹闄ゃ

濡傛灉鎯宠灏嗘煇涓CHAR鐨勯暱搴﹁涓哄ぇ浜255锛屾墽琛岀殑CREATE TABLEALTER TABLE璇彞灏嗗け璐ュ苟鎻愮ず閿欒锛

mysql> CREATE TABLE c1 (col1 INT, col2 CHAR(500));
ERROR 1074 (42000): Column length too big for column 'col' (max = 255); use BLOB or TEXT instead
mysql> SHOW CREATE TABLE c1;
ERROR 1146 (42S02): Table 'test.c1' doesn't exist

CHARCHARACTER鐨勭畝鍐欍NATIONAL CHAR(鎴栧叾绛夋晥鐭舰寮NCHAR)鏄爣鍑嗙殑瀹氫箟CHAR鍒楀簲浣跨敤 榛樿瀛楃闆嗙殑SQL鏂规硶銆傝繖鍦MySQL涓负榛樿鍊笺

BINARY灞炴ф槸鎸囧畾鍒楀瓧绗﹂泦鐨勪簩鍏 鏍″瑙勫垯鐨勭畝鍐欍傛帓搴忓拰姣旇緝鍩轰簬鏁板煎瓧绗﹀笺

鍒楃被鍨CHAR BYTECHAR BINARY鐨勪竴涓埆鍚嶃傝繖鏄负浜嗕繚璇佸吋瀹规с

鍙互涓CHAR鎸囧畾ASCII灞炴с傚畠鍒嗛厤latin1瀛楃闆嗐

鍙互涓CHAR鎸囧畾UNICODE灞炴с傚畠鍒嗛厤ucs2瀛楃闆嗐

MySQL鍏佽鍒涘缓绫诲瀷CHAR(0)鐨勫垪銆傝繖涓昏鐢ㄤ簬蹇呴』鏈変竴涓垪浣嗗疄闄呬笂涓嶄娇鐢ㄥ肩殑鏃х増鏈殑搴旂敤绋嬪簭鐩稿吋瀹广傚綋浣犻渶瑕佸彧鑳藉彇涓や釜鍊肩殑鍒楁椂涔熷緢濂斤細娌℃湁瀹氫箟涓NOT NULL鐨勪竴涓CHAR(0)鍒楀彧鍗犵敤涓浣嶏紝鍙彲浠ュ彇鍊NULL''(绌哄瓧绗︿覆)

         CHAR

杩欐槸CHAR(1)鐨勫悓涔夎瘝銆

         [NATIONAL] VARCHAR(M) [BINARY]

鍙橀暱瀛楃涓层M 琛ㄧず鏈澶у垪闀垮害銆M鐨勮寖鍥存槸065,535(VARCHAR鐨勬渶澶у疄闄呴暱搴︾敱鏈闀跨殑琛岀殑澶у皬鍜屼娇鐢ㄧ殑瀛楃闆嗙‘瀹氥傛渶澶鏈夋晥闀垮害鏄65,532瀛楄妭锛

娉ㄩ噴锛MySQL 5.1閬典粠鏍囧噯SQL瑙勮寖锛屽苟涓斾笉鍒犻櫎VARCHAR鍊肩殑灏鹃儴绌烘牸銆

VARCHAR鏄瓧绗VARYING鐨勭畝鍐欍

BINARY灞炴ф槸鎸囧畾鍒楃殑瀛楃闆嗙殑浜屽厓 鏍″瑙勫垯鐨勭畝鍐欍傛帓搴忓拰姣旇緝鍩轰簬鏁板煎瓧绗﹀笺

VARCHAR淇濆瓨鏃剁敤涓涓瓧鑺傛垨涓や釜瀛楄妭闀跨殑鍓嶇紑+鏁版嵁銆傚鏋VARCHAR鍒楀0鏄庣殑闀垮害澶т簬255锛岄暱搴﹀墠缂鏄袱涓瓧鑺傘

         BINARY(M)

BINARY绫诲瀷绫讳技浜CHAR绫诲瀷锛屼絾淇濆瓨浜岃繘鍒跺瓧鑺傚瓧绗︿覆鑰屼笉鏄潪浜岃繘鍒跺瓧绗︿覆銆

         VARBINARY(M)

VARBINARY绫诲瀷绫讳技浜VARCHAR绫诲瀷锛屼絾淇濆瓨浜岃繘鍒跺瓧鑺傚瓧绗︿覆鑰屼笉鏄潪浜岃繘鍒跺瓧绗︿覆銆

         TINYBLOB

鏈澶ч暱搴︿负255(281)瀛楄妭鐨BLOB鍒椼

         TINYTEXT

鏈澶ч暱搴︿负255(281)瀛楃鐨TEXT鍒椼

         BLOB[(M)]

鏈澶ч暱搴︿负65,535(2161)瀛楄妭鐨BLOB鍒椼

鍙互缁欏嚭璇ョ被鍨嬬殑鍙夐暱搴M銆傚鏋滅粰鍑猴紝鍒MySQL灏嗗垪鍒涘缓涓烘渶灏忕殑浣嗚冻浠ュ绾M瀛楄妭闀跨殑鍊肩殑BLOB绫诲瀷銆

         TEXT[(M)]

鏈澶ч暱搴︿负65,535(2161)瀛楃鐨TEXT鍒椼

鍙互缁欏嚭鍙夐暱搴M銆傚垯MySQL灏嗗垪鍒涘缓涓烘渶灏忕殑浣嗚冻浠ュ绾M瀛楃闀跨殑鍊肩殑TEXT绫诲瀷銆

         MEDIUMBLOB

鏈澶ч暱搴︿负16,777,215(2241)瀛楄妭鐨BLOB鍒椼

         MEDIUMTEXT

鏈澶ч暱搴︿负16,777,215(2241)瀛楃鐨TEXT鍒椼

         LONGBLOB

鏈澶ч暱搴︿负4,294,967,2954GB(2321)瀛楄妭鐨BLOB鍒椼LONGBLOB鍒楃殑鏈澶鏈夋晥(鍏佽鐨)闀垮害鍙栧喅浜庡鎴风/鏈嶅姟鍣ㄥ崗璁腑閰嶇疆鏈澶у寘澶у皬鍜屽彲鐢ㄧ殑鍐呭瓨銆

         LONGTEXT

鏈澶ч暱搴︿负4,294,967,2954GB(2321)瀛楃鐨TEXT鍒椼LONGTEXT鍒楃殑鏈澶鏈夋晥(鍏佽鐨)闀垮害鍙栧喅浜庡鎴风/鏈嶅姟鍣ㄥ崗璁腑閰嶇疆鏈澶у寘澶у皬鍜屽彲鐢ㄧ殑鍐呭瓨銆

         ENUM('value1','value2',...)

鏋氫妇绫诲瀷銆傚彧鑳芥湁涓涓肩殑瀛楃涓诧紝浠庡煎垪'value1''value2'...NULL涓垨鐗规畩 ''閿欒鍊间腑閫夊嚭銆ENUM鍒楁渶澶氬彲浠ユ湁65,535涓埅鐒朵笉鍚岀殑鍊笺ENUM鍊煎湪鍐呴儴鐢ㄦ暣鏁拌〃绀恒

         SET('value1','value2',...)

涓涓缃傚瓧绗︿覆瀵硅薄鍙互鏈夐浂涓垨澶氫釜鍊硷紝姣忎釜鍊煎繀椤绘潵鑷垪鍊'value1''value2'...SET鍒楁渶澶氬彲浠ユ湁64涓垚鍛樸SET鍊煎湪鍐呴儴鐢ㄦ暣鏁拌〃绀恒

11.2. 鏁板肩被鍨

MySQL鏀寔鎵鏈夋爣鍑SQL鏁板兼暟鎹被鍨嬨傝繖浜涚被鍨嬪寘鎷弗鏍兼暟鍊兼暟鎹被鍨(INTEGERSMALLINTDECIMALNUMERIC)锛屼互鍙婅繎浼兼暟鍊兼暟鎹被鍨(FLOATREALDOUBLE PRECISION)銆傚叧閿瓧INTINTEGER鐨勫悓涔夎瘝锛屽叧閿瓧DECDECIMAL鐨勫悓涔夎瘝銆

BIT鏁版嵁绫诲瀷淇濆瓨浣嶅瓧娈靛硷紝骞朵笖鏀寔MyISAMMEMORYInnoDBBDB琛ㄣ

浣滀负SQL鏍囧噯鐨勬墿灞曪紝MySQL涔熸敮鎸佹暣鏁扮被鍨TINYINTMEDIUMINTBIGINT銆備笅闈㈢殑琛ㄦ樉绀轰簡闇瑕佺殑姣忎釜鏁存暟绫诲瀷鐨勫瓨鍌ㄥ拰鑼冨洿銆

绫诲瀷

瀛楄妭

鏈灏忓

鏈澶у

 

 

(甯︾鍙风殑/鏃犵鍙风殑)

(甯︾鍙风殑/鏃犵鍙风殑)

TINYINT

1

-128

127

 

 

0

255

SMALLINT

2

-32768

32767

 

 

0

65535

MEDIUMINT

3

-8388608

8388607

 

 

0

16777215

INT

4

-2147483648

2147483647

 

 

0

4294967295

BIGINT

8

-9223372036854775808

9223372036854775807

 

 

0

18446744073709551615

MySQL杩樻敮鎸侀夋嫨鍦ㄨ绫诲瀷鍏抽敭瀛楀悗闈㈢殑鎷彿鍐呮寚瀹氭暣鏁板肩殑鏄剧ず瀹藉害(渚嬪锛INT(4))銆傝鍙夋樉绀哄搴﹁瀹氱敤浜庢樉绀哄搴﹀皬浜庢寚瀹氱殑鍒楀搴︾殑鍊兼椂浠庡乏渚у~婊″搴︺

鏄剧ず瀹藉害骞朵笉闄愬埗鍙互鍦ㄥ垪鍐呬繚瀛樼殑鍊肩殑鑼冨洿锛屼篃涓嶉檺鍒惰秴杩囧垪鐨勬寚瀹氬搴︾殑鍊肩殑鏄剧ず銆

褰撶粨鍚堝彲閫夋墿灞曞睘鎬ZEROFILL浣跨敤鏃讹紝 榛樿琛ュ厖鐨勭┖鏍肩敤闆朵唬鏇裤備緥濡傦紝瀵逛簬澹版槑涓INT(5) ZEROFILL鐨勫垪锛屽4妫绱负00004銆傝娉ㄦ剰濡傛灉鍦ㄦ暣鏁板垪淇濆瓨瓒呰繃鏄剧ず瀹藉害鐨勪竴涓硷紝褰MySQL涓哄鏉傝仈鎺ョ敓鎴愪复鏃惰〃鏃朵細閬囧埌闂锛屽洜涓哄湪杩欎簺鎯呭喌涓MySQL鐩镐俊鏁版嵁閫傚悎鍘熷垪瀹藉害銆

鎵鏈夋暣鏁扮被鍨嬪彲浠ユ湁涓涓彲閫(闈炴爣鍑)灞炴UNSIGNED銆傚綋浣犳兂瑕佸湪鍒楀唴鍙厑璁搁潪璐熸暟鍜岃鍒楅渶瑕佽緝澶х殑涓婇檺鏁板艰寖鍥存椂鍙互浣跨敤鏃犵鍙峰笺

娴偣鍜屽畾鐐圭被鍨嬩篃鍙互涓UNSIGNED銆傚悓鏁扮被鍨嬶紝璇ュ睘鎬ч槻姝㈣礋鍊间繚瀛樺埌鍒椾腑銆傜劧鑰岋紝涓庢暣鏁扮被鍨嬩笉鍚岀殑鏄紝鍒楀肩殑涓婅寖鍥翠繚鎸佷笉鍙樸

濡傛灉涓轰竴涓暟鍊煎垪鎸囧畾ZEROFILLMySQL鑷姩涓鸿鍒楁坊鍔UNSIGNED灞炴с

瀵逛簬娴偣鍒楃被鍨嬶紝鍦MySQL涓崟绮惧害鍊间娇鐢4涓瓧鑺傦紝鍙岀簿搴﹀间娇鐢8涓瓧鑺傘

FLOAT绫诲瀷鐢ㄤ簬琛ㄧず杩戜技鏁板兼暟鎹被鍨嬨SQL鏍囧噯鍏佽鍦ㄥ叧閿瓧FLOAT鍚庨潰鐨勬嫭鍙峰唴閫夋嫨鐢ㄤ綅鎸囧畾绮惧害(浣嗕笉鑳戒负鎸囨暟鑼冨洿)MySQL杩樻敮鎸佸彲閫夌殑鍙敤浜庣‘瀹氬瓨鍌ㄥぇ灏忕殑绮惧害瑙勫畾銆023鐨勭簿搴﹀搴FLOAT鍒楃殑4瀛楄妭鍗曠簿搴︺2453鐨勭簿搴﹀搴DOUBLE鍒楃殑8瀛楄妭鍙岀簿搴︺

MySQL鍏佽浣跨敤闈炴爣鍑嗚娉曪細FLOAT(M,D)REAL(M,D)DOUBLE PRECISION(M,D)銆傝繖閲岋紝鈥(M,D)鈥濊〃绀鸿鍊间竴鍏辨樉绀M浣嶆暣鏁帮紝鍏朵腑D浣嶄綅浜庡皬鏁扮偣鍚庨潰銆備緥濡傦紝瀹氫箟涓FLOAT(7,4)鐨勪竴涓垪鍙互鏄剧ず涓-999.9999MySQL淇濆瓨鍊兼椂杩涜鍥涜垗浜斿叆锛屽洜姝ゅ鏋滃湪FLOAT(7,4)鍒楀唴鎻掑叆999.00009锛岃繎浼肩粨鏋滄槸999.0001

MySQLDOUBLE瑙嗕负DOUBLE PRECISION(闈炴爣鍑嗘墿灞)鐨勫悓涔夎瘝銆MySQL杩樺皢REAL瑙嗕负DOUBLE PRECISION(闈炴爣鍑嗘墿灞)鐨勫悓涔夎瘝锛岄櫎闈SQL鏈嶅姟鍣ㄦā寮忓寘鎷REAL_AS_FLOAT閫夐」銆

涓轰簡淇濊瘉鏈澶у彲鑳界殑鍙Щ妞嶆э紝闇瑕佷娇鐢ㄨ繎浼兼暟鍊兼暟鎹煎瓨鍌ㄧ殑浠g爜搴斾娇鐢FLOATDOUBLE PRECISION锛屼笉瑙勫畾绮惧害鎴栦綅鏁般

DECIMALNUMERIC绫诲瀷鍦MySQL涓涓虹浉鍚岀殑绫诲瀷銆傚畠浠敤浜庝繚瀛樺繀椤讳负纭垏绮惧害鐨勫硷紝渚嬪璐у竵鏁版嵁銆傚綋澹版槑璇ョ被鍨嬬殑鍒楁椂锛屽彲浠(骞朵笖閫氬父瑕)鎸囧畾绮惧害鍜屾爣搴︼紱渚嬪锛

salary DECIMAL(5,2)

鍦ㄨ渚嬪瓙涓紝5鏄簿搴︼紝2鏄爣搴︺傜簿搴﹁〃绀轰繚瀛樺肩殑涓昏浣嶆暟锛屾爣搴﹁〃绀哄皬鏁扮偣鍚庨潰鍙互淇濆瓨鐨勪綅鏁般

MySQL 5.1涓互浜岃繘鍒舵牸寮忎繚瀛DECIMALNUMERIC鍊笺

鏍囧噯SQL瑕佹眰salary鍒楄兘澶熺敤5浣嶆暣鏁颁綅鍜屼袱浣嶅皬鏁颁繚瀛樹换浣曞笺傚洜姝わ紝鍦ㄨ繖绉嶆儏鍐典笅鍙互淇濆瓨鍦salary鍒楃殑鍊肩殑鑼冨洿鏄粠-999.99999.99

鍦ㄦ爣鍑SQL涓紝璇硶DECIMAL(M)绛変环浜DECIMAL(M,0)銆傚悓鏍凤紝璇硶DECIMAL绛変环浜DECIMAL(M,0)锛屽彲浠ラ氳繃璁$畻纭畾M鐨勫笺傚湪MySQL 5.1涓敮鎸DECIMALNUMERIC鏁版嵁绫诲瀷鐨勫彉閲忓舰寮忋M榛樿鍊兼槸10

DECIMALNUMERIC鐨勬渶澶т綅鏁版槸65锛屼絾鍏蜂綋鐨DECIMALNUMERIC鍒楃殑瀹為檯鑼冨洿鍙楀叿浣撳垪鐨勭簿搴︽垨鏍囧害绾︽潫銆傚鏋滄绫诲垪鍒嗛厤鐨勫煎皬鏁扮偣鍚庨潰鐨勪綅鏁拌秴杩囨寚瀹氱殑鏍囧害鍏佽鐨勮寖鍥达紝鍊艰杞崲涓鸿鏍囧害銆(鍏蜂綋鎿嶄綔涓庢搷浣滅郴缁熸湁鍏筹紝浣嗕竴鑸粨鏋滃潎琚埅鍙栧埌鍏佽鐨勪綅鏁帮級

BIT鏁版嵁绫诲瀷鍙敤鏉ヤ繚瀛樹綅瀛楁鍊笺BIT(M)绫诲瀷鍏佽瀛樺偍M浣嶅笺M鑼冨洿涓164

瑕佹寚瀹氫綅鍊硷紝鍙互浣跨敤b'value'绗︺value鏄竴涓敤01缂栧啓鐨勪簩杩涘埗鍊笺備緥濡傦紝b'111'b'100000000'鍒嗗埆琛ㄧず7128銆傚弬瑙9.1.5鑺傦紝鈥滀綅瀛楁鍊尖

濡傛灉涓BIT(M)鍒楀垎閰嶇殑鍊肩殑闀垮害灏忎簬M浣嶏紝鍦ㄥ肩殑宸﹁竟鐢0濉厖銆備緥濡傦紝涓BIT(6)鍒楀垎閰嶄竴涓b'101'锛屽叾鏁堟灉涓庡垎閰b'000101'鐩稿悓銆

褰撹鍦ㄤ竴涓暟鍊煎垪鍐呬繚瀛樹竴涓秴鍑鸿鍒楀厑璁歌寖鍥寸殑鍊兼椂锛MySQL鐨勬搷浣滃彇鍐充簬姝ゆ椂鏈夋晥鐨SQL妯″紡銆傚鏋滄ā寮忔湭璁剧疆锛MySQL灏嗗艰鍓埌鑼冨洿鐨勭浉搴旂鐐癸紝骞朵繚瀛樿鍑忓ソ鐨勫笺備絾鏄紝濡傛灉妯″紡璁剧疆涓traditional(鈥滀弗鏍妯″紡)锛岃秴鍑鸿寖鍥寸殑鍊煎皢琚嫆缁濆苟鎻愮ず閿欒锛屽苟涓旀牴鎹SQL鏍囧噯鎻掑叆浼氬け璐ャ傚弬瑙5.3.2鑺傦紝鈥淪QL鏈嶅姟鍣ㄦā寮忊

濡傛灉INT鍒楁槸UNSIGNED锛屽垪鑼冨洿鐨勫ぇ灏忕浉鍚岋紝浣嗗叾绔偣浼氬彉涓哄埌04294967295銆傚鏋滀綘璇曞浘淇濆瓨-99999999999999999999锛屼互闈炰弗鏍兼ā寮忎繚瀛樺埌鍒椾腑鐨勫兼槸04294967296

濡傛灉鍦ㄦ诞鐐规垨瀹氱偣鍒椾腑鍒嗛厤鐨勫艰秴杩囨寚瀹(鎴栭粯璁)绮惧害鍜屾爣搴﹁瀹氱殑鑼冨洿锛MySQL浠ラ潪涓ユ牸妯″紡淇濆瓨琛ㄧず鑼冨洿鐩稿簲绔偣鐨勫笺

MySQL娌℃湁宸ヤ綔鍦ㄤ弗鏍兼ā寮忔椂锛屽浜ALTER TABLELOAD DATA INFILEUPDATE鍜屽琛INSERT璇彞锛岀敱浜庤鍓彂鐢熺殑杞崲灏嗘姤鍛婁负璀﹀憡銆傚綋MySQL宸ヤ綔鍦ㄤ弗鏍兼ā寮忔椂锛岃繖浜涜鍙ュ皢澶辫触锛屽苟涓旈儴鍒嗘垨鍏ㄩ儴鍊间笉浼氭彃鍏ユ垨鏇存敼锛屽彇鍐充簬鏄惁琛ㄤ负浜嬪姟琛ㄥ拰鍏跺畠鍥犵礌銆傝鎯呭弬瑙5.3.2鑺傦紝鈥淪QL鏈嶅姟鍣ㄦā寮忊

11.3. 鏃ユ湡鍜屾椂闂寸被鍨

琛ㄧず鏃堕棿鍊肩殑DATE鍜屾椂闂寸被鍨嬩负DATETIMEDATETIMESTAMPTIMEYEAR銆傛瘡涓椂闂寸被鍨嬫湁涓涓湁鏁堝艰寖鍥村拰涓涓鈥濆硷紝褰撴寚瀹氫笉鍚堟硶鐨MySQL涓嶈兘琛ㄧず鐨勫兼椂浣跨敤鈥鈥濆笺TIMESTAMP绫诲瀷鏈変笓鏈夌殑鑷姩鏇存柊鐗规э紝灏嗗湪鍚庨潰鎻忚堪銆

濡傛灉璇曞浘鎻掑叆涓涓笉鍚堟硶鐨勬棩鏈燂紝MySQL灏嗙粰鍑鸿鍛婃垨閿欒銆傚彲浠ヤ娇鐢ALLOW_INVALID_DATES SQL妯″紡璁MySQL鎺ュ彈鏌愪簺鏃ユ湡锛屼緥濡'1999-11-31'銆傚綋浣犳兂瑕佷繚瀛樹竴涓鍙兘閿欒鐨鈥濈敤鎴峰凡缁忓湪鏁版嵁搴撲腑鎸囧畾(渚嬪锛屼互web褰㈠紡)鐢ㄤ簬灏嗘潵澶勭悊鐨勫兼椂寰堟湁鐢ㄣ傚湪杩欑妯″紡涓嬶紝MySQL鍙獙璇佹湀鑼冨洿涓轰粠012锛屾棩鑼冨洿涓轰粠031銆傝繖浜涜寖鍥村彲浠ュ寘鎷浂锛屽洜涓MySQL鍏佽鍦DATEDATETIME鍒椾繚瀛樻棩/鏈堝拰鏃ユ槸闆剁殑鏃ユ湡銆傝繖鍦ㄥ簲鐢ㄧ▼搴忛渶瑕佷繚瀛樹竴涓綘涓嶇煡閬撶‘鍒囨棩鏈熺殑鐢熸棩鏃堕潪甯告湁鐢ㄣ傚湪杩欑鎯呭喌涓嬶紝鍙渶瑕佸皢鏃ユ湡淇濆瓨涓'1999-00-00''1999-01-00'銆傚鏋滀繚瀛樻绫绘棩鏈燂紝DATE_SUB()DATE_ADD绛夐渶瑕佸畬鏁存棩鏈熺殑鍑芥暟涓嶄細寰楀埌姝g‘鐨勭粨鏋溿(濡傛灉浣犱笉鎯冲湪鏃ユ湡涓嚭鐜伴浂锛屽彲浠ヤ娇鐢NO_ZERO_IN_DATE SQL妯″紡)

MySQL杩樺厑璁稿皢'0000-00-00'淇濆瓨涓衡滀吉鏃ユ湡鈥(濡傛灉涓嶄娇鐢NO_ZERO_DATE SQL妯″紡)銆傝繖鍦ㄦ煇浜涙儏鍐典笅姣斾娇鐢NULL鍊兼洿鏂逛究(骞朵笖鏁版嵁鍜岀储寮曞崰鐢ㄧ殑绌洪棿鏇村皬)

sql_mode绯荤粺鍙橀噺璁剧疆涓虹浉搴旀ā寮忓硷紝鍙互鏇寸‘鍒囦綘鎯宠MySQL鏀寔鍝鏃ユ湡銆傚弬瑙5.3.2鑺傦紝鈥淪QL鏈嶅姟鍣ㄦā寮忊

褰撲娇鐢ㄦ棩鏈熷拰鏃堕棿绫诲瀷鏃跺簲璁颁綇浠ヤ笅鍑犵偣锛

         MySQL浠ユ爣鍑嗚緭鍑烘牸寮忔绱㈢粰瀹氭棩鏈熸垨鏃堕棿绫诲瀷鐨勫硷紝浣嗗畠灏藉姏瑙i噴浣犳寚瀹氱殑鍚勭杈撳叆鍊兼牸寮(渚嬪锛屽綋浣犳寚瀹氫竴涓垎閰嶇粰鎴栦笌鏃ユ湡鎴栨椂闂寸被鍨嬭繘琛屾瘮杈冪殑鍊兼椂)銆傚彧鏀寔涓嬮潰绔犺妭涓弿杩扮殑鏍煎紡銆傛湡鏈涗綘鑳芥彁渚涙湁鏁堝笺傚鏋滀綘浣跨敤鍏跺畠鏍煎紡鐨勫间細鍙戠敓鎰忔兂涓嶅埌鐨勭粨鏋溿

         鍖呭惈涓や綅骞村肩殑鏃ユ湡浼氫护浜烘ā绯婏紝鍥犱负涓栫邯涓嶇煡閬撱MySQL浣跨敤浠ヤ笅瑙勫垯瑙i噴涓や綅骞村硷細

o        70-99鑼冨洿鐨勫勾鍊艰浆鎹负1970-1999

o        00-69鑼冨洿鐨勫勾鍊艰浆鎹负2000-2069

         灏界MySQL灏濊瘯瑙i噴鍑犵鏍煎紡鐨勫硷紝鏃ユ湡鎬绘槸浠ュ勾--鏃ラ『搴(渚嬪锛'98-09-04')锛岃屼笉鏄叾瀹冨湴鏂瑰父鐢ㄧ殑鏈--骞存垨鏃--骞撮『搴(渚嬪锛'09-04-98''04-09-98')

         濡傛灉鍊肩敤浜庢暟鍊间笂涓嬫枃涓紝MySQL鑷姩灏嗘棩鏈熸垨鏃堕棿绫诲瀷鐨勫艰浆鎹负鏁板瓧锛屽弽涔嬩害鐒躲

         MySQL閬囧埌涓涓棩鏈熸垨鏃堕棿绫诲瀷鐨勮秴鍑鸿寖鍥存垨瀵逛簬璇ョ被鍨嬩笉鍚堟硶鐨勫兼椂(濡傛湰鑺傚紑濮嬫墍鎻忚堪)锛屽畠灏嗚鍊艰浆鎹负璇ョ被鐨勨鈥濆笺備竴涓緥澶栨槸瓒呭嚭鑼冨洿鐨TIME鍊艰瑁佸壀鍒TIME鑼冨洿鐨勭浉搴旂鐐广

涓嬮潰鐨勮〃鏄剧ず浜嗗悇绫烩鈥濆肩殑鏍煎紡銆傝娉ㄦ剰濡傛灉鍚敤NO_ZERO_DATE SQL妯″紡锛屼娇鐢ㄨ繖浜涘间細浜х敓璀﹀憡銆

鍒楃被鍨

鈥濆

DATETIME

'0000-00-00 00:00:00'

DATE

'0000-00-00'

TIMESTAMP

00000000000000

TIME

'00:00:00'

YEAR

0000

         鈥濆兼槸鐗规畩鍊硷紝浣嗕綘鍙互浣跨敤琛ㄥ唴鏄剧ず鐨勫兼樉寮忎繚瀛樻垨寮曠敤瀹冧滑銆備綘涔熷彲浠ヤ娇鐢ㄥ'0'0鏉ヤ繚瀛樻垨寮曠敤锛屽啓璧锋潵鏇村鏄撱

         MyODBC涓娇鐢ㄧ殑鈥鈥濇棩鏈熸垨鏃堕棿鍊煎湪MyODBC 2.50.12鍜屼互涓婄増鏈腑琚嚜鍔ㄨ浆鎹负NULL锛屽洜涓ODBC涓嶈兘澶勭悊姝ょ被鍊笺

11.3.1. DATETIME銆丏ATE鍜孴IMESTAMP绫诲瀷

DATETIMEDATETIMESTAMP绫诲瀷鏄浉鍏崇殑銆傝鑺傛弿杩颁簡瀹冧滑鐨勭壒寰侊紝瀹冧滑鐨勭浉浼肩偣鍜屼笉鍚岀偣銆

褰撲綘闇瑕佸悓鏃跺寘鍚棩鏈熷拰鏃堕棿淇℃伅鐨勫兼椂鍒欎娇鐢DATETIME绫诲瀷銆MySQL'YYYY-MM-DD HH:MM:SS'鏍煎紡妫绱㈠拰鏄剧ずDATETIME鍊笺傛敮鎸佺殑鑼冨洿涓'1000-01-01 00:00:00''9999-12-31 23:59:59'(鏀寔鈥濊〃绀哄敖绠″厛鍓嶇殑鍊煎彲鑳藉伐浣滐紝浣嗘病鏈変繚璇)

褰撲綘鍙渶瑕佹棩鏈熷艰屼笉闇瑕佹椂闂撮儴鍒嗘椂搴斾娇鐢DATE绫诲瀷銆MySQL'YYYY-MM-DD'鏍煎紡妫绱㈠拰鏄剧ずDATE鍊笺傛敮鎸佺殑鑼冨洿鏄'1000-01-01''9999-12-31'

TIMESTAMP鍒楃被鍨嬬殑灞炴т笉鍥哄畾锛屽彇鍐充簬MySQL鐗堟湰鍜屾湇鍔″櫒杩愯鐨SQL妯″紡銆傝繖浜涘睘鎬у皢鍦ㄦ湰鑺傚悗闈㈡弿杩般

鍙互浣跨敤浠讳綍甯歌鏍煎紡鎸囧畾DATETIMEDATETIMESTAMP鍊硷細

         'YYYY-MM-DD HH:MM:SS''YY-MM-DD HH:MM:SS'鏍煎紡鐨勫瓧绗︿覆銆傚厑璁糕涓嶄弗鏍鈥濊娉曪細浠讳綍鏍囩偣绗﹂兘鍙互鐢ㄥ仛鏃ユ湡閮ㄥ垎鎴栨椂闂撮儴鍒嗕箣闂寸殑闂村壊绗︺備緥濡傦紝'98-12-31 11:30:45''98.12.31 11+30+45''98/12/31 11*30*45''98@12@31 11^30^45'鏄瓑浠风殑銆

         'YYYY-MM-DD''YY-MM-DD'鏍煎紡鐨勫瓧绗︿覆銆傝繖閲屼篃鍏佽浣跨敤鈥滀笉涓ユ牸鐨勨濊娉曘備緥濡傦紝'98-12-31''98.12.31''98/12/31''98@12@31'鏄瓑浠风殑銆

         'YYYYMMDDHHMMSS''YYMMDDHHMMSS'鏍煎紡鐨勬病鏈夐棿鍓茬鐨勫瓧绗︿覆锛屽亣瀹氬瓧绗︿覆瀵逛簬鏃ユ湡绫诲瀷鏄湁鎰忎箟鐨勩備緥濡傦紝'19970523091528''970523091528'琚В閲婁负'1997-05-23 09:15:28'锛屼絾'971122129015'鏄笉鍚堟硶鐨(瀹冩湁涓涓病鏈夋剰涔夌殑鍒嗛挓閮ㄥ垎)锛屽皢鍙樹负'0000-00-00 00:00:00'

         'YYYYMMDD''YYMMDD'鏍煎紡鐨勬病鏈夐棿鍓茬鐨勫瓧绗︿覆锛屽亣瀹氬瓧绗︿覆瀵逛簬鏃ユ湡绫诲瀷鏄湁鎰忎箟鐨勩備緥濡傦紝'19970523''970523'琚В閲婁负 '1997-05-23'锛屼絾'971332'鏄笉鍚堟硶鐨(瀹冩湁涓涓病鏈夋剰涔夌殑鏈堝拰鏃ラ儴鍒)锛屽皢鍙樹负'0000-00-00'

         YYYYMMDDHHMMSSYYMMDDHHMMSS鏍煎紡鐨勬暟瀛楋紝鍋囧畾鏁板瓧瀵逛簬鏃ユ湡绫诲瀷鏄湁鎰忎箟鐨勩備緥濡傦紝19830905132800830905132800琚В閲婁负 '1983-09-05 13:28:00'

         YYYYMMDDYYMMDD鏍煎紡鐨勬暟瀛楋紝鍋囧畾鏁板瓧瀵逛簬鏃ユ湡绫诲瀷鏄湁鎰忎箟鐨勩備緥濡傦紝19830905830905琚В閲婁负'1983-09-05'

         鍑芥暟杩斿洖鐨勭粨鏋滐紝鍏跺奸傚悎DATETIMEDATE鎴栬TIMESTAMP涓婁笅鏂囷紝渚嬪NOW()CURRENT_DATE

鏃犳晥DATETIMEDATE鎴栬TIMESTAMP鍊艰杞崲涓虹浉搴旂被鍨嬬殑鈥鈥濆('0000-00-00 00:00:00''0000-00-00'鎴栬00000000000000)

瀵逛簬鍖呮嫭鏃ユ湡閮ㄥ垎闂村壊绗︾殑瀛楃涓插硷紝濡傛灉鏃ュ拰鏈堢殑鍊煎皬浜10锛屼笉闇瑕佹寚瀹氫袱浣嶆暟銆'1979-6-9''1979-06-09'鏄浉鍚岀殑銆傚悓鏍凤紝瀵逛簬鍖呮嫭鏃堕棿閮ㄥ垎闂村壊绗︾殑瀛楃涓插硷紝濡傛灉鏃躲佸垎鍜岀鐨勫煎皬浜10锛屼笉闇瑕佹寚瀹氫袱浣嶆暟銆'1979-10-30 1:2:3''1979-10-30 01:02:03'鐩稿悓銆

鏁板瓧鍊煎簲涓6812鎴栬14浣嶉暱銆傚鏋滀竴涓暟鍊兼槸814浣嶉暱锛屽垯鍋囧畾涓YYYYMMDDYYYYMMDDHHMMSS鏍煎紡锛屽墠4浣嶆暟琛ㄧず骞淬傚鏋滄暟瀛 鏄612浣嶉暱锛屽垯鍋囧畾涓YYMMDDYYMMDDHHMMSS鏍煎紡锛屽墠2浣嶆暟琛ㄧず骞淬傚叾瀹冩暟瀛楄瑙i噴涓轰豢浣涚敤闆跺~鍏呭埌浜嗘渶杩戠殑闀垮害銆

鎸囧畾涓洪潪闄愬畾绗﹀瓧绗︿覆鐨勫间娇鐢ㄧ粰瀹氱殑闀垮害杩涜瑙i噴銆傚鏋滃瓧绗︿覆涓814瀛楃闀匡紝鍓4浣嶆暟琛ㄧず骞淬傚惁鍒欙紝鍓2浣嶆暟琛ㄧず骞淬備粠宸﹀悜鍙宠В閲婂瓧绗︿覆鍐呭嚭鐜扮殑鍚勯儴鍒嗭紝浠ュ彂鐜板勾銆佹湀銆佹棩銆佸皬鏃躲佸垎鍜岀鍊笺傝繖璇存槑涓嶅簲浣跨敤灏戜簬6瀛楃鐨勫瓧绗︿覆銆備緥濡傦紝濡傛灉浣犳寚瀹'9903'锛岃涓哄畠琛ㄧず19993鏈堬紝MySQL灏嗗湪浣犵殑琛ㄥ唴鎻掑叆涓涓鈥濇棩鏈熷笺傝繖鏄洜涓哄勾鍜屾湀鍊兼槸9903锛屼絾鏃ラ儴鍒嗗畬鍏ㄤ涪澶憋紝鍥犳璇ュ间笉鏄竴涓悎娉曠殑鏃ユ湡銆備絾鏄紝鍙互鏄庢樉鎸囧畾涓涓浂鍊兼潵浠h〃缂哄皯鐨勬湀鎴栨棩閮ㄥ垎銆備緥濡傦紝鍙互浣跨敤'990300'鏉ユ彃鍏ュ'1999-03-00'

鍦ㄤ竴瀹氱▼搴︿笂锛屽彲浠ュ皢涓涓棩鏈熺被鍨嬬殑鍊煎垎閰嶇粰涓涓笉鍚岀殑鏃ユ湡绫诲瀷銆備絾鏄紝鍊煎彲鑳戒細鏇存敼鎴栦涪澶变竴浜涗俊鎭細

         濡傛灉浣犱负涓涓DATETIMETIMESTAMP瀵硅薄鍒嗛厤涓涓DATE鍊硷紝缁撴灉鍊肩殑鏃堕棿閮ㄥ垎琚缃负'00:00:00'锛屽洜涓DATE鍊兼湭鍖呭惈鏃堕棿淇℃伅銆

         濡傛灉浣犱负涓涓DATE瀵硅薄鍒嗛厤涓涓DATETIMETIMESTAMP鍊硷紝缁撴灉鍊肩殑鏃堕棿閮ㄥ垎琚垹闄わ紝鍥犱负DATE鍊兼湭鍖呭惈鏃堕棿淇℃伅銆

         璁颁綇灏界鍙互浣跨敤鐩稿悓鐨勬牸寮忔寚瀹DATETIMEDATETIMESTAMP鍊硷紝涓嶅悓绫诲瀷鐨勫肩殑鑼冨洿鍗翠笉鍚屻備緥濡傦紝TIMESTAMP鍊间笉鑳芥棭浜1970鎴栨櫄浜2037銆傝繖璇存槑涓涓棩鏈燂紝渚嬪'1968-01-01'锛岃櫧鐒跺浜DATETIMEDATE鍊兼槸鏈夋晥鐨勶紝浣嗗浜TIMESTAMP鍊煎嵈鏃犳晥锛屽鏋滃垎閰嶇粰杩欐牱涓涓璞″皢琚浆鎹负0

褰撴寚瀹氭棩鏈熷兼椂璇锋敞鎰忔煇浜涚己闄凤細

         鎸囧畾涓哄瓧绗︿覆鐨勫煎厑璁哥殑闈炰弗鏍兼牸寮忓彲鑳戒細娆洪獥銆備緥濡傦紝鍊'10:11:12'鐢变簬鈥:鈥欓棿鍓茬鐪嬩笂鍘诲彲鑳借薄鏃堕棿鍊硷紝浣嗗鏋滅敤浜庢棩鏈熶笂涓嬫枃鍊煎垯琚В閲婁负骞'2010-11-12'銆傚'10:45:15'琚浆鎹负'0000-00-00'鍥犱负'45'涓嶆槸鍚堟硶鏈堛

         鍦ㄩ潪涓ユ牸妯″紡锛MySQL鏈嶅姟鍣ㄥ彧瀵规棩鏈熺殑鍚堟硶鎬ц繘琛屽熀鏈鏌ワ細骞淬佹湀鍜屾棩鐨勮寖鍥村垎鍒槸1000999900120031銆備换浣曞寘鍚秴鍑鸿繖浜涜寖鍥寸殑閮ㄥ垎鐨勬棩鏈熻杞崲鎴'0000-00-00'銆傝娉ㄦ剰浠嶇劧鍏佽浣犱繚瀛橀潪娉曟棩鏈燂紝渚嬪'2002-04-31'銆傝鎯崇‘淇濅笉浣跨敤涓ユ牸妯″紡鏃舵棩鏈熸湁鏁堬紝搴旀鏌ュ簲鐢ㄧ▼搴忋

鍦ㄤ弗鏍兼ā寮忥紝闈炴硶鏃ユ湡涓嶈鎺ュ彈锛屽苟涓斾笉杞崲銆

璇︾粏淇℃伅鍙傝5.3.2鑺傦紝鈥淪QL鏈嶅姟鍣ㄦā寮忊

         鍖呭惈涓や綅骞村肩殑鏃ユ湡浼氫护浜烘ā绯婏紝鍥犱负涓栫邯涓嶇煡閬撱MySQL浣跨敤浠ヤ笅瑙勫垯瑙i噴涓や綅骞村硷細

o        00-69鑼冨洿鐨勫勾鍊艰浆鎹负2000-2069

o        70-99鑼冨洿鐨勫勾鍊艰浆鎹负1970-1999

11.3.1.1.聽鑷狹ySQL 4.1浠ユ潵鐨凾IMESTAMP灞炴

娉ㄩ噴锛鏃х増鏈殑MySQL(4.1涔嬪墠)TIMESTAMP鍒楃被鍨嬬殑灞炴у湪璁稿鏂归潰浜庢湰鑺傛墍鎻忚堪鐨勫ぇ澶т笉鍚屻傚鏋滀綘闇瑕佸鏃х殑TIMESTAMP鏁版嵁杩涜杞寲浠ヤ究鍦MySQL 5.1涓伐浣滐紝璇︽儏璇峰弬瑙MySQL 4.1 鍙傝冩墜鍐

TIMESTAMP鍒楃殑鏄剧ず鏍煎紡涓DATETIME鍒楃浉鍚屻傛崲鍙ヨ瘽璇达紝鏄剧ず瀹藉害鍥哄畾鍦19瀛楃锛屽苟涓旀牸寮忎负YYYY-MM-DD HH:MM:SS

MySQL鏈嶅姟鍣ㄤ篃鍙互浠MAXDB妯″紡杩愯銆傚綋鏈嶅姟鍣ㄤ互璇ユā寮忚繍琛屾椂锛TIMESTAMPDATETIME鐩哥瓑銆備篃灏辨槸璇达紝濡傛灉鍒涘缓琛ㄦ椂鏈嶅姟鍣ㄤ互MAXDB妯″紡杩愯锛TIMESTAMP鍒楀垱寤轰负DATETIME鍒椼傜粨鏋滄槸锛岃鍒椾娇鐢DATETIME鏄剧ず鏍煎紡锛屾湁鐩稿悓鐨勫艰寖鍥达紝骞朵笖娌℃湁鑷姩瀵瑰綋鍓嶇殑鏃ユ湡鍜屾椂闂磋繘琛屽垵濮嬪寲鎴栨洿鏂般

瑕佹兂鍚敤MAXDB妯″紡锛屽湪鍚姩鏈嶅姟鍣ㄦ椂浣跨敤--sql-mode=MAXDB鏈嶅姟鍣ㄩ夐」鎴栧湪杩愯鏃堕氳繃璁剧疆鍏ㄥ眬sql_mode鍙橀噺灏SQL鏈嶅姟鍣ㄦā寮忚缃负MAXDB

mysql> SET GLOBAL sql_mode=MAXDB

瀹㈡埛绔彲浠ユ寜鐓т笅闈㈡柟娉曡鏈嶅姟鍣ㄤ负瀹冪殑杩炴帴浠MAXDB妯″紡杩愯锛

mysql> SET SESSION sql_mode=MAXDB;

 

MySQL涓嶆帴鍙楀湪鏃ユ垨鏈堝垪鍖呮嫭涓涓浂鎴栧寘鍚潪娉曟棩鏈熷肩殑鏃堕棿鎴冲笺傝瑙勫垯鐨勫敮涓渚嬪鏄壒娈婂'0000-00-00 00:00:00'

浣犲彲浠ラ潪甯哥伒渚垮湴纭畾浠涔堟椂鍊欏垵濮嬪寲鍜屾洿鏂TIMESTAMP鍜屽鍝簺鍒楄繘琛屽垵濮嬪寲鍜屾洿鏂帮細

         浣犲彲浠ュ皢褰撳墠鐨勬椂闂存埑鎸囧畾涓洪粯璁ゅ煎拰鑷姩鏇存柊鐨勫笺備絾鍙兘閫夋嫨涓涓紝鎴栬呬袱鑰呴兘涓嶉夈(涓嶅彲鑳戒竴涓垪閫夋嫨涓涓涓鸿屽彟涓涓垪閫夋嫨鍙︿竴涓涓猴級

         浣犲彲浠ユ寚瀹氬摢涓TIMESTAMP鍒楄嚜鍔ㄥ垵濮嬪寲鎴栨洿鏂颁负褰撳墠鐨勬棩鏈熷拰鏃堕棿銆備笉鍐嶉渶瑕佷负绗1TIMESTAMP鍒椼

璇锋敞鎰忎笅闈㈣璁烘墍淇℃伅鍙傜敤浜庡垱寤烘椂鏈惎鐢MAXDB妯″紡鐨勮〃鐨TIMESTAMP鍒椼(濡備笂鎵杩帮紝MAXDB妯″紡浣垮垪鍒涘缓涓DATETIME鍒楋級鎺у埗TIMESTAMP鍒楃殑鍒濆鍖栧拰鏇存柊鐨勮鍒欏涓嬫墍绀猴細

         濡傛灉涓涓〃鍐呯殑绗1TIMESTAMP鍒楁寚瀹氫负涓涓DEFAULT鍊硷紝鍒欎笉鑳藉拷鐣ャ 榛樿鍊煎彲浠ヤ负CURRENT_TIMESTAMP鎴栧父閲忔棩鏈熷拰鏃堕棿鍊笺

         DEFAULT NULL涓庣1TIMESTAMP 鍒楃殑DEFAULT CURRENT_TIMESTAMP鐩稿悓銆傚浜庡叾瀹TIMESTAMP鍒楋紝DEFAULT NULL琚涓DEFAULT 0

         琛ㄥ唴鐨勪换浣曚竴涓TIMESTAMP鍒楀彲浠ヨ缃负鑷姩鍒濆鍖栦负褰撳墠鏃堕棿鎴冲拰/鎴栨洿鏂般

         CREATE TABLE璇彞涓紝鍙互鐢ㄤ笅闈㈢殑浠讳綍涓绉嶆柟寮忓0鏄庣1TIMESTAMP鍒楋細

o        DEFAULT CURRENT_TIMESTAMPON UPDATE CURRENT_TIMESTAMP瀛愬彞锛屽垪涓洪粯璁ゅ间娇鐢ㄥ綋鍓嶇殑鏃堕棿鎴筹紝骞朵笖鑷姩鏇存柊銆

o        涓嶄娇鐢DEFAULTON UPDATE瀛愬彞锛屼笌DEFAULT CURRENT_TIMESTAMP ON UPDATECURRENT_TIMESTAMP鐩稿悓銆

o        DEFAULT CURRENT_TIMESTAMP瀛愬彞涓嶇敤ON UPDATE瀛愬彞锛屽垪涓洪粯璁ゅ间娇鐢ㄥ綋鍓嶇殑鏃堕棿鎴充絾鏄笉鑷姩鏇存柊銆

o        涓嶇敤DEFAULT瀛愬彞浣嗙敤ON UPDATE CURRENT_TIMESTAMP瀛愬彞锛屽垪鏈夐粯璁ゅ0骞惰嚜鍔ㄦ洿鏂般

o        鐢ㄥ父閲DEFAULT鍊硷紝鍒楁湁缁欏嚭鐨 榛樿鍊笺傚鏋滃垪鏈変竴涓ON UPDATE CURRENT_TIMESTAMP瀛愬彞锛屽畠鑷姩鏇存柊锛屽惁鍒欎笉銆

鎹㈠彞璇濊锛屼綘鍙互涓哄垵濮嬪煎拰鑷姩鏇存柊鐨勫间娇鐢ㄥ綋鍓嶇殑鏃堕棿鎴筹紝鎴栬呭叾涓竴涓娇鐢紝鎴栬呬袱涓殕涓嶄娇鐢ㄣ(渚嬪锛屼綘鍙互鎸囧畾ON UPDATE鏉ュ惎鐢ㄨ嚜鍔ㄦ洿鏂拌屼笉璁╁垪鑷姩鍒濆鍖栵級

         DEFAULTON UPDATE瀛愬彞涓彲浠ヤ娇鐢CURRENT_TIMESTAMPCURRENT_TIMESTAMP()鎴栬NOW()銆傚畠浠潎鍏锋湁鐩稿悓鐨勬晥鏋溿

涓や釜灞炴х殑椤哄簭骞朵笉閲嶈銆傚鏋滀竴涓TIMESTAMP鍒楀悓鏃舵寚瀹氫簡DEFAULTON UPDATE锛屼换浣曚竴涓彲浠ュ湪鍙︿竴涓殑鍓嶉潰銆

渚嬪瓙锛屼笅闈㈣繖浜涜鍙ユ槸绛夋晥鐨勶細

CREATE TABLE t (ts TIMESTAMP);
CREATE TABLE t (ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                             ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t (ts TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
                             DEFAULT CURRENT_TIMESTAMP);

         瑕佷负TIMESTAMP鍒楄屼笉鏄1鍒楁寚瀹氳嚜鍔ㄩ粯璁ゆ垨鏇存柊锛屽繀椤婚氳繃灏嗙1TIMESTAMP鍒楁樉寮忓垎閰嶄竴涓父閲DEFAULT鍊兼潵绂佺敤鑷姩鍒濆鍖栧拰鏇存柊銆(渚嬪锛DEFAULT 0DEFAULT'2003-01-01 00:00:00')銆傜劧鍚庯紝瀵逛簬鍏跺畠TIMESTAMP鍒楋紝瑙勫垯涓庣1TIMESTAMP鍒楃浉鍚岋紝渚嬪鎯呭喌鏄笉鑳藉拷鐣DEFAULTON UPDATE瀛愬彞銆傚鏋滆繖鏍峰仛锛屽垯涓嶄細鑷姩杩涜鍒濆鍖栨垨鏇存柊銆

渚嬪锛氫笅闈㈣繖浜涜鍙ユ槸绛夋晥鐨勶細

CREATE TABLE t (
    ts1 TIMESTAMP DEFAULT 0,
    ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                  ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t (
    ts1 TIMESTAMP DEFAULT 0,
    ts2 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
                  DEFAULT CURRENT_TIMESTAMP);

鍙互瀵规瘡涓繛鎺ヨ缃綋鍓嶇殑鏃跺尯锛岀浉鍏虫弿杩板弬瑙5.10.8鑺傦紝鈥淢ySQL鏈嶅姟鍣ㄦ椂鍖烘敮鎸佲TIMESTAMP鍊间互UTC鏍煎紡淇濆瓨锛屽瓨鍌ㄦ椂瀵瑰綋鍓嶇殑鏃跺尯杩涜杞崲锛屾绱㈡椂鍐嶈浆鎹㈠洖褰撳墠鐨勬椂鍖恒傚彧瑕佹椂鍖鸿瀹氬间负甯搁噺锛屼究鍙互寰楀埌淇濆瓨鏃剁殑鍊笺傚鏋滀繚瀛樹竴涓TIMESTAMP鍊硷紝搴旀洿鏀规椂鍖虹劧鍚庢绱㈣鍊硷紝瀹冧笌浣犱繚瀛樼殑鍊间笉鍚屻傝繖鏄洜涓哄湪涓や釜鏂瑰悜鐨勮浆鎹腑娌℃湁浣跨敤鐩稿悓鐨勬椂鍖恒傚綋鍓嶇殑鏃跺尯鍙互鐢ㄤ綔time_zone绯荤粺鍙橀噺鐨勫笺

鍙互鍦TIMESTAMP鍒楃殑瀹氫箟涓寘鎷NULL灞炴т互鍏佽鍒楀寘鍚NULL鍊笺備緥濡傦細

CREATE TABLE t
(
  ts1 TIMESTAMP NULL DEFAULT NULL,
  ts2 TIMESTAMP NULL DEFAULT 0,
  ts3 TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP
);

濡傛灉鏈寚瀹NULL灞炴э紝灏嗗垪璁剧疆涓NULL璁剧疆鍒欎細灏嗗畠璁剧疆涓哄綋鍓嶇殑鏃堕棿鎴炽傝娉ㄦ剰鍏佽NULL鍊肩殑TIMESTAMP鍒椾笉浼氶噰鐢ㄥ綋鍓嶇殑鏃堕棿鎴筹紝闄ら潪瑕佷箞鍏 榛樿鍊煎畾涔変负CURRENT_TIMESTAMP锛屾垨鑰NOW()CURRENT_TIMESTAMP琚彃鍏ュ埌璇ュ垪鍐呫傛崲鍙ヨ瘽璇达紝鍙湁浣跨敤濡備笅瀹氫箟鍒涘缓锛屽畾涔変负 NULLTIMESTAMP鍒楁墠浼氳嚜鍔ㄦ洿鏂帮細

CREATE TABLE t (ts NULLDEFAULT CURRENT_TIMESTAMP)

鍚﹀垯-涔熷氨鏄锛屽鏋滀娇鐢NULL鑰屼笉鏄DEFAULT TIMESTAMP鏉ュ畾涔TIMESTAMP鍒楋紝濡備笅鎵绀...

CREATE TABLE t1 (ts NULL DEFAULT NULL);
CREATE TABLE t2 (ts NULL DEFAULT '0000-00-00 00:00:00');

...鍒欏繀椤绘樉寮忔彃鍏ヤ竴涓搴斿綋鍓嶆棩鏈熷拰鏃堕棿鐨勫笺備緥濡傦細

INSERT INTO t1 VALUES (NOW());
INSERT INTO t2 VALUES (CURRENT_TIMESTAMP);

11.3.2. TIME绫诲瀷

MySQL'HH:MM:SS'鏍煎紡妫绱㈠拰鏄剧ずTIME(鎴栧浜庡ぇ鐨勫皬鏃跺奸噰鐢'HHH:MM:SS'鏍煎紡)TIME鍊肩殑鑼冨洿鍙互浠'-838:59:59''838:59:59'銆傚皬鏃堕儴鍒嗕細鍥犳澶х殑鍘熷洜鏄TIME绫诲瀷涓嶄粎鍙互鐢ㄤ簬琛ㄧず涓澶╃殑鏃堕棿(蹇呴』灏忎簬24灏忔椂)锛岃繕鍙兘涓烘煇涓簨浠惰繃鍘荤殑鏃堕棿鎴栦袱涓簨浠朵箣闂寸殑鏃堕棿闂撮殧(鍙互澶т簬24灏忔椂锛屾垨鑰呯敋鑷充负璐)

浣犲彲浠ョ敤鍚勭鏍煎紡鎸囧畾TIME鍊硷細

         'D HH:MM:SS.fraction'鏍煎紡鐨勫瓧绗︿覆銆傝繕鍙互浣跨敤涓嬮潰浠讳綍涓绉嶁闈炰弗鏍鈥濊娉曪細'HH:MM:SS.fraction''HH:MM:SS''HH:MM''D HH:MM:SS''D HH:MM''D HH''SS'銆傝繖閲D琛ㄧず鏃ワ紝鍙互鍙034涔嬮棿鐨勫笺傝娉ㄦ剰MySQL杩樹笉淇濆瓨鍒嗘暟銆

         'HHMMSS'鏍煎紡鐨勬病鏈夐棿鍓茬鐨勫瓧绗︿覆锛屽亣瀹氭槸鏈夋剰涔夌殑鏃堕棿銆備緥濡傦紝'101112'琚悊瑙d负'10:11:12'锛屼絾'109712'鏄笉鍚堟硶鐨(瀹冩湁涓涓病鏈夋剰涔夌殑鍒嗛挓閮ㄥ垎)锛屽皢鍙樹负'00:00:00'

         HHMMSS鏍煎紡鐨勬暟鍊硷紝鍋囧畾鏄湁鎰忎箟鐨勬椂闂淬備緥濡傦紝101112琚悊瑙d负'10:11:12'銆備笅闈㈡牸寮忎篃鍙互鐞嗚В锛SSMMSSHHMMSSHHMMSS.fraction銆傝娉ㄦ剰MySQL杩樹笉淇濆瓨鍒嗘暟銆

         鍑芥暟杩斿洖鐨勭粨鏋滐紝鍏跺奸傚悎TIME涓婁笅鏂囷紝渚嬪CURRENT_TIME

瀵逛簬鎸囧畾涓哄寘鎷椂闂撮儴鍒嗛棿鍓茬鐨勫瓧绗︿覆鐨TIME鍊硷紝濡傛灉鏃躲佸垎鎴栬呯鍊煎皬浜10锛屽垯涓嶉渶瑕佹寚瀹氫袱浣嶆暟銆'8:3:2''08:03:02'鐩稿悓銆

TIME鍒楀垎閰嶇畝鍐欏兼椂搴旀敞鎰忋傛病鏈夊啋鍙凤紝MySQL瑙i噴鍊兼椂鍋囧畾鏈鍙宠竟鐨勪袱浣嶈〃绀虹銆(MySQL瑙i噴TIME鍊间负杩囧幓鐨勬椂闂磋屼笉鏄綋澶╃殑鏃堕棿锛渚嬪锛屼綘鍙兘璁や负'1112'1112琛ㄧず'11:12:00'(11鐐硅繃12)锛屼絾MySQL灏嗗畠浠В閲婁负'00:11:12'(11鍒嗭紝12 )銆傚悓鏍凤紝'12'12 琚В閲婁负 '00:00:12'銆傜浉鍙嶏紝TIME鍊间腑浣跨敤鍐掑彿鍒欒偗瀹氳鐪嬩綔褰撳ぉ鐨勬椂闂淬備篃灏辨槸璇达紝'11:12'琛ㄧず'11:12:00'锛岃屼笉鏄'00:11:12'

瓒呭嚭TIME鑼冨洿浣嗗悎娉曠殑鍊艰瑁佷负鑼冨洿鏈鎺ヨ繎鐨勭鐐广備緥濡傦紝'-850:00:00''850:00:00'琚浆鎹负'-838:59:59''838:59:59'

鏃犳晥TIME鍊艰杞崲涓'00:00:00'銆傝娉ㄦ剰鐢变簬'00:00:00'鏈韩鏄竴涓悎娉TIME鍊硷紝鍙粠琛ㄥ唴淇濆瓨鐨勪竴涓'00:00:00'鍊艰繕涓嶈兘璇村嚭鍘熸潵鐨勫兼槸 '00:00:00'杩樻槸涓嶅悎娉曠殑鍊笺

11.3.3. YEAR绫诲瀷

YEAR绫诲瀷鏄竴涓崟瀛楄妭绫诲瀷鐢ㄤ簬琛ㄧず骞淬

MySQLYYYY鏍煎紡妫绱㈠拰鏄剧ずYEAR鍊笺傝寖鍥存槸19012155

鍙互鎸囧畾鍚勭鏍煎紡鐨YEAR鍊硷細

         鍥涗綅瀛楃涓诧紝鑼冨洿涓'1901''2155'

         鍥涗綅鏁板瓧锛岃寖鍥翠负19012155

         涓や綅瀛楃涓诧紝鑼冨洿涓'00''99''00''69''70''99'鑼冨洿鐨勫艰杞崲涓2000206919701999鑼冨洿鐨YEAR鍊笺

         涓や綅鏁存暟锛岃寖鍥翠负1991697099鑼冨洿鐨勫艰杞崲涓2001206919701999鑼冨洿鐨YEAR鍊笺傝娉ㄦ剰涓や綅鏁存暟鑼冨洿涓庝袱浣嶅瓧绗︿覆鑼冨洿绋嶆湁涓嶅悓锛屽洜涓轰綘涓嶈兘鐩存帴灏嗛浂鎸囧畾涓烘暟瀛楀苟灏嗗畠瑙i噴涓2000銆備綘蹇呴』灏嗗畠鎸囧畾涓轰竴涓瓧绗︿覆'0''00'鎴栧畠琚В閲婁负0000

         鍑芥暟杩斿洖鐨勭粨鏋滐紝鍏跺奸傚悎YEAR涓婁笅鏂囷紝渚嬪NOW()

闈炴硶YEAR鍊艰杞崲涓0000

11.3.4. Y2K浜嬪疁鍜屾棩鏈熺被鍨

MySQL鏈韩瀵逛簬2000(Y2K)鏄畨鍏ㄧ殑(鍙傝1.4.5鑺傦紝鈥2000骞村吋瀹规р)锛屼絾杈撳叆缁MySQL鐨勫煎彲鑳戒笉瀹夊叏銆備换浣曞寘鍚袱浣嶅勾鍊肩殑杈撳叆閮戒細浠や汉妯$硦锛屽洜涓轰笘绾笉鐭ラ亾銆傝繖浜涘煎繀椤昏В閲婁负鍥涗綅褰㈠紡锛屽洜涓MySQL鍐呴儴浣跨敤鍥涗綅鏉ヤ繚瀛樺勾銆

瀵逛簬DATETIMEDATETIMESTAMPYEAR绫诲瀷锛MySQL浣跨敤浠ヤ笅瑙勫垯瑙i噴鍚ā绯婂勾鍊肩殑鏃ユ湡锛

         00-69鑼冨洿鐨勫勾鍊艰浆鎹负2000-2069

         70-99鑼冨洿鐨勫勾鍊艰浆鎹负1970-1999

璇疯浣忚繖浜涜鍒欏彧鏄悎鐞嗙寽娴嬫暟鎹艰〃绀轰粈涔堛傚鏋MySQL浣跨敤鐨勫惎鍙戜笉鑳戒骇鐢熸纭殑鍊硷紝浣犲簲鎻愪緵鍖呭惈鍥涗綅骞村肩殑纭垏杈撳叆銆

ORDER BY鍙互姝g‘鎺掑簭鏈変袱浣嶅勾鐨TIMESTAMPYEAR鍊笺

閮ㄥ垎鍑芥暟濡MIN()MAX()TIMESTAMPYEAR杞崲涓轰竴涓暟瀛椼傝繖璇存槑浣跨敤鏈変袱浣嶅勾鍊肩殑鍊硷紝杩欎簺鍑芥暟涓嶈兘宸ヤ綔姝g‘銆傚湪杩欑鎯呭喌涓嬬殑淇鏂规硶鏄皢TIMESTAMPYEAR杞崲涓哄洓浣嶅勾鏍煎紡鎴栦娇鐢MIN(DATE_ADD(TIMESTAMP,INTERVAL 0 DAYS))

11.4. String绫诲瀷

瀛楃涓茬被鍨嬫寚CHARVARCHARBINARYVARBINARYBLOBTEXTENUMSET銆傝鑺傛弿杩颁簡杩欎簺绫诲瀷濡備綍宸ヤ綔浠ュ強濡備綍鍦ㄦ煡璇腑浣跨敤杩欎簺绫诲瀷銆

11.4.1. CHAR鍜孷ARCHAR绫诲瀷

CHARVARCHAR绫诲瀷绫讳技锛屼絾瀹冧滑淇濆瓨鍜屾绱㈢殑鏂瑰紡涓嶅悓銆傚畠浠殑鏈澶ч暱搴﹀拰鏄惁灏鹃儴绌烘牸琚繚鐣欑瓑鏂归潰涔熶笉鍚屻傚湪瀛樺偍鎴栨绱㈣繃绋嬩腑涓嶈繘琛屽ぇ灏忓啓杞崲銆

CHARVARCHAR绫诲瀷澹版槑鐨勯暱搴﹁〃绀轰綘鎯宠淇濆瓨鐨勬渶澶у瓧绗︽暟銆備緥濡傦紝CHAR(30)鍙互鍗犵敤30涓瓧绗︺

CHAR鍒楃殑闀垮害鍥哄畾涓哄垱寤鸿〃鏃跺0鏄庣殑闀垮害銆傞暱搴﹀彲浠ヤ负浠0255鐨勪换浣曞笺傚綋淇濆瓨CHAR鍊兼椂锛屽湪瀹冧滑鐨勫彸杈瑰~鍏呯┖鏍间互杈惧埌鎸囧畾鐨勯暱搴︺傚綋妫绱㈠埌CHAR鍊兼椂锛屽熬閮ㄧ殑绌烘牸琚垹闄ゆ帀銆傚湪瀛樺偍鎴栨绱㈣繃绋嬩腑涓嶈繘琛屽ぇ灏忓啓杞崲銆

VARCHAR鍒椾腑鐨勫间负鍙彉闀垮瓧绗︿覆銆傞暱搴﹀彲浠ユ寚瀹氫负065,535涔嬮棿鐨勫笺(VARCHAR鐨勬渶澶ф湁鏁堥暱搴︾敱鏈澶ц澶у皬鍜屼娇鐢ㄧ殑瀛楃闆嗙‘瀹氥傛暣浣撴渶澶ч暱搴︽槸65,532瀛楄妭锛

CHAR瀵规瘮锛VARCHAR鍊间繚瀛樻椂鍙繚瀛橀渶瑕佺殑瀛楃鏁帮紝鍙﹀姞涓涓瓧鑺傛潵璁板綍闀垮害(濡傛灉鍒楀0鏄庣殑闀垮害瓒呰繃255锛屽垯浣跨敤涓や釜瀛楄妭)

VARCHAR鍊间繚瀛樻椂涓嶈繘琛屽~鍏呫傚綋鍊间繚瀛樺拰妫绱㈡椂灏鹃儴鐨勭┖鏍间粛淇濈暀锛岀鍚堟爣鍑SQL

濡傛灉鍒嗛厤缁CHARVARCHAR鍒楃殑鍊艰秴杩囧垪鐨勬渶澶ч暱搴︼紝鍒欏鍊艰繘琛岃鍓互浣垮叾閫傚悎銆傚鏋滆瑁佹帀鐨勫瓧绗︿笉鏄┖鏍硷紝鍒欎細浜х敓涓鏉¤鍛娿傚鏋滆鍓潪绌烘牸瀛楃锛屽垯浼氶犳垚閿欒(鑰屼笉鏄鍛)骞堕氳繃浣跨敤涓ユ牸SQL妯″紡绂佺敤鍊肩殑鎻掑叆銆傚弬瑙5.3.2鑺傦紝鈥淪QL鏈嶅姟鍣ㄦā寮忊

涓嬮潰鐨勮〃鏄剧ず浜嗗皢鍚勭瀛楃涓插间繚瀛樺埌CHAR(4)VARCHAR(4)鍒楀悗鐨勭粨鏋滐紝璇存槑浜CHARVARCHAR涔嬮棿鐨勫樊鍒細

CHAR(4)

瀛樺偍闇姹

VARCHAR(4)

瀛樺偍闇姹

''

'    '

4涓瓧鑺

''

1涓瓧鑺

'ab'

'ab  '

4涓瓧鑺

'ab '

3涓瓧鑺

'abcd'

'abcd'

4涓瓧鑺

'abcd'

5涓瓧鑺

'abcdefgh'

'abcd'

4涓瓧鑺

'abcd'

5涓瓧鑺

璇锋敞鎰忎笂琛ㄤ腑鏈鍚庝竴琛岀殑鍊煎彧閫傜敤涓嶄娇鐢ㄤ弗鏍兼ā寮鏃讹紱濡傛灉MySQL杩愯鍦ㄤ弗鏍兼ā寮忥紝瓒呰繃鍒楅暱搴︿笉鐨勫淇濆瓨锛屽苟涓斾細鍑虹幇閿欒銆

CHAR(4)VARCHAR(4)鍒楁绱㈢殑鍊煎苟涓嶆绘槸鐩稿悓锛屽洜涓烘绱㈡椂浠CHAR鍒楀垹闄や簡灏鹃儴鐨勭┖鏍笺傞氳繃涓嬮潰鐨勪緥瀛愯鏄庤宸埆锛

mysql> CREATE TABLE vc (v VARCHAR(4), c CHAR(4));
Query OK, 0 rows affected (0.02 sec)
 
mysql> INSERT INTO vc VALUES ('ab  ', 'ab  ');
Query OK, 1 row affected (0.00 sec)
 
mysql> SELECT CONCAT(v, '+'), CONCAT(c, '+') FROM vc;
+----------------+----------------+
| CONCAT(v, '+') | CONCAT(c, '+') |
+----------------+----------------+
| ab  +          | ab+            |
+----------------+----------------+
1 row in set (0.00 sec)

鏍规嵁鍒嗛厤缁欏垪鐨勫瓧绗﹂泦鏍″瑙勫垯瀵CHARVARCHAR鍒椾腑鐨勫艰繘琛屾帓搴忓拰姣旇緝銆

璇锋敞鎰忔墍鏈MySQL鏍″瑙勫垯灞炰簬PADSPACE绫汇傝繖璇存槑鍦MySQL涓殑鎵鏈CHARVARCHAR鍊兼瘮杈冩椂涓嶉渶瑕佽冭檻浠讳綍灏鹃儴绌烘牸銆備緥濡傦細

mysql> CREATE TABLE names (myname CHAR(10), yourname VARCHAR(10));
Query OK, 0 rows affected (0.09 sec)
 
mysql> INSERT INTO names VALUES ('Monty ', 'Monty ');
Query OK, 1 row affected (0.00 sec)
 
mysql> SELECT myname = 'Monty  ', yourname = 'Monty  ' FROM names;
+--------------------+----------------------+
| myname = 'Monty  ' | yourname = 'Monty  ' |
+--------------------+----------------------+
|                  1 |                    1 |
+--------------------+----------------------+
1 row in set (0.00 sec)

璇锋敞鎰忔墍鏈MySQL鐗堟湰鍧囧姝わ紝骞朵笖瀹冧笉鍙SQL鏈嶅姟鍣ㄦā寮忕殑褰卞搷銆

瀵逛簬灏鹃儴濉厖瀛楃琚鍓帀鎴栨瘮杈冩椂灏嗗畠浠拷瑙嗘帀鐨勬儏褰紝濡傛灉鍒楃殑绱㈠紩闇瑕佸敮涓鐨勫硷紝鍦ㄥ垪鍐呮彃鍏ヤ竴涓彧鏄~鍏呭瓧绗︽暟涓嶅悓鐨勫煎皢浼氶犳垚澶嶅埗閿奸敊璇

CHAR BYTECHAR BINARY鐨勫埆鍚嶃傝繖鏄负浜嗕繚璇佸吋瀹规с

ASCII灞炴т负CHAR鍒楀垎閰latin1瀛楃闆嗐UNICODE灞炴у垎閰ucs2瀛楃闆嗐

11.4.2. BINARY鍜孷ARBINARY绫诲瀷

BINARYVARBINARY绫荤被浼间簬CHARVARCHAR锛屼笉鍚岀殑鏄畠浠寘鍚簩杩涘埗瀛楃涓茶屼笉瑕侀潪浜岃繘鍒跺瓧绗︿覆銆備篃灏辨槸璇达紝瀹冧滑鍖呭惈瀛楄妭瀛楃涓茶屼笉鏄瓧绗﹀瓧绗︿覆銆傝繖璇存槑瀹冧滑娌℃湁瀛楃闆嗭紝骞朵笖鎺掑簭鍜屾瘮杈冨熀浜庡垪鍊煎瓧鑺傜殑鏁板煎笺

BINARYVARBINARY鍏佽鐨勬渶澶ч暱搴︿竴鏍凤紝濡傚悓CHARVARCHAR锛屼笉鍚岀殑鏄BINARYVARBINARY鐨勯暱搴︽槸瀛楄妭闀垮害鑰屼笉鏄瓧绗﹂暱搴︺

BINARYVARBINARY鏁版嵁绫诲瀷涓嶅悓浜CHAR BINARYVARCHAR BINARY鏁版嵁绫诲瀷銆傚浜庡悗涓绉嶇被鍨嬶紝BINARY灞炴т笉浼氬皢鍒楄涓轰簩杩涘埗瀛楃涓插垪銆傜浉鍙嶏紝瀹冭嚧浣夸娇鐢ㄥ垪瀛楃闆嗙殑浜屽厓 鏍″瑙勫垯锛屽苟涓斿垪鑷韩鍖呭惈闈炰簩杩涘埗瀛楃瀛楃涓茶屼笉鏄簩杩涘埗瀛楄妭瀛楃涓层備緥濡CHAR(5) BINARY琚涓CHAR(5) CHARACTER SET latin1 COLLATE latin1_bin锛屽亣瀹氶粯璁ゅ瓧绗﹂泦鏄latin1銆傝繖涓嶅悓浜BINARY(5)锛屽畠淇濆瓨5瀛楄妭浜岃繘鍒跺瓧绗︿覆锛屾病鏈夊瓧绗﹂泦鎴 鏍″瑙勫垯銆

褰撲繚瀛BINARY鍊兼椂锛屽湪瀹冧滑鍙宠竟濉厖鍊间互杈惧埌鎸囧畾闀垮害銆傚~鍏呭兼槸0x00(闆跺瓧鑺)銆傛彃鍏ュ兼椂鍦ㄥ彸渚ф坊鍔0x00 on锛屽苟涓旈夋嫨鏃朵笉鍒犻櫎灏鹃儴鐨勫瓧鑺傘傛瘮杈冩椂鎵鏈夊瓧鑺傚緢閲嶈锛屽寘鎷ORDER BYDISTINCT鎿嶄綔銆傛瘮杈冩椂0x00瀛楄妭鍜岀┖鏍兼槸涓嶅悓鐨勶紝0x00<绌烘牸銆

渚嬪锛氬浜庝竴涓BINARY(3)鍒楋紝褰撴彃鍏ユ椂 'a' 鍙樹负 'a \0''a\0'鎻掑叆鏃跺彉涓'a\0\0'銆傚綋閫夋嫨鏃朵袱涓彃鍏ョ殑鍊煎潎涓嶆洿鏀广

瀵逛簬VARBINARY锛屾彃鍏ユ椂涓嶅~鍏呭瓧绗︼紝閫夋嫨鏃朵笉瑁佸壀瀛楄妭銆傛瘮杈冩椂鎵鏈夊瓧鑺傚緢閲嶈锛屽寘鎷ORDER BYDISTINCT鎿嶄綔銆傛瘮杈冩椂0x00瀛楄妭鍜岀┖鏍兼槸涓嶅悓鐨勶紝0x00<绌烘牸銆

瀵逛簬灏鹃儴濉厖瀛楃琚鍓帀鎴栨瘮杈冩椂灏嗗畠浠拷瑙嗘帀鐨勬儏褰紝濡傛灉鍒楃殑绱㈠紩闇瑕佸敮涓鐨勫硷紝鍦ㄥ垪鍐呮彃鍏ヤ竴涓彧鏄~鍏呭瓧绗︽暟涓嶅悓鐨勫煎皢浼氶犳垚澶嶅埗閿奸敊璇

濡傛灉浣犺鍒掍娇鐢ㄨ繖浜涙暟鎹被鍨嬫潵淇濆瓨浜岃繘鍒舵暟鎹苟涓旈渶瑕佹绱㈢殑鍊间笌淇濆瓨鐨勫煎畬鍏ㄧ浉鍚岋紝搴旇冭檻鍓嶉潰鎵杩扮殑濉厖鍜岃鍓壒寰併備笅闈㈢殑渚嬪瓙璇存槑浜嗙敤0x00濉厖鐨BINARY鍊煎浣曞奖鍝嶅垪鍊兼瘮杈冿細

mysql> CREATE TABLE t (c BINARY(3));
Query OK, 0 rows affected (0.01 sec)
 
mysql> INSERT INTO t SET c = 'a';
Query OK, 1 row affected (0.01 sec)
 
mysql> SELECT HEX(c), c = 'a', c = 'a\0\0' from t;
+--------+---------+-------------+
| HEX(c) | c = 'a' | c = 'a\0\0' |
+--------+---------+-------------+
| 610000 |       0 |           1 |
+--------+---------+-------------+
1 row in set (0.09 sec)

濡傛灉妫绱㈢殑鍊煎繀椤讳笌鎸囧畾杩涜瀛樺偍鑰屾病鏈夊~鍏呯殑鍊肩浉鍚岋紝鏈濂戒娇鐢BLOB鏁版嵁绫诲瀷銆

鍒涘缓琛ㄦ椂锛MySQL鍙互榛橀粯鏇存敼BINARYVARBINARY鍒楃殑绫诲瀷銆傚弬瑙13.1.5.1鑺傦紝鈥滄矇瀵傜殑鍒楄鏍煎彉鏇粹

11.4.3. BLOB鍜孴EXT绫诲瀷

BLOB鏄竴涓簩杩涘埗澶у璞★紝鍙互瀹圭撼鍙彉鏁伴噺鐨勬暟鎹傛湁4BLOB绫诲瀷锛TINYBLOBBLOBMEDIUMBLOBLONGBLOB銆傚畠浠彧鏄彲瀹圭撼鍊肩殑鏈澶ч暱搴︿笉鍚屻

4TEXT绫诲瀷锛TINYTEXTTEXTMEDIUMTEXTLONGTEXT銆傝繖浜涘搴4BLOB绫诲瀷锛屾湁鐩稿悓鐨勬渶澶ч暱搴﹀拰瀛樺偍闇姹傘

鍙傝11.5鑺傦紝鈥滃垪绫诲瀷瀛樺偍闇姹傗

BLOB 鍒楄瑙嗕负浜岃繘鍒跺瓧绗︿覆(瀛楄妭瀛楃涓)TEXT鍒楄瑙嗕负闈炰簩杩涘埗瀛楃涓(瀛楃瀛楃涓)BLOB鍒楁病鏈夊瓧绗﹂泦锛屽苟涓旀帓搴忓拰姣旇緝鍩轰簬鍒楀煎瓧鑺傜殑鏁板煎笺TEXT鍒楁湁涓涓瓧绗﹂泦锛屽苟涓旀牴鎹瓧绗﹂泦鐨 鏍″瑙勫垯瀵瑰艰繘琛屾帓搴忓拰姣旇緝銆

TEXTBLOB鍒楃殑瀛樺偍鎴栨绱㈣繃绋嬩腑锛屼笉瀛樺湪澶у皬鍐欒浆鎹€

褰撴湭杩愯鍦ㄤ弗鏍兼ā寮忔椂锛屽鏋滀綘涓BLOBTEXT鍒楀垎閰嶄竴涓秴杩囪鍒楃被鍨嬬殑鏈澶ч暱搴︾殑鍊煎硷紝鍊艰鎴彇浠ヤ繚璇侀傚悎銆傚鏋滄埅鎺夌殑瀛楃涓嶆槸绌烘牸锛屽皢浼氫骇鐢熶竴鏉¤鍛娿備娇鐢ㄤ弗鏍SQL妯″紡锛屼細浜х敓閿欒锛屽苟涓斿煎皢琚嫆缁濊屼笉鏄埅鍙栧苟缁欏嚭璀﹀憡銆傚弬瑙5.3.2鑺傦紝鈥淪QL鏈嶅姟鍣ㄦā寮忊

鍦ㄥぇ澶氭暟鏂归潰锛屽彲浠ュ皢BLOB鍒楄涓鸿兘澶熻冻澶熷ぇ鐨VARBINARY鍒椼傚悓鏍凤紝鍙互灏TEXT鍒楄涓VARCHAR鍒椼BLOBTEXT鍦ㄤ互涓嬪嚑涓柟闈笉鍚屼簬VARBINARYVARCHAR

         褰撲繚瀛樻垨妫绱BLOBTEXT鍒楃殑鍊兼椂涓嶅垹闄ゅ熬閮ㄧ┖鏍笺(杩欎笌VARBINARYVARCHAR鍒楃浉鍚岋級

璇锋敞鎰忔瘮杈冩椂灏嗙敤绌烘牸瀵TEXT杩涜鎵╁厖浠ラ傚悎姣旇緝鐨勫璞★紝姝eCHARVARCHAR

         瀵逛簬BLOBTEXT鍒楃殑绱㈠紩锛屽繀椤绘寚瀹氱储寮曞墠缂鐨勯暱搴︺傚浜CHARVARCHAR锛屽墠缂闀垮害鏄彲閫夌殑銆傚弬瑙7.4.3鑺傦紝鈥滃垪绱㈠紩鈥

         BLOBTEXT鍒椾笉鑳芥湁 榛樿鍊笺

LONGLONG VARCHAR瀵瑰簲MEDIUMTEXT鏁版嵁绫诲瀷銆傝繖鏄负浜嗕繚璇佸吋瀹规с傚鏋TEXT鍒楃被鍨嬩娇鐢BINARY灞炴э紝灏嗕负鍒楀垎閰嶅垪瀛楃闆嗙殑浜屽厓 鏍″瑙勫垯銆

MySQL杩炴帴绋嬪簭/ODBCBLOB鍊煎畾涔変负LONGVARBINARY锛屽皢TEXT鍊煎畾涔変负LONGVARCHAR

鐢变簬BLOBTEXT鍊煎彲鑳戒細闈炲父闀匡紝浣跨敤瀹冧滑鏃跺彲鑳介亣鍒颁竴浜涚害鏉燂細

         褰撴帓搴忔椂鍙娇鐢ㄨ鍒楃殑鍓max_sort_length涓瓧鑺傘max_sort_length鐨 榛樿鍊兼槸1024锛涜鍊煎彲浠ュ湪鍚姩mysqld鏈嶅姟鍣ㄦ椂浣跨敤--max_sort_length閫夐」杩涜鏇存敼銆傚弬瑙5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥

杩愯鏃跺鍔max_sort_length鐨勫煎彲浠ュ湪鎺掑簭鎴栫粍鍚堟椂浣挎洿澶氱殑瀛楄妭鏈夋剰涔夈備换浣曞鎴风鍙互鏇存敼鍏朵細璇max_sort_length鍙橀噺鐨勫硷細

mysql> SET max_sort_length = 2000;
mysql> SELECT id, comment FROM tbl_name
    -> ORDER BY comment;

褰撲綘鎯宠浣胯秴杩max_sort_length鐨勫瓧鑺傛湁鎰忎箟锛屽鍚暱鍊肩殑BLOBTEXT鍒椾娇鐢GROUP BYORDER BY鐨勫彟涓绉嶆柟寮忔槸灏嗗垪鍊艰浆鎹负鍥哄畾闀垮害鐨勫璞°傛爣鍑嗘柟娉曟槸浣跨敤SUBSTRING鍑芥暟銆備緥濡傦紝涓嬮潰鐨勮鍙ュcomment鍒楃殑2000涓瓧鑺傝繘琛屾帓搴忥細

mysql> SELECT id, SUBSTRING(comment,1,2000) FROM tbl_name
    -> ORDER BY SUBSTRING(comment,1,2000);

         BLOBTEXT瀵硅薄鐨勬渶澶уぇ灏忕敱鍏剁被鍨嬬‘瀹氾紝浣嗗湪瀹㈡埛绔拰鏈嶅姟鍣ㄤ箣闂村疄闄呭彲浠ヤ紶閫掔殑鏈澶у肩敱鍙敤鍐呭瓨鏁伴噺鍜岄氫俊缂撳瓨鍖哄ぇ灏忕‘瀹氥備綘鍙互閫氳繃鏇存敼max_allowed_packet鍙橀噺鐨勫兼洿鏀规秷鎭紦瀛樺尯鐨勫ぇ灏忥紝浣嗗繀椤诲悓鏃朵慨鏀规湇鍔″櫒鍜屽鎴风绋嬪簭銆備緥濡傦紝鍙互浣跨敤 mysqlmysqldump鏉ユ洿鏀瑰鎴风鐨max_allowed_packet鍊笺傚弬瑙7.5.2鑺傦紝鈥滆皟鑺傛湇鍔″櫒鍙傛暟鈥8.3鑺傦紝鈥渕ysql锛歁ySQL鍛戒护琛屽伐鍏封8.8鑺傦紝鈥渕ysqldump锛氭暟鎹簱澶囦唤绋嬪簭鈥

姣忎釜BLOBTEXT鍊煎垎鍒敱鍐呴儴鍒嗛厤鐨勫璞¤〃绀恒傝繖涓庡叾瀹冨垪绫诲瀷褰㈡垚瀵规瘮锛屽悗鑰呮槸褰撴墦寮琛ㄦ椂涓烘瘡1鍒楀垎閰嶅瓨鍌ㄥ紩鎿庛

11.4.4. ENUM绫诲瀷

ENUM鏄竴涓瓧绗︿覆瀵硅薄锛屽叾鍊兼潵鑷〃鍒涘缓鏃跺湪鍒楄瀹氫腑鏄惧紡鏋氫妇鐨勪竴鍒楀笺

鍦ㄦ煇浜涙儏鍐典笅锛ENUM鍊间篃鍙互涓虹┖瀛楃涓('')NULL

         濡傛灉浣犲皢涓涓潪娉曞兼彃鍏ENUM(涔熷氨鏄锛屽厑璁哥殑鍊煎垪涔嬪鐨勫瓧绗︿覆)锛屽皢鎻掑叆绌哄瓧绗︿覆浠ヤ綔涓虹壒娈婇敊璇笺傝瀛楃涓蹭笌鈥滄櫘閫氣濈┖瀛楃涓蹭笉鍚岋紝璇ュ瓧绗︿覆鏈夋暟鍊煎0銆傚悗闈㈡湁璇︾粏璁ㄨ銆

         濡傛灉灏ENUM鍒楀0鏄庝负鍏佽NULLNULL鍊煎垯涓鸿鍒楃殑涓涓湁鏁堝硷紝骞朵笖 榛樿鍊间负NULL銆傚鏋ENUM鍒楄澹版槑涓NOT NULL锛屽叾榛樿鍊间负鍏佽鐨勫煎垪鐨勭1涓厓绱犮

姣忎釜鏋氫妇鍊兼湁涓涓储寮曪細

         鏉ヨ嚜鍒楄瀹氱殑鍏佽鐨勫煎垪涓殑鍊间粠1寮濮嬬紪鍙枫

         绌哄瓧绗︿覆閿欒鍊肩殑绱㈠紩鍊兼槸0銆傝繖璇存槑浣犲彲浠ヤ娇鐢ㄤ笅闈㈢殑SELECT璇彞鏉ユ壘鍑哄垎閰嶄簡闈炴硶ENUM鍊肩殑琛岋細

                mysql> SELECT * FROM tbl_name WHERE enum_col=0;

         NULL鍊肩殑绱㈠紩鏄NULL

渚嬪锛屽畾涔変负ENUM鐨勫垪('one''two''three')鍙互鏈変笅闈㈡墍绀轰换浣曞笺傝繕鏄剧ず浜嗘瘡涓肩殑绱㈠紩锛

绱㈠紩

NULL

NULL

''

0

'one'

1

'two'

2

'three'

3

鏋氫妇鏈澶氬彲浠ユ湁65,535涓厓绱犮

褰撳垱寤鸿〃鏃讹紝ENUM鎴愬憳鍊肩殑灏鹃儴绌烘牸灏嗚嚜鍔ㄨ鍒犻櫎銆

褰撴绱㈡椂锛屼繚瀛樺湪ENUM鍒楃殑鍊间娇鐢ㄥ垪瀹氫箟涓墍浣跨敤鐨勫ぇ灏忓啓鏉ユ樉绀恒傝娉ㄦ剰鍙互涓ENUM鍒楀垎閰嶅瓧绗﹂泦鍜 鏍″瑙勫垯銆傚浜庝簩杩涘埗鎴栧ぇ灏忓啓鏁忔劅鐨勬牎瀵硅鍒欙紝褰撲负鍒楀垎閰嶅兼椂搴旇冭檻澶у皬鍐欍

濡傛灉鍦ㄦ暟鍊间笂涓嬫枃涓绱竴涓ENUM鍊硷紝灏嗚繑鍥炲垪鍊肩殑绱㈠紩銆備緥濡傦紝浣犲彲浠ヨ繖鏍蜂粠ENUM鍒楁悳绱㈡暟鍊煎硷細

mysql> SELECT enum_col+0 FROM tbl_name;

濡傛灉灏嗕竴涓暟瀛椾繚瀛樺埌ENUM鍒楋紝鏁板瓧琚涓虹储寮曪紝骞朵笖淇濆瓨鐨勫兼槸璇ョ储寮曞搴旂殑鏋氫妇鎴愬憳銆(浣嗘槸锛岃繖涓嶉傚悎LOAD DATA锛屽畠灏嗘墍鏈夎緭鍏ヨ涓哄瓧绗︿覆锛涓嶅缓璁娇鐢ㄧ被浼兼暟瀛楃殑鏋氫妇鍊兼潵瀹氫箟涓涓ENUM鍒楋紝鍥犱负杩欏緢瀹规槗寮曡捣娣锋穯銆備緥濡傦紝涓嬮潰鐨勫垪鍚湁瀛楃涓插'0''1''2'鐨勬灇涓炬垚鍛橈紝浣嗘暟鍊肩储寮曞间负123

numbers ENUM('0','1','2')

鏍规嵁鏋氫妇鎴愬憳鍦ㄥ垪瀹氫箟涓垪鍑虹殑椤哄簭瀵ENUM鍊艰繘琛屾帓搴忋(鎹㈠彞璇濊锛ENUM鍊兼牴鎹储寮曠紪鍙疯繘琛屾帓搴忥級渚嬪锛屽浜ENUM('a''b')'a'鎺掑湪'b'鍓嶉潰锛屼絾瀵逛簬ENUM('b''a')'b'鎺掑湪'a'鍓嶉潰銆傜┖瀛楃涓叉帓鍦ㄩ潪绌哄瓧绗︿覆鍓嶉潰锛屽苟涓NULL鍊兼帓鍦ㄦ墍鏈夊叾瀹冩灇涓惧煎墠闈€傝鎯抽槻姝㈡剰鎯充笉鍒扮殑缁撴灉锛屾寜瀛楁瘝椤哄簭瑙勫畾ENUM鍒椼傝繕鍙互浣跨敤GROUP BY  CAST(col AS CHAR)GROUP BY  CONCAT(col)鏉ョ‘淇濇寜鐓ц瘝姹囧鍒楄繘琛屾帓搴忚屼笉鏄敤绱㈠紩鏁板瓧銆

濡傛灉浣犳兂瑕佺‘瀹氫竴涓ENUM鍒楃殑鎵鏈夊彲鑳界殑鍊硷紝浣跨敤SHOW COLUMNS FROM tbl_name LIKE enum_col锛屽苟瑙f瀽杈撳嚭涓2鍒楃殑ENUM瀹氫箟銆

11.4.5. SET绫诲瀷

SET鏄竴涓瓧绗︿覆瀵硅薄锛屽彲浠ユ湁闆舵垨澶氫釜鍊硷紝鍏跺兼潵鑷〃鍒涘缓鏃惰瀹氱殑鍏佽鐨勪竴鍒楀笺傛寚瀹氬寘鎷涓SET鎴愬憳鐨SET鍒楀兼椂鍚勬垚鍛樹箣闂寸敤閫楀彿(,)闂撮殧寮銆傝繖鏍SET鎴愬憳鍊兼湰韬笉鑳藉寘鍚楀彿銆

渚嬪锛屾寚瀹氫负SET('one', 'two') NOT NULL鐨勫垪鍙互鏈変笅闈㈢殑浠讳綍鍊硷細

''
'one'
'two'
'one,two'

SET鏈澶氬彲浠ユ湁64涓笉鍚岀殑鎴愬憳銆

褰撳垱寤鸿〃鏃讹紝SET鎴愬憳鍊肩殑灏鹃儴绌烘牸灏嗚嚜鍔ㄨ鍒犻櫎銆

褰撴绱㈡椂锛屼繚瀛樺湪SET鍒楃殑鍊间娇鐢ㄥ垪瀹氫箟涓墍浣跨敤鐨勫ぇ灏忓啓鏉ユ樉绀恒傝娉ㄦ剰鍙互涓SET鍒楀垎閰嶅瓧绗﹂泦鍜 鏍″瑙勫垯銆傚浜庝簩杩涘埗鎴栧ぇ灏忓啓鏁忔劅鐨勬牎瀵硅鍒欙紝褰撲负鍒楀垎閰嶅兼椂搴旇冭檻澶у皬鍐欍

MySQL鐢ㄦ暟瀛椾繚瀛SET鍊硷紝鎵淇濆瓨鍊肩殑浣庨樁浣嶅搴旂1SET鎴愬憳銆傚鏋滃湪鏁板间笂涓嬫枃涓绱竴涓SET鍊硷紝妫绱㈢殑鍊肩殑浣嶈缃搴旂粍鎴愬垪鍊肩殑SET鎴愬憳銆備緥濡傦紝浣犲彲浠ヨ繖鏍蜂粠涓涓SET鍒楁绱㈡暟鍊煎硷細

mysql> SELECT set_col+0 FROM tbl_name;

濡傛灉灏嗕竴涓暟瀛椾繚瀛樺埌SET鍒椾腑锛屾暟瀛椾腑浜岃繘鍒惰〃绀轰腑鐨勪綅纭畾浜嗗垪鍊间腑鐨SET鎴愬憳銆傚浜庢寚瀹氫负SET('a','b','c','d')鐨勫垪锛屾垚鍛樻湁涓嬮潰鐨勫崄杩涘埗鍜屼簩杩涘埗鍊硷細

SET鎴愬憳

鍗佽繘鍒跺

浜岃繘鍒跺

'a'

1

0001

'b'

2

0010

'c'

4

0100

'd'

8

1000

 

濡傛灉浣犱负璇ュ垪鍒嗛厤涓涓9锛屽叾浜岃繘鍒跺舰寮忎负1001锛屽洜姝ょ1涓拰绗4SET鍊兼垚鍛'a''d'琚夋嫨锛岀粨鏋滃间负 'a,d'

瀵逛簬鍖呭惈澶氫釜SET鍏冪礌鐨勫硷紝褰撴彃鍏ュ兼椂鍏冪礌鎵鍒楃殑椤哄簭骞朵笉閲嶈銆傚湪鍊间腑涓涓粰瀹氱殑鍏冪礌鍒椾簡澶氬皯娆′篃涓嶉噸瑕併傚綋浠ュ悗妫绱㈣鍊兼椂锛屽间腑鐨勬瘡涓厓绱犲嚭鐜颁竴娆★紝鏍规嵁琛ㄥ垱寤烘椂鎸囧畾鐨勯『搴忓垪鍑哄厓绱犮備緥濡傦紝鍋囧畾鏌愪釜鍒楁寚瀹氫负SET('a','b','c','d')

mysql> CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));

鎻掑叆鍊'a,d''d,a''a,d,d''a,d,a''d,a,d':

mysql> INSERT INTO myset (col) VALUES 
-> ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

褰撴绱㈡椂鎵鏈夎繖浜涘兼樉绀轰负 'a,d'

mysql> SELECT col FROM myset;
+------+
| col  |
+------+
| a,d  |
| a,d  |
| a,d  |
| a,d  |
| a,d  |
+------+
5 rows in set (0.04 sec)

濡傛灉灏SET鍒楄缃负涓涓笉鏀寔鐨勫硷紝鍒欒鍊艰蹇界暐骞跺彂鍑鸿鍛婏細

mysql> INSERT INTO myset (col) VALUES ('a,d,d,s');
Query OK, 1 row affected, 1 warning (0.03 sec)
 
mysql> SHOW WARNINGS;
+---------+------+------------------------------------------+
| Level   | Code | Message                                  |
+---------+------+------------------------------------------+
| Warning | 1265 | Data truncated for column 'col' at row 1 |
+---------+------+------------------------------------------+
1 row in set (0.04 sec)
 
mysql> SELECT col FROM myset;
+------+
| col  |
+------+
| a,d  |
| a,d  |
| a,d  |
| a,d  |
| a,d  |
| a,d  |
+------+
6 rows in set (0.01 sec)

SET鍊兼寜鏁板瓧椤哄簭鎺掑簭銆NULL鍊兼帓鍦ㄩ潪NULL SET鍊肩殑鍓嶉潰銆

閫氬父鎯呭喌锛屽彲浠ヤ娇鐢FIND_IN_SET()鍑芥暟鎴LIKE鎿嶄綔绗︽悳绱SET鍊硷細

mysql> SELECT * FROM tbl_name WHERE FIND_IN_SET('value',set_col)>0;
mysql> SELECT * FROM tbl_name WHERE set_col LIKE '%value%';

1涓鍙ユ壘鍑SET_col鍖呭惈value set鎴愬憳鐨勮銆傜2涓被浼硷紝浣嗘湁鎵涓嶅悓锛氬畠鍦ㄥ叾瀹冨湴鏂规壘鍑set_col鍖呭惈value鐨勮锛岀敋鑷虫槸鍦ㄥ彟涓涓SET鎴愬憳鐨勫瓙瀛楃涓蹭腑銆

涓嬮潰鐨勮鍙ヤ篃鏄悎娉曠殑锛

mysql> SELECT * FROM tbl_name WHERE set_col & 1;
mysql> SELECT * FROM tbl_name WHERE set_col = 'val1,val2';

1涓鍙ュ鎵惧寘鍚1set鎴愬憳鐨勫笺傜2涓鍙ュ鎵句竴涓‘鍒囧尮閰嶇殑鍊笺傚簲娉ㄦ剰绗2绫荤殑姣旇緝銆傚皢set鍊间笌'val1,val2'姣旇緝杩斿洖鐨勭粨鏋滀笌鍚'val2,val1'姣旇緝杩斿洖鐨勭粨鏋滀笉鍚屻傛寚瀹氬兼椂鐨勯『搴忓簲涓庡湪鍒楀畾涔変腑鎵鍒楃殑椤哄簭鐩稿悓銆

濡傛灉鎯宠涓SET鍒楃‘瀹氭墍鏈夊彲鑳界殑鍊硷紝浣跨敤SHOW COLUMNS FROM tbl_name LIKE set_col骞惰В鏋愯緭鍑轰腑绗2鍒楃殑SET瀹氫箟銆

11.5. 鍒楃被鍨嬪瓨鍌ㄩ渶姹

鏍规嵁绫诲埆鍒楀嚭浜MySQL鏀寔鐨勬瘡涓垪绫诲瀷鐨勫瓨鍌ㄩ渶姹傘

MyISAM琛ㄤ腑琛岀殑鏈澶уぇ灏忎负65,534瀛楄妭銆傛瘡涓BLOBTEXT鍒 璐︽埛鍙崰鍏朵腑鐨59涓瓧鑺傘

濡傛灉MyISAM琛ㄥ寘鎷彉闀垮垪绫诲瀷锛岃褰曟牸寮忎篃鏄彲鍙橀暱搴︺傚綋鍒涘缓琛ㄦ椂锛屽湪鏌愪簺鏉′欢涓嬶紝MySQL鍙互灏嗕竴涓垪浠庡彉闀跨被鍨嬫敼涓哄浐瀹氶暱搴︾殑绫诲瀷鎴栧弽涔嬩害鐒躲傝缁嗕俊鎭弬瑙13.1.5.1鑺傦紝鈥滄矇瀵傜殑鍒楄鏍煎彉鏇粹

鏁板肩被鍨嬪瓨鍌ㄩ渶姹

鍒楃被鍨

瀛樺偍闇姹

TINYINT

1涓瓧鑺

SMALLINT

2涓瓧鑺

MEDIUMINT

3涓瓧鑺

INT, INTEGER

4涓瓧鑺

BIGINT

8涓瓧鑺

FLOAT(p)

濡傛灉0 <= p <= 244涓瓧鑺, 濡傛灉25 <= p <= 538涓瓧鑺

FLOAT

4涓瓧鑺

DOUBLE [PRECISION], item REAL

8涓瓧鑺

DECIMAL(M,D), NUMERIC(M,D)

鍙橀暱锛涘弬瑙佷笅闈㈢殑璁ㄨ

BIT(M)

澶х害(M+7)/8涓瓧鑺

DECIMAL(NUMERIC)鐨勫瓨鍌ㄩ渶姹備笌鍏蜂綋鐗堟湰鏈夊叧锛

浣跨敤浜岃繘鍒舵牸寮忓皢9涓崄杩涘埗(鍩轰簬10)鏁板帇缂╀负4涓瓧鑺傛潵琛ㄧずDECIMAL鍒楀笺傛瘡涓肩殑鏁存暟鍜屽垎鏁伴儴鍒嗙殑瀛樺偍鍒嗗埆纭畾銆傛瘡涓9浣嶆暟鐨勫嶆暟闇瑕4涓瓧鑺傦紝骞朵笖鈥滃墿浣欑殑鈥濅綅闇瑕4涓瓧鑺傜殑涓閮ㄥ垎銆備笅琛ㄧ粰鍑轰簡瓒呭嚭浣嶆暟鐨勫瓨鍌ㄩ渶姹傦細

鍓╀綑鐨

瀛楄妭

浣嶆暟

鏁扮洰

0

0

1

1

2

1

3

2

4

2

5

3

6

3

7

4

8

4

9

4

鏃ユ湡鍜屾椂闂寸被鍨嬬殑瀛樺偍闇姹

鍒楃被鍨

瀛樺偍闇姹

DATE

3涓瓧鑺

DATETIME

8涓瓧鑺

TIMESTAMP

4涓瓧鑺

TIME

3涓瓧鑺

YEAR

1涓瓧鑺

瀛楃涓茬被鍨嬬殑瀛樺偍闇姹

鍒楃被鍨

瀛樺偍闇姹

CHAR(M)

M涓瓧鑺傦紝0 <= M <= 255

VARCHAR(M)

L+1涓瓧鑺傦紝鍏朵腑L <= M 0 <= M <= 65535(鍙傝涓嬮潰鐨勬敞閲)

BINARY(M)

M涓瓧鑺傦紝0 <= M <= 255

VARBINARY(M)

L+1涓瓧鑺傦紝鍏朵腑L <= M 0 <= M <= 255

TINYBLOB, TINYTEXT

L+1涓瓧鑺傦紝鍏朵腑L < 28

BLOB, TEXT

L+2涓瓧鑺傦紝鍏朵腑L < 216

MEDIUMBLOB, MEDIUMTEXT

L+3涓瓧鑺傦紝鍏朵腑L < 224

LONGBLOB, LONGTEXT

L+4涓瓧鑺傦紝鍏朵腑L < 232

ENUM('value1','value2',...)

12涓瓧鑺傦紝鍙栧喅浜庢灇涓惧肩殑涓暟(鏈澶65,535涓)

SET('value1','value2',...)

1234鎴栬8涓瓧鑺傦紝鍙栧喅浜set鎴愬憳鐨勬暟鐩(鏈澶64涓垚鍛)

VARCHARBLOBTEXT绫绘槸鍙橀暱绫诲瀷銆傛瘡涓被鍨嬬殑瀛樺偍闇姹傚彇鍐充簬鍒楀肩殑瀹為檯闀垮害(鐢ㄥ墠闈㈢殑琛ㄤ腑鐨L琛ㄧず)锛岃屼笉鏄绫诲瀷鐨勬渶澶у彲鑳界殑澶у皬銆備緥濡傦紝VARCHAR(10)鍒楀彲浠ュ绾虫渶澶ч暱搴︿负10鐨勫瓧绗︿覆銆傚疄闄呭瓨鍌ㄩ渶姹傛槸瀛楃涓(L)鐨勯暱搴︼紝鍔犱笂涓涓褰曞瓧绗︿覆闀垮害鐨勫瓧鑺傘傚浜庡瓧绗︿覆'abcd'L4锛屽瓨鍌ㄩ渶瑕5涓瓧鑺傘

瀵逛簬CHARVARCHARTEXT绫诲瀷锛屽墠闈㈢殑琛ㄤ腑鐨勫LM搴旇В閲婁负瀛楃鏁扮洰锛屽苟涓斿垪瀹氫箟涓殑杩欎簺绫诲瀷鐨勯暱搴﹁〃绀哄瓧绗︽暟鐩備緥濡傦紝瑕佹兂淇濆瓨涓涓TINYTEXT鍊奸渶瑕L瀛楃+ 1涓瓧鑺傘

瑕佹兂璁$畻鐢ㄤ簬淇濆瓨鍏蜂綋CHARVARCHAR鎴栬TEXT鍒楀肩殑瀛楄妭鏁帮紝闇瑕佽冭檻璇ュ垪浣跨敤鐨勫瓧绗﹂泦銆傚湪鍏蜂綋鎯呭喌涓紝褰撲娇鐢Unicode鏃讹紝蹇呴』璁颁綇鎵鏈Unicode瀛楃浣跨敤鐩稿悓鐨勫瓧鑺傛暟銆備负浜嗙粏鍒嗙敤浜庝笉鍚岀被Unicode瀛楃浣跨敤鐨勫瓨鍌紝鍙傝10.5鑺傦紝鈥淯nicode鏀寔鈥

娉ㄩ噴锛VARCHAR鍒楃殑鏈夋晥鏈澶ч暱搴︿负65,532瀛楃銆

NDBCLUSTER寮曟搸鍙敮鎸佸浐瀹氬搴︾殑鍒椼傝繖璇存槑MySQL绨囦腑鐨勮〃涓殑VARCHAR鍒楃殑琛屼负濡傚悓绫诲瀷CHAR(涓嶅悓鐨勬槸姣忎釜璁板綍浠嶇劧鏈変竴涓澶栧瓧鑺傜┖闂)銆備緥濡傦紝鍦Cluster琛ㄤ腑锛屽0鏄庝负VARCHAR(100)鐨勫垪涓殑姣忎釜璁板綍瀛樺偍鏃跺皢鍗犵敤101涓瓧鑺傦紝鏃犺瀹為檯瀛樺偍鐨勮褰曚腑鐨勫瓧绗︿覆鐨勯暱搴︿负澶氬皯銆

BLOBTEXT绫婚渶瑕 123鎴栬4涓瓧鑺傛潵璁板綍鍒楀肩殑闀垮害锛屽彇鍐充簬璇ョ被鐨勬渶澶у彲鑳界殑闀垮害銆傚弬瑙11.4.3鑺傦紝鈥淏LOB鍜孴EXT绫诲瀷

NDB Cluster瀛樺偍寮曟搸涓紝TEXTBLOB鍒楃殑瀹炴柦鏄笉鍚岀殑锛屽叾涓TEXT鍒椾腑鐨勬瘡涓褰曠敱涓や釜鍗曠嫭閮ㄥ垎缁勬垚銆備竴涓槸鍥哄畾澶у皬(256瀛楄妭)锛屽苟涓斿疄闄呬笂淇濆瓨鍦ㄥ師琛ㄤ腑銆傚彟涓涓寘鎷秴鍑256瀛楄妭鐨勪换浣曟暟鎹紝淇濆瓨鍦ㄩ殣鍚殑琛ㄤ腑銆傜2涓〃涓殑璁板綍鎬绘槸2,000瀛楄妭闀裤傝繖璇存槑濡傛灉size<= 256TEXT鍒楃殑澶у皬涓256(鍏朵腑size琛ㄧず璁板綍鐨勫ぇ灏)锛涘惁鍒欙紝澶у皬鏄256 +size+(2000(size256)%2000)

ENUM瀵硅薄鐨勫ぇ灏忕敱涓嶅悓鐨勬灇涓惧肩殑鏁扮洰纭畾銆傛灇涓剧敤涓涓瓧鑺傦紝鍙互鏈255涓彲鑳界殑鍊笺傚綋鏋氫妇鐨勫间綅浜25665,535涔嬮棿鏃讹紝鐢ㄤ袱涓瓧鑺傘傚弬瑙11.4.4鑺傦紝鈥淓NUM绫诲瀷鈥

SET瀵硅薄鐨勫ぇ灏忕敱涓嶅悓鐨set鎴愬憳鐨勬暟閲忕‘瀹氥傚鏋set澶у皬鏄N锛屽璞″崰(N+7)/8涓瓧鑺傦紝鍥涜垗浜斿叆鍒1234鎴栬8涓瓧鑺傘SET鏈澶氬彲浠ユ湁64涓垚鍛樸傚弬瑙11.4.5鑺傦紝鈥淪ET绫诲瀷鈥

11.6. 閫夋嫨姝g‘鐨勫垪绫诲瀷

涓轰簡浼樺寲瀛樺偍锛屽湪浠讳綍鎯呭喌涓嬪潎搴斾娇鐢ㄦ渶绮剧‘鐨勭被鍨嬨備緥濡傦紝濡傛灉鍒楃殑鍊肩殑鑼冨洿涓轰粠199999锛岃嫢浣跨敤鏁存暟锛屽垯MEDIUMINT UNSIGNED鏄ソ鐨勭被鍨嬨傚湪鎵鏈夊彲浠ヨ〃绀鸿鍒楀肩殑绫诲瀷涓紝璇ョ被鍨嬩娇鐢ㄧ殑瀛樺偍鏈灏戙

鐢ㄧ簿搴︿负65浣嶅崄杩涘埗鏁(鍩轰簬10)DECIMAL 鍒楄繘琛屾墍鏈夊熀鏈绠(+-*/)銆傚弬瑙11.1.1鑺傦紝鈥滄暟鍊肩被鍨嬫杩扳

浣跨敤鍙岀簿搴︽搷浣滃DECIMAL鍊艰繘琛岃绠椼傚鏋滃噯纭害涓嶆槸澶噸瑕佹垨濡傛灉閫熷害涓烘渶楂樹紭鍏堢骇锛DOUBLE绫诲瀷鍗宠冻澶熶簡銆備负浜嗚揪鍒伴珮绮惧害锛屽彲浠ヨ浆鎹㈠埌淇濆瓨鍦BIGINT涓殑瀹氱偣绫诲瀷銆傝繖鏍峰彲浠ョ敤64浣嶆暣鏁拌繘琛屾墍鏈夎绠楋紝鏍规嵁闇瑕佸皢缁撴灉杞崲鍥炴诞鐐瑰笺

11.7. 浣跨敤鏉ヨ嚜鍏朵粬鏁版嵁搴撳紩鎿庣殑鍒楃被鍨

涓轰簡浣跨敤鐢卞叾瀹冨崠鏂圭紪鍐欑殑SQL鎵ц浠g爜锛MySQL鎸夌収涓嬭〃鎵绀哄鍒楃被鍨嬭繘琛屾槧灏勩傞氳繃杩欎簺鏄犲皠锛屽彲浠ュ緢瀹规槗鍦颁粠鍏跺畠鏁版嵁搴撳紩鎿庡皢琛ㄥ畾涔夊鍏ュ埌MySQL涓細

鍏跺畠鍗栨柟绫诲瀷

MySQL绫诲瀷

BOOL,

TINYINT

BOOLEAN

TINYINT

CHAR VARYING(M)

VARCHAR(M)

DEC

DECIMAL

FIXED

DECIMAL

FLOAT4

FLOAT

FLOAT8

DOUBLE

INT1

TINYINT

INT2

SMALLINT

INT3

MEDIUMINT

INT4

INT

INT8

BIGINT

LONG VARBINARY

MEDIUMBLOB

LONG VARCHAR

MEDIUMTEXT

LONG

MEDIUMTEXT

MIDDLEINT

MEDIUMINT

NUMERIC

DECIMAL

鍦ㄥ垱寤鸿〃鏃跺鍒楃被鍨嬭繘琛屾槧灏勶紝鐒跺悗鍘熸潵鐨勭被鍨嬪畾涔夎涓㈠純銆傚鏋滀綘浣跨敤鍏跺畠鍗栨柟鐨勭被鍨嬪垱寤轰竴涓〃锛岀劧鍚庢墽琛DESCRIBE tbl_name璇彞锛MySQL浣跨敤绛夋晥鐨MySQL绫诲瀷鏉ユ姤鍛婅〃鐨勭粨鏋勩備緥濡傦細

mysql> CREATE TABLE t (a BOOL, b FLOAT8, c LONG, d NUMERIC);
Query OK, 0 rows affected (0.08 sec)
 
mysql> DESCRIBE t;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| a     | tinyint(1)    | YES  |     | NULL    |       |
| b     | double        | YES  |     | NULL    |       |
| c     | mediumtext    | YES  |     | NULL    |       |
| d     | decimal(10,0) | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

杩欐槸MySQL鍙傝冩墜鍐岀殑缈昏瘧鐗堟湰锛屽叧浜嶮ySQL鍙傝冩墜鍐岋紝璇疯闂dev.mysql.com銆 鍘熷鍙傝冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆