This tutorial will learn you SQL INSERT INTO with definition, syntax, query, & example. You will learn to insert data into a table with or without a column name. After reading each point, you will easily execute an insert query to store values in the database. So, read the complete tutorial carefully lest you should leave any point.
SQL INSERT INTO Statement
Definition – INSERT INTO is a SQL Statement that can be used to insert new data into a table.
Learn more –
SQL Interview Questions and Answers
INSERT INTO Syntax
You can insert values into a table with the help of the following syntax –
Syntax –
INSERT INTO table_name ( column1_name, column2_name,..., column(n) ) VALUES (column1_value, column2_value, ..., column(n)_value );
Important Point –.
- INSERT INTO statement may be written Uppercase or lowercase.
- Column Names & values must be separated by a comma
- Also, Column Name & values must be in the same order.
- Before executing the INSERT INTO query, you will have to create a SQL database & table
- You must declare string values within a quotation.
- You should not declare numeric values within a quotation.
- Always declare column names without quotation.
- You can execute the INSERT INTO query multiple times for inserting multiple values.
- You can skip the column name from the INSERT INTO query if you need to insert values in all the columns of a table.
INSERT INTO Example
Suppose that we have created a table students
with five columns like full_name
,age
, gender
, email_address
, address
. you can insert values into this table using the following query –
Query – 1
This query will insert values into the first rows of a students
table
INSERT INTO students (full_name, age, gender, email_address, address) VALUES ('Sunil Kumar', 25, 'Male', 'xyzxyz@gmail.com', 'Patna');
Query – 2
If you again execute another following query, It will insert values into the second rows of a students
table.
INSERT INTO students (full_name, age, gender, email_address, address) VALUES ('Mrs Monika', 18, 'Female', 'xyzxyz@gmail.com', 'New Delhi');
SQL INSERT INTO Specified Column
As you have seen syntax with the column name. Even you have insert values in all the full_name
,age
, gender
, email_address
, address
of students
table in the previous example. Now you will learn to insert values into only a specified column of a table.
Example – 1
You can insert a value in a single specified column full_name
Query –
INSERT INTO students (full_name) VALUES ('Rapson Jani');
Example – 2
You can insert values in two specified columns full_name
and email_address
INSERT INTO students (full_name, email_address) VALUES ('Rapson Jani','abcdxyz@gmail.com');
Example – 3
You can also insert values in three specified columns age
, email_address
and address
INSERT INTO students (age, email_address, address) VALUES (24,'abcd123@gmail.com','Jaipur');
Example – 4
You can also insert values in all the specified columns full_name
, age
, gender
, email_address
and address
Query –
INSERT INTO students (full_name, age, gender, email_address, address) VALUES ('Kanika Kappor', 22, 'Female', 'xyzxyz123@gmail.com', 'Mumbai');
Note –
You can change the order position of the column name in the above query. see the following query
INSERT INTO students (gender, full_name, address, age, email_address) VALUES ('Female', 'Kanika Kappor','Mumbai', 22, 'xyzxyz123@gmail.com');
SQL INSERT INTO without Column
You can skip the column name from the INSERT INTO statement if you need to insert values in all the columns. But you must have to declare values in the same order of column name in a table.
Note – I recommend you should use the INSERT INTO statement with column names for security.
Syntax –
You can use the following INSERT INTO syntaxes without specifying the column name
INSERT INTO table_name VALUES ( column1_value, column2_value, .... column(n)_value);
Example –
Suppose that a table students
has column order full_name
,age
, gender
, email_address
, address
. Now, you have to insert values into the same order of the column using the following query –
Query –
INSERT INTO students VALUES ('Kanika Kappor', 22, 'Female', 'xyzxyz123@gmail.com', 'Mumbai');
How to INSERT Values into a table in SQL
You have to follow the following steps to insert values into a table –
- First of all, install the MySQL package on your computer
- Create a MySQL Database like
codingstatus
- Create a table like
student
in MySQL database - Insert values into a table using INSERT INTO query
mysql>CREATE DATABASE codingstatus; mysql>USE codingstatus; mysql>CREATE TABLE students ( full_name varchar(50), age int(10), gender varchar(20), email_address varchar(50) address varchar(100), ); mysql>INSERT INTO students (full_name, age, gender, email_address, address) VALUES ('Manisha Kappor', 21, 'Female', 'xyzx123@gmail.com', 'Noida');
Tutorial Summary
Dear Developer, I have this tutorial according to my working experience. I hope that You have learned SQL INSERT INTO a Table. Now you can insert any types of values into the table.
If you have any questions, Kindly ask me through the comment box. Even, you should share this tutorial with your friends.