我认为我的联系人列表数据库设计是正确的,但存在错误。 Access 2013 告诉我:
You cannot add or change a record because a related record is required in table 'COMPANY'
我是 Access 新手,这是一门名为“计算机概念 II”的类(class)。我已经完成了我的任务,并严格按照指示进行。该类(class)仅在线进行,我以3种不同的方式尝试了3次,但未能从讲师那里得到任何反馈。
这是我尝试过的事情的分割:
- 正如错误所示,我尝试通过关闭 PERSON 表,然后在 FAMILY 表中打开并输入数据来解决该问题。我在不同的州组成了3个家庭,我的真实地址和姓氏输入为第四个。已保存 FAMILY 表。
- 再次打开 PERSON 表并开始从左到右输入数据(屏幕截图)。使用向下箭头键或双击自动调整列宽时,我在开始时出现错误,引用了 COMPANY 表。
找到并阅读了以下看似相关的帖子,但它们都没有提供我正在寻找的帮助:
"You cannot add or change a record because a related record is required", but related record exists?
You cannot add or change a record because a related record is required in table
业务规则
- 一个人只能有一个家庭,但一个家庭可以有很多人。
- 一个人可以为一家或零家公司工作。
- 一家公司可以有零名或多名员工。
- 对于 MS Access 而言,PERSON 是strong 表。
我的 ERD 图
构建表格
- 构建了 PERSON 表。
- 构建了 COMPANY 表。
- 构建了 FAMILY 表。
建立关系
- 创建了表之间的关系(请参阅所附的屏幕截图)。为了实现可选性,将 PERSON 设置为“强”表。
- FAMILY 表中最初包含 ID-FAM 和 LNAME 字段,这两个字段都是必填字段并已编入索引。
- PERSON 表中最初有 ID-PER、ID-FAM_FK 和 ID-COM_FK 作为索引。
- 最初将 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/