Here is the general syntax that you can use in order to insert records into a table using SQL:
INSERT INTO table_name (column_1, column_2, column_3,...) VALUES ('value_1', 'value_2', 'value_3',...)
Let’s now review an example to see how to apply the above syntax in practice.
The Example
Let’s suppose that you created an empty table called the ‘product‘ table which contains 3 columns: product_id, product_name, and price:
CREATE TABLE product ( product_id int primary key, product_name nvarchar(50), price int )
The ultimate goal is to insert the following 4 records into the table:
product_id | product_name | price |
1 | Computer | 800 |
2 | TV | 1200 |
3 | Printer | 150 |
4 | Desk | 400 |
Insert Records Into the Table using SQL
The query below can be used to insert the 4 records into the ‘product’ table (note that you should not place a comma after the closing bracket of the last value):
INSERT INTO product (product_id, product_name, price) VALUES (1,'Computer',800), (2,'TV',1200), (3,'Printer',150), (4,'Desk',400)
After running the above query, the 4 records will be inserted into the ‘product’ table.
To verify that the records were added to the table, let’s run the following SELECT query:
SELECT * FROM product
You’ll now see the newly inserted records:
product_id | product_name | price |
1 | Computer | 800 |
2 | TV | 1200 |
3 | Printer | 150 |
4 | Desk | 400 |
Insert Additional Records to the Table
You can insert additional records to the table at anytime using the same technique as reviewed above.
For example, let’s insert two additional records to the ‘product’ table:
INSERT INTO product (product_id, product_name, price) VALUES (5,'Chair',120), (6,'Tablet',300)
After running the query, the two additional records will be inserted into the ‘product’ table.
Let’s rerun the SELECT query:
SELECT * FROM product
You’ll see that the two additional records were indeed inserted at the bottom of the table:
product_id | product_name | price |
1 | Computer | 800 |
2 | TV | 1200 |
3 | Printer | 150 |
4 | Desk | 400 |
5 | Chair | 120 |
6 | Tablet | 300 |
You may also want to check the following page for additional SQL tutorials.