You need to enable JavaScript to run this app.
导航
删除语句 (DELETE)
最近更新时间:2025.02.07 17:12:16首次发布时间:2024.11.01 11:00:45

DELETE 语句用于删除目标表指定列的行数据。

使用限制

当前仅唯一键表支持使用更新语句(UPDATE),其他类型的表请使用“ALTER TABLE - DELETE”语句。

语法
DELETE FROM [db.]table WHERE expr;

示例
-- 引擎默认保证 unique key 在分区内的唯一性
-- 注:UNIQUE KEY 不支持 Nullable

CREATE TABLE  test.unique_table
(
  `event_time` DateTime,
  `product_id` UInt64,
  `city` String,
  `category` String,
  `amount` UInt32,
  `revenue` UInt64
)
ENGINE = CnchMergeTree
PARTITION BY toDate(event_time)
ORDER BY (city, category)
UNIQUE KEY product_id;

INSERT INTO  test.unique_table VALUES
('2020-10-29 23:40:00', 10001, 'Beijing', '男装', 5, 500),
('2020-10-29 23:40:00', 10002, 'Beijing', '男装', 2, 200),
('2020-10-29 23:40:00', 10003, 'Beijing', '男装', 1, 100);

-- 通过 DELETE FROM 来删除revenue == 500的数据
DELETE FROM  test.unique_table WHERE revenue = 500;
-- 通过 DELETE FROM 来删除revenue >= 200的数据
DELETE FROM  test.unique_table WHERE revenue >= 200;

-- 查询结果中已删除revenue >= 200的数据
select * from  test.unique_table order by toDate(event_time), product_id;
┌──────────event_time─┬─product_id─┬─city────┬─category─┬─amount─┬─revenue─┐
│ 2020-10-29 23:40:00 │      10003 │ Beijing │ 男装     │      1 │     100 │
└─────────────────────┴────────────┴─────────┴──────────┴────────┴─────────┘

注意

如果需要删除整个分区的数据,建议通过DROP PARTITION命令进行删除,更高效和快速。