2012-02-29 57 views
0

我在我的数据库中设置了2个表格 - fitness_report和result。SQL触发器插入到一个表中,多次复制到另一个表中

fitness_report具有以下各列(按顺序):

report_id, test_period, test_date, student_id 

结果具有以下各列(按顺序):

test_id, student_id, report_id, score 

我需要发生的是在创建一个新行时什么在表fitness_report上, 条目对结果表进行如下,其中student_id和report_id从fitness_report上的新行复制而来:

1, student_id, report_id, null 
2, student_id, report_id, null 
3, student_id, report_id, null 
4, student_id, report_id, null 
5, student_id, report_id, null 
6, student_id, report_id, null 

你能否建议最好的方法去做这件事。

干杯

+0

你的DB是mysql? – 2012-02-29 08:22:22

+0

@ThitLwinOo是的,DB是mySQL – user1201502 2012-02-29 08:36:18

回答

1

您可以在INSERT事件创建触发器。检查this

CREATE TRIGGER myTrigger AFTER INSERT ON fitness_report 
    FOR EACH ROW BEGIN 
    INSERT INTO results SET student_id = NEW.student_id, report_id=NEW.report_id; 
    END; 
+0

这会将student_id和report_id插入到结果表中,但是如何插入静态数据呢? (test_id和分数)? – user1201502 2012-02-29 08:29:49

+0

将这项工作: CREATE TRIGGER newReport INSERT ON fitness_report FOR EACH ROW之后开始 \t INSERT INTO结果(1,NEW.student_id,NEW.report_id,NULL); INSERT INTO results(2,NEW.student_id,NEW.report_id,null); INSERT INTO results(3,NEW.student_id,NEW.report_id,null); INSERT INTO results(4,NEW.student_id,NEW.report_id,null); \t INSERT INTO results(4,NEW.student_id,NEW.report_id,null); INSERT INTO results(5,NEW.student_id,NEW.report_id,null); END; – user1201502 2012-02-29 08:53:48

+0

尝试'INSERT INTO结果SET student_id = NEW.student_id,report_id = NEW.report_id,score = null;'。你可以重复INSERT语句来插入多条记录。 – 2012-02-29 09:12:14

相关问题