# DROP TYPE

> 从数据库中删除用户定义的自定义类型
> Canonical HTML: https://tursodb.cn/zh-cn/docs/sql-reference/statements/drop-type/
> Upstream Source (MIT): https://docs.turso.tech/sql-reference/statements/drop-type
> Language: zh-CN

---
# 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-cn/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-cn/docs/sql-reference/statements/create-type/) 用于定义自定义类型
* [DROP DOMAIN](/zh-cn/docs/sql-reference/statements/drop-domain/) 用于删除域
* [数据类型](/zh-cn/docs/sql-reference/data-types/) 了解类型系统概述
