Skip to content

Commit

Permalink
Update docs for expression defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
dveeden committed Dec 13, 2024
1 parent da0d150 commit 97605c0
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions data-type-default-values.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()), '-', '')`
Expand All @@ -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.

0 comments on commit 97605c0

Please sign in to comment.