错误1072 MySQL

时间:2017-03-25 21:32:41

标签: mysql sql

在尝试添加外键属性并将我的属性链接到另一个表时,我继续收到错误1072。我已经尝试过不同的写作方式,但我继续收到消息:" department_id并不存在于表格中#34;。如果你能给我一手会很棒!非常感谢你!

这是我的代码:

Alter table employee
add constraint department_fk
foreign key ( department_id)
references department(department_id);

以下是我的其余表格:

Create table Department (
    department_id integer auto_increment primary key not null,
    department_name varchar (50) not null,
    Office_number varchar(50) not null,
    phone char (20) not null
);


Create table employee (
    employee_id integer auto_increment primary key not null,
    first_name varchar (25) not null,
    last_name varchar (25) not null,
    phone char(20) not null,
    email varchar (100) not null,
    salary decimal (10,2)
);

Create table project (
    project_id integer auto_increment primary key not null,
    max_hours time not null,
    start_date datetime not null,
    end_date datetime not null,
    Project_lead integer,
    Constraint project_fk1 
    foreign key (employee_id)
    references employee(employee_id),
    Department_id integer,
    Constraint project_fk2 
    foreign key (department_id) 
    references department (department_id)  
);

Create table assignment (
    employee_id integer not null,
    project_id integer not null,
    hours_worked time not null,
    primary key (employee_id, project_id),
    constraint assignment_fk1 
    foreign key(employee_id) 
    references employee(employee_id),
    constraint assignment_fk2 
    foreign key (project_id) 
    references project(project_id)
);

Alter table project
drop foreign key department_id;

提前谢谢!

2 个答案:

答案 0 :(得分:2)

您的员工表中需要一个外键列:

Create table employee (
   employee_id integer auto_increment primary key not null,
   first_name varchar (25) not null,
   last_name varchar (25) not null,
   phone char(20) not null,
   email varchar (100) not null,

   dept_id integer,

   salary decimal (10,2)
);

Alter table employee
add constraint department_fk
foreign key ( dept_id)
references department(department_id);

答案 1 :(得分:0)

信任该消息;) - 您的员工表中不存在“department_id”列!