You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/sql-reference/10-sql-commands/00-ddl/01-table/10-ddl-create-table.md
+18-34
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ sidebar_position: 1
5
5
6
6
import FunctionDescription from '@site/src/components/FunctionDescription';
7
7
8
-
<FunctionDescriptiondescription="Introduced or updated: v1.2.666"/>
8
+
<FunctionDescriptiondescription="Introduced or updated: v1.2.714"/>
9
9
10
10
import EEFeature from '@site/src/components/EEFeature';
11
11
@@ -106,46 +106,30 @@ create table t_new compression='lz4' as select * from t_old;
106
106
107
107
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).
108
108
109
-
## Default Values
109
+
## Column Default Values
110
110
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
-
CREATETABLEt_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:
122
112
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.
124
117
125
118
```sql
126
-
DESC t_default_value;
119
+
CREATESEQUENCEstaff_id_seq;
127
120
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
+
CREATETABLEstaff (
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
+
);
139
126
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 ...
141
129
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 ...
0 commit comments