在拟议的ER图中消除关系之间的循环

发布于 2025-01-26 14:54:45 字数 693 浏览 2 评论 0原文

在为简单数据库制作ER模式时,我遇到了以下问题:

  • 我在图中获得了一个周期,我不知道它是多余的还是可以以某种方式消除它。

我大规模提出了问题:

  1. 访问实体记录乘车访问伦敦。该实体包含有关其到达,出发和总访问时间的信息。
  2. 车辆实体包含有关车辆原产地,二氧化碳排放及其数字板的信息。
  3. 该实体日期包含与其相关的每周日期的每个日期以及添加任何区域的假期名称的信息。

实体日期的区域与车辆区域匹配。实体的entry_date/end_date 访问匹配到实体date的日期。最后,将实体车辆的数字平板匹配到实体的编号plate访问。这样,我开始提到的周期出现了。

ER图如下:

”问题的图表

如果我尚未解释的问题有任何疑问,请随时问我。我欢迎提出改进ER图的建议,以删除周期,或者只是将其保留,就像您认为是正确的那样。

In making an ER schema for a simple database, I have encountered the following problem:

  • I get a cycle in the diagram, which I don't know if it is redundant or I could eliminate it somehow.

I present the problem on a large scale:

  1. The visit entity records visits to London by a vehicle. This entity contains information on their arrival, departure and total visit time.
  2. The vehicle entity contains information on the vehicle's place of origin, CO2 emissions and its number plate.
  3. The entity date contains information for each date of the day of the week to which it corresponds and the name of the holiday for any region added.

The region of the entity Date is matched to the Vehicle region. The entry_date/end_date of the entity Visit is matched to the date of the entity Date. Finally, the number plate of the entity Vehicle is matched to the number plate of the entity Visit. In this way, the cycle that I mentioned at the beginning appears.

The ER diagram is as follows:

ER diagram of the problem

If there are any questions about the problem that I have not explained, please do not hesitate to ask me. I welcome suggestions for improving the ER diagram, either to remove the cycle or to simply keep it as it is if you think it is correct.

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

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

发布评论

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

评论(2

舟遥客 2025-02-02 14:54:45

我的两分钱 -

“日期”确实不是实体或表格的好名字。
首先,传达您真正提及的内容太笼统了。
其次,这是大多数通用语言中的保留关键词。您只会给编程带来不必要的麻烦。

您使用“日期”获取假日名称(特定地区)和工作日,对吗?
我的建议是,您只需要在此表中节省假期,因为可以用最常见的编程语言来弄清楚工作日。

此“日期”表只是一个查找表,可以帮助您找出假期,您无需在“日期”和访问之间执行关系,

我还建议您添加区域表来强制执行一致的命名。

这是DB图,我重命名为“日期”到“假期”

这是SQL Server实现 -

create table region (
  region_code varchar(100) primary key
 ,region_name varchar(100)
)

create table holiday (
  holiday_date date not null
 ,region_code varchar(100) not null
 ,holiday_name varchar(100) not null
)

alter table holiday add primary key (holiday_date, region_code)
alter table holiday add foreign key (region_code) references region (region_code)

create table vehicle (
  number_plate varchar(100) primary key
 ,region_code varchar(100) not null
 ,CO2_emission varchar(100)
)

alter table vehicle add foreign key (region_code) references region (region_code)

create table visit (
  number_plate varchar(100) not null
 ,entry_date date not null
 ,end_date date
)

alter table visit add primary key (number_plate, entry_date)
alter table visit add foreign key (number_plate) references vehicle (number_plate)

My two cents -

"Date" is really not a good name for entity or table.
First, it is too general to convey what you really refer to.
Second, it is a reserved key word in most common languages. You just cause unnecessary trouble for programming.

You use "Date" to get holiday name (for particular region) and week day, right?
My suggestion is that you only need to save holidays in this table because weekday can be figured out in most common programming language.

This "Date" table is just a lookup table to help you find out holiday, you do not need to enforce relation between "Date" and Visit

I'd also suggest you add Region table to enforce consistent naming.

Here is the DB diagram, I renamed "date" to "holiday"
enter image description here

Here is the SQL server implementation -

create table region (
  region_code varchar(100) primary key
 ,region_name varchar(100)
)

create table holiday (
  holiday_date date not null
 ,region_code varchar(100) not null
 ,holiday_name varchar(100) not null
)

alter table holiday add primary key (holiday_date, region_code)
alter table holiday add foreign key (region_code) references region (region_code)

create table vehicle (
  number_plate varchar(100) primary key
 ,region_code varchar(100) not null
 ,CO2_emission varchar(100)
)

alter table vehicle add foreign key (region_code) references region (region_code)

create table visit (
  number_plate varchar(100) not null
 ,entry_date date not null
 ,end_date date
)

alter table visit add primary key (number_plate, entry_date)
alter table visit add foreign key (number_plate) references vehicle (number_plate)
枫以 2025-02-02 14:54:45

车辆日期实体之间的关系是多余的。您仍然可以找到每次访问的日期,从现有关系中完成。如果将ER图转换为DB表,则将更加清楚。

为什么 entry_date exit_date 访问实体的两个属性?这些与日期实体的多对2关系已经考虑了这些。从访问中删除这两个属性。最后,向访问实体添加唯一的ID。

The Relate relationship between Vehicle and Date entity is redundant here. You can still find the dates for each visit a vehicle completed from the existing relationships. If you convert the ER diagram to DB tables, it'll be more clear.

Why are entry_date and exit_date two attributes of the Visit entity? These are already considered by the many-to-2 relationship with the Date entity. Remove these two attributes along with number_plate from Visit. Lastly, add an unique id to the Visit entity.

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