The INSERT INTO statement is used to add new rows of data into a MySQL table.
DML Command
INSERT INTO is used to add one or more new records into an existing table.VALUES clause holds the actual data for each column.(name, age, dept)).The general pattern of an INSERT INTO statement is:
-- Basic syntax for inserting a single row
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);
-- Syntax for inserting multiple rows at once
INSERT INTO table_name (column1, column2, column3)
VALUES (value1a, value2a, value3a),
(value1b, value2b, value3b);
The column list is optional only when you provide values for all columns in the exact table order. However, it is strongly recommended to always specify the column names for clarity and safety.
Suppose we have a table named students with columns: id, name, age, department.
-- Insert a single record into the 'students' table
INSERT INTO students (name, age, department)
VALUES ('John Doe', 22, 'Engineering');
-- Insert multiple records into the 'students' table
INSERT INTO students (name, age, department)
VALUES ('Jane Smith', 23, 'Science'),
('Mark Johnson', 20, 'Arts');
-- Assuming 'id' is AUTO_INCREMENT, we can skip it and insert NULL for optional fields
INSERT INTO students (name, age, department)
VALUES ('Alice Brown', 21, NULL);
INSERT INTO statement adds one or more new rows to the students table.id is an AUTO_INCREMENT column, MySQL automatically generates its value.NULL for a column stores an actual NULL value, as long as the column allows it.NULL only when you intentionally want an “unknown / empty” value and the column allows it.INSERT INTO to avoid issues if the table structure changes later.students table (if not already created) with columns: id, name, age, department.INSERT INTO statement.NULL into the department column to see how it behaves.