You need to enable JavaScript to run this app.
文档中心
ByteHouse 企业版

ByteHouse 企业版

复制全文
下载 pdf
DDL
重命名语句(RENAME)
复制全文
下载 pdf
重命名语句(RENAME)

RENAME 语句可用于重命名数据库、表或字典,支持在单个查询中重命名多个实体。

注意

具有多个实体的RENAME查询是非原子操作。

RENAME DATABASE

重命名数据库。

语法

RENAME DATABASE [databaseIdentifier] TO [databaseIdentifier2] [,...] 

示例

-- 示例中的sample_cluster,sample_db 需按实际情况修改
-- 1. 创建库
CREATE DATABASE IF NOT EXISTS sample_db ON CLUSTER sample_cluster;

-- 2. 若目标数据库 sample1 已存在,则先删除,避免重命名冲突
DROP DATABASE IF EXISTS sample_db1 ON CLUSTER sample_cluster;

-- 3. 将数据库 sample 重命名为 sample_db1
RENAME DATABASE sample_db to sample_db1 ON CLUSTER sample_cluster;

RENAME TABLE

重命名一个或多个表。
重命名表是一个轻量级的操作。 如果在 TO 子句中指定了不同的数据库名称,该表将被移动到目标数据库。但是源数据库和目标数据库的存储目录必须位于同一文件系统中,否则操作将失败并返回错误。
当在一个 RENAME TABLE 语句中同时重命名多个表时,该操作不是原子性的。操作可能部分成功。如果操作在执行过程中中断,只有部分表会被成功重命名/移动。在其他会话查询正在被重命名的表时,可能会遇到 Table ... does not exist ... 或类似错误,直到所有重命名操作完成。

语法

RENAME TABLE
  [tableIdentifier] TO [tableIdentifier],
  [tableIdentifier] TO [tableIdentifier];

示例

-- 示例中的 sample_cluster,sample_db,sample_table,sample_table_local 需按实际情况修改
-- 目标:将 sample_db.sample_table 表重命名为 sample_db.sample_table01 表
-- 1. 创建库
CREATE DATABASE IF NOT EXISTS sample_db ON CLUSTER sample_cluster;

-- 2. 创建表
-- 本地表(local table)
CREATE TABLE IF NOT EXISTS
  `sample_db`.`sample_table_local`
  ON CLUSTER sample_cluster(`k1` String NOT NULL) ENGINE = HaMergeTree
ORDER BY
  (k1)
SETTINGS
  index_granularity = 8192;

-- 分布式表(distributed table)
CREATE TABLE IF NOT EXISTS
  `sample_db`.`sample_table`
  ON CLUSTER sample_cluster AS `sample_db`.`sample_table_local` ENGINE = Distributed(
    'sample_cluster',
    'sample_db',
    'sample_table_local',
    rand()
  );

-- 3. 将local表 sample_table_local 重命名为 sample_table01_local
RENAME TABLE sample_db.sample_table_local to sample_db.sample_table01_local ON CLUSTER sample_cluster;

-- 4. 新建一张分布式sample_table01表(分布式表实际不存数据,新表映射的local表是sample_table01_local)
DROP TABLE sample_db.sample_table ON CLUSTER sample_cluster;
-- 新建distributed table
CREATE TABLE IF NOT EXISTS
  `sample_db`.`sample_table01`
  ON CLUSTER sample_cluster AS `sample_db`.`sample_table01_local` ENGINE = Distributed(
    'sample_cluster',
    'sample_db',
    'sample_table01_local',
    rand()
  );

RENAME DICTIONARY

重命名一个或多个字典。 此查询可用于在数据库之间移动字典。

语法

RENAME DICTIONARY
  [databaseIdentifier.]dict_name TO [databaseIdentifier.]dict_name
  [, ...];

示例

  • 单个字典重命名
    db0 数据库中的字典 dict_A 重命名为 db1 数据库中的 dict_B

    -- 1. 创建库
    CREATE DATABASE IF NOT EXISTS sample_db ON CLUSTER sample_cluster;
    
    -- 2. 创建字典
    CREATE DICTIONARY IF NOT EXISTS sample_db.sample_dict ON CLUSTER sample_cluster (
        `user_id` UInt64,
        `username` String,
        `age` UInt8 DEFAULT 0,
        `gender` UInt8 DEFAULT 0,
        `last_login` DateTime
    )
    PRIMARY KEY user_id
    SOURCE(MySQL(
        host 'mysql_host.example.com'
        port 3306
        user 'dict_reader'
        password 'secure_password'
        db 'user_db'
        table 'users'
    ))
    LAYOUT(FLAT())
    LIFETIME(MIN 300 MAX 900);
    
    -- 3. 若目标字典 new_sample_dict 已存在,则先删除,避免重命名冲突
    DROP DICTIONARY IF EXISTS sample_db.new_sample_dict;
    
    -- 4. 重命名字典
    RENAME DICTIONARY sample_db.sample_dict TO sample_db.new_sample_dict;
    
  • 多个字典重命名
    db0 中的 dict_A 重命名为 db1.dict_B,同时将 db2 中的 dict_C 重命名为 db2.dict_D

    -- 1. 创建库
    CREATE DATABASE IF NOT EXISTS sample_db ON CLUSTER sample_cluster;
    
    -- 2. 创建字典 1
    CREATE DICTIONARY IF NOT EXISTS sample_db.sample_dict1 ON CLUSTER sample_cluster (
        `user_id` UInt64,
        `username` String,
        `age` UInt8 DEFAULT 0,
        `gender` UInt8 DEFAULT 0,
        `last_login` DateTime
    )
    PRIMARY KEY user_id
    SOURCE(MySQL(
        host 'mysql_host.example.com'
        port 3306
        user 'dict_reader'
        password 'secure_password'
        db 'user_db'
        table 'users'
    ))
    LAYOUT(FLAT())
    LIFETIME(MIN 300 MAX 900);
    
    -- 3. 创建字典 2
    CREATE DICTIONARY IF NOT EXISTS sample_db.sample_dict2 ON CLUSTER sample_cluster (
        `user_id` UInt64,
        `username` String,
        `age` UInt8 DEFAULT 0,
        `gender` UInt8 DEFAULT 0,
        `last_login` DateTime
    )
    PRIMARY KEY user_id
    SOURCE(MySQL(
        host 'mysql_host.example.com'
        port 3306
        user 'dict_reader'
        password 'secure_password'
        db 'user_db'
        table 'users'
    ))
    LAYOUT(FLAT())
    LIFETIME(MIN 300 MAX 900);
    
    -- 4. 若目标字典 new_sample_dict 已存在,则先删除,避免重命名冲突
    DROP DICTIONARY IF EXISTS sample_db.new_sample_dict1;
    DROP DICTIONARY IF EXISTS sample_db.new_sample_dict2;
    
    -- 5. 重命名多个字典
    RENAME DICTIONARY 
       sample_db.sample_dict1 TO sample_db.new_sample_dict1, 
       sample_db.sample_dict2 TO sample_db.new_sample_dict2;
    
最近更新时间:2026.03.13 11:34:10
这个页面对您有帮助吗?
有用
有用
无用
无用