Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much real storage is used with a varchar(100) declaration in mysql?

Tags:

sql

mysql

If I have a table with a field which is declared as accepting varchar(100) and then I actually insert the word "hello" how much real storage will be used on the mysql server? Also will an insert of NULL result in no storage being used even though varchar(100) is declared?

What ever the answer is, is it consistent accross different database implementations?

like image 729
Benj Avatar asked Nov 24 '09 14:11

Benj


People also ask

How much storage does varchar use?

SQL varchar usually holds 1 byte per character and 2 more bytes for the length information.

What does varchar 100 mean?

VARCHAR is a variable length string data type, so it holds only the characters you assign to it. VARCHAR takes up 1 byte per character, + 2 bytes to hold length information. For example, if you set a VARCHAR(100) data type = 'Jen', then it would take up 3 bytes (for J, E, and N) plus 2 bytes, or 5 bytes in all.

How big is a varchar 200?

Show activity on this post. 100 characters. To be clear: Storing a string 100 characters in a varchar(200) field will take 101 bytes.


1 Answers

If I have a table with a field which is declared as accepting varchar(100) and then I actually insert the word "hello" how much real storage will be used on the mysql server?

Mysql will store 5 bytes plus one byte for the length. If the varchar is greater than 255, then it will store 2 bytes for the length.

Note that this is dependent on the charset of the column. If the charset is utf8, mysql will require up to 3 bytes per character. Some storage engines (i.e. memory) will always require the maximum byte length per character for the character set.

Also will an insert of NULL result in no storage being used even though varchar(100) is declared?

Making a column nullable means that mysql will have to set aside an extra byte per up to 8 nullable columns per row. This is called the "null mask".

What ever the answer is, is it consistent accross different database implementations?

It's not even consistent between storage engines within mysql!

like image 129
ʞɔıu Avatar answered Sep 20 '22 19:09

ʞɔıu