2010-06-01 50 views

回答

0

确实,MySQL docs指定“当CHAR值被检索时,尾随空格被删除。”这很奇怪,因为其他数据库似乎总是保持填充(我可以证实对于Oracle)。 SQL-92 standard指示右侧填充的空格是char的一部分,例如,在p上的CAST函数的定义中。 148.当源(SV =源值)和目标(TV =目标值,LTD =目标数据类型的长度),则:

ii) If the length in characters of SV is larger than LTD, then 
    TV is the first LTD characters of SV. If any of the re- 
    maining characters of SV are non-<space> characters, then a 
    completion condition is raised: warning-string data, right 
    truncation. 

iii) If the length in characters M of SV is smaller than LTD, 
    then TV is SV extended on the right by LTD-M <space>s. 

也许这是MySQL的许多odditiesgotchas的只是另一个。

并回答你的问题:如果你不想尾随空格,你应该使用VARCHAR来代替。

0

我认为'char'的定义是空格填充以填充字段。它们被认为是固定长度,并且将空间填充为固定长度。

数据类型'varchar'被定义为变量char,它们是而不是填充空间的空间。

虽然我通常在SQL Server上工作,但我可能是错的。

+0

你可能是对的,但在MySQL中似乎修剪了字段。 – Dan 2010-06-01 15:44:21