我表里一個列name是char(8),我插入一條記錄name是“tom”,我感覺我查length(name)結果應該是8,因為不足8的,后邊被補5個空格,這就叫固定長度。
但是事實我查length(name)結果是3,不應該啊?
uj5u.com熱心網友回復:
還真沒注意這個問題,原來的理解和樓主是一樣的,但官網檔案上是這樣寫的https://dev.mysql.com/doc/refman/5.7/en/char.html
If a given value is stored into the CHAR(4) and VARCHAR(4) columns, the values retrieved from the columns are not always the same because trailing spaces are removed from CHAR columns upon retrieval. The following example illustrates this difference:
uj5u.com熱心網友回復:
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.可以試著先執行 set session sql_mode = ’ PAD_CHAR_TO_FULL_LENGTH‘
再執行length函式試試看結果
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/109455.html
標籤:MySQL
