无法使用外键约束将数据添加到数据库中

时间:2014-03-02 00:18:31

标签: mysql

参考我之前的问题

Unable to add foreign keys to mysql database

我想知道,由于外键约束已经存在,将新数据插入到该部分中的最佳方法是什么,接受和教授表格?我不想删除表并重建模式,因为需要重新输入太多数据。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您可以将值插入到表中,如

insert into table_name values("a", "b", "c");

这是insert语句的常用语法。来到你的桌子

插入其他表时,请确保您没有违反主键和外键约束。

例如:

Student:
ID    name    Dept_name    tot_credit
1     abc     EE            3
2     def     CS            3

Department:

Deptname    building    budget
EE           1            200
CS           2            299
IS           3            900

course:
courseid    title    dept_name    credits
44       Algorithms   CS           3
899       OS          CS           3

现在,如果您尝试插入部分,它应该是这样的:

insert into section values(44, 199, 5, 2000, 500, 50, "Whatever format");

因为这仅指课程表

好的,试试这个:

insert into teaches values(instructor_id(existing in instructor table), course_id(match section table), secid(from section table), semester(from section table), year(from section table));
祝你好运。

相关问题