You’ve successfully created your first database and tables. Now it’s time to learn how to manage the data within them. In this guide, you’ll master the four fundamental actions that form the core of nearly every database-driven application: the MySQL CRUD operations.
CRUD is an acronym that stands for:
- Create – Add new data.
- Read – Retrieve data.
- Update – Modify existing data.
- Delete – Remove data.
Let’s dive into each one using the users and posts tables we created in our previous tutorial.
C for Create: Using the `INSERT` Statement
The INSERT INTO statement is used to add new rows of data into a table. The syntax involves specifying the table, the columns you want to fill, and then the values for those columns.
Let’s add our first user to the users table:
INSERT INTO users (username, email, password)
VALUES ('john_doe', 'john@example.com', 'secure_password_123');
After running this, a new row is created in the users table with the details for ‘john_doe’. The `id` and `created_at` fields will be filled in automatically because of how we designed the table.
R for Read: Using the `SELECT` Statement
The SELECT statement is how you retrieve, or “read,” data from your database. This is arguably the most common command you will ever use in SQL.
To see everything in the users table, you can use an asterisk (*), which is a wildcard for “all columns”:
SELECT * FROM users;
To be more specific, you can list the exact columns you want. To filter your results, you use the WHERE clause.
Let’s find the user with an `id` of 1 and only get their username and email:
SELECT username, email FROM users WHERE id = 1;
U for Update: Using the `UPDATE` Statement
The UPDATE statement is used to modify existing data. This is where you must be careful. Forgetting the WHERE clause is a classic beginner mistake that will update every single row in your table.
Let’s change John Doe’s email address. Notice the WHERE clause targeting his specific `id`:
UPDATE users
SET email = 'john.d@new-example.com'
WHERE id = 1;
This command finds the user where the `id` is 1 and changes only their email field.
D for Delete: Using the `DELETE` Statement
The DELETE statement is used to remove one or more rows from a table. Just like with UPDATE, the WHERE clause is absolutely critical.
Warning: If you run DELETE FROM users; without a WHERE clause, you will permanently erase all user data from the table!
Let’s say we want to remove the user with an `id` of 1:
DELETE FROM users WHERE id = 1;
📝 Your Turn: Practice CRUD Operations
It’s time to put your new skills to the test! Open MySQL and run the following commands to populate and manage your ‘blog’ database.
1. Insert 5 users into your users table.
INSERT INTO users (username, email, password) VALUES
('alice', 'alice@example.com', 'pass1'),
('bob', 'bob@example.com', 'pass2'),
('charlie', 'charlie@example.com', 'pass3'),
('diana', 'diana@example.com', 'pass4'),
('edward', 'edward@example.com', 'pass5');
(Hint: After inserting, run `SELECT * FROM users;` to get the `id` for each user. You’ll need these for the next step!)
2. Insert 5 blog posts into your posts table. Make sure the `author_id` matches the `id` of one of your users.
INSERT INTO posts (title, content, author_id) VALUES
('My First Post', 'This is the content of my first post.', 1),
('MySQL is Fun', 'Learning about databases is great!', 2),
('CRUD Explained', 'Create, Read, Update, Delete!', 1),
('Advanced SQL', 'What should I learn next?', 3),
('Hello World', 'A classic start to any project.', 2);
Now, try these tasks on your own:
- Select all posts written by Alice (whose `author_id` is 1).
- Update the title of “My First Post” to “A New Beginning”.
- Delete the post titled “Hello World”.
Conclusion
Congratulations! You now understand and can use the four essential MySQL CRUD operations. These four commands—INSERT, SELECT, UPDATE, and DELETE—are the foundation upon which almost all database interactions are built. Practice them until they become second nature.
In our next lesson, we’ll explore more advanced ways to retrieve data using JOINS to combine information from multiple tables. For more practice, you can also check out the excellent tutorials at W3Schools SQL.
Read More:Â Advanced JavaScript Topics: A Deep Dive into Modules, Event Delegation, Storage, and Asynchronous Concepts
Read More:Â How to Style React Components: A 2025 Guide
