From ae4a2e9c6ff9ec383def0f65e763fd95f427b43a Mon Sep 17 00:00:00 2001 From: Anna Date: Sat, 23 Jan 2021 22:46:24 +0300 Subject: [PATCH] Add REPLACE query desc --- docs/en/engines/database-engines/atomic.md | 15 ++++ docs/en/engines/database-engines/index.md | 2 + .../sql-reference/statements/create/table.md | 75 +++++++++++++++++-- 3 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 docs/en/engines/database-engines/atomic.md diff --git a/docs/en/engines/database-engines/atomic.md b/docs/en/engines/database-engines/atomic.md new file mode 100644 index 00000000000..026f1ba04e4 --- /dev/null +++ b/docs/en/engines/database-engines/atomic.md @@ -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/) diff --git a/docs/en/engines/database-engines/index.md b/docs/en/engines/database-engines/index.md index 7b298ffe3f8..e2a45be9c09 100644 --- a/docs/en/engines/database-engines/index.md +++ b/docs/en/engines/database-engines/index.md @@ -18,4 +18,6 @@ You can also use the following database engines: - [MaterializeMySQL](../../engines/database-engines/materialize-mysql.md) +- [Atomic](../../engines/database-engines/atomic.md) + [Original article](https://clickhouse.tech/docs/en/database_engines/) diff --git a/docs/en/sql-reference/statements/create/table.md b/docs/en/sql-reference/statements/create/table.md index b1a5fdb19b5..3e7b53426c2 100644 --- a/docs/en/sql-reference/statements/create/table.md +++ b/docs/en/sql-reference/statements/create/table.md @@ -14,7 +14,7 @@ By default, tables are created only on the current server. Distributed DDL queri ### With Explicit Schema {#with-explicit-schema} ``` 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], 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} ``` 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. -## From a Table Function {#from-a-table-function} +### From a Table Function {#from-a-table-function} ``` 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). ``` 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. @@ -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 It’s 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)