Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools Database Foundations Introduction to Data, Databases and SQL Creating Tables

VARCHAR(xxx) memory space allocation

mysql CREATE TABLE movies (title VARCHAR(200), year INTEGER); Does it means that each row in the database is going to occupy 204 bytes (200 bytes for the movie title plus 4 bytes for the integer), no matter the data in it?

2 Answers

Hugo Paz
Hugo Paz
15,622 Points

No. The size will depend of what is stored there. The only issue might be possible performance impact: in MySQL, temporary tables and MEMORY tables store a VARCHAR column as a fixed-length column, padded out to its maximum length. If you design VARCHAR columns much larger than the greatest size you need, you will consume more memory than you have to. This affects cache efficiency, sorting speed, etc.

Hi Hugo, Great thanks for your answer. I read in the MySql documentation that temporary tables are created and dropped during the same session and MEMORY tables live only in RAM. Does this mean that when a MySQL server works with the tables, it stores VARCHAR as a fixed length column in RAM, but optimise the size when storing to hard disk? Cheers Luca

Matthew Day
Matthew Day
6,740 Points

Great question. I, too, would like to know the answer to this one.