> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# ADD SQLBLACKLIST

> ADD SQLBLACKLIST adds a regular expression to the SQL blacklist to forbid certain SQL patterns.

## 功能

将一个 SQL 正则表达式添加至 SQL 黑名单。启用 SQL 黑名单功能后，数据库 会将所有需要执行的 SQL 语句与黑名单中的 SQL 正则表达式进行比较。数据库 不会执行与黑名单中任何正则表达式相匹配的 SQL，并返回错误。

有关 SQL 黑名单的更多信息，请参阅 [管理 SQL 黑名单](/documents/cn/4.2/administration/management/resource_management/Blacklist.md)。

**Tip**

* 该操作需要 SYSTEM 级的 BLACKLIST 权限。请参考 [GRANT](/documents/cn/4.2/sql-reference/sql-statements/account-management/GRANT.md) 为用户赋权。
* 黑名单仅适用于 SELECT 语句、INSERT 语句（v3.1 及以上版本）以及 CTAS 语句（v3.4 及以上版本）。

## 语法

```SQL theme={null}
ADD SQLBLACKLIST "<sql_reg_expr>"
```

## 参数说明

`sql_reg_expr`：用于指定 SQL 语句的正则表达式。为了区分 SQL 语句中的特殊字符和正则表达式中的特殊字符，您需要使用转义符（\）作为 SQL 语句中特殊字符的前缀，如 `(`、`)` 以及 `+`。由于 `(` 和 `)` 在 SQL 语句中经常被使用，数据库 可以直接识别 SQL 语句中的 `(` 和 `)`。所以您无需为 `(` 和 `)` 添加转义符。

## 示例

示例一：将 `count(\*)` 添加到 SQL 黑名单。

```Plain theme={null}
mysql> ADD SQLBLACKLIST "select count(\\*) from .+";
```

示例二：将 `count(distinct )` 添加到 SQL 黑名单。

```Plain theme={null}
mysql> ADD SQLBLACKLIST "select count(distinct .+) from .+";
```

示例三：将 `order by limit x, y，1 <= x <=7, 5 <=y <=7` 加入SQL黑名单。

```Plain theme={null}
mysql> ADD SQLBLACKLIST "select id_int from test_all_type_select1 
    order by id_int 
    limit [1-7], [5-7]";
```

示例四：将复杂的 SQL 正则表达式添加到 SQL 黑名单中。该示例是为了演示如何使用转义符来表示 SQL 语句中的 `*` 和 `-`。

```Plain theme={null}
mysql> ADD SQLBLACKLIST 
    "select id_int \\* 4, id_tinyint, id_varchar 
        from test_all_type_nullable 
    except select id_int, id_tinyint, id_varchar 
        from test_basic 
    except select (id_int \\* 9 \\- 8) \\/ 2, id_tinyint, id_varchar 
        from test_all_type_nullable2 
    except select id_int, id_tinyint, id_varchar 
        from test_basic_nullable";
```

示例五：禁止所有 INSERT INTO 语句：

```sql theme={null}
ADD SQLBLACKLIST "(?i)^insert\\s+into\\s+.*";
```

示例六：禁止所有 INSERT INTO ... VALUES 语句：

```sql theme={null}
ADD SQLBLACKLIST "(?i)^insert\\s+into\\s+.*values\\s*\\(";
```

示例七：禁止所有 INSERT INTO ... VALUES 语句，但针对系统定义视图 `_statistics_.column_statistics` 的操作除外：

```sql theme={null}
ADD SQLBLACKLIST "(?i)^insert\\s+into\\s+(?!column_statistics\\b).*values\\s*\\(";
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.