Reputation: 1
I have a databases table 'stu_inf'o with columns 'stu_id (primary key)', 'name (varchar)' 'grade_id (foreign key)' and 'age (int)'.
The grade_level table has the following columns grade_level_id (primary key) and grade_level (varchar).
I have assigned a grade_id to all students. At the end of the year the grade_level will have to change to the next grade_id. Eg all 1 will have to change to 2 and all 2 to 3 and so on.
Changing these manually in the database will be time consuming and prone to errors like grade_id skipping and not moving some students at all.
How do I achieve this automatically using php and mysql.
Upvotes: 0
Views: 118
Reputation: 2425
UPDATE stu_int SET grade_id = grade_id + 1 WHERE grade_id < x
(x is your maximum grade_level_id), this will allow you to update the table even if a student has reached the maximum grade level without causing a foreign key violation. You may also just delete the student who has reached the maximum grade level:
UPDATE stu_info SET grade_id = grade_id + 1
DELETE FROM stu_int WHERE grade_id = x
Upvotes: 1