[英]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: 数据库模型应该允许我对数据库数据执行以下操作:
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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.