0

I want to create constraint orderDate must be smaller than deliveryDate? Help me.

2 Answers2

2

Supposing the table name is MyTable:

ALTER TABLE [dbo].[MyTable] WITH CHECK 
ADD CONSTRAINT [CK_MyTable_date1] CHECK (orderDate <= deliveryDate)

ALTER TABLE [dbo].[MyTable] CHECK CONSTRAINT [CK_MyTable_date1]
GO
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
bjnr
  • 3,353
  • 1
  • 18
  • 32
1

There are two ways to do it.

First While creating the table and after the creation:

While creating the table:

CREATE TABLE Price (
PriceID INT PRIMARY KEY IDENTITY (1,1),
OriginalPrice FLOAT NOT NULL,
CurrentPrice FLOAT NOT NULL,
Discount FLOAT,
ShippingCost FLOAT NOT NULL,
Tax FLOAT NOT NULL,
CHECK (CurrentPrice <= OriginalPrice));

After creation the table:

ALTER TABLE Price ADD CHECK (CurrentPrice <= OriginalPrice);
--or
ALTER TABLE Price ADD CONSTRAINT CK_Price_Current_vs_Original
CHECK (CurrentPrice <= OriginalPrice);

You can go for the date fields in the same sense. For more info please Read this.

Community
  • 1
  • 1
Ram Singh
  • 6,664
  • 35
  • 100
  • 166