简体   繁体   English

数据库表架构和聚合根

[英]Database Table Schema and Aggregate Roots

Applicaiton is single user, 1-tier(1 pc), database SqlCE. 应用是单用户,1层(1 pc),数据库SqlCE。 DataService layer will be (I think) : Repository returning domain objects and quering database with LinqToSql (dbml). DataService层将是(我认为):存储库返回域对象并使用LinqToSql(dbml)查询数据库。 There are obviously a lot more columns, this is simplified view. 显然有很多列,这是简化视图。

LogTime in separate table: http://i53.tinypic.com/9h8cb4.png LogTime在单独的表中: http ://i53.tinypic.com/9h8cb4.png

LogTime in ItemTimeLog table (as Time): http://i51.tinypic.com/4dvv4.png ItemTimeLog表中的LogTime(作为时间): http ://i51.tinypic.com/4dvv4.png

alt text http://i53.tinypic.com/9h8cb4.png 替代文字http://i53.tinypic.com/9h8cb4.png

This is my first attempt of creating a >2 tables database. 这是我创建> 2表数据库的第一次尝试。 I think the table schema makes sense, but I need some reassurance or critics. 我认为表模式很有意义,但是我需要一些保证或批评。 Because the table relations looks quite scary to be honest. 说实话,因为表关系看起来很吓人。 I'm hoping you could; 我希望你能;

  • Look at the table schema and respond if there are clear signs of troubles or errors that you spot right away.. And if you have time, 查看表模式,如果有明显迹象表明您立即发现了问题或错误,请做出响应。如果有时间,

  • Look at Program Summary/Questions, and see if the table layout makes makes sense to those points. 查看“程序摘要/问题”,查看表格布局是否对这些要点有意义。

Please be brutal, I will try to defend :) 请野蛮,我会努力捍卫:)

Program summary: 计划摘要:

a) A set of categories, each having a set of strategies (1:m) a)一组类别,每个类别都有一组策略(1:m)

b) Each day a number of items will be produced. b)每天将生产许多物品。 And each strategy MAY reference it. 每个策略都可以引用它。 (So there can be 50 items, and a strategy may reference 23 of them) (因此,可以有50个项目,一个策略可以引用其中的23个项目)

c) An item can be referenced by more than one strategy. c)一个项目可以被多个策略引用。 So I think it's an m:m relation. 所以我认为这是一个m:m关系。

d) Status values will be logged at fixed time-fractions through the day, for: - .... each Strategy.....each StrategyItem....each item d)状态值将在一天中的固定时间段记录:-....每个策略.....每个StrategyItem ..每个项目

e) An action on an item may be executed by a strategy that reference it. e)对一个项目的动作可以由引用它的策略来执行。 - This is logged as ItemAction (Could have called it StrategyItemAction) -记录为ItemAction(可以将其称为StrategyItemAction)

User Requsts 用户权益

b) -> e) described the main activity mode of the program. b)-> e)描述了程序的主要活动模式 To work with only today's DayLog , for each category. 每个类别只能使用今天的DayLog 2nd priority activity is retrieval of history , which typically will be From all categories, from day x to day y; 第二优先活动是历史记录的检索 ,通常是从第x天到y天的所有类别; Get all StrategyDailyLog. 获取所有StrategyDailyLog。

Questions 问题

  1. First, does the overall layout look sound? 首先,整体布局看起来是否合理? I'm worried to see that there are so many relationships in all directions, connecting everything. 我很担心看到各个方向的联系如此之多,联系紧密。 Is this normal, or does it look like trouble? 这是正常现象还是看起来很麻烦?

  2. StrategyItem is made to represent an m:m relationship. 使StrategyItem表示m:m关系。 Is it correct as I noted 1:m / 1:1 (marked red) ? 如我指出的1:m / 1:1(标记为红色)是否正确?

  3. StrategyItemTimeLog and ItemTimeLog; StrategyItemTimeLog和ItemTimeLog; Logs values that both need to be retrieved together, when retreiving a StrategyItem. 检索StrategyItem时,记录需要一起检索的值。 Reason I separated is that the first one is strategy-specific, and several strategies can reference same item. 我分开的原因是,第一个是特定于策略的,有几个策略可以引用同一项目。 So I thought not to duplicate those values that are not dependent no strategy, but only on the item. 因此,我认为不要重复那些不依赖于任何策略而仅依赖于项目的值。 Hence I also dragged out the LogTime, as it seems to be the only parameter to unite the logs. 因此,我也拖出了LogTime,因为它似乎是统一日志的唯一参数。 But this all looks quite disturbing with those 3 tables. 但是,这三个表似乎都令人不安。 Does it make sense at all? 真的有道理吗? Or you have suggestion? 还是您有建议?

  4. Pink circles shows my vague attempt of Aggregate Root Paths. 粉色圆圈表示我模糊的“汇总根路径”尝试。 I've been thinking in terms of "what entity is responsible for delete". 我一直在思考“哪个实体负责删除”。 Though I'm unsure about the actual root. 虽然我不确定实际的根源。 I think it's Category. 我认为这是类别。 Does it make sense related to User Requests described above? 与上述用户请求相关吗?


