MySQL - 超类型/子类型设计

2024-04-21

我需要创建以下数据库:

对于半卡车,我不需要额外的子类型,而对于汽车,我只需要这 3 个子类型,对于轿车,我需要四个子类型。 对于 SELECT,我将使用 JOIN(规范化数据库),但我需要找到一种简单的方法来进行 INSERT。

  • 车辆表存储常用信息
  • 半挂车存储半挂车的特定信息
  • 汽车表具有汽车的特定字段和链接到三个子类型的 car_type 字段
  • Van、Suv 和 Sedan(以及其他类型,如果我需要的话)应该位于一张表 CAR_TYPE 中
  • 但是,对于轿车类型,我需要有其他子类型,这些子类型可能应该包含在另一个表中。 Suv 和 Vans 不需要这些子类型(在现实生活中 suv 中,vans 可以具有与轿车相同的子类型,但在我的情况下则不然)。

我需要完全按照图中的方式创建该数据库。

到目前为止,我的第一个方法是拥有下表:

  • 车辆:veh_id、veh_type(半挂车、汽车)、...、other_fields
  • Vehicle_semis: veh_id, ..., other_semis_fields
  • Vehicle_car:veh_id、car_type(厢型车、SUV、轿车)、other_car_specific_fields
  • car_type:car_type_id,类型
  • Sedan_type:sedan_type_id,类型

我的问题是,我不确定这是否是正确的方法,并且我不确切知道如何在表之间创建关系。

有任何想法吗?

谢谢你!

UPDATE:

The following diagram is based on @Mike 's answer: enter image description here


在开始之前,我想指出“汽油”指的是燃料或一种发动机,而不是一种轿车。在你继续沿着这条路走之前,请仔细思考。 (语义在数据库设计中比大多数人想象的更重要。)

你想做的事情相当简单,但不一定容易。这种超类型/子类型设计(也称为独占弧)的重要一点是使有关轿车的行不可能引用有关半卡车等的行。

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”的单行表并设置对它们的外键引用。

在生产中,我会撤销对基表的权限,然后使用

  • 可更新视图来执行插入和更新,或者
  • 执行插入和更新的存储过程。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MySQL - 超类型/子类型设计 的相关文章

随机推荐