Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create table in SQLite only if it doesn't exist already

I want to create a table in a SQLite database only if doesn't exist already. Is there any way to do this? I don't want to drop the table if it exists, only create it if it doesn't.

like image 790
user461112 Avatar asked Nov 04 '10 15:11

user461112


People also ask

Can we create empty table?

If you want the table to be empty use the WHERE 1=0 or TOP (0) on both sides of the UNION ALL. If you want a copy of the table with data then just put the WHERE 1=0 or TOP (0) on one side.

Does table exist SQLite?

In an SQLite database, the names of all the tables are enlisted in the sqlite_master table. So in order to check if a table exists or not we need to check that if the name of the particular table is in the sqlite_master table or not.

What happens if you create a table that already exists?

When you try to create a table with an already existing table name, you will receive an error message, and no table will be modified or created.


1 Answers

From http://www.sqlite.org/lang_createtable.html:

CREATE TABLE IF NOT EXISTS some_table (id INTEGER PRIMARY KEY AUTOINCREMENT, ...); 
like image 166
David Wolever Avatar answered Oct 13 '22 16:10

David Wolever