SQL Data Types
SQL data types define the type of value that can be stored in a table column. For example, if you want a column to store only integer values, you can define its data type as INT.
SQL data types can be broadly divided into the following categories.
- Numeric data types such as: INT, TINYINT, BIGINT, FLOAT, REAL, etc.
- Date and Time data types such as: DATE, TIME, DATETIME, etc.
- Character and String data types such as: CHAR, VARCHAR, TEXT, etc.
- Unicode character string data types such as: NCHAR, NVARCHAR, NTEXT, etc.
- Binary data types such as: BINARY, VARBINARY, etc.
- Miscellaneous data types - CLOB, BLOB, XML, CURSOR, TABLE, etc.
Note: Not all data types are supported by every relational database vendor.
For example, the Oracle database doesn't support DATETIME, and MySQL doesn't support CLOB. When designing database schemas and writing SQL queries, make sure to check if the data types are supported.
SQL Numeric Data Types
Data Type | from-to |
BIT | 0 to 1 |
TINYINT | 0 to 255 |
SMALLINT | -32,768 to 32,767 |
INT | -2,147,483,648 to 2,147,483,647 |
BIGINT | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
DECIMAL | -1038 + 1 to 1038 - 1 |
NUMERIC | -1038 + 1 to 1038 - 1 (same as DECIMAL) |
FLOAT | -1.79E+308 to 1.79E+308 |
REAL | -3.40E+38 to 3.40E+38 |
SQL Date and Time Data Types
Data Type | Description |
DATE | Stores date in the format YYYY-MM-DD |
TIME | Stores time in the format HH:MI:SS |
DATETIME | Stores date and time information in the format YYYY-MM-DD HH:MI:SS |
TIMESTAMP | Stores number of seconds passed since the Unix epoch ('1970-01-01 00:00:00' UTC) |
YEAR | Stores year in a 2-digit or 4-digit format. Range 1901 to 2155 in 4-digit format. Range 70 to 69, representing 1970 to 2069. |
SQL Character and String Data Types
Note: These data types are for character streams. They should not be used with Unicode data.
Data Type | Description |
CHAR | Fixed length with a maximum length of 8,000 characters |
VARCHAR | Variable-length storage with a maximum length of 8,000 characters |
VARCHAR(max) | Variable-length storage with provided max characters, not supported in MySQL |
TEXT | Variable-length storage with a maximum size of 2GB data |
Difference between VARCHAR
and TEXT
In MySQL, TEXT
can store bigger amounts of data up to a maximum of 4 gigabytes (232−1232−1) characters, whereas VARCHAR
can store strings of variable length up to 65,535 characters.
A VARCHAR
can be part of an index whereas a TEXT
field requires you to specify a prefix length, which can be part of an index.
VARCHAR
is stored inline with the table (at least for the MyISAM storage engine), making it potentially faster when the size is reasonable. Of course, how much faster depends on both your data and your hardware. Meanwhile, TEXT
is stored off table with the table having a pointer to the location of the actual storage.
Using a TEXT
column in a sort will require the use of a disk-based temporary table, as the MEMORY (HEAP) storage engine.
VARCHAR
and TEXT
are both PostgreSQL character data types. VARCHAR(N)
is an alias for CHARACTER VARYING(N)
, a data type that accepts text data. N is a positive integer and represents the maximum number of characters the type can store in length. In other words, a VARCHAR(N)
column can store a string of up to N characters. When trying to store a longer string, PostgreSQL raises the following error:
Value too long for type character varying(N)
As required by the SQL standard, if the characters in excess are all spaces, the string will be truncated to the maximum length and no error will be thrown.
Instead, when casting a value to CHARACTER VARYING(N)
or VARCHAR(N)
, the over-length string will simply be truncated to the first N characters without raising an error. Again, this is a rule imposed by the SQL standard. The length N is optional and must be an integer greater than zero and smaller than or equal to 10485760. When N is omitted, VARCHAR
is equivalent to TEXT
.
TEXT
is a data type to store strings of any length. Specifically, it is PostgreSQL's native string data type as most built-in functions operating on strings accept or return TEXT
values. Keep in mind that TEXT
is not in the SQL standard data type, but most DMBS technologies provide it.
The characters that can be stored in either of these data types are determined by the database character set, which is chosen when creating the database. To avoid slowing down access to columns with short values, very long strings are stored in background tables. In any case, the longest character string that can be stored in VARCHAR
or TEXT
is about 1 GB.
The way that TEXT
and VARCHAR
are stored in the database is another difference between them. Given that VARCHAR
columns hold data in a variable-length format, the amount of storage space needed for each item depends on how long it is. Contrarily, because TEXT
columns are stored in a static-length format, the amount of storage space needed for each value is constant.
Due to these variations, one data type may be better suited for a certain use case than the other. For storing short to medium-length strings like names, locations, and descriptions, VARCHAR
is typically a preferable option. For storing greater amounts of textual data, such as articles, comments, and messages, TEXT
is a preferable option.
There are performance differences between TEXT
and VARCHAR
in addition to differences in the volume of data they can contain. VARCHAR
typically performs better than TEXT
because it needs less storage and provides quicker access to the data. When handling higher amounts of data, this speed benefit may be lost.
SQL Unicode Character and String Data Types
Note: These data types are not supported in MySQL databases.
Data Type | Description |
NCHAR | Fixed length with a maximum length of 4,000 characters |
NVARCHAR | Variable-length storage with a maximum length of 4,000 characters |
NVARCHAR(max) | Variable-length storage with provided max characters |
NTEXT | Variable-length storage with a maximum size of 1GB data |
SQL Binary Data Types
Data Type | Description |
BINARY | Fixed length with a maximum length of 8,000 bytes |
VARBINARY | Variable-length storage with a maximum length of 8,000 bytes |
VARBINARY(max) | Variable-length storage with provided max bytes |
IMAGE | Variable-length storage with a maximum size of 2 GB binary data |
SQL Miscellaneous Data Types
Data Type | Description |
CLOB | Character large objects that can hold up to 2 GB |
BLOB | For large binary objects |
XML | For storing XML data |
JSON | For storing JSON data |