forked from docs/doc-exports
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>
38 lines
6.3 KiB
HTML
38 lines
6.3 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="body0000001183607139"><p id="mrs_01_24201__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__section3168913182114"><h4 class="sectiontitle">Basic Syntax</h4><ul id="mrs_01_24201__ul67474276217"><li id="mrs_01_24201__li17747132716210">Method 1: Creating a table named <strong id="mrs_01_24201__b1705700123111318">table_name</strong> in the specified <strong id="mrs_01_24201__b1429674490111318">database_name</strong> database.<p id="mrs_01_24201__p7747927132116">If the table creation statement does not contain <strong id="mrs_01_24201__b1331239488111318">database_name</strong>, the name of the database selected during client login is used by default.</p>
|
|
<p id="mrs_01_24201__p1874792711218"><strong id="mrs_01_24201__b1734558719111318">CREATE TABLE [IF NOT EXISTS] </strong><em id="mrs_01_24201__i285211467111318">[database_name.]table_name</em><strong id="mrs_01_24201__b378034047111318"> [ON CLUSTER </strong><em id="mrs_01_24201__i965283096111318">ClickHouse cluster name</em><strong id="mrs_01_24201__b1149816173111318">]</strong></p>
|
|
<p id="mrs_01_24201__p174711273217"><strong id="mrs_01_24201__b19747172720215">(</strong></p>
|
|
<p id="mrs_01_24201__p67477273211"><strong id="mrs_01_24201__b474715272218"></strong><em id="mrs_01_24201__i574722717219">name1</em><strong id="mrs_01_24201__b107479271217"> [</strong><em id="mrs_01_24201__i1074722719219">type1</em><strong id="mrs_01_24201__b474772732110">] [DEFAULT|</strong><strong id="mrs_01_24201__b1274722715211">MATERIALIZED</strong><strong id="mrs_01_24201__b207472277217">|ALIAS </strong><em id="mrs_01_24201__i117471127102113">expr1</em><strong id="mrs_01_24201__b474717277219">],</strong></p>
|
|
<p id="mrs_01_24201__p07476278214"><strong id="mrs_01_24201__b17471327132119"></strong><em id="mrs_01_24201__i1274772715211">name2</em><strong id="mrs_01_24201__b147471279213"><em id="mrs_01_24201__i127471927112118"> </em>[</strong><em id="mrs_01_24201__i1574782742110">type2</em><strong id="mrs_01_24201__b11747527172118">] [DEFAULT|</strong><strong id="mrs_01_24201__b11747227122112">MATERIALIZED</strong><strong id="mrs_01_24201__b1747527112117">|ALIAS </strong><em id="mrs_01_24201__i4747027142110">expr2</em><strong id="mrs_01_24201__b12747427152111">],</strong></p>
|
|
<p id="mrs_01_24201__p11747227152119"><strong id="mrs_01_24201__b674712792111">...</strong></p>
|
|
<p id="mrs_01_24201__p874782715210"><strong id="mrs_01_24201__b6747102772114">) </strong><em id="mrs_01_24201__i18747202711211">ENGINE</em> = <em id="mrs_01_24201__i3747122722118">engine</em><em id="mrs_01_24201__i17747142712113">_name()</em></p>
|
|
<p id="mrs_01_24201__p147471127162119">[<strong id="mrs_01_24201__b167471827172117">PARTITION BY</strong> <em id="mrs_01_24201__i1747827142116">expr_list</em>]</p>
|
|
<p id="mrs_01_24201__p3747927122114">[<strong id="mrs_01_24201__b9747112716215">ORDER BY</strong> <em id="mrs_01_24201__i9747227132111">expr_list</em>]</p>
|
|
<div class="caution" id="mrs_01_24201__note1747127122118"><span class="cautiontitle"><img src="public_sys-resources/caution_3.0-en-us.png"> </span><div class="cautionbody"><p id="mrs_01_24201__p20747112722115">You are advised to use <strong id="mrs_01_24201__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__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_24198.html">Using the ClickHouse Data Migration Tool</a>.</p>
|
|
</div></div>
|
|
</li><li id="mrs_01_24201__li37478278215">Method 2: Creating a table with the same structure as <strong id="mrs_01_24201__b817123745111318">database_name2.table_name2</strong> and specifying a different table engine for the table<p id="mrs_01_24201__p13747122762117">If no table engine is specified, the created table uses the same table engine as <strong id="mrs_01_24201__b1036275726111318">database_name2.table_name2</strong>.</p>
|
|
<p id="mrs_01_24201__p17747327152116"><strong id="mrs_01_24201__b5747152714213">CREATE TABLE [IF NOT EXISTS] </strong><em id="mrs_01_24201__i574762722110">[database_name.]table_name</em><strong id="mrs_01_24201__b147471727162119"> AS </strong>[<em id="mrs_01_24201__i12747727132115">database_name2</em>.]<em id="mrs_01_24201__i1274792712111">table_name</em>2<strong id="mrs_01_24201__b9747202710216"> </strong>[ENGINE = <em id="mrs_01_24201__i2747102722116">engine</em><em id="mrs_01_24201__i157479272216">_name</em>]</p>
|
|
</li><li id="mrs_01_24201__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__b819822053312">SELECT</strong> clause and filling it with the result of the <strong id="mrs_01_24201__b514382223312">SELECT</strong> clause<p id="mrs_01_24201__p9747192711215"><strong id="mrs_01_24201__b37472027122118">CREATE TABLE [IF NOT EXISTS] </strong><em id="mrs_01_24201__i1674716275212">[</em><em id="mrs_01_24201__i3747102772115">database_name</em><em id="mrs_01_24201__i474742752117">.]table_name</em><strong id="mrs_01_24201__b4747327102117"> </strong><em id="mrs_01_24201__i1574732716218">ENGINE</em> = <em id="mrs_01_24201__i13747192722116">engine</em><em id="mrs_01_24201__i117479277214">_name</em><strong id="mrs_01_24201__b19747162719212"> AS SELECT </strong>...</p>
|
|
</li></ul>
|
|
</div>
|
|
<div class="section" id="mrs_01_24201__section1817894102220"><h4 class="sectiontitle">Example</h4><pre class="screen" id="mrs_01_24201__screen149191111455">-- Create a table named <strong id="mrs_01_24201__b347107002111318">test</strong> in the <strong id="mrs_01_24201__b1122365085111318">default</strong> database and <strong id="mrs_01_24201__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>
|
|
|