1. 概述
delete-stmt
隐藏
WITH
RECURSIVE
common-table-expression
,
DELETE
FROM
qualified-table-name
returning-clause
expr
WHERE
common-table-expression
显示
table-name
(
column-name
)
AS
NOT
MATERIALIZED
(
select-stmt
)
,
select-stmt
显示
WITH
RECURSIVE
common-table-expression
,
SELECT
DISTINCT
result-column
,
ALL
FROM
table-or-subquery
join-clause
,
WHERE
expr
GROUP
BY
expr
HAVING
expr
,
WINDOW
window-name
AS
window-defn
,
VALUES
(
expr
)
,
,
compound-operator
select-core
ORDER
BY
LIMIT
expr
ordering-term
,
OFFSET
expr
,
expr
compound-operator
显示
UNION
UNION
INTERSECT
EXCEPT
ALL
join-clause
显示
table-or-subquery
join-operator
table-or-subquery
join-constraint
join-constraint
显示
USING
(
column-name
)
,
ON
expr
join-operator
显示
NATURAL
LEFT
OUTER
JOIN
,
RIGHT
FULL
INNER
CROSS
ordering-term
显示
expr
COLLATE
collation-name
DESC
ASC
NULLS
FIRST
NULLS
LAST
result-column
显示
expr
AS
column-alias
*
table-name
.
*
table-or-subquery
显示
schema-name
.
table-name
AS
table-alias
INDEXED
BY
index-name
NOT
INDEXED
table-function-name
(
expr
)
,
AS
table-alias
(
select-stmt
)
(
table-or-subquery
)
,
join-clause
window-defn
显示
(
base-window-name
PARTITION
BY
expr
,
ORDER
BY
ordering-term
,
frame-spec
)
frame-spec
显示
GROUPS
BETWEEN
UNBOUNDED
PRECEDING
AND
UNBOUNDED
FOLLOWING
RANGE
ROWS
UNBOUNDED
PRECEDING
expr
PRECEDING
CURRENT
ROW
expr
PRECEDING
CURRENT
ROW
expr
FOLLOWING
expr
PRECEDING
CURRENT
ROW
expr
FOLLOWING
EXCLUDE
CURRENT
ROW
EXCLUDE
GROUP
EXCLUDE
TIES
EXCLUDE
NO
OTHERS
expr
显示
literal-value
bind-parameter
schema-name
.
table-name
.
column-name
unary-operator
expr
expr
binary-operator
expr
function-name
(
function-arguments
)
filter-clause
over-clause
(
expr
)
,
CAST
(
expr
AS
type-name
)
expr
COLLATE
collation-name
expr
NOT
LIKE
GLOB
REGEXP
MATCH
expr
expr
ESCAPE
expr
expr
ISNULL
NOTNULL
NOT
NULL
expr
IS
NOT
DISTINCT
FROM
expr
expr
NOT
BETWEEN
expr
AND
expr
expr
NOT
IN
(
select-stmt
)
expr
,
schema-name
.
table-function
(
expr
)
table-name
,
NOT
EXISTS
(
select-stmt
)
CASE
expr
WHEN
expr
THEN
expr
ELSE
expr
END
raise-function
filter-clause
显示
function-arguments
显示
DISTINCT
expr
,
*
ORDER
BY
ordering-term
,
ordering-term
显示
expr
COLLATE
collation-name
DESC
ASC
NULLS
FIRST
NULLS
LAST
literal-value
显示
CURRENT_TIMESTAMP
numeric-literal
string-literal
blob-literal
NULL
TRUE
FALSE
CURRENT_TIME
CURRENT_DATE
over-clause
显示
OVER
window-name
(
base-window-name
PARTITION
BY
expr
,
ORDER
BY
ordering-term
,
frame-spec
)
frame-spec
显示
GROUPS
BETWEEN
UNBOUNDED
PRECEDING
AND
UNBOUNDED
FOLLOWING
RANGE
ROWS
UNBOUNDED
PRECEDING
expr
PRECEDING
CURRENT
ROW
expr
PRECEDING
CURRENT
ROW
expr
FOLLOWING
expr
PRECEDING
CURRENT
ROW
expr
FOLLOWING
EXCLUDE
CURRENT
ROW
EXCLUDE
GROUP
EXCLUDE
TIES
EXCLUDE
NO
OTHERS
ordering-term
显示
expr
COLLATE
collation-name
DESC
ASC
NULLS
FIRST
NULLS
LAST
raise-function
显示
RAISE
(
ROLLBACK
,
error-message
)
IGNORE
ABORT
FAIL
select-stmt
显示
WITH
RECURSIVE
common-table-expression
,
SELECT
DISTINCT
result-column
,
ALL
FROM
table-or-subquery
join-clause
,
WHERE
expr
GROUP
BY
expr
HAVING
expr
,
WINDOW
window-name
AS
window-defn
,
VALUES
(
expr
)
,
,
compound-operator
select-core
ORDER
BY
LIMIT
expr
ordering-term
,
OFFSET
expr
,
expr
compound-operator
显示
UNION
UNION
INTERSECT
EXCEPT
ALL
join-clause
显示
table-or-subquery
join-operator
table-or-subquery
join-constraint
join-constraint
显示
USING
(
column-name
)
,
ON
expr
join-operator
显示
NATURAL
LEFT
OUTER
JOIN
,
RIGHT
FULL
INNER
CROSS
ordering-term
显示
expr
COLLATE
collation-name
DESC
ASC
NULLS
FIRST
NULLS
LAST
result-column
显示
expr
AS
column-alias
*
table-name
.
*
table-or-subquery
显示
schema-name
.
table-name
AS
table-alias
INDEXED
BY
index-name
NOT
INDEXED
table-function-name
(
expr
)
,
AS
table-alias
(
select-stmt
)
(
table-or-subquery
)
,
join-clause
window-defn
显示
(
base-window-name
PARTITION
BY
expr
,
ORDER
BY
ordering-term
,
frame-spec
)
frame-spec
显示
GROUPS
BETWEEN
UNBOUNDED
PRECEDING
AND
UNBOUNDED
FOLLOWING
RANGE
ROWS
UNBOUNDED
PRECEDING
expr
PRECEDING
CURRENT
ROW
expr
PRECEDING
CURRENT
ROW
expr
FOLLOWING
expr
PRECEDING
CURRENT
ROW
expr
FOLLOWING
EXCLUDE
CURRENT
ROW
EXCLUDE
GROUP
EXCLUDE
TIES
EXCLUDE
NO
OTHERS
type-name
显示
name
(
signed-number
,
signed-number
)
(
signed-number
)
signed-number
显示
qualified-table-name
显示
schema-name
.
table-name
AS
alias
INDEXED
BY
index-name
NOT
INDEXED
returning-clause
显示
RETURNING
expr
AS
column-alias
*
,
DELETE 命令从由 qualified-table-name 标识的表中删除记录。
如果 WHERE 子句不存在,则删除表中的所有记录。如果提供 WHERE 子句,则仅删除 WHERE 子句 布尔表达式 为真的行。表达式为假或 NULL 的行将保留。
2. CREATE TRIGGER 中 DELETE 语句的限制
以下限制适用于在 CREATE TRIGGER 语句主体中出现的 DELETE 语句
在触发器主体中,作为 DELETE 语句的一部分指定的 table-name 必须是不限定的。换句话说,触发器中不允许对表名使用 schema-name . 前缀。如果触发器所附加的表不在 temp 数据库中,则触发器主体中的 DELETE 语句必须对与该触发器位于同一个数据库中的表进行操作。如果触发器所附加的表在 TEMP 数据库中,则被删除表的非限定名称将以与顶级语句相同的方式解析(首先搜索 TEMP 数据库,然后搜索主数据库,然后按附加顺序搜索任何其他数据库)。
触发器中的 DELETE 语句不允许使用 INDEXED BY 和 NOT INDEXED 子句。
触发器中的 DELETE 语句不支持 LIMIT 和 ORDER BY 子句(如下所述)。
触发器不支持 RETURNING 子句。
3. 可选的 LIMIT 和 ORDER BY 子句
如果 SQLite 使用 SQLITE_ENABLE_UPDATE_DELETE_LIMIT 编译时选项进行编译,则 DELETE 语句的语法将通过添加可选的 ORDER BY 和 LIMIT 子句来扩展
delete-stmt-limited
WITH
RECURSIVE
common-table-expression
,
DELETE
FROM
qualified-table-name
WHERE
expr
returning-clause
ORDER
BY
ordering-term
,
LIMIT
expr
OFFSET
expr
,
expr
如果 DELETE 语句具有 LIMIT 子句,则将通过评估伴随的表达式并将其转换为整数值来找到将被删除的最大行数。如果评估 LIMIT 子句的结果不能无损地转换为整数值,则将发生错误。负 LIMIT 值将解释为“无限制”。如果 DELETE 语句还具有 OFFSET 子句,则类似地对其进行评估并将其转换为整数值。同样,如果该值不能无损地转换为整数,则会发生错误。如果没有 OFFSET 子句,或者计算出的整数值为负,则有效 OFFSET 值为零。
如果 DELETE 语句具有 ORDER BY 子句,则在应用 LIMIT 和 OFFSET 子句以确定实际删除的子集之前,将根据 ORDER BY 对在没有 LIMIT 子句的情况下将被删除的所有行进行排序。跳过前 M 行(其中 M 是通过评估 OFFSET 子句表达式找到的值),并删除接下来的 N 行(其中 N 是 LIMIT 表达式的值)。如果在考虑 OFFSET 子句后剩余的行少于 N 行,或者如果 LIMIT 子句评估为负值,则删除所有剩余的行。
如果 DELETE 语句没有 ORDER BY 子句,则在应用 LIMIT 和 OFFSET 子句以确定实际删除的子集之前,将以任意顺序组装在没有 LIMIT 子句的情况下将被删除的所有行。
DELETE 语句上的 ORDER BY 子句仅用于确定哪些行位于 LIMIT 范围内。删除行的顺序是任意的,不受 ORDER BY 子句的影响。这意味着,如果存在 RETURNING 子句 ,则语句返回的行可能不会按 ORDER BY 子句指定的顺序排列。
4. 截断优化
当从 DELETE 语句中同时省略 WHERE 子句和 RETURNING 子句时,并且被删除的表没有触发器,SQLite 使用一种优化来擦除整个表内容,而无需逐个访问表的每行。这种“截断”优化使删除操作运行得更快。在 SQLite 版本 3.6.5 (2008-11-12)之前,截断优化还意味着 sqlite3_changes() 和 sqlite3_total_changes() 接口以及 count_changes pragma 实际上不会返回已删除的行数。从 版本 3.6.5 (2008-11-12)开始,已修复该问题。
可以通过使用 SQLITE_OMIT_TRUNCATE_OPTIMIZATION 编译时开关重新编译 SQLite 来永久禁用所有查询的截断优化。
也可以使用 sqlite3_set_authorizer() 接口在运行时禁用截断优化。如果授权器回调针对 SQLITE_DELETE 操作代码返回 SQLITE_IGNORE ,则 DELETE 操作将继续执行,但将绕过截断优化,并且将逐个删除行。
本页最后修改于 2022-01-08 05:02:57 UTC