> On Apr 22, 2019, at 6:37 PM, Tommy Lane <km4...@km4lvw.com> wrote:
> 
>       How  would I go about updating an existing table to also have a
> parent integer and child integer column.

Use `pragma user_version` to get/set a schema version number in your database. 
It defaults to zero.
Associate each schema change with a user_version value. So call this one `1`.

When you open a db, check its user_version against the version# of each 
supported schema, in order. If it’s one less, apply that schema change, 
increment the user_version, and check the next one. That way all applicable 
schema updates will be applied in order.

In the case of this specific change, you’d use an ALTER TABLE ADD COLUMN 
statement to add the `parent` and `child` columns. All rows will now have the 
default values of those columns; if that’s not appropriate, you’ll need to run 
some UPDATE statements to initialize them to the proper values.

(Nitpick: You don’t need both `parent` and `child`; they’re redundant. In a 
properly normalized schema, you’d only have one; you can go the other direction 
by querying. My quick 2¢ opinion is to keep `child`. That way you can find 
current versions of entries with `child is null`.)

—Jens
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to