I understand that if the length is > 255 it will require 2 bytes to store the length prefix. I just need to verify my assumptions. Please tell me the length prefix and the length of string('abcd') for charsets(latin1, ucs2, utf8mb4). For a varchar(20) column. The documentaton says:
[...] For the string 'abcd', L is 4 and the storage requirement is five bytes. If the same column is instead declared to use the ucs2 double-byte character set, the storage requirement is 10 bytes: The length of 'abcd' is eight bytes and the column requires two bytes to store lengths because the maximum length is greater than 255 (up to 510 bytes). [...]
Reference: 11.7 Data Type Storage Requirements (Oracle | Docs)
I just don't understand why for 'abcd' with charset 'ucs2' it requires 2 byte length prefix even after it being less than 255.
[current length] <> [maximum length]
. The 2 bytes are likely required for all potential future values, even if the current value doesn't explicitly require them. It's the same reason there's an airbag on the passenger side of my car, even when I'm driving alone, and even if I only ever drive alone.