February 20, 2006 at 3:22 pm
create table dbo.tblRentalContract
(ContractID int Identity primary key,
CarID varchar(20) not null references tblCar (CarID),
CustomerID int not null references tblCustomer (CustomerID),
PickupOfficeID int not null references tblRentalOffice (OfficeID),
PickupDateTime DateTime not null,
PickupOdometer int not null check (PickupOdometer>0),
ReturnOfficeID int null references tblRentalOffice (OfficeID),
ReturnDateTime DateTime null check (ReturnDateTime > PickupDateTime),
ReturnOdometer int null check (ReturnOdometer>PickupOdometer))
THEN MY RESULT IS
Server: Msg 8141, Level 16, State 1, Line 39
Column CHECK constraint for column 'ReturnDateTime' references another column, table 'dbo.tblRentalContract'.
Server: Msg 8141, Level 16, State 1, Line 39
Column CHECK constraint for column 'ReturnOdometer' references another column, table 'dbo.tblRentalContract'.
How to do I define it as a table level check constraint?
February 21, 2006 at 7:57 am
Remove them from the create table and alter the table as below:
Alter table dbo.tblRentalContract add
Constraint C1 check (ReturnDateTime > PickupDateTime),
Constraint C2 check (ReturnOdometer>PickupOdometer)
See the topic "constraints, overview" in BOL for details on how to do this. You can also add the constraints in the table declaration, but it can't be inline with the column declaration. Must be after all of the columns are declared.
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply