我正在设计的数据库有一个employees
表;可以有多种类型的员工,其中一种是医务人员。该数据库还需要描述医务人员之间的多对多关系以及他们拥有的能力。
是否可以创建一个medical_employees
只有一id
列的表,其唯一目的是指定哪些员工是医生?该id
列具有引用该employees
表的外键约束。下面的代码应该让我的问题更清楚:
/* Defines a generic employee */
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL
);
/* Specifies which employees are medics */
CREATE TABLE medical_employees (
id INT NOT NULL,
FOREIGN KEY (id) references employees(id);
);
/* Specifies what competences a medic can have */
CREATE TABLE medical_competences (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL
);
/* A many-to-many relation between medical employees and
their competences. */
CREATE TABLE medical_employees_competences (
id INT PRIMARY KEY AUTO_INCREMENT,
medic_id INT NOT NULL,
competence_id INT NOT NULL,
FOREIGN KEY (medic_id) REFERENCES medical_employees(id),
FOREIGN KEY (competence_id) REFERENCES medical_competences(id)
);