Add REPLACE query desc

This commit is contained in:
Anna 2021-01-23 22:46:24 +03:00
parent 15f4ae26c2
commit ae4a2e9c6f
3 changed files with 87 additions and 5 deletions

View File

@ -0,0 +1,15 @@
---
toc_priority: 32
toc_title: Atomic
---
# Atomic {#atomic}
It is an experimental database engine, it supports non-blocking `DROP` and `RENAME TABLE` queries and atomic `EXCHANGE TABLES t1 AND t2` queries.
## Creating a Database {#creating-a-database}
CREATE DATABASE test ENGINE = Atomic;
[Original article](https://clickhouse.tech/docs/en/engines/database_engines/atomic/) <!--hide-->

View File

@ -18,4 +18,6 @@ You can also use the following database engines:
- [MaterializeMySQL](../../engines/database-engines/materialize-mysql.md) - [MaterializeMySQL](../../engines/database-engines/materialize-mysql.md)
- [Atomic](../../engines/database-engines/atomic.md)
[Original article](https://clickhouse.tech/docs/en/database_engines/) <!--hide--> [Original article](https://clickhouse.tech/docs/en/database_engines/) <!--hide-->

View File

@ -14,7 +14,7 @@ By default, tables are created only on the current server. Distributed DDL queri
### With Explicit Schema {#with-explicit-schema} ### With Explicit Schema {#with-explicit-schema}
``` sql ``` sql
CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster] CREATE [OR REPLACE] TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
( (
name1 [type1] [NULL|NOT NULL] [DEFAULT|MATERIALIZED|ALIAS expr1] [compression_codec] [TTL expr1], name1 [type1] [NULL|NOT NULL] [DEFAULT|MATERIALIZED|ALIAS expr1] [compression_codec] [TTL expr1],
name2 [type2] [NULL|NOT NULL] [DEFAULT|MATERIALIZED|ALIAS expr2] [compression_codec] [TTL expr2], name2 [type2] [NULL|NOT NULL] [DEFAULT|MATERIALIZED|ALIAS expr2] [compression_codec] [TTL expr2],
@ -34,21 +34,21 @@ If necessary, primary key can be specified, with one or more key expressions.
### With a Schema Similar to Other Table {#with-a-schema-similar-to-other-table} ### With a Schema Similar to Other Table {#with-a-schema-similar-to-other-table}
``` sql ``` sql
CREATE TABLE [IF NOT EXISTS] [db.]table_name AS [db2.]name2 [ENGINE = engine] CREATE [OR REPLACE] TABLE [IF NOT EXISTS] [db.]table_name AS [db2.]name2 [ENGINE = engine]
``` ```
Creates a table with the same structure as another table. You can specify a different engine for the table. If the engine is not specified, the same engine will be used as for the `db2.name2` table. Creates a table with the same structure as another table. You can specify a different engine for the table. If the engine is not specified, the same engine will be used as for the `db2.name2` table.
## From a Table Function {#from-a-table-function} ### From a Table Function {#from-a-table-function}
``` sql ``` sql
CREATE TABLE [IF NOT EXISTS] [db.]table_name AS table_function() CREATE [OR REPLACE] TABLE [IF NOT EXISTS] [db.]table_name AS table_function()
``` ```
Creates a table with the structure and data returned by a [table function](../../../sql-reference/table-functions/index.md#table-functions). Creates a table with the structure and data returned by a [table function](../../../sql-reference/table-functions/index.md#table-functions).
``` sql ``` sql
CREATE TABLE [IF NOT EXISTS] [db.]table_name ENGINE = engine AS SELECT ... CREATE [OR REPLACE] TABLE [IF NOT EXISTS] [db.]table_name ENGINE = engine AS SELECT ...
``` ```
Creates a table with a structure like the result of the `SELECT` query, with the `engine` engine, and fills it with data from SELECT. Creates a table with a structure like the result of the `SELECT` query, with the `engine` engine, and fills it with data from SELECT.
@ -259,3 +259,68 @@ CREATE TEMPORARY TABLE [IF NOT EXISTS] table_name
In most cases, temporary tables are not created manually, but when using external data for a query, or for distributed `(GLOBAL) IN`. For more information, see the appropriate sections In most cases, temporary tables are not created manually, but when using external data for a query, or for distributed `(GLOBAL) IN`. For more information, see the appropriate sections
Its possible to use tables with [ENGINE = Memory](../../../engines/table-engines/special/memory.md) instead of temporary tables. Its possible to use tables with [ENGINE = Memory](../../../engines/table-engines/special/memory.md) instead of temporary tables.
## REPLACE TABLE {#replace-table-query}
`REPLACE` query allows you to add, drop, and rename data in the tables.
!!!note "Note"
This query is supported only for [Atomic](../../../engines/database_engines/atomic.md) database engine.
If you need to reject(delete) some data from a table, you can create a new table and fill it with a `SELECT` statement that doesn't retrieve unwanted data, then rename it to old table name:
```sql
CREATE TABLE myNewTable AS myOldTable;
```
Then load data with:
```sql
INSERT INTO myNewTable SELECT * FROM myOldTable WHERE CounterID <12345;
```
Drop old table and rename:
```sql
DROP TABLE myOldTable;
RENAME TABLE myNewTable TO myOldTable;
```
Instead of above, you can use the following:
```sql
REPLACE TABLE myOldTable SELECT * FROM myOldTable WHERE CounterID <12345;
```
**Examples:**
`REPLACE` for a non-existent table will cause an error.
```sql
CREATE DATABASE base ENGINE = Atomic;
REPLACE TABLE base.t1 (n UInt64, s String) ENGINE = MergeTree ORDER BY n; -- { serverError 60 }
```
Using `REPLACE` query to change data in columns.
```sql
CREATE OR REPLACE TABLE base.t1 (n UInt64, s String) ENGINE = MergeTree ORDER BY n;
INSERT INTO base.t1 VALUES (1, 'test');
SELECT * FROM base.t1;
CREATE OR REPLACE TABLE base.t1 (n UInt64, s Nullable(String)) ENGINE = MergeTree ORDER BY n;
INSERT INTO base.t1 VALUES (2, null);
SELECT * FROM base.t1;
REPLACE TABLE base.t1 (n UInt64) ENGINE = MergeTree ORDER BY n;
INSERT INTO base.t1 VALUES (3);
SELECT * FROM base.t1;
```
```text
1 test
2 \N
3
```
[Original article](https://clickhouse.tech/docs/en/sql-reference/statements/create/table) <!--hide-->