English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

6 answers

The CHAR and VARCHAR types are similar, but differ in the way they are stored and retrieved. So take a bit time to read what I am telling you.

The CHAR and VARCHAR types are declared with a length that indicates the maximum number of characters you want to store. For example, CHAR(M) can hold up to M characters. Same thing with VARCHAR(M), where M is an integer > 1 (Not great to make it one, I will explain later)

The length of a CHAR column is fixed to the length that you declare when you create the table. So when you do the CREATE TABLE Syntax, and you declare a length of CHAR, that value is used throughout the fixed structure. The length can be any value from 0 to 255.

Pay attention that when CHAR values are stored, they are right-padded with spaces to the specified length. When CHAR values are retrieved, trailing spaces are removed. So if I input " Hey" and I read it back it will say "hey" But it is stored as " hey" (waste of space)

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535. (The maximum effective length of a VARCHAR in some DBMS is determined by the maximum row size and the character set used. The maximum length overall is 65,532 bytes.)

In contrast to CHAR, VARCHAR values are stored using only as many characters as are needed, plus one byte to record the length (two bytes for columns that are declared with a length longer than 255).

VARCHAR values are not padded when they are stored. Handling of trailing spaces is version-dependent. Some DBMS include trailing spaces and some dont.

If you assign a value to a CHAR or VARCHAR column that exceeds the column's maximum length, the value is truncated to fit that length.

I hope I helped, I gave you the detailed definition of what it is used. Some example on how they are is in this thing I did in the bottom.


(Value) => CHAR(4) = Storage Required

'' => ' ' = 4 bytes
'ab' => 'ab ' = 4 bytes
'abcd' => 'abcd' = 4 bytes
'abcdefgh' => 'abcd' = 4 bytes


(Value) => VARCHAR(4) = Storage Required

'' => '' = 1 byte
'ab' => 'ab' = 3 bytes
'abcd' => 'abcd' = 5 bytes
'abcdefgh' => 'abcd' = 5 bytes

Notice how they change

2006-06-13 04:04:54 · answer #1 · answered by ? 6 · 0 0

Difference Between Char And Varchar

2016-12-13 09:18:56 · answer #2 · answered by Anonymous · 0 0

Char Vs Varchar

2016-11-04 22:31:27 · answer #3 · answered by casson 4 · 0 0

The difference is how the value is stored in the field. If a field has a datatype of varchar, then the amount of space it takes to store the value is variable. If the datatype is char, then the amount of space it takes to store the value is static.

For example, if you have a name field that is size 20 char, then a name like 'Tony' will take up 20 characters in the field even though the actual number of letters in 'Tony' is four.

If you have a name field that is size 20 varchar, then a name like 'Tony' will take up 4 characters in the field, which is the actual number of letters in 'Tony'.

Using varchar saves space. You should only use char when you know the size of the value of the field will always be the same, such as zip code (which is always 5 characters).

2006-06-13 03:47:31 · answer #4 · answered by K C 1 · 0 0

A char field holds the number of characters that you specify in the defining length. This will include trailing blanks.

A varchar field is a variable length complex field. This is made up of a 2 byte length field followed by the string.
The length must be set as well as the string.

2006-06-13 04:05:53 · answer #5 · answered by AnalProgrammer 7 · 0 0

var

2006-06-13 03:37:54 · answer #6 · answered by sexxxy 2 · 0 0

fedest.com, questions and answers