Why does the MySQL CHAR datatype have no preceding length byte?
In MySQL documentation one can find that CHAR datatype has no preceding byte with string length.
In contrast VARCHAR datatype has 1 or 2 preceding bytes where string length is stored. Which is understandable. Database engine needs to know what is the string length in order to read it.
How does database engine know what is CHAR datatype length? What logic/algorithm is behind it?
The length of a CHAR column is fixed to the length that you declare when you create the table. The length can be any value from 0 to 255. When CHAR values are stored, they are right-padded with spaces to the specified length.
So there is no need to annotate the value in each row, because it's always exactly the same as the length defined for the column. The length is stored once in the table metadata, no matter how many rows belong to that table.
Whereas VARCHAR is stored in a variable-length number of bytes on each row, up to the maximum defined for the column.
In C, they might have used null-terminated strings instead of a length prefix. But in SQL, null is a legitimate character to store in a string, so that would not have been a good string terminator.