将外键添加到表中

时间:2016-10-03 17:39:58

标签: mysql sql god

我有一段时间没有练过SQL,我忘了如何在表格中添加外键

    mysql> alter table students 
add foreign key fk_unit(unitid) 
references unit(unitid) 
on delete no action 
on update cascade;
    ERROR 1072 (42000): Key column 'unitid' doesn't exist in table

我想知道为什么会这样?我的单位表有一个叫做unitid的主键,为什么会一直发生?

3 个答案:

答案 0 :(得分:0)

试试这个它应该有效....

ALTER TABLE students 
ADD CONSTRAINT FK_UnitId FOREIGN KEY (unitid)
    REFERENCES  unit(unitid);

答案 1 :(得分:0)

试试这个

ALTER TABLE Students
ADD FOREIGN KEY (unitid)
REFERENCES unit(unitid)

答案 2 :(得分:0)

您的查询是正确的。看起来像field' unitid'学生们没有找到他们的作品。表或它有不同的名称。