Yang, Tong 3f5759eed2 MRS comp-lts 2.0.38.SP20 version
Reviewed-by: Hasko, Vladimir <vladimir.hasko@t-systems.com>
Co-authored-by: Yang, Tong <yangtong2@huawei.com>
Co-committed-by: Yang, Tong <yangtong2@huawei.com>
2023-01-19 17:08:45 +00:00

38 lines
8.7 KiB
HTML

<a name="mrs_01_24201"></a><a name="mrs_01_24201"></a>
<h1 class="topictitle1">CREATE TABLE: Creating a Table</h1>
<div id="body8662426"><p id="mrs_01_24201__en-us_topic_0000001173470852_p1919324122119">This section describes the basic syntax and usage of the SQL statement for creating a ClickHouse table.</p>
<div class="section" id="mrs_01_24201__en-us_topic_0000001173470852_section3168913182114"><h4 class="sectiontitle">Basic Syntax</h4><ul id="mrs_01_24201__en-us_topic_0000001173470852_ul67474276217"><li id="mrs_01_24201__en-us_topic_0000001173470852_li17747132716210">Method 1: Creating a table named <strong id="mrs_01_24201__en-us_topic_0000001173470852_b1705700123111318">table_name</strong> in the specified <strong id="mrs_01_24201__en-us_topic_0000001173470852_b1429674490111318">database_name</strong> database.<p id="mrs_01_24201__en-us_topic_0000001173470852_p7747927132116">If the table creation statement does not contain <strong id="mrs_01_24201__en-us_topic_0000001173470852_b1331239488111318">database_name</strong>, the name of the database selected during client login is used by default.</p>
<p id="mrs_01_24201__en-us_topic_0000001173470852_p1874792711218"><strong id="mrs_01_24201__en-us_topic_0000001173470852_b1734558719111318">CREATE TABLE [IF NOT EXISTS] </strong><em id="mrs_01_24201__en-us_topic_0000001173470852_i285211467111318">[database_name.]table_name</em><strong id="mrs_01_24201__en-us_topic_0000001173470852_b378034047111318"> [ON CLUSTER </strong><em id="mrs_01_24201__en-us_topic_0000001173470852_i965283096111318">ClickHouse cluster name</em><strong id="mrs_01_24201__en-us_topic_0000001173470852_b1149816173111318">]</strong></p>
<p id="mrs_01_24201__en-us_topic_0000001173470852_p174711273217"><strong id="mrs_01_24201__en-us_topic_0000001173470852_b19747172720215">(</strong></p>
<p id="mrs_01_24201__en-us_topic_0000001173470852_p67477273211"><strong id="mrs_01_24201__en-us_topic_0000001173470852_b474715272218"></strong><em id="mrs_01_24201__en-us_topic_0000001173470852_i574722717219">name1</em><strong id="mrs_01_24201__en-us_topic_0000001173470852_b107479271217"> [</strong><em id="mrs_01_24201__en-us_topic_0000001173470852_i1074722719219">type1</em><strong id="mrs_01_24201__en-us_topic_0000001173470852_b474772732110">] [DEFAULT|</strong><strong id="mrs_01_24201__en-us_topic_0000001173470852_b1274722715211">MATERIALIZED</strong><strong id="mrs_01_24201__en-us_topic_0000001173470852_b207472277217">|ALIAS </strong><em id="mrs_01_24201__en-us_topic_0000001173470852_i117471127102113">expr1</em><strong id="mrs_01_24201__en-us_topic_0000001173470852_b474717277219">],</strong></p>
<p id="mrs_01_24201__en-us_topic_0000001173470852_p07476278214"><strong id="mrs_01_24201__en-us_topic_0000001173470852_b17471327132119"></strong><em id="mrs_01_24201__en-us_topic_0000001173470852_i1274772715211">name2</em><strong id="mrs_01_24201__en-us_topic_0000001173470852_b147471279213"><em id="mrs_01_24201__en-us_topic_0000001173470852_i127471927112118"> </em>[</strong><em id="mrs_01_24201__en-us_topic_0000001173470852_i1574782742110">type2</em><strong id="mrs_01_24201__en-us_topic_0000001173470852_b11747527172118">] [DEFAULT|</strong><strong id="mrs_01_24201__en-us_topic_0000001173470852_b11747227122112">MATERIALIZED</strong><strong id="mrs_01_24201__en-us_topic_0000001173470852_b1747527112117">|ALIAS </strong><em id="mrs_01_24201__en-us_topic_0000001173470852_i4747027142110">expr2</em><strong id="mrs_01_24201__en-us_topic_0000001173470852_b12747427152111">],</strong></p>
<p id="mrs_01_24201__en-us_topic_0000001173470852_p11747227152119"><strong id="mrs_01_24201__en-us_topic_0000001173470852_b674712792111">...</strong></p>
<p id="mrs_01_24201__en-us_topic_0000001173470852_p874782715210"><strong id="mrs_01_24201__en-us_topic_0000001173470852_b6747102772114">) </strong><em id="mrs_01_24201__en-us_topic_0000001173470852_i18747202711211">ENGINE</em> = <em id="mrs_01_24201__en-us_topic_0000001173470852_i3747122722118">engine</em><em id="mrs_01_24201__en-us_topic_0000001173470852_i17747142712113">_name()</em></p>
<p id="mrs_01_24201__en-us_topic_0000001173470852_p147471127162119">[<strong id="mrs_01_24201__en-us_topic_0000001173470852_b167471827172117">PARTITION BY</strong> <em id="mrs_01_24201__en-us_topic_0000001173470852_i1747827142116">expr_list</em>]</p>
<p id="mrs_01_24201__en-us_topic_0000001173470852_p3747927122114">[<strong id="mrs_01_24201__en-us_topic_0000001173470852_b9747112716215">ORDER BY</strong> <em id="mrs_01_24201__en-us_topic_0000001173470852_i9747227132111">expr_list</em>]</p>
<div class="caution" id="mrs_01_24201__en-us_topic_0000001173470852_note1747127122118"><span class="cautiontitle"><img src="public_sys-resources/caution_3.0-en-us.png"> </span><div class="cautionbody"><p id="mrs_01_24201__en-us_topic_0000001173470852_p20747112722115">You are advised to use <strong id="mrs_01_24201__en-us_topic_0000001173470852_b11747427132110">PARTITION BY</strong> to create table partitions when creating a ClickHouse table. The ClickHouse data migration tool migrates data based on table partitions. If you do not use <strong id="mrs_01_24201__en-us_topic_0000001173470852_b933044056111318">PARTITION BY</strong> to create table partitions during table creation, the table data cannot be migrated on the GUI in <a href="mrs_01_24053.html">Using the ClickHouse Data Migration Tool</a>.</p>
</div></div>
</li><li id="mrs_01_24201__en-us_topic_0000001173470852_li37478278215">Method 2: Creating a table with the same structure as <strong id="mrs_01_24201__en-us_topic_0000001173470852_b817123745111318">database_name2.table_name2</strong> and specifying a different table engine for the table<p id="mrs_01_24201__en-us_topic_0000001173470852_p13747122762117">If no table engine is specified, the created table uses the same table engine as <strong id="mrs_01_24201__en-us_topic_0000001173470852_b1036275726111318">database_name2.table_name2</strong>.</p>
<p id="mrs_01_24201__en-us_topic_0000001173470852_p17747327152116"><strong id="mrs_01_24201__en-us_topic_0000001173470852_b5747152714213">CREATE TABLE [IF NOT EXISTS] </strong><em id="mrs_01_24201__en-us_topic_0000001173470852_i574762722110">[database_name.]table_name</em><strong id="mrs_01_24201__en-us_topic_0000001173470852_b147471727162119"> AS </strong>[<em id="mrs_01_24201__en-us_topic_0000001173470852_i12747727132115">database_name2</em>.]<em id="mrs_01_24201__en-us_topic_0000001173470852_i1274792712111">table_name</em>2<strong id="mrs_01_24201__en-us_topic_0000001173470852_b9747202710216"> </strong>[ENGINE = <em id="mrs_01_24201__en-us_topic_0000001173470852_i2747102722116">engine</em><em id="mrs_01_24201__en-us_topic_0000001173470852_i157479272216">_name</em>]</p>
</li><li id="mrs_01_24201__en-us_topic_0000001173470852_li147471627202117">Method 3: Using the specified engine to create a table with the same structure as the result of the <strong id="mrs_01_24201__en-us_topic_0000001173470852_b819822053312">SELECT</strong> clause and filling it with the result of the <strong id="mrs_01_24201__en-us_topic_0000001173470852_b514382223312">SELECT</strong> clause<p id="mrs_01_24201__en-us_topic_0000001173470852_p9747192711215"><strong id="mrs_01_24201__en-us_topic_0000001173470852_b37472027122118">CREATE TABLE [IF NOT EXISTS] </strong><em id="mrs_01_24201__en-us_topic_0000001173470852_i1674716275212">[</em><em id="mrs_01_24201__en-us_topic_0000001173470852_i3747102772115">database_name</em><em id="mrs_01_24201__en-us_topic_0000001173470852_i474742752117">.]table_name</em><strong id="mrs_01_24201__en-us_topic_0000001173470852_b4747327102117"> </strong><em id="mrs_01_24201__en-us_topic_0000001173470852_i1574732716218">ENGINE</em> = <em id="mrs_01_24201__en-us_topic_0000001173470852_i13747192722116">engine</em><em id="mrs_01_24201__en-us_topic_0000001173470852_i117479277214">_name</em><strong id="mrs_01_24201__en-us_topic_0000001173470852_b19747162719212"> AS SELECT </strong>...</p>
</li></ul>
</div>
<div class="section" id="mrs_01_24201__en-us_topic_0000001173470852_section1817894102220"><h4 class="sectiontitle">Example</h4><pre class="screen" id="mrs_01_24201__en-us_topic_0000001173470852_screen149191111455">-- Create a table named <strong id="mrs_01_24201__en-us_topic_0000001173470852_b347107002111318">test</strong> in the <strong id="mrs_01_24201__en-us_topic_0000001173470852_b1122365085111318">default</strong> database and <strong id="mrs_01_24201__en-us_topic_0000001173470852_b888158834111318">default_cluster</strong> cluster.
CREATE TABLE default.test ON CLUSTER default_cluster
(
`EventDate` DateTime,
`id` UInt64
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/default/test', '{replica}')
PARTITION BY toYYYYMM(EventDate)
ORDER BY id</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="mrs_01_24199.html">Common ClickHouse SQL Syntax</a></div>
</div>
</div>