0

I have a simple query, It might be answered before but I didn't find the precise answer for the same.

My question is:

I created a table using JPA through entity. Now I shows string fields as varchar(255). Now I'm wondering that what will happen when I'll save some data in that field of size let say 10? Will it save that data in 10 bytes or it will use 255 bytes?

Any clarification will be appreciated.

2 Answers 2

2

I thought in MySql up-to 255 chars with a varchar it is the number of characters you store plus 1 for length. So if you put a value that's 10 characters long, that'll be 11 bytes on disk.

UPDATE Found a link to the documentation to back that up. First table demonstrates the difference between char and varchar: https://dev.mysql.com/doc/refman/8.0/en/char.html

0
0
  • Don't lose sleep over the issue, but...

  • VARCHAR(255) CHARACTER SET utf8mb4 needs a 2-byte length field, since it could up to 1020 bytes.

  • Certain temp table operations expand the VARCHAR into CHAR during complex SELECT statements, thereby taking more space, albeit temporarily.

  • Good practice is to use a reasonable max-length, but big enough for anything you might eventually put in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.