简体   繁体   中英

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? 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.

You can create a function where you verify if the balance is > than your @value.
And then you add that function to the constraint.

Try to look at this:

Can a Check constraint relate to another table?

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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