Inserting Data into Tables
Adding new data to a table is done using theINSERT statement. Think of it as adding a new row to your spreadsheet.
Syntax:
Example: Adding a New Product
Imagine you’ve just launched a new product, “Fitness Band,” and want to add it to the Products table.
This is how simple it is to add new data!
Updating Data in Tables
What if your inventory changes, and you need to update the quantity of products in stock? That’s where theUPDATE statement comes in.
Syntax:
Example: Restocking a Product
Suppose a new shipment of Yoga Mats arrives, and you need to increase their stock by 30 units.Result: The stock for “Yoga Mat” is updated:
Deleting Data from Tables
Sometimes, you’ll need to remove outdated or incorrect data. For this, you use theDELETE statement.
Syntax:
Example: Removing a Discontinued Product
Let’s say the “Wireless Mouse” is no longer sold. Here’s how you can remove it from the Products table:Result: The “Wireless Mouse” is gone from the table.
Scenario: Managing Inventory
Let’s tie everything together with a realistic example:- Adding a new product:
You receive a new product, “Treadmill,” and want to add it to the database:
- Updating existing stock:
A shipment of “Bluetooth Speakers” arrives, so you increase their stock by 10 units:
- Removing a discontinued product:
The “Smartwatch” is discontinued, so you delete it from the table:
Summary
- Use
INSERTto add new rows to your tables. - Use
UPDATEto modify existing data, but always double-check yourWHEREclause to avoid accidental updates. - Use
DELETEto remove rows, but be cautious—deleting without aWHEREclause removes all rows in the table.