外键错误
我一直在更改所有表来定义 FK,但在尝试使用此表 StudentRsp 时出现错误。
ALTER TABLE StudentRsp
add CONSTRAINT fk_rspDate
FOREIGN KEY (rspDate)
REFERENCES LecturerRsp(rspDate);
我收到错误
错误消息: 中没有主键或候选键 与引用列列表匹配的引用表“LecturerRsp” 在外键“fk_rspDate”中。无法创建约束。看 以前的错误。
rspDate 被定义为 LecturerRsp 中的主键
I have been altering all my tables to define the FK and am getting an error when attempting this table studentRsp .
ALTER TABLE StudentRsp
add CONSTRAINT fk_rspDate
FOREIGN KEY (rspDate)
REFERENCES LecturerRsp(rspDate);
Am getting the error
Error Message: There are no primary or candidate keys in the
referenced table 'LecturerRsp' that match the referencing column list
in the foreign key 'fk_rspDate'. Could not create constraint. see
previous errors.
The rspDate is defined as a primary key in LecturerRsp
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查找 StudentRsp 中包含 LecturerRsp 表中不存在的字段的行。换句话说,约束要求外键列中的所有字段都与主键列中的字段匹配,并且 StudentRsp 中有一个在 LecturerRsp
示例中不存在的键:
Look for rows in your StudentRsp that have a field that doesn't exist in your LecturerRsp table. In other words the constraint requires all fields in the foriegn key column to match a field in the primary key column and there is a key in the StudentRsp that doesn't exist in the LecturerRsp
Example:
错误消息实际上非常清楚 - 只需阅读它......
您想要引用表
LecturerRsp
及其列rspDate
- 但此消息清楚地告诉您:rspDate
不是该表上的主键,也不是唯一索引/约束的一部分。这两个中的任何一个都是您能够从外键引用该列的要求。
要解决此问题,请执行以下操作:
LecturerRsp
上的主键更改为rspDate
列,如果您无法执行这两项操作)事物,那么您无法从外键引用该列。
The error message is pretty clear acutally - just read it...
You want to reference your table
LecturerRsp
and its columnrspDate
- but this message clearly tells you:rspDate
is not the primary key on that table, nor is it part of a unique index/constraint.Either of those two is a requirement for your to be able to reference that column from a foreign key.
To fix this:
LecturerRsp
to be on columnrspDate
If you cannot do either of these two things, then you cannot reference that column from a foreign key.