Search code examples
mysqlsqlddlalter-table

I want to set a default column value based on another column and add 114 days to it


I tried this:

ALTER TABLE búgatás 
    ADD malacozás_ideje AS DATEADD(DAY, 114, Dátum);

I want 'malacozás_ideje' column to have the same value as Dátum + 114 days. Is this even possible?


Solution

  • If you want a generated column in MySQL:

    ALTER TABLE búgatás ADD malacozás_ideje date
        generated always as (Dátum + interval 114 day);
    

    If you want to just initialize it:

    ALTER TABLE búgatás ADD malacozás_ideje date;
    
    UPDATE búgatás
        SET malacozás_ideje date = (Dátum + interval 114 day);