CREATE TABLE¶
The CREATE TABLE
statement is used to create a new table in an existing database.
Tip
To create a table based on the result of a select query, see CREATE TABLE AS.
To create a table based on files like Parquet and ORC, see CREATE FOREIGN TABLE
Syntax¶
CREATE [ OR REPLACE ] TABLE [ "<schema_name>". ]"<table_name>"
{
( <column_def> [, ...] [ { NULL | NOT NULL } ]
| LIKE "<source_table_name>" [ INCLUDE PERMISSIONS ] )
}
| [ CLUSTER BY "<column_name>" [, ...] ]
-- column_def:
{ "<column_name>" <type_name> [ DEFAULT ] [ <column_constraint> ] }
-- column_constraint:
{ NOT NULL | NULL }
-- DEFAULT:
DEFAULT <default_value>
| IDENTITY [ ( <start_with> [ , increment_by ] ) ] [ check_specification ]
-- start_with: A value that is used for the very first row loaded into the table.
-- increment_by: Incremental value that is added to the identity value of the previous row that was loaded.
-- check_specification:
CHECK( 'CS compression_spec' )
-- compression_spec:
{ "default" | "p4d" | "dict" | "rle" | "sequence" | "flat" }
Parameters¶
The following parameters can be used when creating a table:
Parameter |
Description |
---|---|
|
Creates a new table and overwrites any existing table by the same name. Does not return an error if the table already exists. |
|
The name of the schema in which to create the table. |
|
The name of the table to create, which must be unique inside the schema. |
|
A comma separated list of column definitions. A minimal column definition includes a name identifier and a datatype. Other column constraints and default values can be added optionally. |
|
A comma separated list of clustering column keys. See CLUSTER BY for more information. |
|
Duplicates the column structure of an existing table. The newly created table is granted default |
|
In addition to the default CREATE TABLE permissions ( |
Usage Notes¶
CREATE TABLE... LIKE
¶When using
CREATE TABLE... LIKE
, the permissions from the source table are inherited by the newly created table. To add extra permissions to the new table, you can utilize theINCLUDE PERMISSIONS
clause.DEFAULT
¶The
DEFAULT
value constraint specifies a default value to use if none is provided in an INSERT or COPY FROM statement. This value can be a literal orNULL
. It’s worth noting that even for nullable columns, you can still explicitly insert aNULL
value using theNULL
keyword, as demonstrated in the example:INSERT INTO cool_animals VALUES (1, 'Gnu', NULL);
- Identity¶
Identity (or sequence) columns in a database table serve as a mechanism for generating unique key values automatically. The identity property ensures that each new row inserted into the table is assigned a value based on the current seed and increment specified for the column.
Warning
The identity property on a column does not guarantee uniqueness. The identity value can be bypassed by specifying it in an INSERT command.
Examples¶
Creating a Standard Table¶
CREATE TABLE cool_animals (
id INT NOT NULL,
name text NOT NULL,
weight FLOAT,
is_agressive BOOL
);
Creating a Table with Default Value Constraints for Some Columns¶
CREATE TABLE cool_animals (
id INT NOT NULL,
name TEXT NOT NULL,
weight FLOAT,
is_agressive BOOL DEFAULT false NOT NULL
);
-- The nullable/non-nullable constraint appears at the end, after the default option.
Creating a Table with an Identity Column¶
Identity does not enforce the uniqueness of values. The identity value can be bypassed by specifying it in an INSERT command.
CREATE TABLE users (
id BIGINT IDENTITY(0,1) NOT NULL , -- Start with 0, increment by 1
name TEXT NOT NULL,
country TEXT DEFAULT 'Unknown' NOT NULL
);
Creating a Table from a SELECT
Query¶
CREATE TABLE
users_uk AS
SELECT
*
FROM
users
WHERE
country = 'United Kingdom';
For more information on creating a new table from the results of a SELECT
query, see CREATE TABLE AS.
Creating a Table with a Clustering Key¶
When data within a table is organized in a sorted manner, the columns responsible for this sorting are termed as clustered. Effective clustering can greatly enhance performance. For instance, in the scenario provided, the start_date
column is anticipated to naturally cluster due to the continuous influx of new users and their corresponding start dates. However, in cases where the clustering of incoming data isn’t inherent, BLUE will automatically cluster it during insertion or bulk loading processes once the clustering key is set.
CREATE TABLE users (
name TEXT NOT NULL,
start_date DATETIME not null,
country TEXT DEFAULT 'Unknown' NOT NULL
) CLUSTER BY start_date;
For more information on data clustering, see CLUSTER BY.
Duplicating the Column Structure of an Existing Table¶
Syntax¶
CREATE [OR REPLACE] TABLE "<table_name>"
{
( "<column_name>"" <column_type> [{NULL | NOT NULL}] [,...])
| LIKE "<source_table_name>" [INCLUDE PERMISSIONS]
}
[CLUSTER BY ...]
Examples¶
This section includes examples of duplicating the column structure of an existing table using the LIKE
clause:
Creating a Table Using an Explicit Column List¶
CREATE TABLE t1 (
x INT DEFAULT 0 NOT NULL,
y TEXT NULL
);
Creating a Second Table Based on the Structure of Another Table¶
Either of the following examples can be used to create a second table based on the structure of another table.
Example 1
CREATE TABLE t2
LIKE t1
;
Example 2
CREATE TABLE t2 (
x INT DEFAULT 0 NOT NULL,
y TEXT NULL
);
The generated output of both of the statements above is identical.
Creating a Table based on Foreign Tables and Views¶
CREATE VIEW v
AS
SELECT x + 1,
y,
y
|| 'abc'
FROM t1;
CREATE TABLE t3 LIKE v;
When duplicating the column structure of an existing table, the target table of the LIKE
clause can be either a native, a regular or an external table, or a view.
The following table describes which properties are copied from the target table to the newly created table:
Property |
Native Table |
External Table |
View |
Column names |
Copied |
Copied |
Copied |
Column types |
Copied |
Copied |
Copied |
|
Copied |
Copied |
Copied |
|
Copied |
Copied |
Does not exist in source object |
Compression specification |
Copied |
Does not exist in source object |
Does not exist in source object |
Default/identity |
Copied |
Does not exist in source object |
Does not exist in source object |
Permissions¶
CREATE TABLE
requires CREATE
permission at the schema level.