154

How do you rename a table in SQLite 3.0?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
readonly
  • 343,444
  • 107
  • 203
  • 205

2 Answers2

279
ALTER TABLE `foo` RENAME TO `bar`

SQLite Query Language: ALTER TABLE

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
John Sheehan
  • 77,456
  • 30
  • 160
  • 194
5

The answer remains to use "ALTER TABLE". But the main documentation page on this topic is pretty thick. What is needed is a simple example of how that works. You can find that here: https://www.sqlitetutorial.net/sqlite-alter-table/

To be precise, in the most basic case it looks like this:

ALTER TABLE existing_table
RENAME TO new_table;

I am not sure if the dot notation works, but I assume that the following is also correct:

ALTER TABLE existing_database.existing_table
RENAME TO new_database.new_table;

If you have spaces in the name, then you need to backticks that are included in the first accepted answer... so you may need something like this if you have spaces.

ALTER TABLE `existing databasename`.`existing tablename`
RENAME TO `newer databasename`.`newer tablename`;
ftrotter
  • 3,066
  • 2
  • 38
  • 52
  • 1
    Another distinction between this answer and the accepted answer (relevant to my use-case anyway) is that you do not need the single back-quotes around the old and new names (so long as they don't contain spaces I imagine). – wopr_xl Jan 24 '23 at 23:06