简体   繁体   English

在SQL更新之前检查数据

[英]checking data before SQL update

I'm switching from using a framework's model handling to using straight sql for the purpose of improving the performance. 为了提高性能,我从使用框架的模型处理切换到使用直接sql。 The framework would handle the model update and insert, and now I'm having a hard time with the validation aspect. 该框架将处理模型的更新和插入,现在我在验证方面很难。

I'm updating a model with several relationships, the framework allows all queries to fail if one fails. 我正在更新具有多个关系的模型,该框架允许所有查询在失败的情况下失败。 I'm trying to emulate this with multiple queries. 我试图用多个查询来模拟。

How do I check validity of several tables before starting to write any data to the database for update and create functions? 在开始将任何数据写入数据库以进行更新和创建功能之前,如何检查几个表的有效性? The added difficulty about the create queries is that the relationship tables need the id of the first one to be either created first, or validity of a key to be ignored on queries. 创建查询的额外困难是关系表需要首先创建第一个ID,或者需要在查询中忽略键的有效性。

Normally you do this with a combination of constraints and transactions . 通常,您将约束事务结合在一起进行。 Create PK, FK, and check constraints as needed. 创建PK,FK,并根据需要检查约束。 Possibly also create update timestamp triggers if you want to ensure you aren't overwriting a row someone else just modified. 如果要确保不覆盖其他人刚刚修改的行,则可能还会创建更新时间戳记触发器 If an error is raised on the insert, update or delete, rollback the transaction (or handle it). 如果在插入,更新或删除时引发错误,请回滚事务(或处理该事务)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM