简体   繁体   English

数据库设计是否重复?

[英]Database design too repetitive?

I'm working on a database schema that's driving me a little mad at the moment because I seem to be repeating the same tables to reproduce behaviour for different types. 我正在研究一个数据库模式,此刻让我有点发疯,因为我似乎要重复相同的表来重现不同类型的行为。

Basically the system consists of live sensors that are monitored over a network, and loggers that are collected via a handset. 基本上,该系统由通过网络监控的实时传感器和通过手机收集的记录器组成。 The sensors and loggers are divided up into Locations, and Locations are divided up into Areas. 传感器和记录器分为“位置”,“位置”分为“区域”。 A location can have up to 1 logger, but can have any number of live sensors. 一个位置最多可以有1个记录器,但可以有任意数量的实时传感器。

I'll try and break the rules down: 我将尝试打破规则:

  1. The system can have 0 to many Areas 系统可以有0到多个区域
  2. An area can have 0 to many Locations 一个区域可以有0到多个位置
  3. A Location can have 0 to 1 Loggers 一个位置可以有0到1个记录器
  4. A Location can have 0 to many LiveAnalogSensors 一个位置可以有0到许多LiveAnalogSensor
  5. A Location can have 0 to many LiveSwitchSensors 一个位置可以有0到许多LiveSwitchSensors
  6. A Location can have 0 to many Actions allowed 一个位置可以有0到多个动作
  7. A LiveAnalogSensor must belong to 1 LiveSensorRelay 一个LiveAnalogSensor必须属于1个LiveSensorRelay
  8. A LiveSwitchSensor must belong to 1 LiveSensorRelay 一个LiveSwitchSensor必须属于1个LiveSensorRelay
  9. A logger can have 0 to many Readings 记录器可以有0到许多读数
  10. A LiveAnalogSensor can have 0 to many Readings LiveAnalogSensor可以读取0到许多读数
  11. A LiveSwitchSensor can have 0 to many Readings LiveSwitchSensor可以读取0到许多读数
  12. A Reading can have an Alarm 读数可能有警报
  13. The system can have 0 to many Actions 系统可以有0到多个动作
  14. An Alarm can have 0 to 1 Action applied to it 警报可以应用0到1的动作
  15. An Alarm can have 0 to 1 AlarmResolution 一个警报可以有0到1个警报分辨率

The schema picture is HERE. 架构图片在这里。

So the scenario is, a reading comes in and is stored (either via the Logger download or a live reading coming in over the network). 因此,场景是一个读数进入并存储(通过Logger下载或通过网络输入的实时读数)。 The reading is out of range so it has an alarm code. 读数超出范围,因此带有警报代码。 An alarm gets generated for that reading. 将为该读数生成警报。 An action is eventually applied to it by the user. 最终,用户会对其执行操作。 If a reading comes in that indicates the alarm condition has ended, an AlarmResolution (or AlarmEnd as I called it in the schema) entry is made linked to the Alarm to show that it has ended, and the time it has ended. 如果出现读数,表明警报条件已结束,则将AlarmResolution(或在我在架构中称为AlarmEnd)的条目链接到Alarm,以显示其结束时间和结束时间。

On analog alarms, whenever a new reading is higher than the last, a new "Peak" reading is stored, which is what the AnalogSensorReadingAlertPeak table is for. 对于模拟警报,只要新读数高于上一个读数,就会存储一个新的“ Peak”读数,这就是AnalogSensorReadingAlertPeak表的作用。

That is basically it. 基本上就是这样。 My issue is how repetitive the schema seems for the different sensors (especially logger and analog-sensor which are basically the same) - and I seem to have a lot of 1 to 0..1 relationships, though I'm less concerned about that. 我的问题是对于不同的传感器(尤其是基本相同的记录器和模拟传感器),该模式的重复性如何-我似乎有很多1到0..1的关系,尽管我对此不太担心。

I'm after a sanity check really. 我真的在进行健全性检查。 I can think of ways to get rid of the repetitiveness, but it always seems to be at the expense of data integrity. 我可以想到摆脱重复性的方法,但是它似乎总是以牺牲数据完整性为代价。

