Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Conditional Constraints?

Author  Topic 

leeholden
Starting Member

34 Posts

Posted - 2002-04-04 : 05:31:42
Are these possible?

This is what I want to do:

create table car_details (
reg_no varchar(10) not null,
number_drivers tinyint not null,
driver1 varchar(30) not null,
driver2 varchar(30),
driver3 varchar(30),

constraint pk_reg primary key (reg_no),
constraint ck_drv check (number_drivers between 1 and 3)
)


I now want to add two conditional constraint that say,
if number_drivers >= 2 then driver2 CANNOT be null
and if number_drivers = 3 then driver3 CANNOT be null


Is this possible?

tdodnz: Note the table definition in the first post and not 3 pages later... very entertaining though.

LarsG
Constraint Violating Yak Guru

284 Posts

Posted - 2002-04-04 : 08:06:53
It should be possible, e.g.

create table car_details (
reg_no varchar(10) not null,
number_drivers smallint not null,
driver1 varchar(30) not null,
driver2 varchar(30),
driver3 varchar(30),
constraint pk_reg primary key (reg_no),
constraint ck_drv check (
(number_drivers = 3 and driver2 is not null and driver3 is not null)
or (number_drivers = 2 and driver2 is not null and driver3 is null)
or (number_drivers = 1 and driver2 is null and driver3 is null)))


Maybe I should add some diatribe about database design?

Go to Top of Page

leeholden
Starting Member

34 Posts

Posted - 2002-04-04 : 09:46:12
Yeah that should work, cheers.

quote:
Maybe I should add some diatribe about database design?


You could do, but it's not my design, external supplier making demands...
Normalise it with a drivers table by any chance?
Go to Top of Page
   

- Advertisement -