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中注意到
emp_id is primary key 和对同一个表中的emp_id的superior_emp_id which is a foreign key引用
创建外键引用了同一张表中的主键,为什么这样的数据库设计对我有帮助?
发布于 2011-08-24 20:32:18
它应该表明一个雇员可以有一个上级
emp_id, fname, lname, superior_emp_id
----------------------
1, 'Big', 'Boss', null,
2, 'Viswanathan', 'Iyer', 1在这个例子中,Big boss是你的老板,他自己没有高于他的老板。
https://stackoverflow.com/questions/7175410
复制相似问题