CHECK statement only IF the condition is met - sql

I have to create a table where I need to check a condition only if another condition is met.
To precise I have a table "repair" with columns "solved" and "price", if the column "solved" has value 0 i need to check that the column "price" is equal to 0 too.
How can I do this in the create table statement?

I think you're looking for
ALTER TABLE Repair
ADD CONSTRAINT MyConstraint CHECK((Solved = 0 AND Price = 0) OR (Solved <> 0 AND Price <> 0));
How can I do this in the create table statement?
CREATE TABLE Repair(
Solved BIT NOT NULL,
Price DECIMAL(19,4) NOT NULL,
CONSTRAINT MyConstraint CHECK((Solved = 0 AND Price = 0) OR (Solved <> 0 AND Price <> 0))
);

Related

Why can't I add a column to an existing table with a checkConstraint that references other columns in SQL

I'm using SQL Server and am trying to add a column and a check constraint. I've found that the following works:
ALTER TABLE table.column
ADD isTrue BIT
GO
ALTER TABLE table.column
ADD CONSTRAINT CK_table_isTrue CHECK ((isTrue = 1 AND column1 = 0 AND column2 = 0 AND column3 IS NULL) OR isTrue = 0)
However a less verbose way of writing this does not work:
ALTER TABLE table.column
ADD isTrue BIT
CONSTRAINT CK_table_isTrue CHECK ((isTrue = 1 AND column1 = 0 AND column2 = 0 AND column3 IS NULL) OR isTrue = 0)
The following error is output:
Column CHECK constraint for column 'isTrue' references another column, table 'table'.
Looking at docs and SO I was unable to determine why this is the case
Your syntax is not quite right. A constraint that references multiple columns is a table constraint. Your're trying to add a table constraint so you need a comma after the datatype definition for isTrue.
ALTER TABLE table.column
ADD isTrue BIT,
CONSTRAINT CK_table_isTrue CHECK ((isTrue = 1 AND column1 = 0 AND column2 = 0 AND column3 IS NULL) OR isTrue = 0);
Without the comma SQL Server thinks you're trying to add a column constraint thus the error that you're referencing a different column.

Alter Table add check for nulls and not nulls

I am using Oracle Express, and I'd like to make a statement to add a check constraint to my Invoices table that allows Payment_Date to be NULL if Payment_Total = 0, AND Payment_Date to be NOT NULL if Payment_Total > 0.
I only understand how to alter a table to add a constraint that checks that a column's value. I don't understand how to make constraints that Allow null values or disallow null values if a certain condition (ColumnValue > SomeValue) is met.
Here is how you can express the check constraint:
alter table t add constraint ck_values
check ((payment_date is null and payment_total = 0) or
(payment_date is not null and payment_total > 0)
);

Best way to use available bool columns in a table to generate a 'Totals' column in a SQL View?

I have 4 columns in a Table 'Order' which are of bit datatype. I have them defined like this:
[IsRebate1Applicable] BIT CONSTRAINT [DF_Order_IsRebate1Applicable] DEFAULT ((0)) NOT NULL,
[IsRebate2Applicable] BIT CONSTRAINT [DF_Order_IsRebate2Applicable] DEFAULT ((0)) NOT NULL,
[IsRebate3Applicable] BIT CONSTRAINT [DF_Order_IsRebate3Applicable] DEFAULT ((0)) NOT NULL,
[IsRebate4Applicable] BIT CONSTRAINT [DF_Order_IsRebate4Applicable] DEFAULT ((0)) NOT NULL,
I am trying to generate a sql view from this table which has a 'Total Rebates' column which should be the SUM of rebates that are marked true.
Rules:
If rebate 1 is true then 2500 else 0
If rebate 2 is true then 1000 else 0
If rebate 3 is true then 750 else 0
If rebate 4 is true then 750 else 0
What would be an appropriate way to achieve this?
Can we do it using SUM & CASE in SQL?
SELECT (IsRebate1Applicable * 2500) + (IsRebate2Applicable * 1000) + (IsRebate3Applicable * 750) + (IsRebate4Applicable * 750)
This works because a bit field with 0 is false; a value of 1 is true. So we multiple the bit field (0/1) by the amount of the rebate.

Using a function in a SQL CHECK constraint

I am trying to replace simple CHECK constraint with an embedded function within a CHECK constraint however it doesn't seem to restrict the data I can enter. The constraint is to prevent product amount of less than 0.25 and more than 5,000. The original check worked fine, however the function doesn't seem to do anything at all.
The original constraint:
ALTER TABLE Prices
ADD CONSTRAINT CheckPrices CHECK ((Amount > 0.25) AND (Amount <= 5000.00))
The function:
ALTER FUNCTION dbo.CheckProductPrices
(
#productSKU int,
#priceDate smalldatetime
)
RETURNS bit
AS
BEGIN
DECLARE #retVal bit = 0
SELECT #retVal = CASE WHEN Amount > 0.25 AND Amount <= 5000.00 THEN 1 ELSE 0 END
FROM Prices
WHERE productSKU = #productSKU
AND priceDate = #priceDate
RETURN #retVal
END
The new CHECK constraint:
ALTER TABLE Prices
ADD CONSTRAINT CheckPrices CHECK (dbo.CheckProductPrices([productItem], [priceValidDate]) = 1)
I don't understand why the new constraint isn't stopping invalid prices the way the original constraint did.

SQL check with an if statement

Quick question on SQL how can I make a check such as:
Alter Table Invoices WITH NOCHECK
ADD CHECK
Now this is the part where I want to add a check so that the column PaymentDate can be null if another column Payment Total is equal to 0 and also that PaymentDate is not null if Payment Total is greater than 0.
Here is a way to do it :
Alter Table Invoices WITH NOCHECK
ADD CHECK ( (PaymentTotal > 0 AND PaymentDate IS NOT NULL)
OR (PaymentTotal = 0 ) )