简体   繁体   English

如何在“标准化”中跟踪更改并存储计算的内容?

[英]How do I track changes and store calculated content in Nermalization?

I'm trying to create a table like this: 我正在尝试创建一个像这样的表:

lives_with_owner_no from    until   under_the_name
1                   1998    2002    1
3                   2002    NULL    1
2                   1997    NULL    2
3                   1850    NULL    3
3                   1999    NULL    4
2                   2002    2002    4
3                   2002    NULL    5

It's the Nermalization example , which I guess is pretty popular. 这是Nermalization的示例 ,我猜它很受欢迎。

Anyway, I think I am just supposed to set up a dependency within MySQL for the from pending a change to the lives_with table or the cat_name table, and then set up a dependency between the until and from column. 无论如何,我想我只是应该在MySQL中为from lives_with表或cat_name表的更改挂起建立依赖关系,然后在untilfrom列之间建立依赖关系。 I figure the owner might want to come and update the cat's info, though, and override the 'from' column, so I have to use PHP? 我认为所有者可能想来更新猫的信息,并覆盖“发件人”列,所以我必须使用PHP? Is there any special way I should do the time stamp on the override (for example, $date = date("Ymd H:i:s"); )? 有什么特殊的方法可以对覆盖进行时间戳记(例如, $date = date("Ymd H:i:s"); )? How do I set up the dependency within MySQL? 如何在MySQL中设置依赖关系?

I also have a column that can be generated by adding other columns together. 我也有一个可以通过将其他列加在一起生成的列。 I guess using the cat example, it would look like: 我想用猫的例子,它看起来像:

combined_family_age family_name
75                  Alley
230                 Koneko
132                 Furrdenand
1,004               Whiskers

Should I add via PHP and then input the values with a query, or should I use MySQL to manage the addition? 我应该通过PHP添加,然后通过查询输入值,还是应该使用MySQL管理添加? Should I use a special engine for this, like MemoryAll? 我是否应该为此使用特殊的引擎,例如MemoryAll?

I disagree with the nermalization example on two counts. 我从两个方面不同意这个神经化的例子。

  1. There is no cat entity in the end. 最终没有猫的实体。 Instead, there is a relation (cat_name_no, cat_name), which in your example has the immediate consequence that you can't tell how many cats named Lara exist. 相反,存在一个关系(cat_name_no,cat_name),在您的示例中,它具有直接的后果,即您无法知道存在多少只名为Lara的猫。 This is an anomaly that can easily be avoided. 这是很容易避免的异常。
  2. The table crams two relations, lives_with_owner and under_the_name into one table. 该表将两个关系(lives_with_owner和under_the_name)填充到一个表中。 That's not a good idea, especially if the data is temporal, as it creates all kinds of nasty anomalies. 这不是一个好主意,尤其是在数据是临时数据的情况下,因为它会产生各种令人讨厌的异常情况。 Instead, you should use a table for each. 相反,您应该为每个表使用一个表。

I would design this database as follows: 我将如下设计该数据库:

create table owner (id integer not null primary key, name varchar(255));
create table cat (id integer not null primary key, current_name varchar(255));
create table cat_lives_with (
  cat_id integer references cat(id),
  owner_id integer references owner(id),
  valid_from date,
  valid_to date);
create table cat_has_name (
  cat_id integer references cat(id),
  name varchar(255),
  valid_from date,
  valid_to date);

So you would have data like: 因此,您将获得如下数据:

id | name 
 1 | Andrea
 2 | Sarah
 3 | Louise

id | current_name
 1 | Ada
 2 | Shelley

cat_id | owner_id | valid_from | valid_to
     1 |        1 | 1998-02-15 | 2002-08-11
     1 |        3 | 2002-08-12 | 9999-12-31
     2 |        2 | 2002-01-08 | 2001-10-23
     2 |        3 | 2002-10-24 | 9999-12-31

cat_id | name     | valid_from | valid_to
     1 | Ada      | 1998-02-15 | 9999-12-31
     2 | Shelley  | 2002-01-08 | 2001-10-23
     2 | Callisto | 2002-10-24 | 9999-12-31

I would use a finer grained date type than just year (in the nermalization example having 2002-2002 as a range can really lead to messy query syntax), so that you can ask queries like select cat_id from owner where '2000-06-02' between valid_from and valid_to . 我将使用比年份更精细的日期类型(在具有2002-2002作为范围的精确化示例中,实际上可能导致凌乱的查询语法),因此您可以select cat_id from owner where '2000-06-02' between valid_from and valid_to查询诸如select cat_id from owner where '2000-06-02' between valid_from and valid_to类的查询select cat_id from owner where '2000-06-02' between valid_from and valid_to As for the question of how to deal with temporal data in the general case: there's an excellent book on the subject, "Developing Time-Oriented Database Applications in SQL" by Richard Snodgrass ( free full-text PDF distributed by Richard Snodgrass ), which i believe can even be legally downloaded as pdf, Google will help you with that. 至于在一般情况下如何处理时态数据的问题:Richard Snodgrass撰写了一本非常出色的书,主题是“在SQL中开发面向时间的数据库应用程序”(Richard Snodgrass 发行的免费全文PDF ),我相信甚至可以合法地以pdf格式下载,Google会为您提供帮助。

Your other question: you can handle the combined_family_age either in sql externally, or, if that column is needed often, with a view. 您的另一个问题:您可以在sql的外部或如果需要经常使用该列的情况下,在sql中使用视图来处理Combined_family_age。 You shouldn't manage the content manually though, let the database calculate that for you. 但是,您不应该手动管理内容,而是让数据库为您计算内容。

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

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