简体   繁体   中英

populate a column with a value based on value of other column alter table sql

I have a table 'Data' with a column 'Date'

I need to add another column called flag and populate it with 0 if the date is less than 2 years from current date and populate it with 1 if the date is more than 2 years from current date.

I did it by adding column using alter table and using update set statement as below

alter table data add flag INTEGER constraint flag_value check (flag in(0,1));

Is there a way to do this using just one alter table statement without using update set?

Is there a way to do this using just one alter table statement without using update set?

Not in sqlite, which does not support computed columns and there is no way to run a default UPDATE via an ALTER TABLE statement.

You could use the view approach from the linked question, or do as you've done and issue separate ALTER TABLE and UPDATE statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM