只要事务尚未提交,SQL Server 是否允许事务中违反约束?
只要事务尚未提交,SQL Server 是否允许事务中的约束违规(即延迟约束)?
我有一个正在运行的未提交事务,当该事务正在运行时,我将更改我的事务数据,这样它将违反一些约束(例如具有重复的主键)。当我提交事务时,数据将处于一致、有效的状态。 SQL 中,特别是 MS SQL Server 中通常允许这样做吗?
Does SQL Server allow constraint violations (i.e. deferred constraints) in a transaction as long as the transaction has not been committed yet?
I have a running, uncommitted transaction and while this transaction is running, I will change my data so that it will violate some constraints (like having duplicate primary keys for example). When I commit the transaction, the data will be in consistent, valid state. Is this generally allowed in SQL and specifically in MS SQL Server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,抱歉。 SQL Server 不允许事务中存在延迟约束。它存在于 SQL Server 6.5 中,但在 SQL Server 2000 中被删除:
每个单独的语句必须一致等,无论它是否在事务中
(例如 Oracle、Postgres、Interbase
一些 RDBMS 确实允许这样做
) href="https://connect.microsoft.com/SQLServer/feedback/details/124728/option-to-defer-foreign-key-constraint-checking-until-transaction-commit" rel="noreferrer">Microsoft Connect 请求,创建于 2006 年,请求此功能:
微软的最后一次回应是在十年前:
这是微软对“走开”的说法。
SQL-92 定义了
该功能于 1992 年 7 月通过 SQL-92 定义。 。语法示例如下:
No, sorry. SQL Server does not allow deferred contraints in a transaction. It was present in SQL Server 6.5, but removed in SQL Server 2000:
Each individual statement must be consistent etc, regardless of whether it is in a transaction
Some RDBMS do allow this (e.g. Oracle, Postgres, Interbase)
Connect
There is a Microsoft Connect request, created in 2006, asking for this feature:
The last response from Microsoft came a decade ago:
Which is Microsoft speak for "go away".
SQL-92 defines it
The feature was defined in July 1992 with SQL-92. An example syntax would be:
您可以在运行事务时禁用约束,然后在完成后重新启用它们。
警告:这将影响所有连接。
You can disable your constraints while running your transaction, and then reenabling them when you are done.
Warning: This will affect all connections.
如果必须(例如从导入文件中清理数据的过程),则将中间数据放入临时表或表变量或临时表中,然后仅在清理后对带有约束的真实表执行操作,并且使数据正确。
If you must (such as a Process to cleanse data from an import file), then put the intermediate data into Temp tables or table variables or staging tables and then only do the actions to the real tables with the constraints after you have cleansed it and made the data correct.
SQL Server 没有延迟约束选项。但在某些情况下,您可以使用支持忽略约束的批量插入,而不会出现禁用问题。有关详细信息,您可以查看以下链接:
通过批量导入操作控制约束检查
批量插入< /a> 和 插入:使用 OPENROWSET和 BULK 批量加载数据
然后在提交事务之前,您必须手动检查整个表的约束。
SQL Server does not have deferred contraints option. But in some cases you can use Bulk Insert that supports ignoring constraints without problems of disabling. For more information you can see these links:
Controlling Constraint Checking by Bulk Import Operations
BULK INSERT and INSERT: Using OPENROWSET and BULK to Bulk Load Data
Then just before committing transaction, you will have to check the constraints on the whole table manually.
您也可以使用合并命令。它们一起解决,绕过约束中间状态。这是一个例子。在插入之前,John 的 pk id=1,Marta 的 pk id=2。名字和姓氏是唯一的。合并命令开关放置在 MERGE 之后,John 的 PK id=2 且 Marta id=1。使用普通更新命令无法切换 Marta 和 Joe 名称。
ALTERNATIVE you can use Merge commands. They resolve together, bypassing the constraint intermediary state. Here is an example. Before insert, John has pk id=1, Marta has pk id=2. Firstname and lastname are unique. The Merge command switch places and after the MERGE John has PK id=2 and Marta id=1. Switching Marta and Joe names are not possible with normal update commands.