简体   繁体   English

如何将数据从一个表动态插入到另一个表?

[英]How should I insert data from one table to another dynamically?

I have 2 tables, videos and video_views . 我有2个表格, videosvideo_views I have a column in videos which must have the values from video_views ie for all the views of a particular video in video_views it must store it in to videos . 我有一列中videos必须具有值video_views即在特定视频的所有意见video_views它必须将其存储到videos This should happen dynamically whenever any update happens in video_views . 每当video_views发生任何更新时,这应该动态发生。 I tried making stored procedures but no luck. 我尝试制作存储过程,但是没有运气。

How should I do this? 我应该怎么做?

If you have just update operation, use 如果您只有更新操作,请使用

 ON UPDATE CASCADE 

for the foreign keys so whenever there is any update in video_views, that would automatically get updated in the VIDEO table.. 外键,因此只要video_views中有任何更新,该更新就会在VIDEO表中自动更新。

By the way, a good approach would be to use the foreign key of video_views table so that you don't need to update records in child table and you won't have any redundant data in the tables. 顺便说一句,一种好的方法是使用video_views表的外键,这样您就不需要更新子表中的记录,并且在表中不会有任何多余的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM