Build Your Own AI Assistant Part 1 - Creating the Assistant
116820 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
114678 Views
Control Arduino with Python using Firmata / PyFirmata
87081 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
57314 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
53588 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
52067 Views
LidarBot
Snaszy NAS a 3D printed NAS for Raspberry Pi
Waveshare CM5 boards
The Best Arduino Robot for Beginners
SMARS Lab upgrade with PyCharm
Chicken Nugget Piano
Mini-Rack 3D Design Tutorial
0h 20m
Using the Raspberry Pi Pico's Built-in Temperature Sensor
0h 24m
Getting Started with SQL
0h 32m
Introduction to the Linux Command Line on Raspberry Pi OS
0h 42m
How to install MicroPython
0h 8m
Wall Drawing Robot Tutorial
0h 22m
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
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 INTO
The 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
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
Example: Adding a new user to the users table.
users
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.
name
email
age
NULL
INSERT INTO users (name, email) VALUES ('David', '[email protected]');
SELECT
To 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.
UPDATE
The 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 table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Note: It’s essential to use the WHERE clause to target specific rows. Omitting it will update every row in the table.
WHERE
Example: Change the age of the user with id 1 to 35.
id
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';
DELETE
The 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 FROM table_name WHERE condition;
Warning: Omitting the WHERE clause 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
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:
INSERT INTO users (name) VALUES ('Alice');
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.
< Previous Next >