談MySQL中char varchar區別
偶爾間,發現一處bug:程式要求將系統當前時間及相關配置資訊按照指定幀結構,發給遠端socket,但有時總會少1個位元組。由於處理流程是在儲存過程中完成的,遂定位到錯誤,歸結為對char varchar區別不夠了解的原因。
The CHAR and VARCHAR types are similar, but differ in the way they are stored and retrieved. They also differ in maximum length and in whether trailing spaces are retained.
MySQL中CHAR VARCHAR的區別(為了不影響大家的理解,特將手冊相關段落附下)。
The CHAR and VARCHAR types are similar, but differ in the way they are stored and retrieved. They also differ in maximum length and in whether trailing spaces are retained.
The length of a CHAR column is fixed to the length that you declare when you create the table. The length can be any value from 0 to 255. When CHAR values are stored, they are right-padded with spaces to the specified length. When CHAR values are retrieved, trailing spaces are removed unless the PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.
Values in VARCHAR column are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size and the character set used.
In contrast to CHAR, VARCHAR values are stored as a one-byte or two-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.
If strict SQL mode is not enabled and you assign a value to a CHAR or VARCHAR column that exceeds the column's maximum length, the value is truncated to fit and a warning is generated. For truncation of nonspace characters, you can cause an error to occur and suppress insertion of the value by using strict SQL mode.
For VARCHAR columns, trailing spaces in excess of the column length are truncated prior to insertion and a warning is generated, regardless of the SQL mode in use. For CHAR columns, truncation of excess trailing spaces from inserted values is performed silently regardless of the SQL mode.
由此可見,在CHAR欄位中如果結尾是空格(CHAR(0x20)),在獲取其值時,將被自動截斷,這也是bug存在的地方。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25432352/viewspace-736130/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL中CHAR和VARCHAR區別MySql
- MySQL 中 VARCHAR 最大長度及 CHAR 和 VARCHAR 的區別MySql
- MySQL中int、char、varchar的效能淺談MySql
- Mysql中varchar與char的區別以及varchar(30)中的30代表的涵義MySql
- varchar和char的區別
- char ,varchar2和varchar的區別
- MySQL字元資料型別char與varchar的區別MySql字元資料型別
- NTMySQL中varchar和char型別的區別heeMySql型別
- oracle中char與varchar2的區別Oracle
- char和varchar2的區別
- Oracle CHAR,VARCHAR,VARCHAR2,nvarchar型別的區別與使用Oracle型別
- char(n)和varchar2(n)區別
- MySQL資料型別操作(char與varchar)MySql資料型別
- MySQL中資料型別(char(n)、varchar(n)、nchar(n)、nvarchar(n)的區別)MySql資料型別
- 關於mysql設定varchar 欄位的預設值''和null的區別,以及varchar和char的區別MySqlNull
- 【基礎】Oracle CHAR,VARCHAR,VARCHAR2,nvarchar型別的區別與使用Oracle型別
- char,nchar,varchar,nvarchar的區別和特點
- Sql server char,nchar,varchar與Nvarchar的區別SQLServer
- MySQL CHAR和VARCHAR資料型別介紹MySql資料型別
- mysql的varchar(N)和int(N)的含義及其與char區別MySql
- VARCHAR2(N CHAR)與VARCHAR2(N)的區別[Oracle基礎]Oracle
- 【MySQL】淺談varchar(N)MySql
- 【MySQL】淺談 varchar(N)MySql
- Oracle的資料型別:char/varchar2Oracle資料型別
- char nchar varchar2 nvarchar2 區別-Oracle (varchar2 max 4K, 2K漢字)Oracle
- char str[]和char *str的區別
- 【基礎】ORACLE CHAR 與 VARCHAROracle
- MYSQL INNODB主鍵使用varchar和int的區別MySql
- mysql中varchar型別最大長度測試MySql型別
- 談談mysql和redis的區別MySqlRedis
- const char*, char const*, char*const的區別
- Rust中 String、str、&str、char 的區別Rust
- 自導自演的面試現場:說說char 和 varchar的區別你瞭解多少?面試
- Oracle 資料型別CHAR, NCHAR, VARCHAR2, NVARCHAR2Oracle資料型別
- Oracle 中varchar2 和nvarchar2區別Oracle
- MySQL中varchar和char定義長度是字元,與Oracle nvarchar2類似MySql字元Oracle
- const char*, char const*, char*const 有何區別?
- Proc中使用char陣列、VARCHAR陣列和char變數進行DELETE操作具體行數的細微區別陣列變數delete