本文介绍了将值插入外键表和主键表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有两个sql表,我必须将值插入这两个表中,以便主表中的一条记录与外键表中的几条记录相关联.请帮助我.上帝保佑您

Hi,
i have two sql tables and i have to insert values into these two tables such that a record in primary table is associated with several records in foreign key table.Pls help me.god bless you

推荐答案

INSERT INTO TAB_STUDENT(name_student, id_teacher_fk)
SELECT 'Joe The Student', id_teacher
  FROM TAB_TEACHER
 WHERE name_teacher = 'Professor Jack'
 LIMIT 1


INSERT INTO teacher (teacherid, teachername) VALUES (''james123'',''James'')


插入老师后,您是否可以插入老师为"james123"的新学生,例如


After inserting the teacher then can you insert new students whose teacher is ''james123'', e.g.

INSERT INTO student (studentid, studentname, teacherid) VALUES (''student1'',''name of student1'',''james123'')


对于块插入,应遵循Christian Graus的解决方案.


For block insert, you should follow the solution by Christian Graus.



这篇关于将值插入外键表和主键表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 21:10