1. 程式人生 > >mysql中char(n)和varchar(n)在資料存入的時候關於是否丟棄最後的空格的區別

mysql中char(n)和varchar(n)在資料存入的時候關於是否丟棄最後的空格的區別

【字串】:
char(length) [BINARY | ASCII | UNICODE] :  本型別資料,存入的時候,會丟棄最後的空格,如存入 'hello  ',只存'hello'

varchar(length) [BINARY] :本型別資料,存入的時候,保留所有的空格

測試程式碼如下: 

drop table if exists temp;
create table if not exists temp(
testChar char(10),
testVarChar varchar(10)
);
insert into temp values('  hello  ','  hello  ');

select length(testChar),CHAR_LENGTH(testVarChar) from temp;

 #返回 7 9
select concat(testChar,'***'),concat(testVarChar,'***') from temp; #返回  hello*** 和  hello  ***