// ClickHouse error reference

57: TABLE_ALREADY_EXISTS (code 57)

You ran CREATE TABLE, CREATE MATERIALIZED VIEW or CREATE DICTIONARY for a name that a table or view in the current database already uses.

エラーコード
57
名前
TABLE_ALREADY_EXISTS
カテゴリ
Tables, columns and metadata
エラーメッセージ
Table {database}.{table} already exists

意味

TABLE_ALREADY_EXISTS (57) means the server already has a table or materialized view with the name you tried to create in that database. A dictionary name clash reports DICTIONARY_ALREADY_EXISTS (code 387) instead, and CREATE DICTIONARY on a name that a table holds reports code 57. With CREATE TABLE IF NOT EXISTS the statement succeeds silently instead. ClickHouse keeps create_query on disk, so a crash mid-create does not usually leave a half-made table; if you see this after a failed migration, the earlier attempt really did commit.

よくある原因

  • The migration ran once already and is being re-run without IF NOT EXISTS on the statement.
  • Two nodes or two workers create the same table name at the same time in a race.
  • A Distributed or Replicated table creates an inner .inner. table and you picked the same name by hand.

Find the cause

`SHOW CREATE TABLE my_table` to see the existing schema, `system.tables` for engine and `metadata_modification_time`, `system.query_log` filtered on `event_date` and `query_kind = 'Create'` to find who made it.

修正方法

  1. Check what exists before creating, then choose: reuse it, or drop and recreate it.

    SELECT name, engine FROM system.tables WHERE database = currentDatabase() AND name = 'my_table';
  2. Make the migration idempotent so re-runs are harmless.

    CREATE TABLE IF NOT EXISTS my_table (id UInt64) ENGINE = MergeTree ORDER BY id;
  3. If the old table is truly unwanted, drop it and recreate with the intended schema. Send the two statements as separate requests: the HTTP interface rejects multi-statement queries with code 62.

    DROP TABLE my_table; CREATE TABLE my_table (id UInt64) ENGINE = MergeTree ORDER BY id;

Other ways to handle it

EXISTS TABLE my_table returns a Boolean you can branch on without catching an error. CREATE TABLE ... AS SELECT also takes IF NOT EXISTS, and the query does nothing when the table exists.

安全に再現する

CREATE TABLE scratch_a (id UInt32) ENGINE = MergeTree ORDER BY id; then run the same CREATE TABLE again without IF NOT EXISTS and you get code 57.

質問

Does IF NOT EXISTS hide a real problem?

It hides the schema mismatch: the statement succeeds but the existing table keeps its old schema. Run ALTER on the existing table when the schema should change.

I dropped the table but still get 57

A plain MergeTree drop frees the name at once. For a Replicated table the name can stay taken briefly after the drop, so retry after a short wait or check system.dropped_tables.

関連エラー

More tables, columns and metadata errors

コードとメッセージのテキストは、 ClickHouse documentation (system.errors)。全容は次で参照 ClickHouse error list または すべてのデータベースエラーを検索.

// エラーが減り、修正も速く

Work on ClickHouse in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for ClickHouse and the other databases you use.

このページのテキストとデータ: CC BY 4.0、出典は SQLly (sqlly.app) と明記してください。 コードと SQL スニペット: MIT. ライセンスの詳細 引用したエラーメッセージの文言は、各データベースベンダーに帰属します。