From 97605c04866ceb79a2dcfc6fa35991719409dd16 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/6] 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 e4be4820b59c7..d98c9d4970b6a 100644 --- a/data-type-default-values.md +++ b/data-type-default-values.md @@ -36,7 +36,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()), '-', '')` @@ -46,9 +46,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 2899343919616b81b418d77f2a39581a8dd873ea 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/6] 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 d98c9d4970b6a..cdfe5030d0849 100644 --- a/data-type-default-values.md +++ b/data-type-default-values.md @@ -71,6 +71,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 96e87ec9f0bec0e39fd69e29017a2156d7dc9081 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/6] 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 cdfe5030d0849..ad66f23445c06 100644 --- a/data-type-default-values.md +++ b/data-type-default-values.md @@ -53,7 +53,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 618d26457a5bad687bd3a6295cf228bc29c4e9a8 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/6] 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 ad66f23445c06..bff9c7cc26d7f 100644 --- a/data-type-default-values.md +++ b/data-type-default-values.md @@ -72,7 +72,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 b20a0f03fdc20b54dfdcbc8069774be04b981cf8 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/6] 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 bff9c7cc26d7f..169b00338ad4c 100644 --- a/data-type-default-values.md +++ b/data-type-default-values.md @@ -90,4 +90,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 d85b81463544e29b59e520324b01473b904edcbe 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/6] 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 169b00338ad4c..a93e2d2a7950d 100644 --- a/data-type-default-values.md +++ b/data-type-default-values.md @@ -46,7 +46,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)