I am trying to add simple constraint in the database. I follow this
My goal: In Tenants table, if LeaseExpirationDate isn't NULL then it must be later than LeaseStartDate.
I tried:
alter table Tenants
add constraint leasedates_check check (leaseexpirationdate is not null or leaseexpirationdate > leasestartdate);
Then I try:
UPDATE
Tenants
SET
leaseexpirationdate = leasestartdate
WHERE
apartmentnumber = 1 and houseid = 100;
The row is updated even if leaseexpirationdate is not null. Where am I wrong? Am I not comparing dates right?
Create Statement:
CREATE TABLE Tenants(
HouseID INT,
ApartmentNumber INT,
LeaseTenantSSN INT NOT NULL,
LeaseStartDate DATE NOT NULL,
LeaseExpirationDate DATE,
Rent DECIMAL(7,2),
LastRentPaidDate DATE,
RentOverdue BOOLEAN,
PRIMARY KEY(HouseID, ApartmentNumber));
I am using Postgresql.