Modifying Table Structure
In this step, we'll learn how to modify existing tables using ALTER TABLE commands. This is a common task when your data requirements change over time.
Let's modify our products table to add some new columns and modify existing ones:
- Add a new column:
ALTER TABLE products
ADD COLUMN manufacturer VARCHAR(100) AFTER name;
This command adds a new column named manufacturer
after the name
column.
- Modify an existing column:
ALTER TABLE products
MODIFY COLUMN description VARCHAR(500) NOT NULL DEFAULT 'No description available';
This command changes the description
column to a variable-length string with a maximum length of 500 characters. It also sets a default value of 'No description available' for new rows.
- Rename a column:
ALTER TABLE products
CHANGE COLUMN weight product_weight DECIMAL(8,2);
This command renames the weight
column to product_weight
and changes its data type to a decimal number with 8 total digits and 2 decimal places.
- Drop a column:
ALTER TABLE products
DROP COLUMN in_stock;
This command removes the in_stock
column from the table.
Let's add a composite index to improve query performance:
ALTER TABLE products
ADD INDEX idx_name_manufacturer (name, manufacturer);
This command creates a composite index on the name
and manufacturer
columns.
To see all the changes we've made:
DESCRIBE products;
SHOW INDEX FROM products;
You should see the updated table structure and indexes:
MariaDB [store]> DESCRIBE products;
+----------------+---------------+------+-----+--------------------------+-------------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------+------+-----+--------------------------+-------------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | MUL | NULL | |
| manufacturer | varchar(100) | YES | | NULL | |
| price | decimal(10,2) | NO | | NULL | |
| description | varchar(500) | NO | | No description available | |
| product_weight | decimal(8,2) | YES | | NULL | |
| created_at | datetime | YES | | current_timestamp() | |
| last_updated | timestamp | NO | | current_timestamp() | on update current_timestamp() |
+----------------+---------------+------+-----+--------------------------+-------------------------------+
8 rows in set (0.001 sec)
MariaDB [store]> SHOW INDEX FROM products;
+----------+------------+-----------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Ignored |
+----------+------------+-----------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| products | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | NO |
| products | 1 | idx_name_manufacturer | 1 | name | A | 0 | NULL | NULL | | BTREE | | | NO |
| products | 1 | idx_name_manufacturer | 2 | manufacturer | A | 0 | NULL | NULL | YES | BTREE | | | NO |
+----------+------------+-----------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
3 rows in set (0.000 sec)