0

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

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'); 

回答

0

发现,我不能一个外键添加到“价值”这就是我要怎样做。

相关问题