Inserting Data into Tables

Tutorial 2 of 5

1. Introduction

This tutorial aims to guide you on how to insert data into tables using SQL. The SQL INSERT statement is used to add new records (or rows) to a table.

By the end of this tutorial:

  • You will understand how to use the SQL INSERT statement.
  • You will learn how to specify data for each column in a table.
  • You will know how to insert data into specific columns.

The prerequisites for this tutorial are basic knowledge of SQL and understanding of relational databases and tables.

2. Step-by-Step Guide

The SQL INSERT statement is used to add new data into a table. It has two main forms:

  • The first form does not specify the column names where the data will be inserted, only their values:

sql INSERT INTO table_name VALUES (value1, value2, ...);

This form of the SQL INSERT statement allows you to add values to all columns of the table.

  • The second form specifies both the column names and the values to be inserted:

sql INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

This form of the SQL INSERT statement allows you to add values to specific columns in the table.

The columns and values need to be specified in the same order. For example, if column1 is for names and column2 is for age, you should input the name first, then the age.

3. Code Examples

Let's take a look at some examples:

Example 1:

Suppose we have a table 'Customers' with the following structure:

ID NAME AGE ADDRESS SALARY

To add a new row to this table:

INSERT INTO Customers 
VALUES (1, 'John', 30, '10 Downing St', 2000);

In this case, we are adding a new customer named John, who is 30 years old, lives at 10 Downing St, and has a salary of 2000.

Example 2:

To insert data into specific columns:

INSERT INTO Customers (ID, NAME, ADDRESS)
VALUES (2, 'Jane', '20 Downing St');

Here, we are adding a new customer with ID 2, named Jane, who lives at 20 Downing St. We didn't specify the AGE and SALARY, so they will be set to their default values (if any).

4. Summary

In this tutorial, you learned how to use the SQL INSERT statement to add new data to a table. You can either add values to all columns or specify the columns and their corresponding values.

Next, you can learn about other SQL statements like UPDATE and DELETE. You can also explore advanced concepts like SQL JOINs and subqueries.

Additional Resources:

  1. W3Schools SQL Tutorial
  2. SQLZoo

5. Practice Exercises

Exercise 1:

For the 'Customers' table, add a new customer with ID 3, named 'Bob', who is 40 years old, lives at '30 Downing St', and has a salary of 3000.

Solution:

INSERT INTO Customers 
VALUES (3, 'Bob', 40, '30 Downing St', 3000);

Exercise 2:

Add a new customer to the 'Customers' table with only ID 4 and NAME 'Alice'. Leave the other fields as default.

Solution:

INSERT INTO Customers (ID, NAME)
VALUES (4, 'Alice');

These exercises will help you practice the SQL INSERT statement. Make sure to run your queries and check if the data is inserted correctly.