考虑一张简单的桌子。
create table dbo.car( car_guid UNIQUEIDENTIFIER default(newid())
, car_type varchar(20) not null
, wind_protector varchar(20) not null
)
insert into dbo.car(car_type, wind_protector) VALUES('HARD_TOP', 'NA')
insert into dbo.car(car_type, wind_protector) VALUES('CONVERTIBLE', 'FLAPBLAST_3')
insert into dbo.car(car_type, wind_protector) values('CONVERTIBLE', 'FLAPBLAST_2')我试图创建一个检查约束,如果car_type是“可转换的”,那么wind_protector可以是"FLAPBLAST_2“或"FLAPBLAST_3”。否则,wind_protector的值是"NA“。列不能为空。
我写了基本的检查约束。
([wind_protector]='FLAPBLAST_3' OR [wind_protector]='FLAPBLAST_3')我不得不跨两列编写check约束,并使用and或逻辑。
有可能做我想要完成的事情吗?
谢谢,
发布于 2022-02-23 20:44:23
我认为你是在追求以下约束:
alter table car
add constraint chk1
check (
( car_type='CONVERTIBLE' and wind_protector in ('FLAPBLAST_2','FLAPBLAST_3'))
or wind_protector='NA'
);发布于 2022-02-23 22:51:27
您可以使用AND OR逻辑使用一个简单的多列约束来完成这一任务。
CHECK (
car_type = 'CONVERTIBLE' AND wind_protector IN ('FLAPBLAST_2', 'FLAPBLAST_3')
OR
car_type = 'CONVERTIBLE' AND wind_protector = 'NA'
)但是这里不需要一个check 约束, wind_protector不是Car的属性,而是CarType的属性。它可以有多个wind_protector。
因此,您需要将模式规范化为适当的第三范式。您还需要几个表:CarType,它包含每个car类型。然后,WindProtector表包含可能的风力保护器选项。最后,一个连接它们并定义哪些组合是可能的表:
create table dbo.CarType (
car_type varchar(20) not null primary key
);
insert dbo.CarType (car_type) VALUES
('HARD_TOP'),
('CONVERTIBLE');
create table dbo.WindProtector (
wind_protector varchar(20) not null primary key
);
insert dbo.WindProtector (wind_protector) VALUES
('NA'),
('FLAPBLAST_2'),
('FLAPBLAST_3');
create table dbo.CarOptions (
options_id int not null primary key -- surrogate key
, car_type varchar(20) not null references CarType (car_type)
, wind_protector varchar(20) not null references WindProtector (wind_protector)
, unique (car_type, wind_protector)
);
insert into dbo.CarOptions (options_id, car_type, wind_protector) VALUES
(1, 'HARD_TOP', 'NA'),
(2, 'CONVERTIBLE', 'FLAPBLAST_3'),
(3, 'CONVERTIBLE', 'FLAPBLAST_2');
create table dbo.Car (
car_guid UNIQUEIDENTIFIER default(newid())
, option_id int not null references CarOptions (options_id)
);
insert dbo.Car (option_id) VALUES
(1),
(2),
(3);根据需求,您可能希望将Car和CarOptions合并到一个表中。
我还建议使用NULL而不是'NA'。
https://stackoverflow.com/questions/71243728
复制相似问题