Home > database >  In the database, how to add a belt to judge the only constraint?
In the database, how to add a belt to judge the only constraint?

Time:10-10

Have a daily sales record database, use logic to delete, is used to judge whether a State field State deleted, and the date fields SalesDate in this table is not repeated, data may have been deleted many times on the same day, no delete data to ensure the date only, and delete the data may appear multiple data on the same day, in this case, do you want to be deleted State judgment to control date only, namely, when the State=1, SalesDate required only, State=0, SalesDate'm not the only, can use constraints to solve this problem? If not, do you have any other plan?

CodePudding user response:

Can only use the trigger:
 -=============================================
Author: yenange
- the Create date: 20191019
Description:
-=============================================
CREATE the TRIGGER trig_t_I_U
ON dbo. T
AFTER INSERT, UPDATE
AS
The BEGIN
SET NOCOUNT ON;
IF the EXISTS (
SELECT the FROM 1 t AS A INNER JOIN INSERTED AS B ON Amy polumbo kId!=p. kId
[State]=1 AND a. AND b. [State]=1
AND A.S alesDate=B.S alesDate
)
The BEGIN
The ROLLBACK TRAN.
RAISERROR (' [State]=1 cases are not allowed to repeat the 'SalesDate, 16, 1);
END
END
GO

CodePudding user response:

Thank you, I try
  • Related