varchar vs char in SQL
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answer ( 1 )
If we talk in easy language, CHAR(n) will always be n bytes long. As the string is <n, the rest of the spaces will be a blank padded area to ensure the complete length of 'n'. Whereas VARCHAR2(n) on the other side will only use the necessary amount of bytes for the string. It will automatically shrink to meet the size of the string.
Eg: Storing "PYTHON" in CHAR(15) will use 17 bytes for the process as 2 bytes are allocated for reading length.
Same string if stored in VARCHAR(15), only 8 bytes will be consumed as 6 bytes for the string and the rest 2 bytes for reading.
So, it's advisable to use VARCHAR2 to avoid memory waste. Rest, the usage is situation dependent.