RienSQL
LESSON 41 / 62 · String data type

String data type

Working with strings in MySQL and PostgreSQL. Basic data types for text information.

The string data type is the most commonly used data type. Thanks to this, both text and various binary data (for example, pictures) are stored in the database.

In MySQL, it is represented by the following types:

CHAR and VARCHAR

TypeDescriptionRange of characters
CHAR(X)Contains non-binary strings. The length is fixed, you specify it when declaring. If the length of the string is less than the specified one, it is padded with right spaces to the specified length.The length can be any in the range from 0 to 255
VARCHAR(X)Contains non-binary strings. The length of the lines is dynamic.The length can be any in the range from 0 to 65,535

BINARY and VARBINARY

The BINARY and VARBINARY data types are similar to VARCHAR and CHAR except that they store binary strings.

TypeDescriptionRange of characters
BINARY(X)Contains binary strings. The length is fixed, you specify it when declaring.The length can be any in the range from 0 to 255
VARBINARY(X)Contains binary strings. The length of the lines is dynamic.The length can be any in the range from 0 to 65,535

BLOB and TEXT

BLOB is used to store large binary data such as pictures. TEXT is also for storing big data, but textual content.

The difference between them is that the sorts and comparisons of stored data for BLOB are case sensitive and not case sensitive in TEXT fields.

TypeDescriptionRange of characters
BLOBContains binary strings.Maximum length 65,535
TEXTContains text lines.Maximum length 65,535

BLOB and TEXT have additional subtypes that differ in the maximum data size that can be stored in them.

TypeRange of characters
TINYBLOBMaximum length 255
MEDIUMBLOBMaximum length 16,777,215
LONGBLOBMaximum length 4,294,967,295
TINYTEXTMaximum length 255
MEDIUMTEXTMaximum length 16,777,215
LONGTEXTMaximum length 4,294,967,295

Practice

Solve it in the editor. The query runs against a sample database in your browser.

Task 1Students table with string columns
Create the Students table with the following fields:
  • id is a field of type INTEGER that is the primary key
  • first_name is a required field of type VARCHAR(50) for the first name
  • last_name is a required field of type VARCHAR(50) for the last name
  • class_code is a required field of type CHAR(3) for a fixed-length class code
  • biography is a field of type TEXT for the student's bio
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.