[Check Point 12.1] Simple Queries in SQL – Chapter 12 Sumita Arora

|
Table of Contents

4. Compare CHAR and VARCHAR datatypes.

  • CHAR
    • Fixed-length string data type where you specify the length. It always occupies the same amount of storage space, padding with spaces if necessary.
    • (For example, if you define a CHAR(10) column and insert the value ‘hello’ into it, MySQL will store it as ‘hello_____’ (where ‘_’ represents a space), padding the value with spaces to make it 10 characters long.)
      This behavior ensures that each value stored in a CHAR column occupies the same amount of storage space, regardless of its actual length.
  • VARCHAR
    • Variable-length string data type where you specify the maximum length. It only occupies the necessary storage space for the actual data, without padding with spaces.
    • For example, if you define a VARCHAR(10) column and insert the value ‘hello’ into it, MySQL will store it as ‘hello’, without any additional spaces added. The column will only occupy as much storage space as needed to store the actual value.
      There are no additional spaces added to make it 10 characters long, unlike CHAR data type where spaces are added for padding.
"Spread the light within, illuminate the hearts around you. Sharing is not just an action, but a sacred journey of connecting souls."~ Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *