在开始之前,我想指出“汽油”指的是燃料或一种发动机,而不是一种轿车。在你继续沿着这条路走之前,请仔细思考。 (语义在数据库设计中比大多数人想象的更重要。)
你想做的事情相当简单,但不一定容易。这种超类型/子类型设计(也称为独占弧)的重要一点是使有关轿车的行不可能引用有关半卡车等的行。
MySQL 使代码更加冗长,因为它不强制执行 CHECK 约束。你很幸运;在您的应用程序中,CHECK 约束可以替换为附加表和外键约束。注释参考SQLabove them.
create table vehicle_types (
veh_type_code char(1) not null,
veh_type_name varchar(10) not null,
primary key (veh_type_code),
unique (veh_type_name)
);
insert into vehicle_types values
('s', 'Semi-truck'), ('c', 'Car');
我可能会在其他平台上将这种情况实现为 CHECK 约束。当代码的含义对用户来说很明显时,您可以这样做。我希望用户知道或弄清楚“s”代表半成品,“c”代表汽车,或者视图/应用程序代码会对用户隐藏代码。
create table vehicles (
veh_id integer not null,
veh_type_code char(1) not null,
other_columns char(1) default 'x',
primary key (veh_id),
unique (veh_id, veh_type_code),
foreign key (veh_type_code) references vehicle_types (veh_type_code)
);
UNIQUE 约束让一对列 {veh_id, veh_type_code} 成为外键引用的目标。这意味着“car”行不可能引用“semi”行,即使是错误的。
insert into vehicles (veh_id, veh_type_code) values
(1, 's'), (2, 'c'), (3, 'c'), (4, 'c'), (5, 'c'),
(6, 'c'), (7, 'c');
create table car_types (
car_type char(3) not null,
primary key (car_type)
);
insert into car_types values
('Van'), ('SUV'), ('Sed');
create table veh_type_is_car (
veh_type_car char(1) not null,
primary key (veh_type_car)
);
我会在其他平台上实现其他一些内容作为 CHECK 约束。 (见下文。)
insert into veh_type_is_car values ('c');
曾经只有一排。
create table cars (
veh_id integer not null,
veh_type_code char(1) not null default 'c',
car_type char(3) not null,
other_columns char(1) not null default 'x',
primary key (veh_id ),
unique (veh_id, veh_type_code, car_type),
foreign key (veh_id, veh_type_code) references vehicles (veh_id, veh_type_code),
foreign key (car_type) references car_types (car_type),
foreign key (veh_type_code) references veh_type_is_car (veh_type_car)
);
veh_type_code 的默认值以及对 veh_type_is_car 的外键引用保证该表中的该行只能与汽车有关,并且可以only参考车辆是汽车。在其他平台上,我只需将 veh_type_code 列声明为veh_type_code char(1) not null default 'c' check (veh_type_code = 'c')
.
insert into cars (veh_id, veh_type_code, car_type) values
(2, 'c', 'Van'), (3, 'c', 'SUV'), (4, 'c', 'Sed'),
(5, 'c', 'Sed'), (6, 'c', 'Sed'), (7, 'c', 'Sed');
create table sedan_types (
sedan_type_code char(1) not null,
primary key (sedan_type_code)
);
insert into sedan_types values
('g'), ('d'), ('h'), ('e');
create table sedans (
veh_id integer not null,
veh_type_code char(1) not null,
car_type char(3) not null,
sedan_type char(1) not null,
other_columns char(1) not null default 'x',
primary key (veh_id),
foreign key (sedan_type) references sedan_types (sedan_type_code),
foreign key (veh_id, veh_type_code, car_type) references cars (veh_id, veh_type_code, car_type)
);
insert into sedans (veh_id, veh_type_code, car_type, sedan_type) values
(4, 'c', 'Sed', 'g'), (5, 'c', 'Sed', 'd'), (6, 'c', 'Sed', 'h'),
(7, 'c', 'Sed', 'e');
如果您必须构建引用轿车的其他表,例如gas_sedans、diesel_sedans等,那么您需要构建类似于“veh_type_is_car”的单行表并设置对它们的外键引用。
在生产中,我会撤销对基表的权限,然后使用
- 可更新视图来执行插入和更新,或者
- 执行插入和更新的存储过程。