KevsRobots Learning Platform
36% Percent Complete
By Kevin McAleer, 5 Minutes
In this lesson, we’ll explore how to insert, modify, and delete data in SQL tables. These operations are commonly referred to as CRUD operations: Create, Read, Update, and Delete.
INSERT INTOThe INSERT INTO statement allows you to add new records (rows) to a table. It specifies the table to insert data into, as well as the values for each column.
INSERT SyntaxINSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example: Adding a new user to the users table.
INSERT INTO users (name, age, email)
VALUES ('Alice', 30, '[email protected]');
Example: Adding multiple users at once.
INSERT INTO users (name, age, email)
VALUES ('Bob', 25, '[email protected]'),
('Charlie', 28, '[email protected]');
If some columns have default values, you can omit them in the INSERT statement.
Example: Inserting only name and email, letting age default to NULL or a specified default value.
INSERT INTO users (name, email)
VALUES ('David', '[email protected]');
SELECTTo verify your data, you can use the SELECT statement to retrieve rows from a table.
Example: View all users in the users table.
SELECT * FROM users;
This command retrieves every column for each row in the table.
UPDATEThe UPDATE statement is used to modify existing data in a table. It specifies the table, the columns to change, and conditions to filter the rows to update.
UPDATE SyntaxUPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Note: It’s essential to use the
WHEREclause to target specific rows. Omitting it will update every row in the table.
Example: Change the age of the user with id 1 to 35.
UPDATE users
SET age = 35
WHERE id = 1;
Example: Change both the age and email of a specific user.
UPDATE users
SET age = 32, email = '[email protected]'
WHERE name = 'Alice';
DELETEThe DELETE statement is used to remove rows from a table. Similar to UPDATE, it’s critical to include a WHERE clause to specify which rows to delete.
DELETE SyntaxDELETE FROM table_name
WHERE condition;
Warning: Omitting the
WHEREclause deletes all rows in the table.
Example: Delete the user with the email “[email protected]”.
DELETE FROM users
WHERE email = '[email protected]';
Example: Remove all data from the users table (use with caution).
DELETE FROM users;
This command empties the table but retains its structure, so you can still insert new data later.
When performing multiple INSERT, UPDATE, or DELETE statements, it’s often beneficial to use transactions. A transaction groups multiple changes, which are only saved if all operations are successful.
BEGIN TRANSACTION;
INSERT INTO users (name, age, email) VALUES ('Eve', 29, '[email protected]');
UPDATE users SET age = 30 WHERE name = 'Eve';
COMMIT;
If any part of the transaction fails, you can use ROLLBACK to undo all changes:
ROLLBACK;
Transactions help ensure data integrity, especially in cases where multiple related changes must either all succeed or fail together.
Insert three new records into the users table with the following data:
INSERT INTO users (name, age, email)
VALUES ('John', 40, '[email protected]'),
('Diana', 35, '[email protected]'),
('Sophia', 22, '[email protected]');
Update John’s age to 41.
UPDATE users
SET age = 41
WHERE name = 'John';
Delete the user “Sophia”.
DELETE FROM users
WHERE name = 'Sophia';
Verify each step by using SELECT * FROM users; to check your results.
Here’s a recap of the CRUD commands and their usage:
| Command | Description | Example |
|---|---|---|
INSERT |
Adds new data into a table | INSERT INTO users (name) VALUES ('Alice'); |
SELECT |
Retrieves data from a table | SELECT * FROM users; |
UPDATE |
Modifies existing data in a table | UPDATE users SET age = 35 WHERE id = 1; |
DELETE |
Removes data from a table | DELETE FROM users WHERE name = 'Bob'; |
With these CRUD operations, you now have the foundational skills to manage data within your SQL database. In the next lesson, we’ll dive deeper into querying data and using SELECT statements with filters.
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments