在表中输入一行时,如何检查另一个表的约束? [英] How do you check constraints from another table when entering a row into a table?

查看:37
本文介绍了在表中输入一行时,如何检查另一个表的约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个表 Account 和 Withdraw.帐户具有属性余额.每次在提款表中输入一个值时,我想检查是否有足够的余额来这样做.如果是,我想从余额中减去该金额.

这不是我想要做的,而是我需求的简化版本.

PS:检查约束"是正确的短语吗?我不太确定.谢谢!

解决方案

这是一个简短的例子:

INSERT INTO mytable(身份证,姓名)选择 1,'测试'不存在的地方(选择 IDFROM myanother_table哪里 id = 1)

<块引用>

注意:如果不理解或不看,很难假设任何事情OP的代码.所以我只是提供了例子.

Say I have two tables Account and Withdraw. Account has an attribute Balance. Every time a value is entered into the table Withdraw I would like to check if there is sufficient balance to do so. If yes, I'd like to subtract that amount from the Balance.

This isn't exactly what I want to do, but a simplified version of my requirement.

PS: Is "check constraints" the correct phrase? I'm not really sure. Thanks!

解决方案

This is short example:

INSERT INTO mytable 
            (id, name) 
SELECT 1, 'test' 
WHERE  NOT EXISTS(SELECT id 
                  FROM   myanother_table 
                  WHERE  id = 1) 

Note: Its hard to assume anything without understand or taking a look of OP's code. So i have just provided example.

这篇关于在表中输入一行时,如何检查另一个表的约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