mysql - sql - Is it possible to set a column value as per the value in another column? -
let's i'm making 'students' database , there's table called 'exams' in it. have following 2 columns- 'marks' , 'grade' in , want 'grade' column automatically receive value based on value entered in 'marks'. 'a' 'marks'>=90, 'b' 'marks'>=80 , on. there way that?
not easily. in mysql, can update:
update students set grade = (case when marks >= 90 'a' when marks >= 80 'b' . . . end);
but out-of-date next insert
or update
statement.
in mysql 5.7.6, can have column generated automatically:
alter table students add grade char(1) (case when marks >= 90 'a' when marks >= 80 'b' . . . end);
this convenient, because column calculated @ query time automatically up-to-date.
in earlier versions, can similar view:
create view v_students select s.*, (case when marks >= 90 'a' when marks >= 80 'b' . . . end) grade students;
this generated column, requires query view.
and finally, can store value, using triggers maintain when marks
changes.
Comments
Post a Comment