简体   繁体   English

关系数据库结构设计建议

[英]Relational database structure design advice

This is a textual description of data for which I need to create a database design (using SQLite) for an application. 这是我需要为应用程序创建数据库设计(使用SQLite)的数据的文本描述。

The application needs to keep a record of operations . 应用程序需要保留操作记录。 Each operation has a Name and its list of parameters. 每个操作都有一个Name及其参数列表。 Each parameter has its Name and a Value. 每个参数都有其名称和值。 However, the values of the parameters will change over the lifetime of the app (in fact the user will be able to changes them using GUI) and we want to keep a history of the values which a certain parameter has had. 但是,参数的值将在应用程序的生命周期内发生变化(实际上用户将能够使用GUI更改它们),并且我们希望保留某个参数所具有的值的历史记录。 Furthermore, each operation can have multiple parameter sets . 此外,每个操作可以具有多个参数集 A parameter set is like an envelope which encompasses a set of parameter values (which all belong to the same operation) and gives this envelope a unique Number and a non-unique Description . 参数集就像一个包含一组参数值的包络(它们都属于同一个操作),并为该包络提供唯一的数字和非唯一的描述

This is what I have so-far: [Database model image][1] 这就是我迄今为止所拥有的:[数据库模型图像] [1]

The database model should allow me to perform these actions on the database data: 数据库模型应该允许我对数据库数据执行以下操作:

  1. Show a list of operations - I know how to do this. 显示操作列表 - 我知道如何执行此操作。
  2. Show a list of parameters for a given operation - I know how to do this. 显示给定操作的参数列表 - 我知道如何执行此操作。
  3. For a given operation, show all its parameters as columns and show the values of the parameters as rows - each row represents a different parameter value from the history of values. 对于给定的操作,将其所有参数显示为列,并将参数的值显示为行 - 每行表示与值历史记录不同的参数值。 I'm stuck at this one. 我被困在这一个。
  4. For a given operation, show a list of all parameter sets which belong to that operation. 对于给定的操作,显示属于该操作的所有参数集的列表。 I'm stuck at this one too. 我也被困在这一个。
  5. For a given operation and for a given parameter set, get the latest values of its parameters. 对于给定的操作和给定的参数集,获取其参数的最新值。 Stuck at this. 坚持这个。

I'm not sure if I should re-work my database model or if I should look for proper SQL statements to accomplish the tasks above with the model that I have. 我不确定我是否应该重新使用我的数据库模型,或者我是否应该寻找适当的SQL语句来完成上述任务,并使用我拥有的模型。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thank you. 谢谢。

EDIT 1 编辑1

I have re-worked my database model according to a helpful advice from @Marek Herman. 我根据@Marek Herman的有用建议重新设计了我的数据库模型 Thanks to that I am able to accomplish tasks 1) 2) 4). 由于我能够完成任务1)2)4)。 Now I'm trying to accomplish 5) which should not be that difficult with the current database model. 现在我正在尝试完成5)当前的数据库模型不应该那么困难。 I have this SQL statement: 我有这个SQL语句:

SELECT Parameter.ParameterIdentifier, ParameterValue.ParameterValue,
ParameterValueVersion.VersionNumber, ParameterValueVersion.ChangedOn
FROM ParameterValueVersion INNER JOIN
(((Operation INNER JOIN Parameter ON Operation.OperationPLC_ID = Parameter.OperationPLC_ID)
INNER JOIN ParameterSet ON Operation.OperationPLC_ID = ParameterSet.OperationPLC_ID)
INNER JOIN ParameterValue ON (ParameterSet.ID = ParameterValue.ParameterSetID) AND
(Parameter.ID = ParameterValue.ParameterID)) ON ParameterValueVersion.ID = ParameterValue.ParameterValueVersionID
WHERE (Operation.OperationPLC_ID=[opID] AND 
ParameterSet.ParameterSetNumber=[parSetNum]);

where [opID] and [parSetNum] are the input parameters. 其中[opID]和[parSetNum]是输入参数。 This SQL statement actually only joins all these tables together on their PK->FK relationship: Operation, Parameter, ParameterSet, ParameterValue, ParameterValueVersion and filters the rows by specified OperationPLC_ID and ParameterSetNumber. 此SQL语句实际上只在它们的PK-> FK关系上将所有这些表连接在一起:Operation,Parameter,ParameterSet,ParameterValue,ParameterValueVersion并按指定的OperationPLC_ID和ParameterSetNumber过滤行。

Here is an example of an output of this SQL statement. 以下是此SQL语句的输出示例 Each row shows a name of a parameter, its value, a version number of the value and date of change of that value. 每行显示参数的名称,其值,值的版本号和该值的更改日期。 Some parameters only have one value (only one version -eg, "OFFSET"). 某些参数只有一个值(只有一个版本-eg,“OFFSET”)。 Some parameters have two values. 一些参数有两个值。 For example "PREFILLING" has a value of "3" which was input on Oct 20, 2016 (and has a version number 1) and it also has a value of "3.5" which was input on Oct 21, 2016 and has a version number of 2. So I'd like to show only the latest versions of the values of the parameters. 例如,“PREFILLING”的值为“3”,于2016年10月20日输入(并且版本号为1),其值为“3.5”,于2016年10月21日输入并具有版本因此我只想显示参数值的最新版本。 Any advice how to modify the SQL statement is much appreciated. 任何建议如何修改SQL语句非常感谢。 Thank you. 谢谢。

EDIT 2 编辑2

I guess I figured out how to perform 5). 我想我想出了如何执行5)。 I had to study a bit how GROUP BY works. 我必须研究GROUP BY的工作原理。 This did the trick: 这样就可以了:

SELECT Parameter.ParameterIdentifier, last(ParameterValue.ParameterValue) AS ParameterValue, last(ParameterValueVersion.ChangedOn) AS ChangedOn, max(ParameterValueVersion.VersionNumber) AS VersionNumber
FROM ParameterValueVersion INNER JOIN
(((Operation INNER JOIN Parameter ON Operation.OperationPLC_ID = Parameter.OperationPLC_ID)
INNER JOIN ParameterSet ON Operation.OperationPLC_ID = ParameterSet.OperationPLC_ID)
INNER JOIN ParameterValue ON (ParameterSet.ID = ParameterValue.ParameterSetID) AND
(Parameter.ID = ParameterValue.ParameterID)) ON ParameterValueVersion.ID = ParameterValue.ParameterValueVersionID
WHERE (((Operation.OperationPLC_ID)=[opID]) AND ((ParameterSet.ParameterSetNumber)=[parSetNum]))
GROUP BY Parameter.ParameterIdentifier
ORDER BY Parameter.ParameterIdentifier

Now I still need to figure out how to perform task no. 现在我仍然需要弄清楚如何执行任务号。 3. I'm gonna study the suggested COALESCE function. 我要研究建议的COALESCE功能。 Thank you. 谢谢。

0) I would connect ParameterSet to Operation and Parameter and not to ParameterValue. 0)我将ParameterSet连接到Operation和Parameter而不是ParameterValue。

  • 1) okay! 1)好的!
  • 2) okay! 2)好的!
  • 3) I think you can use the COALESCE() function to display the columns and then it should be possible to show all parameters with matching OperationID 3)我认为您可以使用COALESCE()函数显示列,然后应该可以显示匹配的OperationID的所有参数
  • 4) you can do that if you do point #0 4)如果你指出#0,你可以这样做
  • 5) same as above I think 5)我认为与上述相同

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

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