sqlite
  1. sqlite-insert-query

SQLite CRUD Operation: Insert Query

In SQLite, the Insert Query is used to insert a new row of data into a table. This operation allows you to create records in the database table by providing data values for each column.

Syntax

The basic syntax for Insert Query in SQLite is as follows:

INSERT INTO table_name(column1,column2,column3,...columnN) VALUES(value1,value2,value3,...valueN);

Here's a more detailed explanation of each part of the syntax:

  • INSERT INTO: This is a required SQLite keyword that specifies the Insert Query.
  • table_name: This is the name of the table you want to insert the record into.
  • (column1,column2,column3,...columnN): This is an optional parameter that specifies the names of the columns you want to insert the data into. If you omit this parameter, you must specify values for all columns in the table.
  • VALUES(value1,value2,value3,...valueN): This is a required parameter that specifies the values you want to insert into the columns you have specified.

Example

Suppose we have a table named employee_details with the following columns: id, first_name, last_name, and address. We can insert a new row into this table using the following Insert Query:

INSERT INTO employee_details (id,first_name,last_name,address) 
VALUES (1,'John','Doe','123 Main Street');

Output

If the above Insert Query is executed successfully, the output will be a new row added to the employee_details table with the values for the id, first_name, last_name, and address columns provided in the query.

 id  | first_name | last_name |     address
-----|------------|-----------|----------------
  1  |   John     |   Doe     | 123 Main Street

Explanation

In the above example, we insert a new row into the employee_details table with the values for the id, first_name, last_name, and address columns provided in the query. The id value is set to 1, the first_name value is set to "John", the last_name value is set to "Doe", and the address value is set to "123 Main Street".

Use

The Insert Query is used to create records in a database table. You can use this operation to add new data to a table or to update an existing table by inserting records with new data values.

Important Points

  • In SQLite, you can insert data into a table using the Insert Query, but you can also use other statements such as the INSERT OR REPLACE, INSERT OR IGNORE, and REPLACE INTO statements.
  • If you are inserting values into all columns of a table, you may omit the column names in the Insert Query.
  • If you are inserting values into only certain columns of a table, you must specify the column names in the Insert Query.
  • When inserting data into a table, you must make sure that the data types of the provided values match the data types of the corresponding columns in the table.
  • When inserting data into a table with auto-incrementing primary keys, you can omit the value for that column and it will be automatically generated by the database.

Summary

In this tutorial, we learned about the Insert Query in SQLite, its syntax, and how to use it to insert data into a table. We saw an example of how to use the Insert Query to add a row to an employee_details table. We also learned about some important points to consider when using the Insert Query in SQLite.

Published on: