Yang, Tong 6182f91ba8 MRS component operation guide_normal 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>
2022-12-09 14:55:21 +00:00

46 lines
5.1 KiB
HTML

<a name="mrs_01_24204"></a><a name="mrs_01_24204"></a>
<h1 class="topictitle1">ALTER TABLE: Modifying a Table Structure</h1>
<div id="body0000001138846340"><p id="mrs_01_24204__p795718189259">This section describes the basic syntax and usage of the SQL statement for modifying a table structure in ClickHouse.</p>
<div class="section" id="mrs_01_24204__section486795662513"><h4 class="sectiontitle">Basic Syntax</h4><p id="mrs_01_24204__p26368513267"><strong id="mrs_01_24204__b196360514267">ALTER TABLE</strong> [<em id="mrs_01_24204__i116367562615">database_name</em>].<em id="mrs_01_24204__i563645142613">name </em>[<strong id="mrs_01_24204__b18636957266">ON CLUSTER</strong> <em id="mrs_01_24204__i1563612542617">cluster</em>] <strong id="mrs_01_24204__b363616510268">ADD</strong>|<strong id="mrs_01_24204__b063611572613">DROP</strong>|<strong id="mrs_01_24204__b1663617522610">CLEAR</strong>|<strong id="mrs_01_24204__b86364542616">COMMENT</strong>|<strong id="mrs_01_24204__b1636205112616">MODIFY </strong><strong id="mrs_01_24204__b17636455267">COLUMN </strong>...</p>
<div class="note" id="mrs_01_24204__note26361251268"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="mrs_01_24204__p36361156264"><strong id="mrs_01_24204__b1236987919114355">ALTER</strong> supports only <em id="mrs_01_24204__i1922405981114355">*</em>MergeTree, Merge, and Distributed engine tables.</p>
</div></div>
</div>
<div class="section" id="mrs_01_24204__section1149622642610"><h4 class="sectiontitle">Example</h4><pre class="screen" id="mrs_01_24204__screen97331558114718">-- Add the <strong id="mrs_01_24204__b1200365585114355">test01</strong> column to the <strong id="mrs_01_24204__b407777739114355">t1</strong> table.
ALTER TABLE t1 ADD COLUMN test01 String DEFAULT 'defaultvalue';
-- Query the modified table <strong id="mrs_01_24204__b173875763114355">t1</strong>.
desc t1
┌─name────┬─type─┬─default_type─┬─default_expression ┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ id │ UInt8 │ │ │ │ │ │
│ name │ String │ │ │ │ │ │
│ address │ String │ │ │ │ │ │
│ test01 │ String │ DEFAULT │ 'defaultvalue' │ │ │ │
└───────┴────┴────────┴────────── ┴───── ┴──────────┴─────────┘
-- Change the type of the <strong id="mrs_01_24204__b2128272342114355">name</strong> column in the <strong id="mrs_01_24204__b1933727057114355">t1</strong> table to UInt8.
ALTER TABLE t1 MODIFY COLUMN name UInt8;
-- Query the modified table <strong id="mrs_01_24204__b1065578535114355">t1</strong>.
desc t1
┌─name────┬─type─┬─default_type─┬─default_expression ┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ id │ UInt8 │ │ │ │ │ │
│ name │ UInt8 │ │ │ │ │ │
│ address │ String │ │ │ │ │ │
│ test01 │ String │ DEFAULT │ 'defaultvalue' │ │ │ │
└───────┴────┴────────┴────────── ┴───── ┴──────────┴─────────┘
-- Delete the <strong id="mrs_01_24204__b243852123114355">test01</strong> column from the <strong id="mrs_01_24204__b2016024879114355">t1</strong> table.
ALTER TABLE t1 DROP COLUMN test01;
-- Query the modified table <strong id="mrs_01_24204__b81130856114355">t1</strong>.
desc t1
┌─name────┬─type─┬─default_type─┬─default_expression ┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ id │ UInt8 │ │ │ │ │ │
│ name │ UInt8 │ │ │ │ │ │
│ address │ String │ │ │ │ │ │
└───────┴────┴────────┴────────── ┴───── ┴──────────┴─────────┘</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>