Similarly to other SQL dialects and programming languages, identifiers in DuckDB's SQL are subject to several rules.
duckdb_keywords()), e.g., SELECT 123 AS SELECT will fail.SELECT 123 AS 1col is invalid."). Quoted identifiers can use any keyword, whitespace or special character, e.g., "SELECT" and " § 🦆 ¶ " are valid identifiers.IDENTIFIER "X", use "IDENTIFIER ""X""".In some cases, duplicate identifiers can occur, e.g., column names may conflict when unnesting a nested data structure. In these cases, DuckDB automatically deduplicates column names by renaming them according to the following rules:
⟨name⟩, the first instance is not renamed.⟨name⟩_⟨count⟩, where ⟨count⟩ starts at 1.For example:
SELECT *
FROM (SELECT UNNEST({'a': 42, 'b': {'a': 88, 'b': 99}}, recursive := true)); | a | a_1 | b |
|---|---|---|
| 42 | 88 | 99 |
Database names are subject to the rules for identifiers.
Additionally, it is best practice to avoid DuckDB's two internal database schema names, system and temp. By default, persistent databases are named after their filename without the extension. Therefore, the filenames system.db and temp.db (as well as system.duckdb and temp.duckdb) result in the database names system and temp, respectively. If you need to attach to a database that has one of these names, use an alias, e.g.:
ATTACH 'temp.db' AS temp2; USE temp2;
SQL keywords and function names are case-insensitive in DuckDB.
For example, the following two queries are equivalent:
select COS(Pi()) as CosineOfPi; SELECT cos(pi()) AS CosineOfPi;
| CosineOfPi |
|---|
| -1.0 |
Identifiers in DuckDB are always case-insensitive, similarly to PostgreSQL. However, unlike PostgreSQL (and some other major SQL implementations), DuckDB also treats quoted identifiers as case-insensitive.
Despite treating identifiers in a case-insensitive manner, each character's case (uppercase/lowercase) is maintained as originally specified by the user even if a query uses different cases when referring to the identifier. For example:
CREATE TABLE tbl AS SELECT cos(pi()) AS CosineOfPi; SELECT cosineofpi FROM tbl;
| CosineOfPi |
|---|
| -1.0 |
To change this behavior, set the preserve_identifier_case configuration option to false.
In case of a conflict, when the same identifier is spelt with different cases, one will be selected randomly. For example:
CREATE TABLE t1 (idfield INTEGER, x INTEGER); CREATE TABLE t2 (IdField INTEGER, y INTEGER); INSERT INTO t1 VALUES (1, 123); INSERT INTO t2 VALUES (1, 456); SELECT * FROM t1 NATURAL JOIN t2;
| idfield | x | y |
|---|---|---|
| 1 | 123 | 456 |
With the preserve_identifier_case configuration option set to false, all identifiers are turned into lowercase:
SET preserve_identifier_case = false; CREATE TABLE tbl AS SELECT cos(pi()) AS CosineOfPi; SELECT CosineOfPi FROM tbl;
| cosineofpi |
|---|
| -1.0 |
© Copyright 2018–2024 Stichting DuckDB Foundation
Licensed under the MIT License.
https://duckdb.org/docs/sql/dialect/keywords_and_identifiers.html