在sql server express中使用键和约束
我有 2 个表:
**FamilyHeads**
HeadID | HeadName
**FamilyMembers**
MemberID | MemberName
现在,我希望如果存在族长,则只有家庭成员可以插入 FamilyMembers 表中。为此,我是否应该将 HeadID 列也包含在 FamilyMembers 表中并从 FamilyHeads 表中引用它。或者这个问题的解决方案是什么。我知道我必须使用主键和外键,但不确定实施情况。请帮忙看一下代码。
I have 2 tables :
**FamilyHeads**
HeadID | HeadName
**FamilyMembers**
MemberID | MemberName
Now, i want that if a family head exists , then only family members could be inserted in the FamilyMembers table. For this, should i include the HeadID column also in my FamilyMembers table and reference it from the FamilyHeads table. or what's the solution for this. I know that i have to use Primary and Foreign keys but not sure with the implementation. Please help with the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加主键
添加外键列
添加 FK
您是否可以有重复的 HeadName?如果否
如果是,您如何知道将哪些成员添加到哪个头?
并在 HeadID 上为 FamilyMembers 添加非唯一索引
Add primary keys
Add columns for foreign key
Add FK
Can you have duplicate HeadNames? if no
If yes, how will you know which members to add to which head?
And add an non-unique index to FamilyMembers on HeadID