LESSON 41 / 62 · String data typeString 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
| Type | Description | Range 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.
| Type | Description | Range 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.
| Type | Description | Range of characters |
|---|---|---|
| BLOB | Contains binary strings. | Maximum length 65,535 |
| TEXT | Contains 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.
| Type | Range of characters |
|---|---|
| TINYBLOB | Maximum length 255 |
| MEDIUMBLOB | Maximum length 16,777,215 |
| LONGBLOB | Maximum length 4,294,967,295 |
| TINYTEXT | Maximum length 255 |
| MEDIUMTEXT | Maximum length 16,777,215 |
| LONGTEXT | Maximum length 4,294,967,295 |
Practice
Solve it in the editor. The query runs against a sample database in your browser.
- 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
Write a query and run it to see the results.