我想在數據庫中添加簡單的約束。 I follow this添加簡單的約束 - 數據庫
我的目標:在租戶表中,如果LeaseExpirationDate不爲NULL,那麼它必須晚於LeaseStartDate。
我想:
alter table Tenants
add constraint leasedates_check check (leaseexpirationdate is not null or leaseexpirationdate > leasestartdate);
然後我嘗試:
UPDATE
Tenants
SET
leaseexpirationdate = leasestartdate
WHERE
apartmentnumber = 1 and houseid = 100;
該行即使leaseexpirationdate is not null
更新。我錯在哪裏?我沒有比較日期嗎?
創建聲明:
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));
我使用PostgreSQL。