|
| 1 | +--- |
| 2 | +layout: global |
| 3 | +title: INSERT INTO |
| 4 | +displayTitle: INSERT INTO |
| 5 | +license: | |
| 6 | + Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + contributor license agreements. See the NOTICE file distributed with |
| 8 | + this work for additional information regarding copyright ownership. |
| 9 | + The ASF licenses this file to You under the Apache License, Version 2.0 |
| 10 | + (the "License"); you may not use this file except in compliance with |
| 11 | + the License. You may obtain a copy of the License at |
| 12 | + |
| 13 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + |
| 15 | + Unless required by applicable law or agreed to in writing, software |
| 16 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + See the License for the specific language governing permissions and |
| 19 | + limitations under the License. |
| 20 | +--- |
| 21 | + |
| 22 | +### Description |
| 23 | + |
| 24 | +The `INSERT INTO` statement inserts new rows into a table. The inserted rows can be specified by value expressions or result from a query. |
| 25 | + |
| 26 | +### Syntax |
| 27 | +{% highlight sql %} |
| 28 | +INSERT INTO [ TABLE ] table_name |
| 29 | + [ PARTITION ( partition_col_name [ = partition_col_val ] [ , ... ] ) ] |
| 30 | + { { VALUES ( { value | NULL } [ , ... ] ) [ , ( ... ) ] } | query } |
| 31 | +{% endhighlight %} |
| 32 | + |
| 33 | +### Parameters |
| 34 | +<dl> |
| 35 | + <dt><code><em>table_name</em></code></dt> |
| 36 | + <dd>The name of an existing table.</dd> |
| 37 | +</dl> |
| 38 | + |
| 39 | +<dl> |
| 40 | + <dt><code><em>PARTITION ( partition_col_name [ = partition_col_val ] [ , ... ] )</em></code></dt> |
| 41 | + <dd>Specifies one or more partition column and value pairs. The partition value is optional.</dd> |
| 42 | +</dl> |
| 43 | + |
| 44 | +<dl> |
| 45 | + <dt><code><em>VALUES ( { value | NULL } [ , ... ] ) [ , ( ... ) ]</em></code></dt> |
| 46 | + <dd>Specifies the values to be inserted. Either an explicitly specified value or a NULL can be inserted. A comma must be used to seperate each value in the clause. More than one set of values can be specified to insert multiple rows.</dd> |
| 47 | +</dl> |
| 48 | + |
| 49 | +<dl> |
| 50 | + <dt><code><em>query</em></code></dt> |
| 51 | + <dd>A query that produces the rows to be inserted. It can be in one of following formats: |
| 52 | + <ul> |
| 53 | + <li>a <code>SELECT</code> statement</li> |
| 54 | + <li>a <code>TABLE</code> statement</li> |
| 55 | + <li>a <code>FROM</code> statement</li> |
| 56 | + </ul> |
| 57 | + </dd> |
| 58 | +</dl> |
| 59 | + |
| 60 | +### Examples |
| 61 | +#### Single Row Insert Using a VALUES Clause |
| 62 | +{% highlight sql %} |
| 63 | + CREATE TABLE students (name VARCHAR(64), address VARCHAR(64), student_id INT) |
| 64 | + USING PARQUET PARTITIONED BY (student_id); |
| 65 | + |
| 66 | + INSERT INTO students |
| 67 | + VALUES ('Amy Smith', '123 Park Ave, San Jose', 111111); |
| 68 | + |
| 69 | + SELECT * FROM students; |
| 70 | + |
| 71 | + + -------------- + ------------------------------ + -------------- + |
| 72 | + | name | address | student_id | |
| 73 | + + -------------- + ------------------------------ + -------------- + |
| 74 | + | Amy Smith | 123 Park Ave, San Jose | 111111 | |
| 75 | + + -------------- + ------------------------------ + -------------- + |
| 76 | +{% endhighlight %} |
| 77 | + |
| 78 | +#### Multi-Row Insert Using a VALUES Clause |
| 79 | +{% highlight sql %} |
| 80 | + INSERT INTO students |
| 81 | + VALUES ('Bob Brown', '456 Taylor St, Cupertino', 222222), |
| 82 | + ('Cathy Johnson', '789 Race Ave, Palo Alto', 333333); |
| 83 | + |
| 84 | + SELECT * FROM students; |
| 85 | + |
| 86 | + + -------------- + ------------------------------ + -------------- + |
| 87 | + | name | address | student_id | |
| 88 | + + -------------- + ------------------------------ + -------------- + |
| 89 | + | Amy Smith | 123 Park Ave, San Jose | 111111 | |
| 90 | + + -------------- + ------------------------------ + -------------- + |
| 91 | + | Bob Brown | 456 Taylor St, Cupertino | 222222 | |
| 92 | + + -------------- + ------------------------------ + -------------- + |
| 93 | + | Cathy Johnson | 789 Race Ave, Palo Alto | 333333 | |
| 94 | + + -------------- + ------------------------------ + -------------- + |
| 95 | +{% endhighlight %} |
| 96 | + |
| 97 | +#### Insert Using a SELECT Statement |
| 98 | +Assuming the `persons` table has already been created and populated. |
| 99 | + |
| 100 | +{% highlight sql %} |
| 101 | + SELECT * FROM persons; |
| 102 | + |
| 103 | + + -------------- + ------------------------------ + -------------- + |
| 104 | + | name | address | ssn | |
| 105 | + + -------------- + ------------------------------ + -------------- + |
| 106 | + | Dora Williams | 134 Forest Ave, Melo Park | 123456789 | |
| 107 | + + -------------- + ------------------------------ + -------------- + |
| 108 | + | Eddie Davis | 245 Market St, Milpitas | 345678901 | |
| 109 | + + -------------- + ------------------------------ + ---------------+ |
| 110 | + |
| 111 | + INSERT INTO students PARTITION (student_id = 444444) |
| 112 | + SELECT name, address FROM persons WHERE name = "Dora Williams"; |
| 113 | + |
| 114 | + SELECT * FROM students; |
| 115 | + |
| 116 | + + -------------- + ------------------------------ + -------------- + |
| 117 | + | name | address | student_id | |
| 118 | + + -------------- + ------------------------------ + -------------- + |
| 119 | + | Amy Smith | 123 Park Ave, San Jose | 111111 | |
| 120 | + + -------------- + ------------------------------ + -------------- + |
| 121 | + | Bob Brown | 456 Taylor St, Cupertino | 222222 | |
| 122 | + + -------------- + ------------------------------ + -------------- + |
| 123 | + | Cathy Johnson | 789 Race Ave, Palo Alto | 333333 | |
| 124 | + + -------------- + ------------------------------ + -------------- + |
| 125 | + | Dora Williams | 134 Forest Ave, Melo Park | 444444 | |
| 126 | + + -------------- + ------------------------------ + -------------- + |
| 127 | +{% endhighlight %} |
| 128 | + |
| 129 | +#### Insert Using a TABLE Statement |
| 130 | +Assuming the `visiting_students` table has already been created and populated. |
| 131 | + |
| 132 | +{% highlight sql %} |
| 133 | + SELECT * FROM visiting_students; |
| 134 | + |
| 135 | + + -------------- + ------------------------------ + -------------- + |
| 136 | + | name | address | student_id | |
| 137 | + + -------------- + ------------------------------ + -------------- + |
| 138 | + | Fleur Laurent | 345 Copper St, London | 777777 | |
| 139 | + + -------------- + ------------------------------ + -------------- + |
| 140 | + | Gordon Martin | 779 Lake Ave, Oxford | 888888 | |
| 141 | + + -------------- + ------------------------------ + -------------- + |
| 142 | + |
| 143 | + INSERT INTO students TABLE visiting_students; |
| 144 | + |
| 145 | + SELECT * FROM students; |
| 146 | + |
| 147 | + + -------------- + ------------------------------ + -------------- + |
| 148 | + | name | address | student_id | |
| 149 | + + -------------- + ------------------------------ + -------------- + |
| 150 | + | Amy Smith | 123 Park Ave, San Jose | 111111 | |
| 151 | + + -------------- + ------------------------------ + -------------- + |
| 152 | + | Bob Brown | 456 Taylor St, Cupertino | 222222 | |
| 153 | + + -------------- + ------------------------------ + -------------- + |
| 154 | + | Cathy Johnson | 789 Race Ave, Palo Alto | 333333 | |
| 155 | + + -------------- + ------------------------------ + -------------- + |
| 156 | + | Dora Williams | 134 Forest Ave, Melo Park | 444444 | |
| 157 | + + -------------- + ------------------------------ + -------------- + |
| 158 | + | Fleur Laurent | 345 Copper St, London | 777777 | |
| 159 | + + -------------- + ------------------------------ + -------------- + |
| 160 | + | Gordon Martin | 779 Lake Ave, Oxford | 888888 | |
| 161 | + + -------------- + ------------------------------ + -------------- + |
| 162 | +{% endhighlight %} |
| 163 | + |
| 164 | +#### Insert Using a FROM Statement |
| 165 | +Assuming the `applicants` table has already been created and populated. |
| 166 | + |
| 167 | +{% highlight sql %} |
| 168 | + SELECT * FROM applicants; |
| 169 | + |
| 170 | + + -------------- + ------------------------------ + -------------- + -------------- + |
| 171 | + | name | address | student_id | qualified | |
| 172 | + + -------------- + ------------------------------ + -------------- + -------------- + |
| 173 | + | Helen Davis | 469 Mission St, San Diego | 999999 | true | |
| 174 | + + -------------- + ------------------------------ + -------------- + -------------- + |
| 175 | + | Ivy King | 367 Leigh Ave, Santa Clara | 101010 | false | |
| 176 | + + -------------- + ------------------------------ + -------------- + -------------- + |
| 177 | + | Jason Wang | 908 Bird St, Saratoga | 121212 | true | |
| 178 | + + -------------- + ------------------------------ + -------------- + -------------- + |
| 179 | + |
| 180 | + INSERT INTO students |
| 181 | + FROM applicants SELECT name, address, id applicants WHERE qualified = true; |
| 182 | + |
| 183 | + SELECT * FROM students; |
| 184 | + |
| 185 | + + -------------- + ------------------------------ + -------------- + |
| 186 | + | name | address | student_id | |
| 187 | + + -------------- + ------------------------------ + -------------- + |
| 188 | + | Amy Smith | 123 Park Ave, San Jose | 111111 | |
| 189 | + + -------------- + ------------------------------ + -------------- + |
| 190 | + | Bob Brown | 456 Taylor St, Cupertino | 222222 | |
| 191 | + + -------------- + ------------------------------ + -------------- + |
| 192 | + | Cathy Johnson | 789 Race Ave, Palo Alto | 333333 | |
| 193 | + + -------------- + ------------------------------ + -------------- + |
| 194 | + | Dora Williams | 134 Forest Ave, Melo Park | 444444 | |
| 195 | + + -------------- + ------------------------------ + -------------- + |
| 196 | + | Fleur Laurent | 345 Copper St, London | 777777 | |
| 197 | + + -------------- + ------------------------------ + -------------- + |
| 198 | + | Gordon Martin | 779 Lake Ave, Oxford | 888888 | |
| 199 | + + -------------- + ------------------------------ + -------------- + |
| 200 | + | Helen Davis | 469 Mission St, San Diego | 999999 | |
| 201 | + + -------------- + ------------------------------ + -------------- + |
| 202 | + | Jason Wang | 908 Bird St, Saratoga | 121212 | |
| 203 | + + -------------- + ------------------------------ + -------------- + |
| 204 | +{% endhighlight %} |
| 205 | + |
| 206 | +Related Statements: |
| 207 | + * [INSERT OVERWRITE statement](sql-ref-syntax-dml-insert-overwrite-table.html) |
| 208 | + * [INSERT OVERWRITE DIRECTORY statement](sql-ref-syntax-dml-insert-overwrite-directory.html) |
| 209 | + * [INSERT OVERWRITE DIRECTORY with Hive format statement](sql-ref-syntax-dml-insert-overwrite-directory-hive.html) |
0 commit comments