DEV Community

cenan ozen for DbModeller.net

Posted on

Exploring String Field Types in Databases: CHAR, VARCHAR, and TEXT

In this post, we will explore and compare the different string field types commonly used in databases: CHAR, VARCHAR, and TEXT. We will discuss their characteristics, use cases, and the considerations that DBAs and developers should keep in mind when choosing the right string field type.

Which string type you choose will depend on the specific needs of your application. If you need to store a fixed length string, such as a ZIP code, then you should use a CHAR column. If you need to store a variable-length string, such as a customer name, then you should use a VARCHAR column. If you need to store a very large string, or binary data, then you should use a TEXT or BLOB column.

Limitations

There usually is a limit, for the length of the field depending on the type.

Here are the maximum VARCHAR field sizes for some common databases:

  • MySQL: the maximum size of a VARCHAR field is 65,535 bytes.
  • PostgreSQL: the maximum size of a VARCHAR field is 1,073,741,823 characters. (1 GB)
  • Oracle Database: the maximum size of a VARCHAR field is 4,000 bytes (which can be fewer than 4,000 characters, depending on character encoding).
  • Microsoft SQL Server: the maximum size of a VARCHAR field is 8,000 bytes.
  • SQLite: the maximum size of a VARCHAR field is 2,147,483,647 bytes. (2 GB)
  • IBM Db2: In IBM Db2, the maximum size of a VARCHAR field depends on the version and configuration, but it is typically at least 32,767 bytes.

and for the CHAR type:

  • In MySQL, the maximum size of a CHAR field is 255 characters. However, this limit can be increased to 65,535 characters by modifying the row format to DYNAMIC or COMPRESSED.
  • PostgreSQL: the maximum size of a CHAR field is 1,073,741,823 characters.
  • Oracle Database, the maximum size of a CHAR field is 2,000 bytes (which can be fewer than 2,000 characters, depending on character encoding).
  • Microsoft SQL Server: the maximum size of a CHAR field is 8,000 bytes.
  • In SQLite, the maximum size of a CHAR field is 2,147,483,647 bytes.
  • In IBM Db2, the maximum size of a CHAR field depends on the version and configuration, but it is typically at least 32,767 bytes.

and finally for the TEXT field type:

Database Maximum size
MySQL 4 GB
PostgreSQL 1 GB
Oracle Database 4 GB
Microsoft SQL Server 2 GB
IBM DB2 2 GB

Performance

Which string type you choose will also affect the performance of your database application. CHAR columns are generally the fastest, followed by VARCHAR columns. TEXT and BLOB columns are the slowest.

Conclusion

Thank you for reading, and see you on another post 👋👋

Top comments (0)