what’s the difference between CHAR and VARCHAR in database

tags: learning database diff-between

content

for varchar(50),

  • a record has:
    • a length prefix (usually 1 byte)
    • the actual string data
  • when a string is an empty string ''
    • it has a length prefix to indicate its length is 0 (1 byte)
    • and the actual string data is 0 (0 byte)
    • total space taken is 1 byte
  • when a string is full length (50)
    • length prefix indicating its length is 50
    • actual string data (1 character is 1 byte, 50 characters are 50 bytes)
    • total space taken is 50 bytes

for char(50),

  • no length prefix
  • every record takes 50 bytes

up

down

reference