Meritshot Tutorials

  1. Home
  2. »
  3. SQL INSERT INTO

SQL Tutorial

SQL INSERT INTO

The INSERT INTO statement is used to add new rows of data to a table in SQL. It allows you to insert values into specific columns or into all columns if values are provided for each.

Basic Syntax

Syntax:

INSERT INTO table_name (column1, column2, column3, …)

VALUES (value1, value2, value3, …);

Example:

Inserting a new record into the employees table with columns: employee_id, first_name, last_name, department, salary, and age.

INSERT INTO employees (employee_id, first_name, last_name, department, salary, age)

VALUES (7, ‘Sita’, ‘Rao’, ‘Finance’, 55000, 29);

Output:

The table employees now includes:

employee_id

first_name

last_name

department

salary

age

7

Sita

Rao

Finance

55000

29

 

Inserting Data into All Columns

If you want to insert data into all columns of the table, you do not need to specify the column names. However, the order of the values must match the order of the columns.

Syntax:

INSERT INTO table_name

VALUES (value1, value2, value3, …);

Example:

Inserting a new record into the employees table without specifying column names:

INSERT INTO employees

VALUES (8, ‘Manish’, ‘Patel’, ‘IT’, 62000, 32);

Output:

The table employees now includes:

employee_id

first_name

last_name

department

salary

age

8

Manish

Patel

IT

62000

32

Inserting Data into All Columns

You can insert multiple rows in a single INSERT INTO statement by separating the sets of values with commas.

Syntax:

INSERT INTO table_name (column1, column2, …)

VALUES (value1, value2, …),

       (value1, value2, …),

       …;

Example:

Inserting multiple records into the employees table:

INSERT INTO employees (employee_id, first_name, last_name, department, salary, age)

VALUES (9, ‘Ananya’, ‘Kumar’, ‘Marketing’, 57000, 28),

       (10, ‘Ravi’, ‘Sharma’, ‘HR’, 49000, 33),

       (11, ‘Deepak’, ‘Jain’, ‘IT’, 66000, 30);

Output:

The table employees now includes:

employee_id

first_name

last_name

department

salary

age

9

Ananya

Kumar

Marketing

57000

28

10

Ravi

Sharma

HR

49000

33

11

Deepak

Jain

IT

66000

30

 

Tips to Remember

  1. Column Order: Ensure the order of values matches the order of columns if column names are not specified.
  2. Data Types: Ensure the data types of the values match the data types of the columns.
  3. Constraints: Be aware of any constraints like NOT NULL or UNIQUE that may affect your ability to insert data.
  4. Use Transactions: When inserting multiple rows, consider using transactions to ensure data integrity.

Power of INSERT INTO

The INSERT INTO statement:

  • Adds New Data: Allows you to populate your database with new records.
  • Supports Multiple Records: Facilitates the insertion of multiple rows in a single operation.
  • Enforces Constraints: Ensures new data adheres to table constraints.

Frequently Asked Questions

Q1: What if I forget to include some columns in the INSERT INTO statement?
A1: If you don’t specify all columns, you must provide values for every column listed. Omitted columns must have default values or allow NULL.

Q2: Can I insert data into a table without specifying column names?
A2: Yes, but you must provide values for every column in the table, and in the correct order.

Q3: What happens if I insert a duplicate value into a column with a UNIQUE constraint?
A3: SQL will return an error if you try to insert a duplicate value into a column with a UNIQUE constraint.

Q4: How can I handle errors during data insertion?
A4: Use transactions to manage errors and roll back changes if something goes wrong. Also, check for constraints and data types to prevent issues.

Q5: Can I use INSERT INTO with a SELECT statement?
A5: Yes, you can insert data from another table using a SELECT statement. This is useful for copying data between tables.

Example:

INSERT INTO employees_backup (employee_id, first_name, last_name, department, salary, age)

SELECT employee_id, first_name, last_name, department, salary, age

FROM employees;