Skip to content
Advertisement

How to do update/migration of data, with help of generic query

I am in middle of trying to get better with sql, but I have some issue/question with one of my exercies. So I thought about this kind of following situation. I have three tables:

STUDENT

COURSE

STUDENT_COURSE

But after some time I have requirments to divide courses into more specific names.

So in COURSE table whould looks like that

And know I am in point when I am stuck. Because I need to update STUDENT_COURSE too. Obviously I can do it with hardcoded values, but in case of table where I have thousends of entries it would take a lot of time. So I was thinking about more generic approach. Student does containt column where LEVEL is stored, and it is corresponding with suffix of course name. So I would like to use it.

Any idea how could I do this kind of migration(I suppose calling it migration is too much)

Advertisement

Answer

I presume there are parent – child relations between tables, so first you need to backup your course and student_course tables.

Then you can truncate student_course and course tables respectively.

With cross product create every single possible courses.

And finally you can update your student_course table.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement