differences between data types of text, char and varchar

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
aaaphp000000
Forum Newbie
Posts: 22
Joined: Mon Aug 29, 2005 5:39 am

differences between data types of text, char and varchar

Post by aaaphp000000 »

could u tell me the differences between data types of text, char and varchar?
i focus on size of each type used.

thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

http://dev.mysql.com/doc/mysql/en/char.html

Basically char is fixed length given by "char(x)" where x is the length of the string between 0 and 255. It uses x+2 bytes of storage if I remember correctly. If the string entered is shorter than x mysql adds spaces to ensure consistency in the number of bytes used.

varchar is variable length up to a maximum length of x "varchar(x)". Until MySQL 5 this was 0 to 255 too. In MySQL 5 it goes into 65K in length.

text is for longer extracts of text. It is all covered in the manual though ;)
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

CHAR : 0-255 chars: takes as much space as defined.
VARCHAR : 0-255 chars : takes as much space as len of string
TEXT: 0-65535 chars : same as above

edit: I see that our old race continues here, d11wtq :lol: difference of 1-2 minutes in posting together.
aaaphp000000
Forum Newbie
Posts: 22
Joined: Mon Aug 29, 2005 5:39 am

Post by aaaphp000000 »

my host uses mysql 4.0 (or 4.1 - can not remember now).

the size or storage (in bytes) for
char(x) is : x+2
varchar(x) is : len+1 (len<=x<=255)
text is : len+2 (len<=65535)

are above right?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Yep!
Post Reply