0% found this document useful (0 votes)
2 views

SQL Data Types

The document outlines various types of SQL literals, including string, numeric, date and time, hexadecimal, bit-value, boolean literals, and NULL values. It also details the formats and examples for each type, as well as SQL data types such as string, numeric, and date/time types, providing descriptions and storage requirements. The document serves as a comprehensive guide for understanding SQL literals and data types in MySQL.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL Data Types

The document outlines various types of SQL literals, including string, numeric, date and time, hexadecimal, bit-value, boolean literals, and NULL values. It also details the formats and examples for each type, as well as SQL data types such as string, numeric, and date/time types, providing descriptions and storage requirements. The document serves as a comprehensive guide for understanding SQL literals and data types in MySQL.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

SQL Literals

The following are the types of literal:

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:

Time Format Descriptions


'D hh:mm:ss', It represents a time in string format with punctuations that
'hh:mm:ss', 'hh:mm', 'D can be used as a delimiter between the time parts. Here, D
hh:mm', 'D hh', or 'ss' for days that have value from 0 to 34. For example, '22
10:11:12', '10:11:12' time value.
'hhmmss' It represents a time in the string format without any
punctuations or delimiter between the time parts. For
example, '101211' is interpreted as '10:12:11', but '109813'
is illegal and becomes '00:00:00' time value.
hhmmss, ss, or mmss, It represents a time in the number format. For example,
101211 is interpreted as '10:12:11' time value.
Date and Time Literals
The following table explains the format of datetime and timestamp values in MySQL:
DateTime Format Descriptions

'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

x'01BF' X'0H' (Because H does not a hexadecimal digit)


x'01bf' 0X0BAF (Because 0X should be written as 0x)
X'01BF'
X'01bf'
0x01BF
0x01bf
Boolean Literals
Boolean literals in MySQL always evaluate in 1 or 0 values. Here, 1 represents true, and 0
represents false constants.

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

b'0011' b'3' (3 is not a binary digit)


B'0011' 0B11 (0B should be written as 0b)
0b0011

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

MEDIUMTEXT Holds a string with a maximum length of 16,777,215 characters

MEDIUMBLOB For BLOBs (Binary Large OBjects). Holds up to 16,777,215 bytes of data

LONGTEXT Holds a string with a maximum length of 4,294,967,295 characters

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)

INTEGER(size) Equal to INT(size)


BIGINT(size) A large integer. Signed range is from -9223372036854775808 to
9223372036854775807. Unsigned range is from 0 to 18446744073709551615.
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.

DEC(size, d) Equal to DECIMAL(size,d)


BIT(size) A bit-value type. The number of bits per value is specified in size.
The size parameter can hold a value from 1 to 64. The default value
for size is 1.
Numeric Data Types
TINYINT(size) A very small integer. Signed range is from -128 to 127. Unsigned range is from 0 to
255. The size parameter specifies the maximum display width (which is 255)

BOOL Zero is considered as false, nonzero values are considered as true.

BOOLEAN Equal to BOOL


SMALLINT(size) A small integer. Signed range is from -32768 to 32767. Unsigned range is from 0 to
65535. The size parameter specifies the maximum display width (which is 255)

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.

DATETIME Values range from '1000-01-01 00:00:00' Displayed as 'yyyy-mm-dd hh:mm:ss'. It


to '9999-12-31 23:59:59'. takes 5 bytes plus fractional seconds for
storage.
TIMESTAMP(m) Values range from '1970-01-01 00:00:01' Displayed as 'YYYY-MM-DD HH:MM:SS'. It
UTC to '2038-01-19 03:14:07' TC. takes 4 bytes plus fractional seconds for
storage.

You might also like