Advertisement
Google Ad Slot: content-top
MySQL INSERT INTO
MySQL INSERT INTO Statement
The INSERT INTO statement in MySQL is used to add new records (rows) into a table.
Syntax:
INSERT INTO table_name (column1, column2, column3) VALUES (value1, value2, value3);
table_name→ The name of the table where data is inserted.(column1, column2, column3)→ The columns where values will be inserted.VALUES (value1, value2, value3)→ The actual values to insert.
Demo Database
Below is a selection from the "Students" table in the school_db database:
student_id |
name |
gender |
city |
age |
score |
course_id |
phone_number |
|
|---|---|---|---|---|---|---|---|---|
1 |
Alice |
Female |
Delhi |
20 |
85 |
101 |
NULL |
1234567890 |
2 |
Bob |
Male |
Mumbai |
22 |
75 |
NULL |
bob@email.com |
NULL |
3 |
Charlie |
Male |
Delhi |
21 |
95 |
102 |
NULL |
NULL |
4 |
David |
Male |
Bangalore |
23 |
65 |
101 |
david@email.com |
NULL |
5 |
Eve |
Female |
Mumbai |
20 |
80 |
103 |
NULL |
NULL |
6 |
Frank |
Male |
Delhi |
22 |
90 |
103 |
NULL |
NULL |
7 |
Alice |
Female |
Mumbai |
19 |
60 |
102 |
NULL |
NULL |
Inserting a Record
- Adds a student named John Doe, aged 20, from New York into the
Studentstable.
Inserting Multiple Records at Once
- Inserts three students in one query.
Inserting Data Without Specifying Columns
- Works only if you provide values for all columns in the correct order.
Insert Data Only in Specified Columns
In MySQL, you don’t have to insert data into all columns of a table. You can specify only the columns you want to insert values into, and the rest will either:
✅ Take their default value (if defined).
✅ Remain NULL (if allowed).
Syntax:
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
- Columns not mentioned will be set to NULL or their default value.
genderwill be default value (Male). Other columns will be null.
The selection from the "Students" table will now look like this:
student_id |
name |
gender |
city |
age |
score |
course_id |
phone_number |
|
|---|---|---|---|---|---|---|---|---|
1 |
Alice Johnson |
Female |
Delhi |
20 |
85 |
101 |
NULL |
1234567890 |
2 |
Bob |
Male |
Mumbai |
22 |
75 |
NULL |
bob@email.com |
NULL |
3 |
Charlie |
Male |
Delhi |
21 |
95 |
102 |
NULL |
NULL |
4 |
David |
Male |
Bangalore |
23 |
65 |
101 |
david@email.com |
NULL |
5 |
Eve |
Female |
Mumbai |
20 |
80 |
103 |
NULL |
NULL |
6 |
Frank |
Male |
Delhi |
22 |
90 |
103 |
NULL |
NULL |
7 |
Alice |
Female |
Mumbai |
19 |
60 |
102 |
NULL |
NULL |
8 |
Bob |
Male |
Chicago |
19 |
NULL |
NULL |
NULL |
NULL |