

 Amazon Redshift 將不再支援從修補程式 198 開始建立新的 Python UDFs。現有 Python UDF 將繼續正常運作至 2026 年 6 月 30 日。如需詳細資訊，請參閱[部落格文章](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# DELETE
<a name="r_DELETE"></a>

刪除資料表中的資料列。

**注意**  
單一 SQL 陳述式的大小上限為 16 MB。

## 語法
<a name="r_DELETE-synopsis"></a>

```
[ WITH [RECURSIVE] common_table_expression [, common_table_expression , ...] ]
DELETE [ FROM ] { table_name | materialized_view_name }
    [ { USING } table_name, ... ]
    [ WHERE condition ]
```

## Parameters
<a name="r_DELETE-parameters"></a>

WITH 子句  
指定一個或多個 *common-table-expressions* 的選用子句。請參閱 [WITH 子句](r_WITH_clause.md)。

FROM  
FROM 關鍵字為選用，但指定了 USING 子句時除外。`delete from event;` 和 `delete event;` 這兩個陳述式是相同的操作，會將 EVENT 資料表的所有資料列移除。  
若要刪除資料表中的所有資料列，請對資料表執行 [TRUNCATE](r_TRUNCATE.md)。TRUNCATE 比 DELETE 更有效率，而且不需要 VACUUM 和 ANALYZE。不過請注意，TRUNCATE 會遞交其執行所在的交易。

 *table\$1name*   
暫時性或持久性資料表。只有資料表的擁有者，或具有資料表 DELETE 權限的使用者可從資料表中刪除資料列。  
請考慮使用 TRUNCATE 命令在大型資料表上快速執行非限定的刪除操作；請參閱 [TRUNCATE](r_TRUNCATE.md)。  
從資料表中刪除大量資料列之後：  
+ 清空資料表以回收儲存空間和重新排序資料列。
+ 分析資料表以更新查詢規劃器的統計資訊。

 *materialized\$1view\$1name*   
具體化視觀表。DELETE 陳述式可在用於 [將擷取串流至具體化視觀表](materialized-view-streaming-ingestion.md) 的具體化視觀表上作用。只有具體化視觀表的擁有者或在具體化視觀表上具有 DELETE 權限的使用者，才能從中刪除資料列。  
您無法使用未授予使用者 IGNORE RLS 權限的資料列層級安全性 (RLS) 政策，在具體化視觀表上執行 DELETE 來進行串流擷取。有一個例外情況：如果執行 DELETE 的使用者已授予 IGNORE RLS，則可成功執行。如需詳細資訊，請參閱 [RLS 政策擁有權和管理](https://docs.aws.amazon.com/redshift/latest/dg/t_rls_ownership.html)。

USING *table\$1name*, ...  
USING 關鍵字會在 WHERE 子句條件中參考其他資料表時，用來加入資料表清單。例如，以下陳述式會從 EVENT 資料表中，刪除所有滿足 EVENT 和 SALES 資料表聯結條件的資料列。SALES 資料表必須在 FROM 清單中明確命名：  

```
delete from event using sales where event.eventid=sales.eventid;
```
若您在 USING 子句中重複目標資料表名稱，則 DELETE 操作會執行自我聯結。您可以在 WHERE 子句中使用子查詢來取代 USING 語法，做為撰寫相同查詢的替代方式。

WHERE *condition*   
此選用子句會限制僅刪除符合條件的資料列。例如，條件可以是資料欄上的限制、聯結條件，或根據查詢結果的條件。查詢可參考 DELETE 命令的目標以外的資料表。例如：  

```
delete from t1
where col1 in(select col2 from t2);
```
如未指定任何條件，則會刪除資料表中的所有資料列。

## 使用須知
<a name="r_DELETE-usage"></a>
+ DELETE 操作在連線至下列任一項的 Amazon Redshift 串流具體化視觀表上執行時，會保留獨佔鎖定：
  +  Amazon Kinesis Data Stream 
  +  Amazon Managed Streaming for Apache Kafka 主題 
  +  支援的外部串流，例如 Confluent Cloud Kafka 主題 

  如需詳細資訊，請參閱[將擷取串流至具體化視觀表](materialized-view-streaming-ingestion.md)。

## 範例
<a name="r_DELETE-examples"></a>

從 CATEGORY 資料表刪除所有資料列：

```
delete from category;
```

從 CATEGORY 資料表刪除 CATID 值介於 0 和 9 之間的資料列：

```
delete from category
where catid between 0 and 9;
```

從 LISTING 資料表刪除其 SELLERID 值不存在 SALES 資料表中的資料列：

```
delete from listing
where listing.sellerid not in(select sales.sellerid from sales);
```

以下兩個查詢都會根據 EVENT 資料表的聯結和對 CATID 資料欄的額外限制，從 CATEGORY 資料表刪除一個資料列：

```
delete from category
using event
where event.catid=category.catid and category.catid=9;
```

```
delete from category
where catid in
(select category.catid from category, event
where category.catid=event.catid and category.catid=9);
```

下列查詢會刪除 `mv_cities` 具體化視觀表中的所有資料列。此範例中的具體化視觀表名稱為範例：

```
delete from mv_cities;
```