SQL Data Types
SQL Data Types
1.String Literals
2.Numeric Literals
3.Date and Time Literals
4.Hexadecimal Literals
5.Bit-Value Literals
6.Boolean Literals
7.NULL Values
String Literal
The string in MySQL is a sequence of characters or bytes that are enclosed in single quotes (') or double
quotes ("). For example, 'first string' and "second string" both are the same. The string of bytes is called
a binary string. Each binary string contains a character set and a collation.
A string literals can also be used with special characters escape sequences. These special characters are
summarised in a tabular form below:
Escape Sequence Character Represented by Sequence
\0 It represents ASCII NULL character.
\b It represents a backspace character.
\n It represents a newline character.
\r It represents carriage return character.
\t It represents tab character.
\\ It represents a backslash (\) character.
\% It represents a % character.
\_ It represents a backslash character.
Numeric Literals
Numeric literals in MySQL are used to specify the two types of literal values: the exact-
value (integer and decimal), and the approximate value (floating-point) literals. It can
be positive or negative values. The exact-value can have an integer, fraction, or both. An
approximate-value is mainly used for scientific notations that contain mantissa and
exponent.
Number Descriptions
Literals
Integer It is represented as a sequence of digits without any fractional parts. If the number preceded by -
sign, it is a negative integer. If the number is preceded by + sign, it is a positive integer. If the
number does not have any sign, it assumes a positive integer. For example, 55, +55, -55 are all
integer numbers.
Decimal It is represented as a sequence of digits with fractional parts. In other words, it contains a whole
number plus fractional part, which is separated by dot(.) operator or decimal point. It can be
integer and non-integer both. It produces the calculation in exact numeric form. For example,
325.90, 355.6 are decimal numbers.
Floating-Point It is a number that contains a floating decimal point. It means there are no fixed digits before and
after the decimal point. It contains two kinds of data types named float and double that produce
an approximate value. For example, 2.36E0, 0.005, and -2,328.679 are all floating-point numbers.
Date and Time Literals
Date and Time values in MySQL can be represented either in the quoted strings or numbers format, which
depends on the exact value and some factors. For example, MySQL interprets any of this '2020-09-22',
'20200922', and 20200922 as a valid date.
•The following table explains the format of date values in MySQL:
Date Format Descriptions
'YYYY-MM-DD' or It represents a date in string format with punctuations that can be used as a
'YY-MM-DD' delimiter between the date parts. For example, '2020-03-31', '2020/03/31',
and '2020^03^31' all are same date values.
'YYYYMMDD' or It represents a date in the string format without any punctuations or
'YYMMDD' delimiter between the date parts. For example, '20200422 and '200522' are
interpreted as '2007-05-23', but '071342' is illegal and becomes '0000-00-00'
date value.
YYYYMMDD or It represents a date in the number format. For example, 20200305 and
YYMMDD 200305 are interpreted as '2020-03-05' date value.
Date and Time Literals
The following table explains the format of time values in MySQL:
'YYYY-MM-DD hh:mm:ss' or It represents a date and time in string format with punctuations
'YY-MM-DD hh:mm:ss' that can be used as a delimiter between the date and time
parts. For example, '2020-05-31 12:30:45', '2020/05/31
12*30*45', and '2020@05@31 12^30^45' all are same values.
'YYYYMMDDhhmmss' or It represents a date in the string format without any
'YYMMDDhhmmss' punctuations or delimiter between the date and time parts. For
example, '20070523091528' and '070523091528' are
interpreted as '2007-05-23 09:15:28', but '071122129015' is
illegal and becomes '0000-00-00 00:00:00' date and time value.
YYYYMMDDhhmmss or It represents a date and time in the number format. For
YYMMDDhhmmss example, 20200105142500 and 200105142500 are interpreted
as '2020-01-05 14:25:00' date and time value.
Hexadecimal Literals
In the numbering system, hexadecimal can be represented as a whole number whose base is 16.
Hexadecimal literal values can be written in the following term:
ox'val'
oX'val'
o0xval
Here, val contains the hexadecimal digits in the range of (0..9 and A..F). In 0xval, leading 0x is case
sensitive; therefore we cannot write it as 0X'val'. However, in the case of lettercase of the digits, the
leading X or 0x does not matter for particular case-sensitive.
The below examples explain the legal and illegal hexadecimal literals:
Legal Illegal
Bit-Value Literals
Bit-value literals in MySQL can be written as b'val' or 0bval notation. Here, val is a binary value that
contains zeros and ones, and the lettercase of any leading value of b does not matter. A leading 0b
value is case sensitive, so we cannot write it as 0B.
The below examples explain the legal and illegal bit-value literals:
Legal Illegal
Null Values
It represents no data. It is not case-sensitive means we can write null literal in any language.
SQL Data Types
String Data Types
Data type Description
CHAR(size) A FIXED length string (can contain letters, numbers, and special characters).
The size parameter specifies the column length in characters - can be from 0 to 255.
Default is 1
VARCHAR(size) A VARIABLE length string (can contain letters, numbers, and special characters).
The size parameter specifies the maximum column length in characters - can be from
0 to 65535
BINARY(size) Equal to CHAR(), but stores binary byte strings. The size parameter specifies the
column length in bytes. Default is 1
VARBINARY(size) Equal to VARCHAR(), but stores binary byte strings. The size parameter specifies the
maximum column length in bytes.
TINYBLOB For BLOBs (Binary Large OBjects). Max length: 255 bytes
TINYTEXT Holds a string with a maximum length of 255 characters
TEXT(size) Holds a string with a maximum length of 65,535 bytes
String Data Types
BLOB(size) For BLOBs (Binary Large OBjects). Holds up to 65,535 bytes of data
MEDIUMBLOB For BLOBs (Binary Large OBjects). Holds up to 16,777,215 bytes of data
LONGBLOB For BLOBs (Binary Large OBjects). Holds up to 4,294,967,295 bytes of data
ENUM(val1, val2, val3, ...) A string object that can have only one value, chosen from a list of possible
values. You can list up to 65535 values in an ENUM list. If a value is inserted
that is not in the list, a blank value will be inserted. The values are sorted in
the order you enter them
SET(val1, val2, val3, ...) A string object that can have 0 or more values, chosen from a list of possible
values. You can list up to 64 values in a SET list
Numeric Data Types
INT(size) A medium integer. Signed range is from -2147483648 to 2147483647. Unsigned range
is from 0 to 4294967295. The size parameter specifies the maximum display width
(which is 255)
FLOAT(size, d) A floating point number. The total number of digits is specified in size. The number of
digits after the decimal point is specified in the d parameter. This syntax is deprecated
in MySQL 8.0.17, and it will be removed in future MySQL versions
FLOAT(p) A floating point number. MySQL uses the p value to determine whether to use FLOAT
or DOUBLE for the resulting data type. If p is from 0 to 24, the data type becomes
FLOAT(). If p is from 25 to 53, the data type becomes DOUBLE()
Numeric Data Types
DOUBLE(size, d) A normal-size floating point number. The total number of digits is
specified in size. The number of digits after the decimal point is
specified in the d parameter
DOUBLE PRECISION(size, d)
DECIMAL(size, d) An exact fixed-point number. The total number of digits is specified
in size. The number of digits after the decimal point is specified in
the d parameter. The maximum number for size is 65. The maximum
number for d is 30. The default value for size is 10. The default value
for d is 0.
MEDIUMINT(size) A medium integer. Signed range is from -8388608 to 8388607. Unsigned range is from
0 to 16777215. The size parameter specifies the maximum display width (which is
255)
Date and Time Data Type
Data Type Maximum Size Explanation
Syntax
YEAR[(2|4)] Year value as 2 digits or 4 digits. The default is 4 digits. It takes 1 byte for
storage.
DATE Values range from '1000-01-01' to '9999- Displayed as 'yyyy-mm-dd'. It takes 3
12-31'. bytes for storage.
TIME Values range from '-838:59:59' to Displayed as 'HH:MM:SS'. It takes 3 bytes
'838:59:59'. plus fractional seconds for storage.