SQL check with an if statement - sql

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 ) )

Related

CHECK statement only IF the condition is met

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))
);

check constraint that references multiple columns in the same table

I am attempting to add a constraint to a DB2 database that will check three columns. I am using a table that is an invoice table that includes start date end date quantity item price etc. for each line item on an invoice. I want to prevent allowing start and end date from being null when a column linestatus = RELELASED. Here is the alter statement that I have so far. My question is why won't this work? I have verified that this table does not have any current instances of all three of these checks.
alter table pluspgbtrans
add constraint start_end_notnull
Check (eip_linestatus = 'RELEASED' AND eip_endate is not null AND eip_startdate is not null)
Your SQL statement is valid.
However, your logic has an error: this check does not apply only if eip_linestatus = 'RELEASED'.
As written, your constraint is asserting that all rows must have eip_linestatus = 'RELEASED' AND eip_endate is not null AND eip_startdate is not null.
So, if any rows in your table have eip_linestatus with a value of anything other than RELEASED, you'll get the SQL0544N error when you try to add the constraint.
To create the constraint you're looking for, you need to handle the other state(s) for eip_linestatus. I can't guess what they are, so here's a potential generic option:
alter table pluspgbtrans
add constraint start_end_notnull check (
(eip_linestatus <> 'RELEASED')
OR
(
eip_linestatus = 'RELEASED'
AND eip_endate is not null
AND eip_startdate is not null
)
);

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)
);

MSSQL - set limit for a column value

I have a table named Test with the following columns.
id PK, int, not null
amount money, not null
I want to set a limit on the amount, say, 1000. I don't want anyone to insert a value greater than 1000 in this column. Can anyone help me on how to do this?
Something like this
CREATE TABLE tablename
(
-------
--------
amount money,
CONSTRAINT chk_amount CHECK (amount <= 1000)
)
You can add a check constraint, like this:
ALTER TABLE Test
ADD CONSTRAINT chk_money CHECK (amount<=1000)
You can create check constraint for your table.
alter table dbo.Your_Table with check add constraint your_Table_Amount check (Amount <= 1000)
go
alter table dbo.Your_Table check constraint your_Table_Amount

Set a number value limit in SQL

I am new to SQL, and I have a table group with a group_cap column. How do I specify in SQL that column group_cap cannot be a value greater than 4. For example, there cannot be more than 4 people in group A.
Create a constraint on the table.
ALTER TABLE [group] ADD CONSTRAINT
CK_group_cap CHECK (group_cap <= 4)
for a range of values, use between
ALTER TABLE [group] ADD CONSTRAINT
CK_group_cap CHECK (group_cap between 1 and 4)