Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

varchar(20) and varchar(50) are same?

Tags:

mysql

varchar

I saw comment "If you have 50 million values between 10 and 15 characters in a varchar(20) column, and the same 50 million values in a varchar(50) column, they will take up exactly the same space. That's the whole point of varchar, as opposed to char.". Can Anybody tell me the reason? See What is a reasonable length limit on person "Name" fields?

like image 446
Mohammed H Avatar asked Jun 21 '12 06:06

Mohammed H


1 Answers

Yes, that is indeed the whole point of VARCHAR. It only takes up as much space as the text is long.

If you had CHAR(50), it would take up 50 bytes (or characters) no matter how short the data really is (it would be padded, usually by spaces).

Can Anybody tell me the reason?

Because people thought it was wasteful to store a lot of useless padding, they invented VARCHAR.

like image 113
Thilo Avatar answered Sep 18 '22 19:09

Thilo