ms-access - 无法添加或更改记录,因为表 'COMPANY' 中需要相关记录

标签 ms-access database-design relational-database ms-access-2013 erd

我认为我的联系人列表数据库设计是正确的,但存在错误。 Access 2013 告诉我:

You cannot add or change a record because a related record is required in table 'COMPANY'

我是 Access 新手,这是一门名为“计算机概念 II”的类(class)。我已经完成了我的任务,并严格按照指示进行。该类(class)仅在线进行,我以3种不同的方式尝试了3次,但未能从讲师那里得到任何反馈。

这是我尝试过的事情的分割:

业务规则

  1. 一个人只能有一个家庭,但一个家庭可以有很多人。
  2. 一个人可以为一家或零家公司工作。
  3. 一家公司可以有零名或多名员工。
  4. 对于 MS Access 而言,PERSONstrong 表。

我的 ERD 图

Eric Hepperle's ERD for Contact List Database in Access 2013

构建表格

  1. 构建了 PERSON 表。
  2. 构建了 COMPANY 表。
  3. 构建了 FAMILY 表。

建立关系

  1. 创建了表之间的关系(请参阅所附的屏幕截图)。为了实现可选性,将 PERSON 设置为“强”表。
  2. FAMILY 表中最初包含 ID-FAM 和 LNAME 字段,这两个字段都是必填字段并已编入索引。
  3. PERSON 表中最初有 ID-PER、ID-FAM_FK 和 ID-COM_FK 作为索引。
  4. 最初将 ID-COM 作为 COMPANY 表中唯一的索引字段和必填字段。

在父表中输入数据时遇到“相关记录”错误

    • 如《使用 Access 2010》文档中所述,我首先在父表 (PERSON) 中输入数据。但是,当我填写完第一行中的所有字段并按“向下箭头”键前进到下一行时,我收到一条错误消息:
"You cannot add or change a record because a related record is required in table 'FAMILY'"

嗯,公司条目完全是可选的,并且不是必需的,所以这有点令人困惑。我不确定我做错了什么或从这里该去哪里。在这个类之前我不知道如何使用 Access,但我想我现在已经很好地掌握了它,并且我严格按照说明进行操作。因此,我认为问题是某个地方的配置错误。

最佳答案

解决了!

在看到Sergey S的评论之前,我自己发现了这一点,但正如他指出的那样,“默认”属性字段中不应该有任何值。在找到解决方案后,我将以下内容发布到类(class)讨论区:

It turns out that Access was automatically setting a default value for my foreign keys [FK] of zero ("0"). I didn't realize it, but what was happening was even though a person wasn't required to have a company, because "0" was in the company field for my person record that was what was causing the error. The reason? I suspect that the database was looking for a company record with an ID of "0". Since my company records start at 2 the "0" record was never found, causing the error.

SOLUTION

The way I solved this was simply to set go into the default field for my 2 foreign keys in the design view, delete the zeros, and make sure there was no default values. Or, another way to put it is: ensure there are no default values for your foreign keys.

I was able to add 3-4 rows of data for each table and I even ran a basic query to test the database. It seems to be working perfectly.

感谢所有贡献者!

关于ms-access - 无法添加或更改记录,因为表 'COMPANY' 中需要相关记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38425633/

相关文章:

mysql - 如何使用 Ruby on Rails 从 MySQL 关系数据库表中获取多个值?

ms-access - 在Access中,通过主表向列表添加值

delphi - 在 Delphi 中获取记录数据到类中

mysql - 如何在Access表单中以多对多关系填充第三个表? (MySQL后端)

mysql - 对值表中的序列建模的好方法是什么?

MySQL设计一对多?

oracle - ORDER BY 子查询和 ROWNUM 违背关系哲学?

database - SailsJS 的外键约束

ms-access - 通过Access绑定(bind)Word文档需要什么引用资料?

sql - 根据日期自动在 MySQL 服务器上创建表?