SQLite Alter Table?

Not quite ALTER TABLE syntax, but it will work in a pinch. Hopefully they'll get ALTER TABLE into the 3.x release some time in the future. Until then here is syntax to do it now. Using transactions it's mostly safe and fairly quick. Probably a good idea to do a VACUUM after you're done.

BEGIN TRANSACTION;
CREATE TEMPORARY TABLE Bar(a,b);
INSERT INTO Bar SELECT a,b FROM Foo;
DROP TABLE Foo;
CREATE TABLE Foo(a,b);
INSERT INTO Foo SELECT a,b FROM Bar;
DROP TABLE Bar;
COMMIT;

Update: SQLite now support some limited ALTER TABLE commands.

Leave A Reply - 2 Replies
Replies
Rob 2006-07-15 01:32pm - No Email - Logged IP: 68.232.166.166

Just wanted to say, that i've bookmarked this page and made use of this little trick numerous times. Thanks much for posting it. Cheers, -- rob

old_skool_kid 2009-03-21 09:16pm - No Email - Logged IP: 78.86.198.202

Wow, thanks for this post. Still using old version of sqlite, and this page ranks highly in google for sorting out table alterations.

Much appreciated!

All content licensed under the Creative Commons License