Changing columns in a table that has foreign keys, views and triggers
(1) By anonymous on 2024-06-30 07:31:16 [link] [source]
I'm designing a database that will require column changes in the future, possibly multiple times.
How can I handle changes to an SQLite table with foreign keys, views, and triggers, such as altering the order or type of columns? Is there a structured method to do this, or do I need to recreate everything by following these steps each time:
- Create a new table.
- Copy the data to the new table.
- Recreate every foreign key, view, and trigger.
- Remove the old table, foreign keys, views, and triggers.
Is there a simpler way to manage these changes?
(2) By anonymous on 2024-06-30 07:47:06 in reply to 1 [link] [source]
To clarify your question and avoid an XY situation, could you advise exactly why you anticipate such a rate of table changes?
While I am sure your needs are legitimate, some table schema changes are needed because the data model is poor, some can be anticipated and columns held in expectation, and there may be a better solution to this underlying problem.
Modification of tables is not an easy part of sqlite3.
TLDR; more information about your situation might be useful.
(3) By anonymous on 2024-06-30 08:05:17 in reply to 2 [source]
I'm designing a system and will start with a simple database, but I plan to expand it over time. The database design might change as new features are added.
Since SQLite doesn't support changing the order or type of columns directly, I'm wondering how to easily handle future changes.
(4) By anonymous on 2024-06-30 09:08:32 in reply to 3 [link] [source]
So, as before, it rather depends on exactly what you are anticipating needing to do.
There are various database migration tools out there, it's fairly trivial to rebuild your database from scratch every time, live updates of databases in place are possible, modifications of tables (while quite limited in sqlite) are possible.
What you are changing and the complexity of your database and where the database is (remote internet server vs local desktop vs mobile vs client, level of availability, risks of downtime or dataloss etc etc) will have a big impact on what you want to do.
There are suggestions and cookbook style recipes within this site's documentation and elsewhere on the internet regarding sqlite schema migrations.
And you still haven't demonstrated that a schema change is likely to be needed at all - eg if you are logging sensors and think you may be adding new sensors over time, this doesn't need a schema change at all, it needs an appropriate normalised data model from the start.
So, I am afraid that your question as posed isn't really specific enough for a clear, comprehensive answer.
(5) By anonymous on 2024-06-30 09:10:19 in reply to 3 [link] [source]
As a further example of my point, the order of columns shouldn't really be of any importance at all in the general case. The only potential concern about this is implementation specific storage and speed considerations with large, high performance databases.
Why would you be concerned about changing the order?
(6) By anonymous on 2024-06-30 09:22:54 in reply to 1 [link] [source]
I originally wrote the question above, and I have a solution at least for parts of my problem.
I tried to work with DBeaver to edit sqlite features, such as column type and order, and it failed many times. I had to manually use sql to restructure the db, which took many steps.
I tried the same with DB Browser for SQLite, and it works much better.
Both programs are probably creating some temp table and making changes and restructuring the db automatically, but DB Browser for SQLite does it better, and many changes were successful even on an existing database.
The result is that I can edit many features on an existing database. Good to know.
(7) By anonymous on 2024-06-30 09:27:11 in reply to 5 [link] [source]
Its a personal database, and I use a database editor to edit it. It is much more convenient to have the fields in a specific order, and its annoying that I need to restructure everything just to create one column in the middle. but I found a solution, as I wrote in another comment.
(8) By Chris Locke (chrisjlocke1) on 2024-06-30 09:45:07 in reply to 3 [link] [source]
Since SQLite doesn't support changing the order or type of columns directly, I'm wondering how to easily handle future changes
If you have table A, simply create table B with the columns you need.
Copy data from table A to table B
Delete table A
rename table B to table A
Profit.
You use DB4S (DB Browser for SQLite) which does the same thing, under the hood.