From 79a14858eaea440063cd248dbf18de40820e51e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 13 Dec 2024 08:56:50 +0100 Subject: [PATCH 1/9] Update docs for expression defaults --- data-type-default-values.md | 44 ++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/data-type-default-values.md b/data-type-default-values.md index 4fd348b1ff1ef..32314b690b7de 100644 --- a/data-type-default-values.md +++ b/data-type-default-values.md @@ -35,7 +35,7 @@ Implicit defaults are defined as follows: Starting from 8.0.13, MySQL supports specifying expressions as default values in the `DEFAULT` clause. For more information, see [Explicit default handling as of MySQL 8.0.13](https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html#data-type-defaults-explicit). -Starting from v8.0.0, TiDB additionally supports specifying the following expressions as default values in the `DEFAULT` clause. +TiDB supports specifying the following expressions as default values in the `DEFAULT` clause. * `UPPER(SUBSTRING_INDEX(USER(), '@', 1))` * `REPLACE(UPPER(UUID()), '-', '')` @@ -45,9 +45,47 @@ Starting from v8.0.0, TiDB additionally supports specifying the following expres * `DATE_FORMAT(NOW(), '%Y-%m-%d %H.%i.%s')` * `DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s')` * `STR_TO_DATE('1980-01-01', '%Y-%m-%d')` +* [`CURRENT_TIMESTAMP()`](/functions-and-operators/date-and-time-functions.md), [`CURRENT_DATE()`](/functions-and-operators/date-and-time-functions.md): both with default fsp +* [`JSON_OBJECT()`](/functions-and-operators/json-functions.md), [`JSON_ARRAY()`](/functions-and-operators/json-functions.md), [`JSON_QUOTE()`](/functions-and-operators/json-functions.md) +* [`NEXTVAL()`](/functions-and-operators/sequence-functions.md#nextval) +* [`RAND()`](/functions-and-operators/numeric-functions-and-operators.md) +* [`UUID()`](/functions-and-operators/miscellaneous-functions.md#uuid), [`UUID_TO_BIN()`](/functions-and-operators/miscellaneous-functions.md#uuid_to_bin) +* [`VEC_FROM_TEXT()`](/vector-search-functions-and-operators.md#vec_from_text) -Starting from v8.0.0, TiDB additionally supports assigning default values to `BLOB`, `TEXT`, and `JSON` data types. However, you can only use expressions to set the default values for these data types. The following is an example of `BLOB`: + +TiDB supports assigning default values to `BLOB`, `TEXT`, and `JSON` data types. However, you can only use expressions to set the default values for these data types and not literals. The following is an example of `BLOB`: + +```sql +CREATE TABLE t2 ( + b BLOB DEFAULT (RAND()) +); +``` + +An example for using a UUID: + +```sql +CREATE TABLE t3 ( + uuid BINARY(16) DEFAULT (UUID_TO_BIN(UUID())), + name VARCHAR(255) +); +``` + +An example for using JSON: ```sql -CREATE TABLE t2 (b BLOB DEFAULT (RAND())); +CREATE TABLE t4 ( + id bigint AUTO_RANDOM PRIMARY KEY, + j json DEFAULT (JSON_OBJECT("a", 1, "b", 2)) +); ``` + +An example for what is not allowed for JSON: + +```sql +CREATE TABLE t5 ( + id bigint AUTO_RANDOM PRIMARY KEY, + j json DEFAULT ('{"a": 1, "b": 2}') +); +``` + +The last two examples describe a similar default, but only the first one is allowed as it is using an expression and not a literal. From 8dc4dcd10fa60324736a266995cc1efd0310343e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 13 Dec 2024 09:28:33 +0100 Subject: [PATCH 2/9] Link to UUID best practices --- data-type-default-values.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/data-type-default-values.md b/data-type-default-values.md index 32314b690b7de..8f237f964e093 100644 --- a/data-type-default-values.md +++ b/data-type-default-values.md @@ -70,6 +70,9 @@ CREATE TABLE t3 ( ); ``` +For more details on how to use UUID, see [UUID best practices](/best-practices/uuid.md). + + An example for using JSON: ```sql From 1fb7c493ca83fc41ead2bf33b8f5bd4e80a89056 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 13 Dec 2024 09:29:38 +0100 Subject: [PATCH 3/9] Fix linter issue --- data-type-default-values.md | 1 - 1 file changed, 1 deletion(-) diff --git a/data-type-default-values.md b/data-type-default-values.md index 8f237f964e093..3c1172b3c71bb 100644 --- a/data-type-default-values.md +++ b/data-type-default-values.md @@ -52,7 +52,6 @@ TiDB supports specifying the following expressions as default values in the `DEF * [`UUID()`](/functions-and-operators/miscellaneous-functions.md#uuid), [`UUID_TO_BIN()`](/functions-and-operators/miscellaneous-functions.md#uuid_to_bin) * [`VEC_FROM_TEXT()`](/vector-search-functions-and-operators.md#vec_from_text) - TiDB supports assigning default values to `BLOB`, `TEXT`, and `JSON` data types. However, you can only use expressions to set the default values for these data types and not literals. The following is an example of `BLOB`: ```sql From 6914a08b5db08eb44976077bfae7b68288cdee28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 13 Dec 2024 09:40:31 +0100 Subject: [PATCH 4/9] Fix linter issue --- data-type-default-values.md | 1 - 1 file changed, 1 deletion(-) diff --git a/data-type-default-values.md b/data-type-default-values.md index 3c1172b3c71bb..441acacd46fde 100644 --- a/data-type-default-values.md +++ b/data-type-default-values.md @@ -71,7 +71,6 @@ CREATE TABLE t3 ( For more details on how to use UUID, see [UUID best practices](/best-practices/uuid.md). - An example for using JSON: ```sql From e5cf0a593578ba5d44a41da72b500ddf0c6869b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Tue, 24 Dec 2024 11:59:17 +0100 Subject: [PATCH 5/9] Update data-type-default-values.md Co-authored-by: xixirangrang --- data-type-default-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data-type-default-values.md b/data-type-default-values.md index 441acacd46fde..6b0eddf982ed6 100644 --- a/data-type-default-values.md +++ b/data-type-default-values.md @@ -89,4 +89,4 @@ CREATE TABLE t5 ( ); ``` -The last two examples describe a similar default, but only the first one is allowed as it is using an expression and not a literal. +The last two examples show similar defaults, but only the first one is valid because it uses an expression rather than a literal. From 3d8ac2de2ae41ea2016584ac2addff25b4ed3810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 27 Dec 2024 12:31:12 +0100 Subject: [PATCH 6/9] Update data-type-default-values.md Co-authored-by: xixirangrang --- data-type-default-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data-type-default-values.md b/data-type-default-values.md index 6b0eddf982ed6..df11e50c82c68 100644 --- a/data-type-default-values.md +++ b/data-type-default-values.md @@ -45,7 +45,7 @@ TiDB supports specifying the following expressions as default values in the `DEF * `DATE_FORMAT(NOW(), '%Y-%m-%d %H.%i.%s')` * `DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s')` * `STR_TO_DATE('1980-01-01', '%Y-%m-%d')` -* [`CURRENT_TIMESTAMP()`](/functions-and-operators/date-and-time-functions.md), [`CURRENT_DATE()`](/functions-and-operators/date-and-time-functions.md): both with default fsp +* [`CURRENT_TIMESTAMP()`](/functions-and-operators/date-and-time-functions.md), [`CURRENT_DATE()`](/functions-and-operators/date-and-time-functions.md): both use the default fsp (fractional seconds precision) * [`JSON_OBJECT()`](/functions-and-operators/json-functions.md), [`JSON_ARRAY()`](/functions-and-operators/json-functions.md), [`JSON_QUOTE()`](/functions-and-operators/json-functions.md) * [`NEXTVAL()`](/functions-and-operators/sequence-functions.md#nextval) * [`RAND()`](/functions-and-operators/numeric-functions-and-operators.md) From 0d29c76ac6e973fed68eacf6af892b229f90b2dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 3 Jan 2025 11:00:43 +0100 Subject: [PATCH 7/9] Apply suggestions from code review Co-authored-by: Grace Cai --- data-type-default-values.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data-type-default-values.md b/data-type-default-values.md index df11e50c82c68..860f3ffef939b 100644 --- a/data-type-default-values.md +++ b/data-type-default-values.md @@ -45,14 +45,14 @@ TiDB supports specifying the following expressions as default values in the `DEF * `DATE_FORMAT(NOW(), '%Y-%m-%d %H.%i.%s')` * `DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s')` * `STR_TO_DATE('1980-01-01', '%Y-%m-%d')` -* [`CURRENT_TIMESTAMP()`](/functions-and-operators/date-and-time-functions.md), [`CURRENT_DATE()`](/functions-and-operators/date-and-time-functions.md): both use the default fsp (fractional seconds precision) +* [`CURRENT_TIMESTAMP()`](/functions-and-operators/date-and-time-functions.md), [`CURRENT_DATE()`](/functions-and-operators/date-and-time-functions.md): both use the default fractional seconds precision (fsp) * [`JSON_OBJECT()`](/functions-and-operators/json-functions.md), [`JSON_ARRAY()`](/functions-and-operators/json-functions.md), [`JSON_QUOTE()`](/functions-and-operators/json-functions.md) * [`NEXTVAL()`](/functions-and-operators/sequence-functions.md#nextval) * [`RAND()`](/functions-and-operators/numeric-functions-and-operators.md) * [`UUID()`](/functions-and-operators/miscellaneous-functions.md#uuid), [`UUID_TO_BIN()`](/functions-and-operators/miscellaneous-functions.md#uuid_to_bin) * [`VEC_FROM_TEXT()`](/vector-search-functions-and-operators.md#vec_from_text) -TiDB supports assigning default values to `BLOB`, `TEXT`, and `JSON` data types. However, you can only use expressions to set the default values for these data types and not literals. The following is an example of `BLOB`: +TiDB supports assigning default values to `BLOB`, `TEXT`, and `JSON` data types. However, you can only use expressions, not literals, to define default values for these data types. The following is an example of `BLOB`: ```sql CREATE TABLE t2 ( @@ -69,7 +69,7 @@ CREATE TABLE t3 ( ); ``` -For more details on how to use UUID, see [UUID best practices](/best-practices/uuid.md). +For more information on how to use UUID, see [UUID best practices](/best-practices/uuid.md). An example for using JSON: From 7b983aca55746742bb4ccc98f0dbe1283c4e7425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 3 Jan 2025 11:13:11 +0100 Subject: [PATCH 8/9] Apply suggestions from code review Co-authored-by: Grace Cai --- data-type-default-values.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data-type-default-values.md b/data-type-default-values.md index 860f3ffef939b..38503494c2d83 100644 --- a/data-type-default-values.md +++ b/data-type-default-values.md @@ -71,7 +71,7 @@ CREATE TABLE t3 ( For more information on how to use UUID, see [UUID best practices](/best-practices/uuid.md). -An example for using JSON: +An example for using `JSON`: ```sql CREATE TABLE t4 ( @@ -80,7 +80,7 @@ CREATE TABLE t4 ( ); ``` -An example for what is not allowed for JSON: +An example for what is not allowed for `JSON`: ```sql CREATE TABLE t5 ( From e810fadbb20112108f0e3d6142f19bf8f585726d Mon Sep 17 00:00:00 2001 From: xixirangrang Date: Mon, 6 Jan 2025 08:25:17 +0800 Subject: [PATCH 9/9] Update data-type-default-values.md --- data-type-default-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data-type-default-values.md b/data-type-default-values.md index 38503494c2d83..bb687b0b9edf4 100644 --- a/data-type-default-values.md +++ b/data-type-default-values.md @@ -50,7 +50,7 @@ TiDB supports specifying the following expressions as default values in the `DEF * [`NEXTVAL()`](/functions-and-operators/sequence-functions.md#nextval) * [`RAND()`](/functions-and-operators/numeric-functions-and-operators.md) * [`UUID()`](/functions-and-operators/miscellaneous-functions.md#uuid), [`UUID_TO_BIN()`](/functions-and-operators/miscellaneous-functions.md#uuid_to_bin) -* [`VEC_FROM_TEXT()`](/vector-search-functions-and-operators.md#vec_from_text) +* [`VEC_FROM_TEXT()`](/vector-search/vector-search-functions-and-operators.md#vec_from_text) TiDB supports assigning default values to `BLOB`, `TEXT`, and `JSON` data types. However, you can only use expressions, not literals, to define default values for these data types. The following is an example of `BLOB`: