# DROP TYPE

> 從資料庫移除使用者自訂的型別
> Canonical HTML: https://tursodb.cn/zh-tw/docs/sql-reference/statements/drop-type/
> Upstream Source (MIT): https://docs.turso.tech/sql-reference/statements/drop-type
> Language: zh-TW

---
# DROP TYPE

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">注意</div><div class="doc-callout-body">

**Turso 擴充功能**：DROP TYPE 是 Turso 專屬的陳述式，標準 SQLite 並未提供。此功能為實驗性，必須先[啟用才能使用](/zh-tw/docs/sql-reference/experimental-features/)。

</div></div>


DROP TYPE 陳述式會從資料庫移除使用者自訂的型別。型別一經移除，其名稱就無法再用於新的欄位定義。

## 語法

```sql
DROP TYPE [IF EXISTS] type-name;
```

## 說明

DROP TYPE 會從資料庫移除具名的自訂型別。除非指定 `IF EXISTS` 子句，否則該型別必須存在。

### 子句

| 子句 | 說明 |
| - | - |
| `IF EXISTS` | 抑制型別不存在時會發生的錯誤。找不到該型別時，此陳述式不會執行任何操作。 |
| `type-name` | 要移除的自訂型別名稱。 |

### 限制

當任何資料表欄位使用某型別時，就無法移除該型別。必須先移除該型別的所有欄位，或先移除資料表，才能移除該型別。

```sql
CREATE TYPE cents BASE integer ENCODE value * 100 DECODE value / 100;

CREATE TABLE prices (
    id INTEGER PRIMARY KEY,
    amount cents
) STRICT;

-- This fails because the 'prices' table has a column of type 'cents'
DROP TYPE cents;
-- Error: type 'cents' is in use

-- Drop the table first, then the type
DROP TABLE prices;
DROP TYPE cents;
```

## 範例

### 移除自訂型別

```sql
CREATE TYPE reversed_text BASE text
    ENCODE turso_reverse(value)
    DECODE turso_reverse(value);

DROP TYPE reversed_text;
```

### 使用 IF EXISTS 移除型別

```sql
-- Safe to run even if the type does not exist
DROP TYPE IF EXISTS reversed_text;
```

## 另請參閱

* [CREATE TYPE](/zh-tw/docs/sql-reference/statements/create-type/) 用於定義自訂型別
* [DROP DOMAIN](/zh-tw/docs/sql-reference/statements/drop-domain/) 用於移除定義域
* [資料型別](/zh-tw/docs/sql-reference/data-types/) 取得型別系統的概觀
