MySQL Tutorial
The INSERT INTO
statement in MySQL is used to add new records (rows) into a table.
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.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 |
Students
table.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).
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
gender
will 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 |