mysql类型设计_MySQL-超类型/子类型设计

本文介绍了如何在MySQL中实现超类型/子类型设计,通过创建多个相关表来确保数据的正确性和一致性。例如,创建了vehicle_types、vehicles、car_types等表,利用外键和唯一约束来限制轿车数据只能引用汽车相关的记录,从而避免了数据错误。在没有CHECK约束的情况下,通过默认值和单行表实现类似的功能。
摘要由CSDN通过智能技术生成

匿名用户

在我开始之前,我想指出,“gas”描述的不是燃料,就是一种发动机,而不是一种轿车。 在你继续沿着这条路走下去之前,好好想一想。 (语义在数据库设计中比大多数人想象的更重要。)

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

MySQL使代码更加冗长,因为它不强制检查约束。 你真幸运; 在应用程序中,CHECK约束可以由附加的表和外键约束替换。 注释引用它们上面的SQL。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');

这就是我可能在其他平台上作为检查约束实现的那种东西。 当代码的含义对用户来说是显而易见的时,您可以这样做。 我希望用户知道或者弄清楚's'代表semis,'c'代表cars,或者视图/应用程序代码会对用户隐藏代码。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)

);

我会在其他平台上实现作为检查约束的其他东西。 (见下文。)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的外键引用,保证此表中的这些行只能与汽车有关,并且只能引用属于汽车的车辆。 在其他平台上,我只将列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”的单行表,并设置对它们的外键引用。

在生产中,我将吊销对基表的权限,并使用执行插入和更新的可更新视图,或

执行插入和更新的存储过程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值