MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

MySQL INSERT: Insert Data (Add Data)

The INSERT INTO statement in MySQL is used to insert new rows into a table. There are two ways to use it:

  1. Specify both the column names and the values to be inserted
  2. Only specify the values, in which case the values will be added for all the columns of the table starting from the first column

Here's the syntax for each:

1. INSERT INTO with specified columns:

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

For example, if you have a table named employees with columns id, name, and salary, and you want to add a new employee to the table, you could use:

INSERT INTO employees (id, name, salary)
VALUES (1, 'John Doe', 50000);

2. INSERT INTO without specified columns:

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

Using the same employees table, the SQL statement would look like:

INSERT INTO employees
VALUES (2, 'Jane Doe', 60000);

Note: If you don't specify the column names, you need to make sure the values are in the same order as the columns in the table.

Inserting multiple rows at once:

You can also insert multiple rows in a single SQL query:

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

For the employees table, it could look like this:

INSERT INTO employees (id, name, salary)
VALUES 
(3, 'Richard Roe', 70000),
(4, 'Jane Smith', 80000),
(5, 'John Smith', 90000);

Remember to always match the number and type of columns in the table with the number and type of values in the VALUES clause.

  1. MySQL INSERT statement example:

    • Description: The INSERT statement is used to add new records to a table in MySQL.
    • Example:
      INSERT INTO your_table (column1, column2, column3) VALUES (value1, value2, value3);
      
  2. How to use INSERT in MySQL:

    • Description: Use the INSERT statement to insert data into a table, specifying the column names and values.
    • Example:
      INSERT INTO employees (employee_name, salary, department_id) VALUES ('John Doe', 60000, 101);
      
  3. Adding records to a table in MySQL:

    • Description: The INSERT statement adds new records to a table, providing values for each column.
    • Example:
      INSERT INTO products (product_name, price, category_id) VALUES ('Laptop', 1200, 3);
      
  4. Inserting data into specific columns in MySQL:

    • Description: Specify the columns for which you are providing values to insert data into specific columns.
    • Example:
      INSERT INTO customers (customer_name, email) VALUES ('Alice', 'alice@example.com');
      
  5. Inserting multiple records with a single INSERT in MySQL:

    • Description: Insert multiple records with a single INSERT statement by providing multiple sets of values.
    • Example:
      INSERT INTO orders (customer_id, order_date) VALUES (101, '2023-01-15'), (102, '2023-01-16'), (103, '2023-01-17');
      
  6. Inserting data with SELECT statement in MySQL:

    • Description: Use the INSERT INTO ... SELECT syntax to insert data based on the result of a SELECT query.
    • Example:
      INSERT INTO new_products (product_name, price)
      SELECT product_name, price FROM old_products WHERE price > 50;
      
  7. MySQL INSERT IGNORE for avoiding duplicate entries:

    • Description: Use INSERT IGNORE to insert data into a table, ignoring duplicate key violations.
    • Example:
      INSERT IGNORE INTO unique_products (product_id, product_name) VALUES (101, 'Product A'), (102, 'Product B');
      
  8. Handling auto-increment columns with INSERT in MySQL:

    • Description: For tables with auto-increment columns, omit the column in the INSERT statement to let MySQL generate the next value.
    • Example:
      INSERT INTO users (username, password) VALUES ('john_doe', 'password123');
      
  9. Examples of using INSERT in MySQL queries:

    • Description: Demonstrate various use cases of the INSERT statement in MySQL queries.
    • Examples:
      INSERT INTO employees (employee_name, salary, department_id) VALUES ('Jane Smith', 70000, 102);
      INSERT INTO orders (customer_id, order_date) VALUES (104, '2023-01-18'), (105, '2023-01-19');