CREATE TABLE
Overview
The CREATE TABLE statement is used to register a table in the current or specified catalog, and defines the table structure (physical columns, computed columns, and metadata columns), time attributes (watermarks), constraints (primary keys), and connector configurations (WITH clause). The registered table can be used as a source table or result table in SQL queries. AI DataLake supports the standard Flink SQL syntax for creating tables, including core capabilities such as partitioned tables and table creation using LIKE.
Application Scenarios
- Data access: Uses CREATE TABLE to define a mapping table for external data sources (such as Kafka and HDFS) to read and write data.
- Streaming ETL: Defines a source table with watermarks and computed columns, and uses window functions to perform real-time data transformations.
- Partition management: Uses PARTITIONED BY to create partitioned tables, optimizing query performance in scenarios with large data volumes.
- Table structure reuse: Quickly creates a new table based on an existing table using the LIKE clause, reducing repeated definitions.
Notes and Constraints
- AI DataLake does not support the CREATE TEMPORARY TABLE syntax. All tables are persistent tables.
- The primary key constraint is declared as NOT ENFORCED. Flink does not verify data uniqueness; this must be ensured by the external storage.
- Only one watermark can be defined for a table, meaning only one event time field can be declared.
- PARTITIONED BY is supported only when the connector supports partitioning (for example, Hive and Paimon). Connectors such as Kafka do not support partitioned tables.
- A computed column cannot reference other computed columns. It can only reference physical columns and metadata columns.
- The availability of metadata columns depends on the connector implementation. Different connectors support different metadata.
- The INCLUDING/EXCLUDING option of the LIKE clause must match the structure of the source table. An incorrect combination will cause table creation to fail.
Syntax Structure
Complete syntax
CREATE TABLE [IF NOT EXISTS] [catalog_name.][db_name.]table_name (
{ <physical_column_definition> | <metadata_column_definition> | <computed_column_definition> }
[ , ...n]
[ <watermark_definition> ]
[ <table_constraint> ]
[ , ...n]
)
[COMMENT table_comment]
[PARTITIONED BY (partition_column_name1, partition_column_name2, ...)]
WITH (key1=val1, key2=val2, ...)
[ LIKE source_table [( <like_options> )] ] Column definition syntax
-- Physical column <physical_column_definition>: column_name column_type [ <column_constraint> ] [COMMENT column_comment] <column_constraint>: [CONSTRAINT constraint_name] PRIMARY KEY NOT ENFORCED -- Metadata column <metadata_column_definition>: column_name column_type METADATA [ FROM metadata_key ] [ VIRTUAL ] -- Computed column <computed_column_definition>: column_name AS computed_column_expression [COMMENT column_comment]
Watermark definition syntax
<watermark_definition>: WATERMARK FOR rowtime_column_name AS watermark_strategy_expression
Primary key constraint syntax
<table_constraint>: [CONSTRAINT constraint_name] PRIMARY KEY (column_name, ...) NOT ENFORCED
LIKE option syntax
<like_options>:
{
{ INCLUDING | EXCLUDING } { ALL | CONSTRAINTS | PARTITIONS }
| { INCLUDING | EXCLUDING | OVERWRITING } { GENERATED | OPTIONS | WATERMARKS }
}[, ...] Parameter Description
Main clause parameters
| Parameter | Mandatory | Description |
|---|---|---|
| IF NOT EXISTS | No | Specifies that no error is reported and the creation is skipped if a table with the same name already exists. If this parameter is not specified and a table with the same name exists, an exception is thrown. |
| catalog_name.db_name | No | Specifies the catalog and database name of the table. If not specified, the current catalog and database are used. |
| table_name | Yes | Specifies the name of the table to be created. It must be unique in the same database. |
| COMMENT table_comment | No | Adds comments to a table. |
| PARTITIONED BY | No | Specifies the partition column. The table is stored in different directories based on the partition column values. This parameter takes effect only when the connector supports partitioning. The partition column must be a physical column defined in the table. |
| WITH (key1=val1, ...) | Yes | Specifies the connector configuration items of the table, which are provided in the form of key-value pairs. The connector parameter is mandatory and determines the data source or destination. |
| LIKE source_table | No | Creates a table based on the structure of an existing table. The INCLUDING, EXCLUDING, and OVERWRITING parameters can be used to control the structure inheritance policy. The table structure can be reused to avoid repeated definition. |
Column type parameters
| Column Type | Keyword | Description |
|---|---|---|
| Physical column | No special keyword | Defines the columns that store data in the physical medium. This is the most commonly used column definition method. The order of physical columns corresponds to the order of fields in the data source. |
| Computed column | AS | A virtual column generated by an expression and is not physically stored in the table. It is often used to define the processing time (AS PROCTIME()) or derived fields. Only physical columns and metadata columns can be referenced, and other computed columns cannot be referenced. |
| Metadata column | METADATA | Accesses metadata information of the connector data source, such as the timestamp and offset of Kafka. The VIRTUAL flag indicates that the metadata is read-only and is not persisted. The availability depends on the connector implementation. |
Watermark parameters
| Parameter | Mandatory | Description |
|---|---|---|
| rowtime_column_name | Yes | Specifies the event time column, which must be of the TIMESTAMP(3) or TIMESTAMP_LTZ(3) type. Only one watermark can be defined for a table. |
| watermark_strategy_expression | Yes | Watermark generation strategy expression, which is used to calculate the timestamp of the watermark. A common strategy is rowtime_column - INTERVAL 'n' SECOND, indicating that a maximum of n seconds of out-of-order is allowed. |
Primary key constraint parameters
| Parameter | Mandatory | Description |
|---|---|---|
| constraint_name | No | Constraint name, which is optional. |
| column_name, ... | Yes | One or more columns that are included in the primary key. These columns cannot be null. |
| NOT ENFORCED | Yes | Flink does not forcibly verify the uniqueness of primary keys. The uniqueness of primary keys must be ensured by external storage. This keyword is mandatory and cannot be omitted. The primary key is used to determine the semantics of the upsert connector. |
LIKE option parameters
| Option | Optional Policy | Description |
|---|---|---|
| ALL | INCLUDING / EXCLUDING | Controls whether to include all features of the source table. INCLUDING ALL indicates that all features are inherited, and EXCLUDING ALL indicates that all features are excluded. |
| CONSTRAINTS | INCLUDING / EXCLUDING | Controls whether to include constraints such as primary keys. INCLUDING CONSTRAINTS indicates that constraints are inherited, and EXCLUDING CONSTRAINTS indicates that constraints are not inherited. |
| PARTITIONS | INCLUDING / EXCLUDING | Controls whether to include partition information. INCLUDING PARTITIONS indicates that partition definitions are inherited, and EXCLUDING PARTITIONS indicates that partition definitions are not inherited. |
| GENERATED | INCLUDING / EXCLUDING / OVERWRITING | Controls the inheritance of computed columns and metadata columns. INCLUDING indicates that the columns are inherited, EXCLUDING indicates that the columns are excluded, and OVERWRITING indicates that the computed columns in the source table are overwritten by the newly defined columns. |
| OPTIONS | INCLUDING / EXCLUDING / OVERWRITING | Controls the inheritance of the WITH options. INCLUDING OPTIONS indicates that all connector configurations are inherited, EXCLUDING OPTIONS indicates that all connector configurations are not inherited, and OVERWRITING OPTIONS indicates that the new configuration overwrites the items with the same name. |
| WATERMARKS | INCLUDING / EXCLUDING / OVERWRITING | Controls the inheritance of watermark definitions. INCLUDING WATERMARKS indicates that watermarks are inherited, EXCLUDING WATERMARKS indicates that watermarks are not inherited, and OVERWRITING WATERMARKS indicates that the new definition overwrites the existing definition. |
Usage Example
AI DataLake supports multiple connectors, each of which has specific usage examples. For details, see Supported Connectors and view the usage examples in the documentation of each connector.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot