How To Create Table in SQLite Using “If Not Exists” Statement (2024)

In databases, massive amounts of data are stored. Once the data is stored, users will be to access these data and use them for different purposes such as marketing, data analysis, or any other purpose.

To store and manage these data you need a Data Management Service called SQLite.

For the unversed, SQLite is a serverless RDBMS, which is used to manage data in the database in the form of tables. SQLite read and writes data directly to ordinary disk files.

In SQLite, it is essential to store data in the form of a table so you can access the specific data by defining the columns and rows in which data is stored.

As I said earlier, the data will be stored in the form of tables. These tables can be created by using the “CREATE TABLE” or the “CREATE TABLE if not exists” statement in SQLite.

The “CREATE TABLE if not exists” statement is mostly used to create tables in databases because it will not allow you to create tables with the same names.

This means you will not able to create the table if the table with the same name already exists in the database which will reduce the confusion.

That’s why a lot of people use the “CREATE TABLE if not exists” statement instead of “CREATE TABLE”.

If you want to create a table with the “CREATE TABLE if not exists” statement and you don’t know how to use this statement then you are in the right place.

In this article, we are going to show you how to create a Table in SQLite using the “if not exists” Statement.

So, without any further ado let’s start,

How to Create Table in SQLite Using “if not exists” Statement

To create tables, you have to open the terminal in SQLite and will create a table, LinuxHint_employees.

Now you have to fire the command given below:

CREATE TABLE LinuxHint_employees (emp_id INT, emp_name CHAR, emp_dep );

It will create the table in SQLite by the name of LinuxHint_employees.

Now to confirm whether the table is created or not type this command,

.tables

This will show you all the available tables.

Once the Table LinuxHint_employees has been created successfully. Now we are going to create a new table with the same name let’s see what happens.

CREATE TABLE LinuxHint_employees (emp_id INT, emp_name CHAR, emp_dep );

As you can see when we execute the statement in the terminal it will generate the error “Error: table LinuxHint_employees already exists”.

We are getting this error because the name we are using to create a new table will already exist.

How to Create Table in SQLite Using “if not exists” Statement

If you use the “if not exists” statement to create tables, first it will analyze the list of tables and then it will create the table if there is no table available with the same name.

If the name you are using to create the table which already exists then it will execute the statement without creating the table.

The general syntax of creating the table by using the “if not exists” statement is given below.

CREATE TABLE IF NOT EXISTS TABLE_NAME (column_name datatype, column_name datatype);

Here, you have to replace TABLE_NAME with the table name. And write column name instead of column_name.

Now let’s try creating the table using the same command using the “if not exists” statement.

CREATE TABLE IF NOT EXISTS LinuxHint_employees (emp_id INT, emp_name CHAR, emp_dep );

The statement will be executed successfully without showing the error. Let’s see whether another table with the same name is created or not.

To display the list of tables you have to enter this command,

.tables

As you can see it has not created the table with the same name as well.

I think that’s all you need to know to create the tables in SQLite by using the “if not exists” statement.

We hope you understood the process of creating the tables. Feel free to ask if you have any confusion or queries related to this topic.

Source: LinuxHint.com

Leave a Comment