EDIT1: (Updated schema, showing typical number of hierarchy items for the first few relations, for 365 days, and additional explanations) EDIT1 :( 更新的架构,显示前365天的最初几个关系的层次结构项目的典型数目,以及其他说明)

1:1 relation: Sorry. 1:1关系:对不起。 I made a mistake. 我犯了一个错误。 The StrategyDailyLog should be 1:m. StrategyDailyLog应该为1:m。 See updated schema. 查看更新的架构。 It is one per Strategy, per day. 每天每个策略一次。

DayLog / StrategyDailyLog: I've been pondering over wether DayLog shall be a part of the hierarchy like this or not. DayLog / StrategyDailyLog:我一直在思考是否DayLog应该成为这种层次结构的一部分。 The purpose of the DayLog table is to hold “sum values” derived from all the StrategyDailyLog tables for the same day. DayLog表的目的是保存从同一天的所有StrategyDailyLog表得出的“总和值”。 Like performance values for this day. 就像今天的效果值一样。 It also holds the date value. 它还保存日期值。 Which allows me to omit a date value in the StrategyDailyLog (Which I feel would kind of be a duplicate modeling of the date-field), but instead the reference to DayLog exist to “find” the date. 这使我可以省略StrategyDailyLog中的日期值(我觉得这有点像是对日期字段的重复建模),但是可以通过引用DayLog来“查找”日期。 I'm not sure if this is an abuse/misconception of normalization. 我不确定这是否是对标准化的滥用/误解。

Null value: I haden't thought about this. 空值:我没有考虑过。 I believe I found 2, as now marked in StrategyDailyLog and ItemAction. 我相信我发现了2,现在已在StrategyDailyLog和ItemAction中标记了该标记。 They can not be null on creation, but they can be set to null if one need to delete either a Strategy, or a StrategyItem. 它们在创建时不能为null,但是如果需要删除Strategy或StrategyItem,可以将它们设置为null。 That should not require a delete of the StrategyDailyLog and the ItemAction. 那不需要删除StrategyDailyLog和ItemAction。 Hence they can be set to null. 因此,可以将它们设置为null。

All Id –columns: My idea was to have ID (autogenerated Integer) as PK for all my tables. 所有Id列:我的想法是将所有表的ID(自动生成的Integer)作为PK。 I believed that also would be sufficient as candidate key. 我认为这也足以作为候选密钥。 Is this not a proper way to make PKs? 这不是制作PK的正确方法吗? It's the only way any table of mine can be identified. 这是可以识别我的任何表格的唯一方法。 I asked a question before if that was ok, maybe I misunderstood, but thought that was a good approach. 我问过一个问题,如果还可以的话,也许我误会了,但是认为这是一个好方法。

m:m relation: This is what I have attempted to do: StrategyItem is the m:m table of StrategyDailyLog / DailyItem. m:m关系:这是我尝试做的事情:StrategyItem是StrategyDailyLog / DailyItem的m:m表。

Ok. 好。 Here is me being brutal. 这是我的残酷行为。 I do not understand the model. 我不了解该模型。 So instead of trying to comment on that so much, here are some thoughts that came to my mind when I looked at it. 因此,当我看着它时,我想到的是一些想法,而不是试图对此发表评论。

