2016-07-10 85 views
0

I folgende Tabelle haben:Relation zwischen Spalte in einer Tabelle

Create table Rent 
(
Id int not null, 
Id_car int not null, 
Date_rent date not null, 
Date_return date, 
Id_pleace_rent int not null, 
Id_pleace_return int 
) 

Ich möchte constraint hinzuzufügen, dass, wenn in der gleichen Zeile in der Spalte "Date_return" NULL ist, dann in der Spalte "Id_pleace_return" too NULL sein muß . Wie kann ich das machen?

Antwort

0

Nun ist dies SQL Server so sollte es etwas in dieser Richtung sein:

ALTER TABLE Rent 
ADD CONSTRAINT Nullability 
CHECK (
(CASE WHEN Date_return IS NULL AND Id_pleace_return IS NOT NULL THEN 0 ELSE 1 END 

) 
GO 
3
ALTER TABLE dbo.Rent 
ADD CONSTRAITN CK_Rent_Verify_Return 
CHECK(
    Date_return IS NULL AND Id_pleace_return IS NULL 
    OR Date_return IS NOT NULL -- AND Id_pleace_return IS NOT NULL 
)