Skip to content

Commit 61298dc

Browse files
soyeric128Chasen-Zhangyoungsofun
authored
docs: nextval(seq) as a column default value (#1737)
* Update nextval.md * Update 10-ddl-create-table.md * Update 10-ddl-create-table.md * Update docs/en/sql-reference/10-sql-commands/00-ddl/01-table/10-ddl-create-table.md Co-authored-by: Yang Xiufeng <yangxiufeng.c@gmail.com> * Update docs/en/sql-reference/10-sql-commands/00-ddl/01-table/10-ddl-create-table.md Co-authored-by: Yang Xiufeng <yangxiufeng.c@gmail.com> * Update docs/en/sql-reference/10-sql-commands/00-ddl/01-table/10-ddl-create-table.md Co-authored-by: Yang Xiufeng <yangxiufeng.c@gmail.com> * Update docs/en/sql-reference/10-sql-commands/00-ddl/01-table/10-ddl-create-table.md Co-authored-by: Yang Xiufeng <yangxiufeng.c@gmail.com> * Update docs/en/sql-reference/10-sql-commands/00-ddl/01-table/10-ddl-create-table.md Co-authored-by: Yang Xiufeng <yangxiufeng.c@gmail.com> --------- Co-authored-by: z <787025321@qq.com> Co-authored-by: Yang Xiufeng <yangxiufeng.c@gmail.com>
1 parent 679c9ef commit 61298dc

File tree

2 files changed

+28
-44
lines changed

2 files changed

+28
-44
lines changed

Diff for: docs/en/sql-reference/10-sql-commands/00-ddl/01-table/10-ddl-create-table.md

+18-34
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sidebar_position: 1
55

66
import FunctionDescription from '@site/src/components/FunctionDescription';
77

8-
<FunctionDescription description="Introduced or updated: v1.2.666"/>
8+
<FunctionDescription description="Introduced or updated: v1.2.714"/>
99

1010
import EEFeature from '@site/src/components/EEFeature';
1111

@@ -106,46 +106,30 @@ create table t_new compression='lz4' as select * from t_old;
106106

107107
By default, **all columns are nullable(NULL)** in Databend. If you need a column that does not allow NULL values, use the NOT NULL constraint. For more information, see [NULL Values and NOT NULL Constraint](../../../00-sql-reference/10-data-types/index.md).
108108

109-
## Default Values
109+
## Column Default Values
110110

111-
```sql
112-
DEFAULT <expr>
113-
```
114-
115-
Specify a default value inserted in the column if a value is not specified via an `INSERT` or `CREATE TABLE AS SELECT` statement.
116-
117-
For example:
118-
119-
```sql
120-
CREATE TABLE t_default_value(a TINYINT UNSIGNED, b VARCHAR DEFAULT 'b');
121-
```
111+
`DEFAULT <expr>` sets a default value for the column when no explicit expression is provided. The default expression can be:
122112

123-
Desc the `t_default_value` table:
113+
- A fixed constant, such as `Marketing` for the `department` column in the example below.
114+
- An expression with no input arguments and returns a scalar value, such as `1 + 1`, `NOW()` or `UUID()`.
115+
- A dynamically generated value from a sequence, such as `NEXTVAL(staff_id_seq)` for the `staff_id` column in the example below.
116+
- NEXTVAL must be used as a standalone default value; expressions like `NEXTVAL(seq1) + 1` are not supported.
124117

125118
```sql
126-
DESC t_default_value;
119+
CREATE SEQUENCE staff_id_seq;
127120

128-
Field|Type |Null|Default|Extra|
129-
-----+----------------+----+-------+-----+
130-
a |TINYINT UNSIGNED|YES |NULL | |
131-
b |VARCHAR |YES |'b' | |
132-
```
133-
134-
Insert a value:
135-
136-
```sql
137-
INSERT INTO T_default_value(a) VALUES(1);
138-
```
121+
CREATE TABLE staff (
122+
staff_id INT DEFAULT NEXTVAL(staff_id_seq), -- Assigns the next number from the sequence 'staff_id_seq' if no value is provided
123+
name VARCHAR(50),
124+
department VARCHAR(50) DEFAULT 'Marketing' -- Defaults to 'Marketing' if no value is provided
125+
);
139126

140-
Check the table values:
127+
-- staff_id is auto-generated when not included in COPY INTO
128+
COPY INTO staff(name, department) FROM @stage ...
141129

142-
```sql
143-
SELECT * FROM t_default_value;
144-
+------+------+
145-
| a | b |
146-
+------+------+
147-
| 1 | b |
148-
+------+------+
130+
-- staff_id is loaded from the staged file
131+
COPY INTO staff FROM @stage ...
132+
COPY INTO staff(staff_id, name, department) FROM @stage ...
149133
```
150134

151135
## Computed Columns

Diff for: docs/en/sql-reference/20-sql-functions/18-sequence-functions/nextval.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,27 @@ This example showcases how sequences and the NEXTVAL function are employed to au
4242
-- Create a new sequence named staff_id_seq
4343
CREATE SEQUENCE staff_id_seq;
4444

45-
-- Create a new table named staff with columns for staff_id, name, and department
45+
-- Create a new table named staff with an auto-generated staff_id
4646
CREATE TABLE staff (
47-
staff_id INT,
47+
staff_id INT DEFAULT NEXTVAL(staff_id_seq),
4848
name VARCHAR(50),
4949
department VARCHAR(50)
5050
);
5151

52-
-- Insert a new row into the staff table, using the next value from the staff_id_seq sequence for the staff_id column
53-
INSERT INTO staff (staff_id, name, department)
54-
VALUES (NEXTVAL(staff_id_seq), 'John Doe', 'HR');
52+
-- Insert a new staff member with an auto-generated staff_id into the staff table
53+
INSERT INTO staff (name, department)
54+
VALUES ('John Doe', 'HR');
5555

56-
-- Insert another row into the staff table, using the next value from the staff_id_seq sequence for the staff_id column
57-
INSERT INTO staff (staff_id, name, department)
58-
VALUES (NEXTVAL(staff_id_seq), 'Jane Smith', 'Finance');
56+
-- Insert another row
57+
INSERT INTO staff (name, department)
58+
VALUES ('Jane Smith', 'Finance');
5959

6060
SELECT * FROM staff;
6161

6262
┌───────────────────────────────────────────────────────┐
6363
│ staff_id │ name │ department │
6464
├─────────────────┼──────────────────┼──────────────────┤
65-
2 │ Jane Smith │ Finance │
66-
1 │ John Doe │ HR │
65+
3 │ Jane Smith │ Finance │
66+
2 │ John Doe │ HR │
6767
└───────────────────────────────────────────────────────┘
6868
```

0 commit comments

Comments
 (0)