forked from docs/doc-exports
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com> Co-authored-by: Su, Xiaomeng <suxiaomeng1@huawei.com> Co-committed-by: Su, Xiaomeng <suxiaomeng1@huawei.com>
76 lines
12 KiB
HTML
76 lines
12 KiB
HTML
<a name="dli_08_0375"></a><a name="dli_08_0375"></a>
|
|
|
|
<h1 class="topictitle1">CREATE TABLE</h1>
|
|
<div id="body8662426"><div class="section" id="dli_08_0375__en-us_topic_0000001262495778_section7901841144614"><h4 class="sectiontitle">Syntax</h4><pre class="screen" id="dli_08_0375__en-us_topic_0000001262495778_screen185623490466">CREATE TABLE table_name
|
|
(
|
|
{ <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, ...)
|
|
|
|
<column_definition>:
|
|
column_name column_type [ <column_constraint> ] [COMMENT column_comment]
|
|
|
|
<column_constraint>:
|
|
[CONSTRAINT constraint_name] PRIMARY KEY NOT ENFORCED
|
|
|
|
<table_constraint>:
|
|
[CONSTRAINT constraint_name] PRIMARY KEY (column_name, ...) NOT ENFORCED
|
|
|
|
<computed_column_definition>:
|
|
column_name AS computed_column_expression [COMMENT column_comment]
|
|
|
|
<watermark_definition>:
|
|
WATERMARK FOR rowtime_column_name AS watermark_strategy_expression
|
|
|
|
<source_table>:
|
|
[catalog_name.][db_name.]table_name</pre>
|
|
</div>
|
|
<div class="section" id="dli_08_0375__en-us_topic_0000001262495778_section17522151024712"><h4 class="sectiontitle">Function</h4><p id="dli_08_0375__en-us_topic_0000001262495778_p1748134364117">Create a table with a specified name.</p>
|
|
</div>
|
|
<div class="section" id="dli_08_0375__en-us_topic_0000001262495778_section10254165117477"><h4 class="sectiontitle">Description</h4><p id="dli_08_0375__en-us_topic_0000001262495778_p133701110194316"><strong id="dli_08_0375__en-us_topic_0000001262495778_b382012118440">COMPUTED COLUMN</strong></p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p136081541134311">A computed column is a virtual column generated using <strong id="dli_08_0375__en-us_topic_0000001262495778_b1639194826519">column_name AS computed_column_expression</strong>. A computed column evaluates an expression that can reference other columns declared in the same table. The column itself is not physically stored within the table. A computed column could be defined using <strong id="dli_08_0375__en-us_topic_0000001262495778_b1027347381519">cost AS price * quantity</strong>. This expression can contain any combination of physical columns, constants, functions, or variables, but cannot contain any subquery.</p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p560864104319">In Flink, a computed column is used to define the time attribute in <strong id="dli_08_0375__en-us_topic_0000001262495778_b598903644519">CREATE TABLE</strong> statements. A processing time attribute can be defined easily via <strong id="dli_08_0375__en-us_topic_0000001262495778_b491321828519">proc AS PROCTIME()</strong> using the system's <strong id="dli_08_0375__en-us_topic_0000001262495778_b1990839455519">PROCTIME()</strong> function. The event time column may be obtained from an existing field. In this case, you can use the computed column to obtain event time. For example, if the original field is not of the <strong id="dli_08_0375__en-us_topic_0000001262495778_b1187084817519">TIMESTAMP(3)</strong> type or is nested in a JSON string, you can use computed columns.</p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p106081541184319">Note:</p>
|
|
<ul id="dli_08_0375__en-us_topic_0000001262495778_ul17894724104412"><li id="dli_08_0375__en-us_topic_0000001262495778_li188941424204417">An expression that defines a computed column in a source table is calculated after data is read from the data source. The column can be used in the <strong id="dli_08_0375__en-us_topic_0000001262495778_b1466301722519">SELECT</strong> statement.</li><li id="dli_08_0375__en-us_topic_0000001262495778_li15109192815444">A computed column cannot be the target of an <strong id="dli_08_0375__en-us_topic_0000001262495778_b589040037519">INSERT</strong> statement. In an <strong id="dli_08_0375__en-us_topic_0000001262495778_b330772187519">INSERT</strong> statement, the schema of the <strong id="dli_08_0375__en-us_topic_0000001262495778_b1795107993519">SELECT</strong> statement must be the same as that of the target table that does not have a computed column.</li></ul>
|
|
</div>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p483119914915"><strong id="dli_08_0375__en-us_topic_0000001262495778_b5886113220497">WATERMARK</strong></p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p10910134310496">The <strong id="dli_08_0375__en-us_topic_0000001262495778_b1716309067519">WATERMARK</strong> clause defines the event time attribute of a table and takes the form <strong id="dli_08_0375__en-us_topic_0000001262495778_b1382227894519">WATERMARK FOR rowtime_column_name AS watermark_strategy_expression</strong>.</p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p791074318496"><strong id="dli_08_0375__en-us_topic_0000001262495778_b434553632519">rowtime_column_name</strong> defines an existing column that is marked as the event time attribute of the table. The column must be of the <strong id="dli_08_0375__en-us_topic_0000001262495778_b1026970440519">TIMESTAMP(3)</strong> type and must be the top-level column in the schema. It can also be a computed column.</p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p891016434498"><strong id="dli_08_0375__en-us_topic_0000001262495778_b1884431627519">watermark_strategy_expression</strong> defines the watermark generation strategy. It allows arbitrary non-query expressions, including computed columns, to calculate the watermark. The expression return type must be <strong id="dli_08_0375__en-us_topic_0000001262495778_b1168920531317">TIMESTAMP(3)</strong>, which represents the timestamp since the Epoch. The returned watermark will be emitted only if it is non-null and its value is greater than the previously emitted local watermark (to preserve the contract of ascending watermarks). The watermark generation expression is evaluated by the framework for every record. The framework will periodically emit the largest generated watermark. If the current watermark is still identical to the previous one, or is null, or the value of the returned watermark is smaller than that of the last emitted one, then no new watermark will be emitted. A watermark is emitted in an interval defined by <strong id="dli_08_0375__en-us_topic_0000001262495778_b757124676519">pipeline.auto-watermark-interval</strong>. If the watermark interval is 0 ms, a watermark will be emitted per record if it is not null and greater than the last emitted one.</p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p17910343124920">When using event time semantics, tables must contain an event time attribute and watermark strategy.</p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p19102437491">Flink provides several commonly used watermark strategies.</p>
|
|
<ul id="dli_08_0375__en-us_topic_0000001262495778_ul4296814105017"><li id="dli_08_0375__en-us_topic_0000001262495778_li1684012075016">Strictly ascending timestamps: <strong id="dli_08_0375__en-us_topic_0000001262495778_b990713330519">WATERMARK FOR rowtime_column AS rowtime_column</strong><p id="dli_08_0375__en-us_topic_0000001262495778_p1065492213506">Emits a watermark of the maximum observed timestamp so far. Rows that have a timestamp bigger than the maximum timestamp are not late.</p>
|
|
</li><li id="dli_08_0375__en-us_topic_0000001262495778_li451913311504">Ascending timestamps: <strong id="dli_08_0375__en-us_topic_0000001262495778_b101481634187">WATERMARK FOR rowtime_column AS rowtime_column - INTERVAL '0.001' SECOND</strong><p id="dli_08_0375__en-us_topic_0000001262495778_p5231133195016">Emits a watermark of the maximum observed timestamp so far minus 1. Rows that have a timestamp bigger than or equal to the maximum timestamp are not late.</p>
|
|
</li><li id="dli_08_0375__en-us_topic_0000001262495778_li1850111413503">Bounded out-of-order timestamps: <strong id="dli_08_0375__en-us_topic_0000001262495778_b14463151815256">WATERMARK FOR rowtime_column AS rowtime_column - INTERVAL 'string' timeUnit</strong><p id="dli_08_0375__en-us_topic_0000001262495778_p0854184235018">Emits a watermark, which is the maximum observed timestamp minus the specified delay, for example, <strong id="dli_08_0375__en-us_topic_0000001262495778_b946420093519">WATERMARK FOR rowtime_column AS rowtime_column - INTERVAL '5' SECOND</strong> is a 5-second delayed watermark strategy.</p>
|
|
<pre class="screen" id="dli_08_0375__en-us_topic_0000001262495778_screen1186917526509">CREATE TABLE Orders (
|
|
user BIGINT,
|
|
product STRING,
|
|
order_time TIMESTAMP(3),
|
|
WATERMARK FOR order_time AS order_time - INTERVAL '5' SECOND
|
|
) WITH ( . . . );</pre>
|
|
</li></ul>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p1736945175116"><strong id="dli_08_0375__en-us_topic_0000001262495778_b525512085119">PRIMARY KEY</strong></p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p812103765113">The primary key constraint is a hint for Flink to leverage for optimizations. It tells that a column or a set of columns of a table or a view are unique and they do not contain null. Neither of columns in a primary can be nullable. The primary key therefore uniquely identifies a row in a table.</p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p141233713517">The primary key constraint can be either declared along with a column definition (a column constraint) or as a single line (a table constraint). For both cases, it should only be declared as a singleton. If you define multiple primary key constraints at the same time, an exception would be thrown.</p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p111273735115">Validity Check</p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p161233765118">SQL standard specifies that a constraint can either be <strong id="dli_08_0375__en-us_topic_0000001262495778_b1202835363519">ENFORCED</strong> or <strong id="dli_08_0375__en-us_topic_0000001262495778_b328776844519">NOT ENFORCED</strong>. This controls if the constraint checks are performed on the incoming/outgoing data. Flink does not own the data and therefore the only mode we want to support is the <strong id="dli_08_0375__en-us_topic_0000001262495778_b475901612519">NOT ENFORCED</strong> mode. It is up to the user to ensure that the query enforces key integrity.</p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p1123370516">Flink will assume correctness of the primary key by assuming that the columns nullability is aligned with the columns in the primary key. Connectors should ensure those are aligned.</p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p1412337115111">Note: In a <strong id="dli_08_0375__en-us_topic_0000001262495778_b162179082519">CREATE TABLE</strong> statement, creating a primary key constraint will alter the columns nullability, which means, a column with a primary key constraint is not nullable.</p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p105085775118"><strong id="dli_08_0375__en-us_topic_0000001262495778_b36558918521">PARTITIONED BY</strong></p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p1865115102523">Partition the created table by the specified columns. A directory is created for each partition if this table is used as a file system sink.</p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p15897181535219"><strong id="dli_08_0375__en-us_topic_0000001262495778_b17531025125218">WITH OPTIONS</strong></p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p8529193365215">Table properties used to create a table source/sink. The properties are usually used to find and create the underlying connector.</p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p4529193375219">The key and value of expression <strong id="dli_08_0375__en-us_topic_0000001262495778_b1186213212380">key1=val1</strong> should both be string literal.</p>
|
|
<p id="dli_08_0375__en-us_topic_0000001262495778_p55293332525">Note: The table registered with the <strong id="dli_08_0375__en-us_topic_0000001262495778_b852614552387">CREATE TABLE</strong> statement can be used as both the table source and table sink. We cannot decide if it is used as a source or sink until it is referenced in the DMLs.</p>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="dli_08_0374.html">Data Definition Language (DDL)</a></div>
|
|
</div>
|
|
</div>
|
|
|