- Docs Home
- About TiDB Cloud
- Get Started
- Develop Applications
- Overview
- Quick Start
- Build a TiDB Developer Cluster
- CRUD SQL in TiDB
- Build a Simple CRUD App with TiDB
- Example Applications
- Connect to TiDB
- Design Database Schema
- Write Data
- Read Data
- Transaction
- Optimize
- Troubleshoot
- Reference
- Cloud Native Development Environment
- Manage Cluster
- Plan Your Cluster
- Create a TiDB Cluster
- Connect to Your TiDB Cluster
- Set Up VPC Peering Connections
- Use an HTAP Cluster with TiFlash
- Scale a TiDB Cluster
- Upgrade a TiDB Cluster
- Delete a TiDB Cluster
- Use TiDB Cloud API (Beta)
- Migrate Data
- Import Sample Data
- Migrate Data into TiDB
- Configure Amazon S3 Access and GCS Access
- Migrate from MySQL-Compatible Databases
- Migrate Incremental Data from MySQL-Compatible Databases
- Migrate from Amazon Aurora MySQL in Bulk
- Import or Migrate from Amazon S3 or GCS to TiDB Cloud
- Import CSV Files from Amazon S3 or GCS into TiDB Cloud
- Import Apache Parquet Files from Amazon S3 or GCS into TiDB Cloud
- Troubleshoot Access Denied Errors during Data Import from Amazon S3
- Export Data from TiDB
- Back Up and Restore
- Monitor and Alert
- Overview
- Built-in Monitoring
- Built-in Alerting
- Third-Party Monitoring Integrations
- Tune Performance
- Overview
- Analyze Performance
- SQL Tuning
- Overview
- Understanding the Query Execution Plan
- SQL Optimization Process
- Overview
- Logic Optimization
- Physical Optimization
- Prepare Execution Plan Cache
- Control Execution Plans
- TiKV Follower Read
- Coprocessor Cache
- Garbage Collection (GC)
- Tune TiFlash performance
- Manage User Access
- Billing
- Reference
- TiDB Cluster Architecture
- TiDB Cloud Cluster Limits and Quotas
- TiDB Limitations
- SQL
- Explore SQL with TiDB
- SQL Language Structure and Syntax
- SQL Statements
ADD COLUMN
ADD INDEX
ADMIN
ADMIN CANCEL DDL
ADMIN CHECKSUM TABLE
ADMIN CHECK [TABLE|INDEX]
ADMIN SHOW DDL [JOBS|QUERIES]
ALTER DATABASE
ALTER INDEX
ALTER TABLE
ALTER TABLE COMPACT
ALTER USER
ANALYZE TABLE
BATCH
BEGIN
CHANGE COLUMN
COMMIT
CHANGE DRAINER
CHANGE PUMP
CREATE [GLOBAL|SESSION] BINDING
CREATE DATABASE
CREATE INDEX
CREATE ROLE
CREATE SEQUENCE
CREATE TABLE LIKE
CREATE TABLE
CREATE USER
CREATE VIEW
DEALLOCATE
DELETE
DESC
DESCRIBE
DO
DROP [GLOBAL|SESSION] BINDING
DROP COLUMN
DROP DATABASE
DROP INDEX
DROP ROLE
DROP SEQUENCE
DROP STATS
DROP TABLE
DROP USER
DROP VIEW
EXECUTE
EXPLAIN ANALYZE
EXPLAIN
FLASHBACK TABLE
FLUSH PRIVILEGES
FLUSH STATUS
FLUSH TABLES
GRANT <privileges>
GRANT <role>
INSERT
KILL [TIDB]
MODIFY COLUMN
PREPARE
RECOVER TABLE
RENAME INDEX
RENAME TABLE
REPLACE
REVOKE <privileges>
REVOKE <role>
ROLLBACK
SELECT
SET DEFAULT ROLE
SET [NAMES|CHARACTER SET]
SET PASSWORD
SET ROLE
SET TRANSACTION
SET [GLOBAL|SESSION] <variable>
SHOW ANALYZE STATUS
SHOW [GLOBAL|SESSION] BINDINGS
SHOW BUILTINS
SHOW CHARACTER SET
SHOW COLLATION
SHOW [FULL] COLUMNS FROM
SHOW CREATE SEQUENCE
SHOW CREATE TABLE
SHOW CREATE USER
SHOW DATABASES
SHOW DRAINER STATUS
SHOW ENGINES
SHOW ERRORS
SHOW [FULL] FIELDS FROM
SHOW GRANTS
SHOW INDEX [FROM|IN]
SHOW INDEXES [FROM|IN]
SHOW KEYS [FROM|IN]
SHOW MASTER STATUS
SHOW PLUGINS
SHOW PRIVILEGES
SHOW [FULL] PROCESSSLIST
SHOW PROFILES
SHOW PUMP STATUS
SHOW SCHEMAS
SHOW STATS_HEALTHY
SHOW STATS_HISTOGRAMS
SHOW STATS_META
SHOW STATUS
SHOW TABLE NEXT_ROW_ID
SHOW TABLE REGIONS
SHOW TABLE STATUS
SHOW [FULL] TABLES
SHOW [GLOBAL|SESSION] VARIABLES
SHOW WARNINGS
SHUTDOWN
SPLIT REGION
START TRANSACTION
TABLE
TRACE
TRUNCATE
UPDATE
USE
WITH
- Data Types
- Functions and Operators
- Overview
- Type Conversion in Expression Evaluation
- Operators
- Control Flow Functions
- String Functions
- Numeric Functions and Operators
- Date and Time Functions
- Bit Functions and Operators
- Cast Functions and Operators
- Encryption and Compression Functions
- Locking Functions
- Information Functions
- JSON Functions
- Aggregate (GROUP BY) Functions
- Window Functions
- Miscellaneous Functions
- Precision Math
- Set Operations
- List of Expressions for Pushdown
- TiDB Specific Functions
- Clustered Indexes
- Constraints
- Generated Columns
- SQL Mode
- Table Attributes
- Transactions
- Views
- Partitioning
- Temporary Tables
- Cached Tables
- Character Set and Collation
- Read Historical Data
- System Tables
mysql
- INFORMATION_SCHEMA
- Overview
ANALYZE_STATUS
CLIENT_ERRORS_SUMMARY_BY_HOST
CLIENT_ERRORS_SUMMARY_BY_USER
CLIENT_ERRORS_SUMMARY_GLOBAL
CHARACTER_SETS
CLUSTER_INFO
COLLATIONS
COLLATION_CHARACTER_SET_APPLICABILITY
COLUMNS
DATA_LOCK_WAITS
DDL_JOBS
DEADLOCKS
ENGINES
KEY_COLUMN_USAGE
PARTITIONS
PROCESSLIST
REFERENTIAL_CONSTRAINTS
SCHEMATA
SEQUENCES
SESSION_VARIABLES
SLOW_QUERY
STATISTICS
TABLES
TABLE_CONSTRAINTS
TABLE_STORAGE_STATS
TIDB_HOT_REGIONS_HISTORY
TIDB_INDEXES
TIDB_SERVERS_INFO
TIDB_TRX
TIFLASH_REPLICA
TIKV_REGION_PEERS
TIKV_REGION_STATUS
TIKV_STORE_STATUS
USER_PRIVILEGES
VIEWS
- System Variables
- API Reference
- Storage Engines
- Dumpling
- Table Filter
- Troubleshoot Inconsistency Between Data and Indexes
- FAQs
- Release Notes
- Support
- Glossary
Literal Values
TiDB literal values include character literals, numeric literals, time and date literals, hexadecimal, binary literals, and NULL literals. This document introduces each of these literal values.
This document describes String literals, Numeric literals, NULL values, Hexadecimal literals, Date and time literals, Boolean literals, and Bit-value literals.
String literals
A string is a sequence of bytes or characters, enclosed within either single quote '
or double quote "
characters. For example:
'example string'
"example string"
Quoted strings placed next to each other are concatenated to a single string. The following lines are equivalent:
'a string'
'a' ' ' 'string'
"a" ' ' "string"
If the ANSI_QUOTES
SQL MODE is enabled, string literals can be quoted only within single quotation marks because a string quoted within double quotation marks is interpreted as an identifier.
The string is divided into the following two types:
- Binary string: It consists of a sequence of bytes, whose charset and collation are both
binary
, and uses byte as the unit when compared with each other. - Non-binary string: It consists of a sequence of characters and has various charsets and collations other than
binary
. When compared with each other, non-binary strings use characters as the unit. A character might contain multiple bytes, depending on the charset.
A string literal may have an optional character set introducer
and COLLATE clause
, to designate it as a string that uses a specific character set and collation.
[_charset_name]'string' [COLLATE collation_name]
For example:
SELECT _latin1'string';
SELECT _binary'string';
SELECT _utf8'string' COLLATE utf8_bin;
You can use N'literal' (or n'literal') to create a string in the national character set. The following statements are equivalent:
SELECT N'some text';
SELECT n'some text';
SELECT _utf8'some text';
To represent some special characters in a string, you can use escape characters to escape:
Escape Characters | Meaning |
---|---|
\0 | An ASCII NUL (X'00') character |
\' | A single quote ' character |
\" | A double quote " character |
\b | A backspace character |
\n | A line break (newline) character |
\r | A carriage return character |
\t | A tab character |
\z | ASCII 26 (Ctrl + Z) |
\\ | A backslash \ character |
\% | A % character |
\_ | A _ character |
If you want to represent "
in the string surrounded by '
, or '
in the string surrounded by "
, you do not need to use escape characters.
For more information, see String Literals in MySQL.
Numeric literals
Numeric literals include integer and DECIMAL literals and floating-point literals.
Integer may include .
as a decimal separator. Numbers may be preceded by -
or +
to indicate a negative or positive value respectively.
Exact-value numeric literals can be represented as 1, .2, 3.4, -5, -6.78, +9.10
.
Numeric literals can also be represented in scientific notation, such as 1.2E3, 1.2E-3, -1.2E3, -1.2E-3
.
For more information, see Numeric Literals in MySQL.
Date and time literals
Date and time literal values can be represented in several formats, such as quoted strings or as numbers. When TiDB expects a date, it interprets any of '2017-08-24'
, '20170824'
and 20170824
as a date.
TiDB supports the following date formats:
'YYYY-MM-DD'
or'YY-MM-DD'
: The-
delimiter here is not strict. It can be any punctuation. For example,'2017-08-24'
,'2017&08&24'
,'2012@12^31'
are all valid date formats. The only special punctuation is '.', which is is treated as a decimal point to separate the integer and fractional parts. Date and time can be separated byT
or a white space. For example,2017-8-24 10:42:00
and2017-8-24T10:42:00
represents the same date and time.'YYYYMMDDHHMMSS'
or'YYMMDDHHMMSS'
: For example,'20170824104520'
and'170824104520'
are regarded as'2017-08-24 10:45:20'
. However, if you provide a value out of range, such as'170824304520'
, it is not treated as a valid date. Note that incorrect formats such asYYYYMMDD HHMMSS
,YYYYMMDD HH:MM:DD
, orYYYY-MM-DD HHMMSS
will fail to insert.YYYYMMDDHHMMSS
orYYMMDDHHMMSS
: Note that these formats have no single or double quotes, but a number. For example,20170824104520
is interpreted as'2017-08-24 10:45:20'
.
DATETIME or TIMESTAMP values can be followed by a fractional part, used to represent microseconds precision (6 digits). The fractional part should always be separated from the rest of the time by a decimal point .
.
The year value containing only two digits is ambiguous. It is recommended to use the four-digit year format. TiDB interprets the two-digit year value according to the following rules:
- If the year value is in the range of
70-99
, it is converted to1970-1999
. - If the year value is in the range of
00-69
, it is converted to2000-2069
.
For month or day values less than 10, '2017-8-4'
is the same as '2017-08-04'
. The same is true for Time. For example, '2017-08-24 1:2:3'
is the same as '2017-08-24 01:02:03'
.
When the date or time value is required, TiDB selects the specified format according to the length of the value:
- 6 digits:
YYMMDD
. - 12 digits:
YYMMDDHHMMSS
. - 8 digits:
YYYYMMDD
. - 14 digits:
YYYYMMDDHHMMSS
.
TiDB supports the following formats for time values:
'D HH:MM:SS'
, or'HH:MM:SS'
,'HH:MM'
,'D HH:MM'
,'D HH'
,'SS'
:D
means days and the valid value range is0-34
.- A number in
HHMMSS
format: For example,231010
is interpreted as'23:10:10'
. - A number in any of
SS
,MMSS
, andHHMMSS
formats can be regarded as time.
The decimal point of the Time type is also .
, with a precision of up to 6 digits after the decimal point.
See MySQL date and time literals for more details.
Boolean Literals
The constants TRUE
and FALSE
are equal to 1 and 0 respectively, which are not case sensitive.
SELECT TRUE, true, tRuE, FALSE, FaLsE, false;
+------+------+------+-------+-------+-------+
| TRUE | true | tRuE | FALSE | FaLsE | false |
+------+------+------+-------+-------+-------+
| 1 | 1 | 1 | 0 | 0 | 0 |
+------+------+------+-------+-------+-------+
1 row in set (0.00 sec)
Hexadecimal literals
Hexadecimal literal values are written using X'val'
or 0xval
notation, where val
contains hexadecimal digits. A leading 0x
is case sensitive and cannot be written as 0X
.
Legal hexadecimal literals:
X'ac12'
X'12AC'
x'ac12'
x'12AC'
0xac12
0x12AC
Illegal hexadecimal literals:
X'1z' (z is not a hexadecimal legal digit)
0X12AC (0X must be written as 0x)
Hexadecimal literals written using X'val'
notation must contain an even number of digits. If the length of val
is an odd number (for example, X'A'
or X'11A'
), to avoid the syntax error, pad the value with a leading zero:
mysql> select X'aff';
ERROR 1105 (HY000): line 0 column 13 near ""hex literal: invalid hexadecimal format, must even numbers, but 3 (total length 13)
mysql> select X'0aff';
+---------+
| X'0aff' |
+---------+
| 0x0aff |
+---------+
1 row in set (0.00 sec)
By default, a hexadecimal literal is a binary string.
To convert a string or a number to a string in hexadecimal format, use the HEX()
function:
mysql> SELECT HEX('TiDB');
+-------------+
| HEX('TiDB') |
+-------------+
| 54694442 |
+-------------+
1 row in set (0.01 sec)
mysql> SELECT X'54694442';
+-------------+
| X'54694442' |
+-------------+
| TiDB |
+-------------+
1 row in set (0.00 sec)
Bit-value literals
Bit-value literals are written using b'val'
or 0bval
notation. The val
is a binary value written using zeros and ones. A leading 0b
is case sensitive and cannot be written as 0B
.
Legal bit-value literals:
b'01'
B'01'
0b01
Illegal bit-value literals:
b'2' (2 is not a binary digit; it must be 0 or 1)
0B01 (0B must be written as 0b)
By default, a bit-value literal is a binary string.
Bit values are returned as binary values, which may not display well in the MySQL client. To convert a bit value to printable form, you can use a conversion function such as BIN()
or HEX()
.
CREATE TABLE t (b BIT(8));
INSERT INTO t SET b = b'00010011';
INSERT INTO t SET b = b'1110';
INSERT INTO t SET b = b'100101';
mysql> SELECT b+0, BIN(b), HEX(b) FROM t;
+------+--------+--------+
| b+0 | BIN(b) | HEX(b) |
+------+--------+--------+
| 19 | 10011 | 13 |
| 14 | 1110 | E |
| 37 | 100101 | 25 |
+------+--------+--------+
3 rows in set (0.00 sec)
NULL Values
NULL
means the data is empty, which is case-insensitive, and is synonymous with \N
(case-sensitive).
NULL
is not the same as 0
, nor the empty string ''
.