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.product_id | product_name | category | price | stock |
---|---|---|---|---|
6 | Fitness Band | Fitness | 3500 | 20 |
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:
product_name | stock |
---|---|
Yoga Mat | 80 |
Deleting Data from Tables
Sometimes, youâll need to remove outdated or incorrect data. For this, you use theDELETE
statement.
WHERE
Clause is not mandatory in DELETE
statement. But without the WHERE
clause you will end up removing all the rows from the table.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
INSERT
to add new rows to your tables. - Use
UPDATE
to modify existing data, but always double-check yourWHERE
clause to avoid accidental updates. - Use
DELETE
to remove rows, but be cautiousâdeleting without aWHERE
clause removes all rows in the table.