[MySQL Tutorial] Adding Data with INSERT Statements
![[MySQL Tutorial] Adding Data with INSERT Statements](/uploads/editor/Mysql-강좌-5강-INSERT로-데이터-추가하기-20260706-793af0.webp)
![[Mysql 강좌] 5강 INSERT로 데이터 추가하기](/uploads/editor/Mysql-%EA%B0%95%EC%A2%8C-5%EA%B0%95-INSERT%EB%A1%9C-%EB%8D%B0%EC%9D%B4%ED%84%B0-%EC%B6%94%EA%B0%80%ED%95%98%EA%B8%B0-20260706-de5bc1.webp)
Let's Store Some Data
So far, we've created databases and tables.
But an empty table serves no purpose.
We have no member information,
no posts,
and no comments.
Now it's time to learn how to store actual data.
The SQL command used for storing data is INSERT.
What Is INSERT?
INSERT is a command that adds new data.
In simple terms,
it's an SQL statement that adds a new row to a table.
For example, when you sign up for a service, your member information gets stored,
and when you write a post on a forum, the post gets saved.
All these operations happen internally through INSERT statements.
Basic INSERT Syntax
The most basic form looks like this:
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
First, specify the table where you want to store the data,
then enter which columns will hold what values.
Adding Member Information
For example, let's assume we have a member table like this:
CREATE TABLE member (
id INT,
name VARCHAR(50),
email VARCHAR(100)
);
To add one member, you would enter:
INSERT INTO member (id, name, email)
VALUES (1, 'Hong Gildong', 'hong@test.com');
After execution, a new member is added to the member table.
Adding Multiple Members
Members aren't just one person.
You can keep adding multiple members.
INSERT INTO member (id, name, email)
VALUES (2, 'Kim Cheolsu', 'kim@test.com');
INSERT INTO member (id, name, email)
VALUES (3, 'Lee Younghee', 'lee@test.com');
Each time you run INSERT, more data gets added.
Saving Multiple Data Entries at Once
You can also save multiple rows of data in one go with INSERT.
INSERT INTO member (id, name, email)
VALUES
(4, 'Park Minsu', 'park@test.com'),
(5, 'Choi Soojin', 'choi@test.com'),
(6, 'Jeong Wuseong', 'jung@test.com');
This method is faster and keeps your SQL cleaner.
It's a common approach used in real-world applications.
Use Single Quotes for Text
Numbers can be entered directly,
but text must always be wrapped in single quotes (').
Correct example:
'Hong Gildong'
Incorrect example:
Hong Gildong
Get into the habit of always using single quotes for text values.
Numbers Don't Need Quotes
Unlike text, numbers don't require quotes.
100
Of course,
'100'
will also work, but it's better to use the correct data type.
Storing Date Data
If you have a DATETIME field, you can store dates like this:
INSERT INTO member
(name, email, created_at)
VALUES
('Hong Gildong', 'hong@test.com', NOW());
NOW() is a function that automatically stores the current date and time.
This is frequently used in forums and sign-up processes.
Entering All Columns in Order
You can omit column names, but it's not recommended.
INSERT INTO member
VALUES
(7, 'Kim Younghee', 'kim@test.com');
This method is risky because if the column order changes, errors can occur.
In professional settings, most developers include column names in their INSERT statements.
Verifying Successful INSERT
To check if data was stored correctly,
you'll learn about SELECT statements in the next lesson.
For example:
SELECT * FROM member;
This command will show you the stored data.
Board Data Storage Example
For a forum or board, you would store data like this:
INSERT INTO board
(title, content, writer)
VALUES
(
'MySQL First Post',
'Practice with INSERT statements.',
'Admin'
);
This is how most forums store data.
When you click the 'Write Post' button on a website, a similar SQL statement runs behind the scenes.
Understanding the INSERT Execution Flow
Imagine writing a post on a forum:
The process goes like this:
User writes a post
↓
Clicks Save button
↓
PHP or Node.js application
↓
Executes INSERT SQL
↓
MySQL saves the data
The INSERT statement we're learning is at the core of this process.
Common Beginner Mistakes
1. Mismatched Column and Value Counts
For example:
INSERT INTO member(id, name)
VALUES(1);
This causes an error.
The number of columns must match the number of values provided.
2. Forgetting Single Quotes Around Text
Hong Gildong
This causes an error.
You must enter it as:
'Hong Gildong'
3. Missing Semicolon (;)
SQL statements typically end with a semicolon.
This is one of the most common mistakes during practice.
4. Using Non-Existent Column Names
Column names must be accurate.
For example, if you try to use a column name like:
username
but it doesn't exist in your table, an error occurs.
Always double-check column names using the DESC command.
Key Takeaways
Here's a quick summary of what we've learned:
INSERT is the SQL command for storing data.
It adds new rows to tables.
Text values must be wrapped in single quotes.
You can save multiple entries at once.
NOW() function stores current date and time automatically.
Including column names is the safest approach.
This knowledge forms the foundation for all future data input operations.
Conclusion
In this lesson, we learned how to add data using INSERT statements.
Now that you can store data, the next step is learning how to retrieve it.
In the following lesson, we'll cover how to use SELECT statements to query data, and practice finding specific information in your database.
INSERT and SELECT are among the most frequently used SQL commands, so be sure to practice them multiple times through hands-on exercises.
FAQ
Why do I need single quotes for text in SQL?
Text values must be enclosed in single quotes to distinguish them from numeric or other data types, preventing syntax errors.
Can I omit column names when using INSERT?
Yes, but it's risky. If the table structure changes, your query may fail. Always include column names for safety and clarity.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [Mysql 강좌] 5강 INSERT로 데이터 추가하기