简体   繁体   English

在表中输入一行时,如何检查另一个表的约束?

[英]How do you check constraints from another table when entering a row into a table?

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? PS:“检查约束”是否正确? 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. 注意:如果不理解或不了解OP的代码,很难假设任何事情。 So i have just provided example. 所以我只是提供了例子。

You can create a function where you verify if the balance is > than your @value. 您可以创建一个函数来验证余额是否大于@value。
And then you add that function to the constraint. 然后将该函数添加到约束中。

Try to look at this: 尝试看一下:

Can a Check constraint relate to another table? Check约束可以与另一个表相关吗?

What you're wanting not a constraint. 您想要的不是约束。 You're looking for a trigger. 您正在寻找触发器。 Take a look at this example. 看一下这个例子。

IF EXISTS ( SELECT  *
            FROM    sys.objects
            WHERE   object_id = OBJECT_ID(N'[dbo].[Account]')
                    AND type IN ( N'U' ) )
    DROP TABLE [dbo].[Account]
GO

CREATE TABLE dbo.Account
    (
      AccountID INT NOT NULL ,
      AccountBalance DECIMAL(19, 2) NOT NULL
    )
GO

IF EXISTS ( SELECT  *
            FROM    sys.objects
            WHERE   object_id = OBJECT_ID(N'[dbo].[Transaction]')
                    AND type IN ( N'U' ) )
    DROP TABLE [dbo].[Transaction]
GO

CREATE TABLE dbo.[Transaction]
    (
      TransactionID INT NOT NULL
                        IDENTITY(1, 1) ,
      AccountID INT NOT NULL ,
      TransactionAmount DECIMAL(19, 2) NOT NULL
    )
GO

CREATE TRIGGER dbo.TRI_Transaction ON dbo.[Transaction]
    AFTER INSERT
AS
    UPDATE  a
    SET     a.AccountBalance = a.AccountBalance + i.TransactionAmount
    FROM    Account a
            JOIN INSERTED i ON ( a.AccountID = i.AccountID )
GO

INSERT  INTO dbo.Account
        ( AccountID, AccountBalance )
VALUES  ( 1234, 0 )

SELECT  *
FROM    dbo.Account a
        LEFT OUTER JOIN dbo.[Transaction] t ON ( a.AccountID = t.AccountID )

INSERT  INTO dbo.[Transaction]
        ( AccountID, TransactionAmount )
VALUES  ( 1234, 10 )

SELECT  *
FROM    dbo.Account a
        LEFT OUTER JOIN dbo.[Transaction] t ON ( a.AccountID = t.AccountID )


INSERT  INTO dbo.[Transaction]
        ( AccountID, TransactionAmount )
VALUES  ( 1234, 20 )

SELECT  *
FROM    dbo.Account a
        LEFT OUTER JOIN dbo.[Transaction] t ON ( a.AccountID = t.AccountID )

INSERT  INTO dbo.[Transaction]
        ( AccountID, TransactionAmount )
VALUES  ( 1234, -15 )


SELECT  *
FROM    dbo.Account a
        LEFT OUTER JOIN dbo.[Transaction] t ON ( a.AccountID = t.AccountID )

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

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