我有一个表交易,我在一个交易中保存两个记录,一个用于借记和其他贷记.
所以我在表creditAmount(Money)和debitAmount(Money)中有两列.@H_301_3@
我想要一个表级约束,每列中的任何一列都不为空.
即如果第3行creditAmount为null,那么debitAmount必须保持一些值和Vice Versa.@H_301_3@
连续插入记录时如何确保相同?@H_301_3@
解决方法
您可以在表格中添加
CHECK constraint:
ALTER TABLE Transactions ADD CONSTRAINT CK_Transactions_DebitOrCreditExists CHECK ((creditAmount IS NULL AND debitAmount IS NOT NULL) OR (creditAmount IS NOT NULL AND debitAmount IS NULL))