
A Database is simply a mess of interconnected Data-tables where you store inter-related data in order to make some descriptive information. In a previous post, I discussed how you can create a Data-table in SQL with examples, just make sure you have read that post before reading this.
After a Data-table is created in your Database you have to insert the data inside it, in that way, you will be able to query those inserted data from the table which those values belong to.
Just suppose you have created the following data-table in your database, but you did not insert the data into the table. So at this moment what you have inside your database is a data-table which is Employee Table and it contains only its column titles ( field names ).
| Emp_ID | Name | Age | Department | Salary |
| 001 | John | 27 | SE | 75000 |
| 002 | Sara | 26 | DB | 60000 |
| 003 | Alex | 28 | SE | 75000 |
| 004 | Max | 28 | GD | 80000 |
Now, you need to insert these data row by row into Employee Table which is created in your database.
In SQL, we use INSERT statement for inserting data rows into a created table in a database. With the INSERT statement, we have to mention the name of the table which the data rows are going to be inserted.
Syntax for INSERT statement
INSERT INTO table_name (column_1, column_2,…….., column_n)
VALUES (value1_for_column_1, value1_for_column_2, ……..,value1_for_column_n),
(value2_for_column_1, value2_for_column_2, ……..,value2_for_column_n),
.
.
.
( value_n_for_column_1, value_n_for_column_2, ……..,value_n_for_column_n);
Three reserved keywords(commands) have been used in the syntax for inserting data up to n number of rows in n number of columns, which are
- INSERT
- INTO
- VALUES
the command “INTO” is used to specify the data-table which we are going to insert the data, after entering the table name you have to enter the column names in that table that you wish to insert data. But, if that table contains NOT NULL columns like PRIMARY KEY’s then those column names must be entered (all your column names is placed inside the bracket).
All the values for the columns in your table are entered after the “VALUES” command, values for a single row are placed inside a round bracket, but if you have multiple rows to be inserted, then all those rows should be placed in different parentheses that are separated by commas in the statement.
Note:it is not compulsory to enter the column names, when you need to insert data for all the columns in your table.
The syntax for INSERT statement when you insert values for all the columns.
INSERT INTO table_name
VALUES(value_1, value_2, value_,…..,value_n);
Examples;
1. let’s insert data for Employee Table,
INSERT INTO employee
VALUES('001','John',27,'SE',75000),
('002','Sara',26,'DB',60000),
('003','Alex',28,'SE',75000),
('004','Max',28,'GD',80000);
2. Add a new row to Employee Table for an employee who has following details
emp_id = ‘005’ , age = 24, department = ‘DB’
INSERT INTO employee (emp_id,age,department)
VALUES(‘005’,24,’DB’);