如何在 Nermalization 中跟踪更改并存储计算内容?

发布于 2024-12-12 03:37:28 字数 1068 浏览 1 评论 0原文

我正在尝试创建一个像这样的表:

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

它是 Nermalization 示例,其中我猜是很受欢迎的。

无论如何,我认为我应该在 MySQL 中为 from 挂起对 lives_with 表或 cat_name 表的更改设置依赖关系,然后在 untilfrom 列之间设置依赖关系。我想主人可能想来更新猫的信息,并覆盖“来自”列,所以我必须使用 PHP?有没有什么特殊的方法可以在覆盖上添加时间戳(例如,$date = date("Ymd H:i:s");)?如何在 MySQL 中设置依赖关系?

我还有一个列,可以通过将其他列添加在一起来生成。我想使用 cat 示例,它看起来像:

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

我应该通过 PHP 添加,然后通过查询输入值,还是应该使用 MySQL 来管理添加?我应该为此使用特殊引擎,例如 MemoryAll 吗?

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.

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. 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? Is there any special way I should do the time stamp on the override (for example, $date = date("Y-m-d H:i:s");)? How do I set up the dependency within 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? Should I use a special engine for this, like MemoryAll?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如何视而不见 2024-12-19 03:37:28

我在两个方面不同意正常化的例子。

  1. 最终没有猫实体。相反,存在一个关系 (cat_name_no, cat_name),在您的示例中,它的直接后果是您无法判断存在多少只名为 Lara 的猫。这是一个很容易避免的异常现象。
  2. 该表将两个关系“lives_with_owner”和“under_the_name”塞进一张表中。这不是一个好主意,特别是如果数据是暂时的,因为它会产生各种令人讨厌的异常。相反,您应该为每个数据使用一个表。

我将按如下方式设计此数据库:

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);

因此,您将拥有如下数据:

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

我将使用比年份更细粒度的日期类型(在归一化示例中,将 2002-2002 作为范围确实会导致混乱的查询语法),以便您可以提出诸如 select cat_id fromowner where '2000-06-02' between valid_from 和 valid_to 之类的查询。
至于如何在一般情况下处理时态数据的问题:有一本关于该主题的优秀书籍,“用 SQL 开发面向时间的数据库应用程序”,作者:Richard Snodgrass (由 Richard Snodgrass 分发的免费全文 PDF),我相信甚至可以合法下载为 pdf,Google 会帮助您与此。

你的另一个问题:你可以在外部的sql中处理combined_family_age,或者,如果经常需要该列,可以使用视图。不过,您不应该手动管理内容,而是让数据库为您计算。

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. 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. 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.
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.

Your other question: you can handle the combined_family_age either in sql externally, or, if that column is needed often, with a view. You shouldn't manage the content manually though, let the database calculate that for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文