Calculating age from birthday with oracle plsql trigger and insert the age in table

前端 未结 3 1623
小蘑菇
小蘑菇 2020-12-21 06:34

i have a table

dates
(dob date,
age number(4)
);

I will insert a date of birth and a trigger will calculate the age and insert that age in

3条回答
  •  囚心锁ツ
    2020-12-21 06:54

    If you do it by a trigger the age is potentially wrong after one day. This is why saving the age in a database table is bad practice and nobody does that. What you need is a view.

    create view person_age as
      select person_id, 
        floor(months_between(trunc(sysdate),birthdate)/12) as age
        from birthdays;
    

    Look at the SQL Fiddle

提交回复
热议问题