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
The INSERT INTO
statement in MySQL is used to insert new rows into a table. There are two ways to use it:
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.
MySQL INSERT statement example:
INSERT
statement is used to add new records to a table in MySQL.INSERT INTO your_table (column1, column2, column3) VALUES (value1, value2, value3);
How to use INSERT in MySQL:
INSERT
statement to insert data into a table, specifying the column names and values.INSERT INTO employees (employee_name, salary, department_id) VALUES ('John Doe', 60000, 101);
Adding records to a table in MySQL:
INSERT
statement adds new records to a table, providing values for each column.INSERT INTO products (product_name, price, category_id) VALUES ('Laptop', 1200, 3);
Inserting data into specific columns in MySQL:
INSERT INTO customers (customer_name, email) VALUES ('Alice', 'alice@example.com');
Inserting multiple records with a single INSERT in MySQL:
INSERT
statement by providing multiple sets of values.INSERT INTO orders (customer_id, order_date) VALUES (101, '2023-01-15'), (102, '2023-01-16'), (103, '2023-01-17');
Inserting data with SELECT statement in MySQL:
INSERT INTO ... SELECT
syntax to insert data based on the result of a SELECT
query.INSERT INTO new_products (product_name, price) SELECT product_name, price FROM old_products WHERE price > 50;
MySQL INSERT IGNORE for avoiding duplicate entries:
INSERT IGNORE
to insert data into a table, ignoring duplicate key violations.INSERT IGNORE INTO unique_products (product_id, product_name) VALUES (101, 'Product A'), (102, 'Product B');
Handling auto-increment columns with INSERT in MySQL:
INSERT
statement to let MySQL generate the next value.INSERT INTO users (username, password) VALUES ('john_doe', 'password123');
Examples of using INSERT in MySQL queries:
INSERT
statement in MySQL queries.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');