forked from docs/doc-exports
Reviewed-by: Kacur, Michal <michal.kacur@t-systems.com> Co-authored-by: Yang, Tong <yangtong2@huawei.com> Co-committed-by: Yang, Tong <yangtong2@huawei.com>
45 lines
6.8 KiB
HTML
45 lines
6.8 KiB
HTML
<a name="mrs_01_24498"></a><a name="mrs_01_24498"></a>
|
|
|
|
<h1 class="topictitle1">Adding a Column</h1>
|
|
<div id="body0000001585194869"><div class="section" id="mrs_01_24498__en-us_topic_0265792790_sa1cb229bf26b4a7a8efd1716defb63de"><h4 class="sectiontitle">Function</h4><p id="mrs_01_24498__en-us_topic_0265792790_ac917e6d6e78044e99179c0e0f5284232">The <strong id="mrs_01_24498__b10507164415011">ADD COLUMNS</strong> command is used to add a column to an existing table.</p>
|
|
</div>
|
|
<div class="section" id="mrs_01_24498__en-us_topic_0265792790_sc019b10a59e9426498a678c024d74f03"><h4 class="sectiontitle">Syntax</h4><p id="mrs_01_24498__p2071145618449"><strong id="mrs_01_24498__b109162289391">ALTER TABLE<em id="mrs_01_24498__i413418372315"> </em></strong><em id="mrs_01_24498__i21358318231">Table name </em><strong id="mrs_01_24498__b1386064203920">ADD COLUMNS</strong><em id="mrs_01_24498__i84901347113911">(col_spec[, col_spec ...])</em></p>
|
|
</div>
|
|
<div class="section" id="mrs_01_24498__en-us_topic_0265792790_s253f086de08745ac890027c0b103cd7a"><h4 class="sectiontitle">Parameter Description</h4>
|
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="mrs_01_24498__en-us_topic_0265792790_ta69e9ecdc1554375a8a1e844b14e00c5" frame="border" border="1" rules="all"><caption><b>Table 1 </b>ADD COLUMNS parameters</caption><thead align="left"><tr id="mrs_01_24498__en-us_topic_0265792790_r61ddb6139b8d4f4ca4a88c6389f8f327"><th align="left" class="cellrowborder" valign="top" width="30%" id="mcps1.3.3.2.2.3.1.1"><p id="mrs_01_24498__en-us_topic_0265792790_aeb0bfa560fb74f1f9e584cc1ef05bb9b">Parameter</p>
|
|
</th>
|
|
<th align="left" class="cellrowborder" valign="top" width="70%" id="mcps1.3.3.2.2.3.1.2"><p id="mrs_01_24498__en-us_topic_0265792790_ae81789e97e6d4953a8342f22d64feea9">Description</p>
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody><tr id="mrs_01_24498__en-us_topic_0265792790_r4a578c7015ba461bab46a5c40c46256a"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.3.3.2.2.3.1.1 "><p id="mrs_01_24498__en-us_topic_0265792790_ac25da7f69c964d93947693ed5b4006cb">tableName</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="70%" headers="mcps1.3.3.2.2.3.1.2 "><p id="mrs_01_24498__en-us_topic_0265792790_a09288a5af7f44e36922b37de34446d94">Table name.</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="mrs_01_24498__en-us_topic_0265792790_rbf8f7998df2b40979c8e9e64d667e180"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.3.3.2.2.3.1.1 "><p id="mrs_01_24498__en-us_topic_0265792790_a440446fee79947738959536aa886e611">col_spec</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="70%" headers="mcps1.3.3.2.2.3.1.2 "><p id="mrs_01_24498__en-us_topic_0265792790_a9e6c31cb47f84b3d920a2ac5563ed7bf">Column specifications, consisting of five fields, <strong id="mrs_01_24498__b1846810441234">col_name</strong>, <strong id="mrs_01_24498__b1935547439">col_type</strong>, <strong id="mrs_01_24498__b169351485310">nullable</strong>, <strong id="mrs_01_24498__b1538410512310">comment</strong>, and <strong id="mrs_01_24498__b56131153236">col_position</strong>.</p>
|
|
<ul id="mrs_01_24498__ul14414405446"><li id="mrs_01_24498__li134414094417"><strong id="mrs_01_24498__b1212351546">col_name</strong>: name of the new column. It is mandatory.<p id="mrs_01_24498__p12341124772913">To add a sub-column to a nested column, specify the full name of the sub-column in this field. For example:</p>
|
|
<ul id="mrs_01_24498__ul18410208133420"><li id="mrs_01_24498__li84101285348">To add sub-column <strong id="mrs_01_24498__b1854310318719">col1</strong> to a nested struct type column <strong id="mrs_01_24498__b132755233713">column users struct<name: string, age: int></strong>, set this field to <strong id="mrs_01_24498__b429719362128">users.col1</strong>.</li><li id="mrs_01_24498__li124108810346">To add sub-column <strong id="mrs_01_24498__b17566124161312">col1</strong> to a nested map type column <strong id="mrs_01_24498__b726914517141">member map<string, struct<n: string, a: int>></strong>, set this field to <strong id="mrs_01_24498__b204951555142020">member.value.col1</strong>.</li><li id="mrs_01_24498__li341018163412">To add sub-column <strong id="mrs_01_24498__b4518161614210">col2</strong> to a nested array type column <strong id="mrs_01_24498__b4448150163216">arraylike array<struct<a1: string, a2: int>></strong>, set this field to <strong id="mrs_01_24498__b1645532783412">arraylike.element.col2</strong>.</li></ul>
|
|
</li><li id="mrs_01_24498__li17901045124410"><strong id="mrs_01_24498__b65771398341">col_type</strong>: type of the new column. It is mandatory.</li><li id="mrs_01_24498__li3901184554410"><strong id="mrs_01_24498__b1588514553363">nullable</strong>: whether the new column can be null. The value can be left empty.</li><li id="mrs_01_24498__li1902104515444"><strong id="mrs_01_24498__b1367813195504">comment</strong>: comment of the new column. The value can be left empty.</li><li id="mrs_01_24498__li1890214457440"><strong id="mrs_01_24498__b1665816504505">col_position</strong>: position where the new column is added. The value can be <strong id="mrs_01_24498__b387152111542">FIRST</strong> or <strong id="mrs_01_24498__b14920112625416">AFTER origin_col</strong>. If it is set to <strong id="mrs_01_24498__b710120359541">FIRST</strong>, the new column will be added to the first column of the table. If it is set to <strong id="mrs_01_24498__b597935916011">AFTER origin_col</strong>, the new column will be added after original column <strong id="mrs_01_24498__b1110672316112">origin_col</strong>. The value can be left empty. <strong id="mrs_01_24498__b1083025617103">FIRST</strong> can be used only when new sub-columns are added to nested columns. Do not use <strong id="mrs_01_24498__b1287846181112">FIRST</strong> in top-level columns. There are no restrictions about the usage of <strong id="mrs_01_24498__b59201842151117">AFTER</strong>.</li></ul>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="mrs_01_24498__en-us_topic_0265792790_s55536c9c534643ed8272f9f4b47fb407"><h4 class="sectiontitle">Example</h4><pre class="screen" id="mrs_01_24498__screen158063184013">alter table h0 add columns(ext0 string);
|
|
alter table h0 add columns(new_col int not null comment 'add new column' after col1);
|
|
alter table complex_table add columns(col_struct.col_name string comment 'add new column to a struct col' after col_from_col_struct);</pre>
|
|
</div>
|
|
<div class="section" id="mrs_01_24498__en-us_topic_0265792790_sbd5eea8637c94038bf6a129ab9ca129f"><h4 class="sectiontitle">Response</h4><p id="mrs_01_24498__en-us_topic_0265792790_ab3f2ebd199c342fb9103cbe4f02c7148">You can run the <strong id="mrs_01_24498__b437075415220">DESCRIBE</strong> command to view the new column.</p>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="mrs_01_24495.html">SparkSQL Schema Evolution and Syntax Description</a></div>
|
|
</div>
|
|
</div>
|
|
|