Thanks. 谢谢。

EDIT: I've revised the title and question slightly after the down vote as it wasn't particularly specific. 编辑:由于没有特别明确的规定,我在向下表决后已对标题和问题进行了一些修改。 I hope it's better now.. 我希望现在会更好。

I'd say it's repetitive if each Sensor (for example) has exactly the same properties (columns). 我要说的是,如果每个Sensor(例如)具有完全相同的属性(列),则是重复的。 If they differ at all, they should have different tables. 如果它们完全不同,则它们应该具有不同的表。

I'd try capturing this using NORMA or similar too to validate the design. 我也尝试使用NORMA或类似工具捕获此内容以验证设计。

I wouldn't worry too much about too many Zero-To-One relationships. 我不会太担心太多的“一对一”关系。 I do worry about having Zero-To-One relationships in the first place. 我不担心零到一的摆在首位的关系。 In the long run, these turn out to be One-to-Many relationships. 从长远来看,这些关系是一对多的关系。

For me, there is nothing more confusing than that mess of an ER diagram that comes standard in many tools. 对我来说,没有什么比许多工具中标准的ER图混乱更令人困惑了。 I use an technique called "Levelized Conceptual Diagrams" to help make sense of my relationships. 我使用一种称为“层次化概念图”的技术来帮助理解我的关系。

You should make a Levelized Diagram - it will make your life really easy when it comes to database designs. 您应该制作一个Levelized Diagram-在进行数据库设计时,它将使您的生活变得非常轻松。

Hopefully I will make this easy - the One side is on the top, the many side on the bottom. 希望我能使这变得容易-一侧在顶部,许多在底部。 Many to many get broken down to two One-to-Many tables. 多对多分解为两个一对多表。

Repeat until done. 重复直到完成。

Link to full image: http://i.stack.imgur.com/VKAGZ.jpg 链接到完整图像: http : //i.stack.imgur.com/VKAGZ.jpg

替代文字

Why do you need tables with only PK fields? 为什么需要仅包含PK字段的表? Can't you relate their children directly to their parents? 您不能将他们的孩子直接与父母联系吗?

Also consider creating a limit table with two fields and a contextID key field, so instead of 2 tables with 4 limit fields each (2 upper and 2 lower limit fields,) have a new table with 3 fields and change your existing tables have 2 fields with FK relationships to the new one, with names like upperLimitContextID and lowerLimitContextID. 还可以考虑创建一个包含两个字段和一个contextID键字段的限制表,因此,要代替具有2个限制字段的2个表(每个限制字段2个上限和2个下限字段),要有一个包含3个字段的新表,并更改现有表中的2个字段与新的FK关系,名称如upperLimitContextID和LowerLimitContextID。

Also, since the action and end tables have the same PK, consider combining them. 另外,由于操作表和结束表具有相同的PK,请考虑将它们组合在一起。 Any time there are tables with the same PK they can be combined. 任何时候只要表具有相同的PK,就可以将它们组合在一起。

In Oracle, an ARC describes mutually exclusive relationships to other tables. 在Oracle中, ARC描述了与其他表的互斥关系。 This concept can be implemented in other databases using nullable FK columns. 可以使用可空FK列在其他数据库中实现此概念。

Depending on the functionality of the tables, you may choose to combine all *Readings or all *Alerts tables into a single table, having nullable foreign key columns to the respective Logger, AnalogSensor, and SwitchSensor tables. 根据表的功能,您可以选择将所有* Readings或所有* Alerts表组合到一个表中,这些表具有指向相应Logger,AnalogSensor和SwitchSensor表的可空外键列。

As a consequence, the *AlertEnd and *AlertAction tables can also be combined (probably adding a Type column if required). 因此,* AlertEnd和* AlertAction表也可以合并(如果需要,可以添加Type列)。

Optionally, add views for the three domains (Logger, AnalogSensor, and SwitchSensor) to simulate the original tables. (可选)添加三个域(Logger,AnalogSensor和SwitchSensor)的视图以模拟原始表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM