🔧 Quick Syntax:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
This is how you tell SQL:
“Yo, here’s the table. I’m giving you these values. Save them like this.”
🧾 Let’s say you have a table called friends
It has the following structure:
Column | Data Type | Notes |
---|---|---|
id | INT | Auto-increment, Primary Key |
name | VARCHAR(50) | Required |
age | INT | Optional |
phone | VARCHAR(15) | Optional |
✅ Adding One Friend
INSERT INTO friends (name, age, phone)
VALUES ('Ravi', 25, '9876543210');
💡 Output:
Query OK, 1 row affected
The data has been added to your table. SQL automatically gives id = 1
since it’s set to auto-increment.
➕ Adding Another Friend
INSERT INTO friends (name, age, phone)
VALUES ('Simran', 28, '9123456789');
💡 Output:
Query OK, 1 row affected
Table Now Looks Like:
id | name | age | phone |
---|---|---|---|
1 | Ravi | 25 | 9876543210 |
2 | Simran | 28 | 9123456789 |
❌ What Happens If You Miss a Required Column?
INSERT INTO friends (age, phone)
VALUES (30, '9999999999');
⚠️ Output:
ERROR 1364 (HY000): Field 'name' doesn't have a default value
Why? The name
column was marked NOT NULL
, so SQL won’t allow it to be empty.
📦 Adding Multiple Friends at Once
INSERT INTO friends (name, age, phone)
VALUES
('Aman', 22, '8888888888'),
('Neha', 26, '7777777777'),
('Karan', 24, '6666666666');
💡 Output:
Query OK, 3 rows affected
Updated Table:
id | name | age | phone |
---|---|---|---|
1 | Ravi | 25 | 9876543210 |
2 | Simran | 28 | 9123456789 |
3 | Aman | 22 | 8888888888 |
4 | Neha | 26 | 7777777777 |
5 | Karan | 24 | 6666666666 |
💤 What If You Don’t Know All Values?
INSERT INTO friends (name, age)
VALUES ('Karan', 24);
SQL will fill the missing phone
column with NULL
, since it’s optional.
🧃 Recap
INSERT INTO
lets you add real data into your table- Always include required columns like
NOT NULL
ones - Use one row at a time or many rows in one go
AUTO_INCREMENT
handles IDs — you don’t have to set them manually