首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >理解为什么外键引用同一个表中的主键?

问理解为什么外键引用同一个表中的主键?
EN

Stack Overflow用户
提问于 2011-08-24 20:28:41
回答 1查看 3.3K关注 0票数 3
代码语言:javascript
复制
create table employee
 (emp_id smallint unsigned not null auto_increment,
  fname varchar(20) not null,
  lname varchar(20) not null,
  start_date date not null,
  end_date date,
  superior_emp_id smallint unsigned,
  dept_id smallint unsigned,
  title varchar(20),
  assigned_branch_id smallint unsigned,
  constraint fk_e_emp_id 
    foreign key (superior_emp_id) references employee (emp_id),
  constraint fk_dept_id
    foreign key (dept_id) references department (dept_id),
  constraint fk_e_branch_id
    foreign key (assigned_branch_id) references branch (branch_id),
  constraint pk_employee primary key (emp_id)
 );

我正在研究这个Example,我在employee table中注意到

代码语言:javascript
复制
emp_id is primary key 

和对同一个表中的emp_id的superior_emp_id which is a foreign key引用

创建外键引用了同一张表中的主键,为什么这样的数据库设计对我有帮助?

EN

回答 1

Stack Overflow用户

发布于 2011-08-24 20:32:18

它应该表明一个雇员可以有一个上级

代码语言:javascript
复制
emp_id, fname, lname, superior_emp_id
----------------------
1, 'Big', 'Boss', null,
2, 'Viswanathan', 'Iyer', 1

在这个例子中,Big boss是你的老板,他自己没有高于他的老板。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7175410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档