π Alter Table Data
Letβs say youβre a small business owner, and your business is growing! You realize you need to keep track of discounts for each order. To do this, you need to change the structure of your existing table by adding a new column.
Suppose you have an orders table like this:
To track discounts, you want to add a discount column. In SQL, you can modify the table structure using the ALTER TABLE statement:
- SQL Table
- SQL Code
- Output
| order_no | order_status | place_of_order |
|----------|--------------|----------------|
| 101 | Delivered | Amazon |
| 512 | Delivered | Amazon |
| 432 | Shipped | Pedbubble |
| 984 | Processing | Store |
| 566 | Delivered | Store |
ALTER TABLE orders
ADD discount;
| order_no | order_status | place_of_order | discount |
|---|---|---|---|
| 101 | Delivered | Amazon | |
| 512 | Delivered | Amazon | |
| 432 | Shipped | Pedbubble | |
| 984 | Processing | Store | |
| 566 | Delivered | Store |
β This is your first step in understanding how data is structured in relational databases. Once you master rows, you're on your way to writing powerful SQL queries!.
We can add new col with
ADDKeywords
ποΈ Deleting a Column from a Tableβ
Sometimes, you may need to remove a column from an existing table. You can do this using the DROP COLUMN command with ALTER TABLE. For example, to remove the place_of_order column from the orders table:
- SQL Table
- SQL Code
- Output
| order_no | order_status | order_amt | customer_id | place_of_order |
|----------|--------------|-----------|-------------|----------------|
| 101 | Delivered | 550 | 3001 | Amazon |
| 512 | Delivered | 113 | 3001 | Amazon |
| 984 | Processing | 54 | 3012 | Store |
| 566 | Delivered | 850 | 3120 | Store |
| 432 | Shipped | 99 | 3003 | Amazon |
ALTER TABLE orders
DROP COLUMN place_of_order;
SELECT * FROM orders;
| order_no | order_status | order_amt | customer_id |
|---|---|---|---|
| 101 | Delivered | 550 | 3001 |
| 512 | Delivered | 113 | 3001 |
| 984 | Processing | 54 | 3012 |
| 566 | Delivered | 850 | 3120 |
| 432 | Shipped | 99 | 3003 |
β Tip: Always double-check before dropping a column, as this operation is irreversible and will permanently remove the data in that column.
βοΈ Renaming a Column in a Tableβ
You can rename a column in SQL using the RENAME COLUMN command with ALTER TABLE. Specify the original column name after RENAME COLUMN, and the new name after TO. For example, to rename the order_amt column to amount in the orders table:
- SQL Table
- SQL Code
- Output
| order_no | order_status | order_amt | customer_id | discount |
|----------|--------------|-----------|-------------|----------|
| 101 | Delivered | 550 | 3001 | |
| 512 | Delivered | 113 | 3001 | |
| 984 | Processing | 54 | 3012 | |
| 566 | Delivered | 850 | 3120 | |
| 432 | Shipped | 99 | 3003 | |
ALTER TABLE orders
RENAME COLUMN order_amt TO amount;
SELECT * FROM orders;
| order_no | order_status | amount | customer_id | discount |
|---|---|---|---|---|
| 101 | Delivered | 550 | 3001 | |
| 512 | Delivered | 113 | 3001 | |
| 984 | Processing | 54 | 3012 | |
| 566 | Delivered | 850 | 3120 | |
| 432 | Shipped | 99 | 3003 |
β
Tip: Use ALTER TABLE ... RENAME COLUMN ... TO ... to safely rename columns without losing data.
β What You Have Learnedβ
In this module, you explored essential SQL table transformation skills, including:
-
Altering Table Structure
How to use theALTER TABLEstatement to add, remove, or rename columns in an existing table. -
Adding Columns
UsingADDwithALTER TABLEto introduce new columns, such as tracking discounts for orders. -
Deleting Columns
Removing unnecessary columns withDROP COLUMNto keep your table structure relevant. -
Renaming Columns
Renaming columns safely usingRENAME COLUMNto improve clarity or adapt to changing requirements. -
Best Practices
Double-checking before dropping columns (as this is irreversible), and using clear, descriptive column names for maintainability.
These skills help you adapt your database schema as your application or business needs evolve.
π Quiz: Test Your Knowledgeβ
1. How can you remove the place_of_order column from the orders table in SQL?β
Here is the original orders table:
| order_no | order_status | order_amt | customer_id | place_of_order |
|---|---|---|---|---|
| 101 | Delivered | 550 | 3001 | Amazon |
| 512 | Delivered | 113 | 3001 | Amazon |
| 984 | Processing | 54 | 3012 | Store |
| 566 | Delivered | 850 | 3120 | Store |
| 432 | Shipped | 99 | 3003 | Amazon |
Answer
- Use the
ALTER TABLEstatement withDROP COLUMNto remove the column:
ALTER TABLE orders
DROP COLUMN place_of_order;
SELECT * FROM orders;
2. How can you add a new column called discount of type int to the orders table in SQL?β
Here is the original orders table:
| order_no | order_status | order_amt | customer_id | place_of_order |
|---|---|---|---|---|
| 101 | Delivered | 550 | 3001 | Amazon |
| 512 | Delivered | 113 | 3001 | Amazon |
| 984 | Processing | 54 | 3012 | Store |
| 566 | Delivered | 850 | 3120 | Store |
| 432 | Shipped | 99 | 3003 | Amazon |
Answer
- Use the
ALTER TABLEstatement withADDto add the new column:
ALTER TABLE orders ADD discount int;
SELECT * FROM orders;