TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The lack of proper “alter table” support in SQLite

90 点作者 paroneayea将近 12 年前

11 条评论

rogerbinns将近 12 年前
I am very familiar with SQLite internals. The answer is already in there. SQLite stores each row as each column value encoded sequentially corresponding to the declared order of the columns. Changing column order or deletions&#x2F;inserts require a rewrite of every row. The one special case that is allowed is adding a column on the end of the schema providing it has a default value.<p>A SQLite provided ALTER TABLE implementation would do exactly what was stated - start a transaction, rename the existing table to a temporary name, create a new one with the desired schema, and copy data across mangling as appropriate before deleting the old table and finishing the transaction. For plain tables this is no big deal, but for more complicated ones there are a lot of issues such as foreign key references, constraints, indices. The majority of the code would be dealing with all these conditions and interactions.<p>It also wouldn&#x27;t be any more efficient than code the developer writes - ie there are no shortcuts available to the SQLite developers that aren&#x27;t available to developers using SQLite. The net effect would be a larger library (they limit to a 250kb library size so something else would need to be chopped), some complex documentation and a heck of a lot of testing code. For something that is relatively rarely needed (see requests on the mailing lists).<p>The chance of something like this ending up in the SQLite core is extremely slim, so you could never depend on it being there anyway.<p>SQLite does have several things to help. There is a user_version pragma you can use to keep track of the schema version and use for upgrading. You can temporarily disable foreign key and constraint enforcing. There are numerous pragmas to get table metadata. The table definitions are stored as SQL strings in a sqlite master table, and a pragma allows you to make that writeable.
评论 #5887080 未加载
评论 #5887077 未加载
评论 #5890162 未加载
评论 #5887645 未加载
revelation将近 12 年前
ALTER is in itself an odd command, useful only in development, never in production, and bringin with it deep architectural implications and an endless list of problems in basically all SQL systems today.<p>In SQLite, an <i>embedded</i> database, it is certainly out of place. Drop, rebuild, and move on with life.
评论 #5887010 未加载
评论 #5887269 未加载
评论 #5887104 未加载
评论 #5886994 未加载
评论 #5887179 未加载
bobbyi_settv将近 12 年前
&gt; a successor called Alembic founded by the same core author as sqlalchemy-migrate<p>Alembic is not founded by the same core author as sqlalchemy-migrate. Alembic is founded by Mike Bayer who is the core author of SQLAlchemy itself.
评论 #5888466 未加载
stevenwei将近 12 年前
It&#x27;s probably relevant to note that both Android and iOS ship with SQLite as the de-facto database storage engine, so there&#x27;s very much a use case for having reliable in-place migrations in SQLite on modern software.<p>I could certainly see the value of having more robust ALTER TABLE support in SQLite itself, and I don&#x27;t find the argument that such functionality will go unused very convincing. In real world scenarios almost everyone is going to have to migrate at some point, and the easier this is to accomplish, the better.
tome将近 12 年前
&gt; I&#x27;m pre-pledging $200.00 towards fixing the problem<p>Nice idea, but wouldn&#x27;t he have to find about one hundred other pledgers to make this financially viable?
评论 #5888056 未加载
k_bx将近 12 年前
&gt; but we also want people to be able to run smallish installations for themselves or their friends and family as well<p>So, what&#x27;s the problem in running PostgreSQL or MySQL in small installations? They really need small amount of space and memory to be installed and operated.
评论 #5888809 未加载
评论 #5888101 未加载
strictfp将近 12 年前
SQlite might be meant to be light, but surely they could implement rudimentary alter table support (perhaps through the create new and rename strategy mentioned here)? The alternative is that each client have to code their own version. I can understand if they say no to features which are rarely used, but if something would be used by the majority of the users, I cannot understand that they would say no for &#x27;lightness&#x27; sake.
mrbaxter将近 12 年前
SQLite is a replacement for fopen(), not a database.
stevoski将近 12 年前
The H2 open source java database implements &quot;alter table&quot; exactly as per the description of sql alchemy: create a new table with the new structure, copy all the data across, delete the original table, rename the new table to the origina name.<p>This could perhaps be acceptable for sqlite.
ams6110将近 12 年前
SQLLite is &quot;lite&quot;<p>If you want alter table support, use a SQL database that provides it. There are many.
ExpiredLink将近 12 年前
So he gets a free, lightweight, immensely practical tool. His answer: complaint.
评论 #5888223 未加载