0

如果员工在部门“红色”中,我想创建一个子类别。由于我为此使用表而不是枚举,因此我必须手动插入静态部门字段值。如何使部门 red 成为引用表 red_object 的外键?或者有没有更好的设计来拥有这个子类别?

http://sqlfiddle.com/#!2/83a2a

create table department(
  deptID int auto_increment primary key,
  deptName varchar(30)
  ) ENGINE = INNODB;

create table red_object(
  objectID int auto_increment primary key,
  objectName varchar(30)
  ) ENGINE = INNODB;

create table employees(
  userId int auto_increment not null primary key,
  firstName varchar(20) not null,
  lastName varchar(20) not null,
  phone tinyint unsigned,
  fax tinyint unsigned,
  position varchar(30),
  jpeg char(50),
  deptID int,
  unique(firstName, LastName, jpeg),
  foreign key (deptID) references department(deptID)
  ) ENGINE = INNODB;

insert into department (deptName) 
  values ('red'), ('white'), ('blue'), ('black');

insert into red_object (objectName) 
  values ('red_square'), ('red_circle'), ('red_octagon');

insert into employees (firstName, lastName) values ('jane' , 'doe');

insert into employees (firstName, lastname, deptID) values ('john', 'Doe', '3');
4

1 回答 1

0

发现我无法将外键添加到我正在尝试做的“值”中。

于 2012-12-13T06:12:43.967 回答