使用 EXISTS 语句查询表或数据库是否存在。执行该语句后,返回一个单独的 UInt8类型的列,如果表或数据库不存在,则包含一个值 0,如果表在指定的数据库中存在,则包含一个值 1。
EXISTS [TEMPORARY] [TABLE|DICTIONARY] [db.]name
-- 1. 创建数据库 CREATE DATABASE IF NOT EXISTS sample_db ON CLUSTER sample_cluster; -- 2. 创建表 DROP TABLE IF EXISTS sample_db.sample_able ON CLUSTER sample_cluster; CREATE TABLE IF NOT EXISTS sample_db.sample_able ON CLUSTER sample_cluster ( id UInt32, name String DEFAULT '', CONSTRAINT constraint1 CHECK id > 10 ) ENGINE=MergeTree() ORDER BY id; -- 3. 查询表是否存在 exists table sample_db.sample_able;