I think you should have look at your 1:1 relationships (all of them). 我认为您应该查看您的1:1关系(所有关系)。 Why is DayLog and StrategyDailyLog separated in two tables? 为什么将DayLog和StrategyDailyLog分为两个表? Probably because you will always have at least one DayLog item but not all DayLog items have a StrategyDailyLog item. 可能是因为您将始终至少有一个DayLog项目,但并非所有DayLog项目都具有StrategyDailyLog项目。 If that is the case you can have a StrategyID FK in DayLog table with allow nulls option. 如果是这种情况,您可以在DayLog表中使用带有allow nulls选项的StrategyID FK。

It would help to understand the model if you could show which fields are required and which fields accept null as a value. 如果您可以显示哪些字段是必需的以及哪些字段接受null作为值,则有助于理解模型。

All your tables have its own id column. 您所有的表都有其自己的id列。 That can be quite confusing when doing 1:1 relations and m:m relations. 在进行1:1关系和m:m关系时,这可能会非常令人困惑。 For a 1:1 relation, usually the relation between the two tables is made on the primary key in both tables. 对于1:1关系,通常在两个表的主键上建立两个表之间的关系。 If you do not do that you have to create a candidate key on the foreign key column. 如果不这样做,则必须在外键列上创建候选键。 In your case that means that StrategyDailyLog should have a candidate key on DayLogID. 在您的情况下,这意味着StrategyDailyLog在DayLogID上应具有候选键。

A m:m relation between two tables is usually solved by adding a new table in between, with the primary keys from both tables. 通常通过在两个表之间添加新表以及两个表的主键来解决两个表之间的m:m关系。 Those fields together is the primary key for the table in the middle. 这些字段一起是中间表的主键。 Lets say for example that you should have am:m relationship between Category and Strategy. 例如,假设您在类别和策略之间应该具有am:m关系。 You should then create a table called CategoryStrategy with two fields CategoryID and StrategyID that together is the primary key for table CategoryStrategy. 然后,您应该创建一个名为CategoryStrategy的表,其中包含两个字段CategoryID和StrategyID,这两个字段一起是表CategoryStrategy的主键。

I hope my comments makes sense and that they are useful to you. 希望我的评论有意义,并且对您有用。

EDIT 2011-01-17 编辑2011-01-17

I do not think that you should have as a principle to use a IDENTITY column as primary key in all tables. 我认为您不应该原则上将IDENTITY列用作所有表中的主键。 A m:m relation does not need it so you should not do it. m:m关系不需要它,因此您不应该这样做。 I also think that you have misunderstood what I meant with a candidate key. 我还认为您误解了候选密钥的含义。 A candidate key is a key that could have been used as the primary key. 候选密钥是可以用作主密钥的密钥。 In MS SQL Server you define a UNIQUE CONSTRAINT for your candidate key. 在MS SQL Server中,为候选键定义一个UNIQUE CONSTRAINT。 Ex: Table StrategyItem have id as PK but the combination of StrategyID and DailyItemID is the candidate key. 例如:表StrategyItem的ID为PK,但是StrategyID和DailyItemID的组合是候选键。 Better would be to remove id and use StrategyID+DailyItemID as PK. 最好删除ID,然后将StrategyID + DailyItemID用作PK。

Below is the schema that I would have built with your description. 以下是我将根据您的描述构建的架构。 I might have missed something important because I do not know everything about what you want to do. 我可能错过了一些重要的事情,因为我不知道您想做什么。 You should not think so much about query performance and building aggregates when designing the schema. 设计架构时,您不应该过多考虑查询性能和构建聚合。 That can be handled by creating indexes on columns and using sum, count and group by in your queries. 这可以通过在列上创建索引并在查询中使用总和,计数和分组来解决。 An index on column Created in the model below would be necessary for your queries on a date or date interval. 对于在日期或日期间隔上进行的查询,以下模型中的Created列上的索引将是必需的。 In MS SQL Server there is something called the clustered index. 在MS SQL Server中,有一些称为聚集索引的东西。 Default the PK of a table is the clustered index but in this case I would make the index on Created column the clustered index. 默认情况下,表的PK是聚集索引,但是在这种情况下,我将使Created列上的索引成为聚集索引。

A Category has 0,1 or more Strategy. 类别具有0.1或更多的策略。 LogItem have on Category and optionally one Strategy LogItem.Created holds date and time. LogItem具有类别,还可以选择包含一个策略LogItem.Created保存日期和时间。

替代文字

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

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