In this tutorial, we will Learn how to add a single row / multiple rows into a table in SQL server. To add the rows, we will be using the Insert Statement.
Table of Contents
Insert row syntax
The simplified syntax of adding a new row into sql server is as follows.
1 2 3 4 | INSERT INTO table_name (column_list) VALUES (value_list); |
The statement starts with INSERT INTO
.
table_name
: The table in which the we are going to insert a row. We can use the Fully Qualified table name here.
column_list
: The list of columns separated by comma.
If we skip a column then the SQL server inserts the default value ( or null if default value is not specified). If the column does not allow null value and default value is also not specified, then the Server will throw an error
VALUES
clause follows the column_list
value_list
The values to insert. Each value is separated by a comma and must match the order of the fields in the column_list
.
Add Row Example
1 2 3 | Create database HR |
Select the HR database
1 2 3 | Use HR |
Create a new table Employee
1 2 3 4 5 6 7 8 9 10 11 | CREATE TABLE Employee ( EmployeeID int NOT NULL PRIMARY KEY, FirstName varchar(50) NOT NULL, LastName varchar(50) NOT NULL, Department varchar(20) NOT NULL, Designation varchar(15) NOT NULL, Salary decimal(10, 0) NOT NULL, Email varchar(50) NULL, ) |
Insert a new row to employee table. Enclose the string in single quotes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Insert into Employee ( EmployeeID, FirstName, LastName, Department, Designation, Salary, Email ) Values (1, 'Sachin', 'Tendulkar', 'Administration', 'CEO', 500000, ) |
Use select query to view the inserted data
1 2 3 | Select * from Employee |
Add Multiple Rows
To add multiple rows, use the following syntax.
1 2 3 4 5 6 | INSERT INTO table_name (column_list) VALUES (value_list_1), (value_list_2), (value_list_3); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | Insert into Employee ( EmployeeID, FirstName, LastName, Department, Designation, Salary, Email ) Values (2, 'Saurav', 'Ganguly', 'Administration', 'CEO', 500000, ), (3, 'Rahul', 'Dravid', 'Administration', 'CEO', 500000, ) |
Reference
Read More