Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions docs/sql-ref-syntax-dml-insert-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ The `INSERT` statement inserts new rows into a table or overwrites the existing
```sql
INSERT [ INTO | OVERWRITE ] [ TABLE ] table_identifier [ partition_spec ] [ ( column_list ) ]
{ VALUES ( { value | NULL } [ , ... ] ) [ , ( ... ) ] | query }

INSERT INTO [ TABLE ] table_identifier REPLACE WHERE boolean_expression query
```

### Parameters
Expand Down Expand Up @@ -58,6 +60,12 @@ INSERT [ INTO | OVERWRITE ] [ TABLE ] table_identifier [ partition_spec ] [ ( co
Specifies the values to be inserted. Either an explicitly specified value or a NULL can be inserted.
A comma must be used to separate each value in the clause. More than one set of values can be specified to insert multiple rows.

* **boolean_expression**

Specifies any expression that evaluates to a result type `boolean`. Two or
more expressions may be combined together using the logical
operators ( `AND`, `OR` ).

* **query**

A query that produces the rows to be inserted. It can be in one of following formats:
Expand Down Expand Up @@ -310,6 +318,39 @@ SELECT * FROM students;
+-------------+--------------------------+----------+
```

##### Insert Using a REPLACE WHERE Statement

```sql
-- Assuming the persons and persons2 table has already been created and populated.
SELECT * FROM persons;
+-------------+--------------------------+---------+
| name| address| ssn|
+-------------+--------------------------+---------+
|Dora Williams|134 Forest Ave, Menlo Park|123456789|
+-------------+--------------------------+---------+
| Eddie Davis| 245 Market St, Milpitas|345678901|
+-------------+--------------------------+---------+

SELECT * FROM persons2;
+-------------+--------------------------+---------+
| name| address| ssn|
+-------------+--------------------------+---------+
| Ashua Hill| 456 Erica Ct, Cupertino|432795921|
+-------------+--------------------------+---------+

-- in an atomic operation, 1) delete rows with ssn = 123456789 and 2) insert rows from persons2
INSERT INTO persons REPLACE WHERE ssn = 123456789 SELECT * FROM persons2

SELECT * FROM persons;
+-------------+--------------------------+---------+
| name| address| ssn|
+-------------+--------------------------+---------+
| Eddie Davis| 245 Market St, Milpitas|345678901|
+-------------+--------------------------+---------+
| Ashua Hill| 456 Erica Ct, Cupertino|432795921|
+-------------+--------------------------+---------+
```

##### Insert Using a TABLE Statement

```sql
Expand Down