|
| 1 | +--- |
| 2 | +id: alter-table |
| 3 | +title: Alter Table Data |
| 4 | +sidebar_label: Alter Table Data |
| 5 | +sidebar_position: 3 |
| 6 | +tags: [html, web-development, attributes, values] |
| 7 | +description: In this tutorial, you will learn about HTML attributes and values. HTML attributes provide additional information about elements, and values define the specific settings or properties of the attributes. |
| 8 | +keywords: [html, web development, attributes, values, html attributes, html values, html tutorial, html basics, web design, web pages, websites, html structure, html attributes tutorial, html values tutorial, html in 2024] |
| 9 | +--- |
| 10 | + |
| 11 | + |
| 12 | +# 📗 Alter Table Data |
| 13 | + |
| 14 | +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. |
| 15 | + |
| 16 | +Suppose you have an `orders` table like this: |
| 17 | + |
| 18 | +To track discounts, you want to add a `discount` column. In SQL, you can modify the table structure using the `ALTER TABLE` statement: |
| 19 | + |
| 20 | + |
| 21 | + :::info |
| 22 | +<Tabs> |
| 23 | + <TabItem value="SQL Table" label="SQL Table"> |
| 24 | + ```sql title="Orders Table" |
| 25 | +| order_no | order_status | place_of_order | |
| 26 | +|----------|--------------|----------------| |
| 27 | +| 101 | Delivered | Amazon | |
| 28 | +| 512 | Delivered | Amazon | |
| 29 | +| 432 | Shipped | Pedbubble | |
| 30 | +| 984 | Processing | Store | |
| 31 | +| 566 | Delivered | Store | |
| 32 | +``` |
| 33 | + </TabItem> |
| 34 | + |
| 35 | +<TabItem value="SQL Code" label="SQL Code"> |
| 36 | + |
| 37 | + |
| 38 | +```sql |
| 39 | +ALTER TABLE orders |
| 40 | +ADD discount; |
| 41 | +``` |
| 42 | + |
| 43 | + </TabItem> |
| 44 | + |
| 45 | + <TabItem value="how-git-works" label="Output "> |
| 46 | + |
| 47 | +| order_no | order_status | place_of_order | discount | |
| 48 | +|----------|--------------|----------------|----------| |
| 49 | +| 101 | Delivered | Amazon | | |
| 50 | +| 512 | Delivered | Amazon | | |
| 51 | +| 432 | Shipped | Pedbubble | | |
| 52 | +| 984 | Processing | Store | | |
| 53 | +| 566 | Delivered | Store | | |
| 54 | + </TabItem> |
| 55 | +</Tabs> |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +✅ 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!. |
| 62 | + |
| 63 | +> We can add new col with `ADD` Keywords |
| 64 | +
|
| 65 | +::: |
| 66 | + |
| 67 | +## 🗑️ Deleting a Column from a Table |
| 68 | + |
| 69 | +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: |
| 70 | + |
| 71 | +<Tabs> |
| 72 | + <TabItem value="SQL Table" label="SQL Table"> |
| 73 | + |
| 74 | + ```sql title="Orders Table" |
| 75 | +| order_no | order_status | order_amt | customer_id | place_of_order | |
| 76 | +|----------|--------------|-----------|-------------|----------------| |
| 77 | +| 101 | Delivered | 550 | 3001 | Amazon | |
| 78 | +| 512 | Delivered | 113 | 3001 | Amazon | |
| 79 | +| 984 | Processing | 54 | 3012 | Store | |
| 80 | +| 566 | Delivered | 850 | 3120 | Store | |
| 81 | +| 432 | Shipped | 99 | 3003 | Amazon | |
| 82 | +``` |
| 83 | + </TabItem> |
| 84 | + |
| 85 | + <TabItem value="SQL Code" label="SQL Code"> |
| 86 | + |
| 87 | +```sql |
| 88 | +ALTER TABLE orders |
| 89 | +DROP COLUMN place_of_order; |
| 90 | +SELECT * FROM orders; |
| 91 | +``` |
| 92 | + |
| 93 | + </TabItem> |
| 94 | + |
| 95 | + <TabItem value="Output" label="Output"> |
| 96 | + |
| 97 | +| order_no | order_status | order_amt | customer_id | |
| 98 | +|----------|--------------|-----------|-------------| |
| 99 | +| 101 | Delivered | 550 | 3001 | |
| 100 | +| 512 | Delivered | 113 | 3001 | |
| 101 | +| 984 | Processing | 54 | 3012 | |
| 102 | +| 566 | Delivered | 850 | 3120 | |
| 103 | +| 432 | Shipped | 99 | 3003 | |
| 104 | + |
| 105 | + </TabItem> |
| 106 | +</Tabs> |
| 107 | + |
| 108 | +:::tip |
| 109 | +✅ Tip: Always double-check before dropping a column, as this operation is irreversible and will permanently remove the data in that column. |
| 110 | +::: |
| 111 | + |
| 112 | +## ✏️ Renaming a Column in a Table |
| 113 | + |
| 114 | +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: |
| 115 | + |
| 116 | +<Tabs> |
| 117 | + <TabItem value="SQL Table" label="SQL Table"> |
| 118 | + |
| 119 | + ```sql title="Orders Table" |
| 120 | +| order_no | order_status | order_amt | customer_id | discount | |
| 121 | +|----------|--------------|-----------|-------------|----------| |
| 122 | +| 101 | Delivered | 550 | 3001 | | |
| 123 | +| 512 | Delivered | 113 | 3001 | | |
| 124 | +| 984 | Processing | 54 | 3012 | | |
| 125 | +| 566 | Delivered | 850 | 3120 | | |
| 126 | +| 432 | Shipped | 99 | 3003 | | |
| 127 | +``` |
| 128 | + </TabItem> |
| 129 | + |
| 130 | + <TabItem value="SQL Code" label="SQL Code"> |
| 131 | + |
| 132 | +```sql |
| 133 | +ALTER TABLE orders |
| 134 | +RENAME COLUMN order_amt TO amount; |
| 135 | +SELECT * FROM orders; |
| 136 | +``` |
| 137 | + |
| 138 | + </TabItem> |
| 139 | + |
| 140 | + <TabItem value="Output" label="Output"> |
| 141 | + |
| 142 | +| order_no | order_status | amount | customer_id | discount | |
| 143 | +|----------|--------------|--------|-------------|----------| |
| 144 | +| 101 | Delivered | 550 | 3001 | | |
| 145 | +| 512 | Delivered | 113 | 3001 | | |
| 146 | +| 984 | Processing | 54 | 3012 | | |
| 147 | +| 566 | Delivered | 850 | 3120 | | |
| 148 | +| 432 | Shipped | 99 | 3003 | | |
| 149 | + |
| 150 | + </TabItem> |
| 151 | +</Tabs> |
| 152 | + |
| 153 | +:::tip |
| 154 | +✅ Tip: Use `ALTER TABLE ... RENAME COLUMN ... TO ...` to safely rename columns without losing data. |
| 155 | +::: |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | +## ✅ What You Have Learned |
| 160 | + |
| 161 | +In this module, you explored essential SQL table transformation skills, including: |
| 162 | + |
| 163 | +- **Altering Table Structure** |
| 164 | + How to use the `ALTER TABLE` statement to add, remove, or rename columns in an existing table. |
| 165 | + |
| 166 | +- **Adding Columns** |
| 167 | + Using `ADD` with `ALTER TABLE` to introduce new columns, such as tracking discounts for orders. |
| 168 | + |
| 169 | +- **Deleting Columns** |
| 170 | + Removing unnecessary columns with `DROP COLUMN` to keep your table structure relevant. |
| 171 | + |
| 172 | +- **Renaming Columns** |
| 173 | + Renaming columns safely using `RENAME COLUMN` to improve clarity or adapt to changing requirements. |
| 174 | + |
| 175 | +- **Best Practices** |
| 176 | + Double-checking before dropping columns (as this is irreversible), and using clear, descriptive column names for maintainability. |
| 177 | + |
| 178 | +These skills help you adapt your database schema as your application or business needs evolve. |
| 179 | + |
| 180 | +--- |
| 181 | + |
| 182 | +## 📝 Quiz: Test Your Knowledge |
| 183 | +#### 1. How can you remove the `place_of_order` column from the `orders` table in SQL? |
| 184 | + |
| 185 | +Here is the original `orders` table: |
| 186 | + |
| 187 | + |
| 188 | +| order_no | order_status | order_amt | customer_id | place_of_order | |
| 189 | +|----------|--------------|-----------|-------------|----------------| |
| 190 | +| 101 | Delivered | 550 | 3001 | Amazon | |
| 191 | +| 512 | Delivered | 113 | 3001 | Amazon | |
| 192 | +| 984 | Processing | 54 | 3012 | Store | |
| 193 | +| 566 | Delivered | 850 | 3120 | Store | |
| 194 | +| 432 | Shipped | 99 | 3003 | Amazon | |
| 195 | + |
| 196 | + |
| 197 | +<details> |
| 198 | + <summary>Answer</summary> |
| 199 | + <ul> |
| 200 | + <li>Use the <code>ALTER TABLE</code> statement with <code>DROP COLUMN</code> to remove the column:</li> |
| 201 | + </ul> |
| 202 | + |
| 203 | + ```sql |
| 204 | + ALTER TABLE orders |
| 205 | + DROP COLUMN place_of_order; |
| 206 | + SELECT * FROM orders; |
| 207 | + ``` |
| 208 | +</details> |
| 209 | +--- |
| 210 | + |
| 211 | +#### 2. How can you add a new column called `discount` of type `int` to the `orders` table in SQL? |
| 212 | + |
| 213 | +Here is the original `orders` table: |
| 214 | + |
| 215 | +| order_no | order_status | order_amt | customer_id | place_of_order | |
| 216 | +|----------|--------------|-----------|-------------|----------------| |
| 217 | +| 101 | Delivered | 550 | 3001 | Amazon | |
| 218 | +| 512 | Delivered | 113 | 3001 | Amazon | |
| 219 | +| 984 | Processing | 54 | 3012 | Store | |
| 220 | +| 566 | Delivered | 850 | 3120 | Store | |
| 221 | +| 432 | Shipped | 99 | 3003 | Amazon | |
| 222 | + |
| 223 | +<details> |
| 224 | + <summary>Answer</summary> |
| 225 | + <ul> |
| 226 | + <li>Use the <code>ALTER TABLE</code> statement with <code>ADD</code> to add the new column:</li> |
| 227 | + </ul> |
| 228 | + |
| 229 | + ```sql |
| 230 | + ALTER TABLE orders ADD discount int; |
| 231 | + SELECT * FROM orders; |
| 232 | + ``` |
| 233 | +</details> |
| 234 | + |
| 235 | +<GiscusComments/> |
0 commit comments