SQL Insert Query Tutorial | SQL INSERT INTO Statement Example
SQL Insert Query Tutorial | SQL Insert Into Statement Example is today’s topic. If you are not familiar with creating a table in SQL, then please check out my how to create a table in SQL tutorial. The INSERT INTO statement is used to add new values into the database. The INSERT INTO statement adds the new record to the table. INSERT INTO can contain the values for some or all of its columns. INSERT INTO can be combined with a SELECT to insert records.
SQL Insert Query Tutorial
The general syntax of SQL INSERT INTO Statement is following.
INSERT INTO table-name (column-names) VALUES (values)
Here, column-names could be column1, column2, column3,…columnN are the names of the columns in a table into which you want to insert a data into the database.
You may not need to specify a column(s) name in the SQL query if you are adding the values for all the columns of a table. But make sure you need to preserve the order of the columns. Let’s take an example.
Step 1: Create a SQL Table.
I am using MacOS and SQLite client. You can use phpMyAdmin or Oracle or other database software.
Now, type the following query to create a table.
CREATE TABLE Apps ( AppID int, AppName varchar(255), CreatorName varchar(255), AppCategory varchar(255), AppPrice int );
So, we have created a table called Apps which has five columns.
Step 2: Insert the values inside the table in SQL
Okay, next step is to use the SQL INSERT INTO query to add the rows.
INSERT INTO Apps (AppID,`AppName`,`CreatorName`,`AppCategory`,`AppPrice`) VALUES (1, 'AppDividend', 'Krunal', 'Software', 50 );
Now, run the query and you will see in the table that one row is added.
So, we have successfully inserted data into the Database.
Insert Multiple Values in Database
We can also insert the multiple values to the table. See the following query.
INSERT INTO Apps (AppID,`AppName`,`CreatorName`,`AppCategory`,`AppPrice`) VALUES (2, 'Escrow', 'LVVM', 'Fashion', 60 ), (3, 'KGB', 'MJ', 'Music', 70 ), (4, 'Moscow', 'Mayor', 'Area', 80 ), (5, 'MoneyControl', 'Mukesh', 'Investment', 90 ), (6, 'Investing', 'Bill', 'Stocks', 100 )
The output of the above SQL query is following.
If you are adding values for all of the columns of a table, you do not need to specify the column names in a SQL query. However, make sure the order of the values is in the same order as the columns in a table.
Populate one table using another table
You can populate the values into the table through the select statement over another table; provided that the other table has the set of fields, which are required to fill the first table. See the following SQL query. Let’s try to add the data to the DummyApp table.
INSERT INTO DummyApp (AppID, `AppName`) SELECT AppID, AppName FROM Apps;
In the above query, we are inserting two column values inside the DummyApp table using Apps table.
The DummyApp table has only two columns which are AppID and AppName.
Also, Apps table has two columns. That is why we can easily add all the values from one table to another table. If you run the above query and you have set up the DummyApp table then all the values from Apps table will be copied to the DummyApp table.
Insert The Data Only in Specified Columns
It is also possible to only insert data in the specific columns. See the following query.
INSERT INTO Apps (AppID, `AppName`) VALUES (10, 'Stavanger');
So, the above query only inserts one row and two column values.
All the other column values should be NULL for that particular row.
Here, you need to be very careful because if some column does not allow the NULL values then SQL Engine throw an exception and the data will not be inserted in the database.
Finally, SQL Insert Query Tutorial | SQL INSERT INTO Statement Example is over.