Aral Code

Designing Tables Lesson 20 of 21

SQL ALTER TABLE, DROP, Indexes and Views

Change a table's structure with ALTER TABLE, delete tables with DROP, speed up searches with indexes, and save useful queries as views.

Tables change as an app grows: a new column is needed, an old table is no longer used, searches get slow. This lesson covers the tools for maintaining a database over time.

ALTER TABLE: adding a column

SQL Add an email column to customers
ALTER TABLE customers ADD COLUMN email TEXT;

UPDATE customers SET email = 'maria.santos@example.com' WHERE id = 1;

SELECT * FROM customers;

Existing rows get NULL in the new column (or the column's default).

Renaming

SQL Rename a column and a table
ALTER TABLE products RENAME COLUMN stock TO quantity;
ALTER TABLE grades RENAME TO scores;

SELECT name, quantity FROM products LIMIT 3;

Other changes (MySQL)

ALTER TABLE customers DROP COLUMN email;                 -- remove a column
ALTER TABLE products MODIFY price DECIMAL(10,2) NOT NULL; -- change a type

SQLite supports DROP COLUMN (since version 3.35) but not MODIFY; changing a column's type in SQLite means rebuilding the table.

DROP TABLE: deleting a whole table

SQL Remove a table completely
CREATE TABLE temp_list (name TEXT);
DROP TABLE temp_list;
DROP TABLE IF EXISTS temp_list;   -- no error even if it's already gone

SELECT name FROM sqlite_master WHERE type = 'table';
CommandRemoves
DELETE FROM t WHERE ...Some rows
DELETE FROM tAll rows (table stays)
TRUNCATE TABLE t (MySQL)All rows, faster (table stays)
DROP TABLE tThe table and all its rows

Indexes: faster searches

Without an index, finding a customer by name means checking every row, like reading a whole book to find one word. An index is like the index at the back of a textbook: it lets the database jump straight to the right rows.

SQL Create an index
CREATE INDEX idx_customers_name ON customers(name);
CREATE INDEX idx_orders_customer ON orders(customer_id);

SELECT name, tbl_name FROM sqlite_master WHERE type = 'index';
  • Index columns you often use in WHERE, JOIN ... ON and ORDER BY.
  • Primary keys and UNIQUE columns get an index automatically.
  • Indexes make reading faster but writing slightly slower, and use extra space. Don't index every column.

With 25 products you won't notice a difference. With 2 million orders, an index can turn a 10-second query into a 0.01-second one.

Views: saved queries

A view is a query saved with a name. You can then select from it as if it were a table:

SQL A view of each order's total
CREATE VIEW order_totals AS
SELECT o.id AS order_id, c.name AS customer, o.order_date, o.status,
       SUM(oi.qty * p.price) AS total
FROM orders o
JOIN customers c    ON c.id = o.customer_id
JOIN order_items oi ON oi.order_id = o.id
JOIN products p     ON p.id = oi.product_id
GROUP BY o.id;

SELECT * FROM order_totals WHERE status = 'utang';

The long join is written once. Anyone can now run SELECT * FROM order_totals without knowing how it's built. Views don't store data themselves; they run their query each time, so they're always up to date.

SQL Using a view like a table
CREATE VIEW order_totals AS
SELECT o.id AS order_id, c.name AS customer, SUM(oi.qty * p.price) AS total
FROM orders o
JOIN customers c ON c.id = o.customer_id
JOIN order_items oi ON oi.order_id = o.id
JOIN products p ON p.id = oi.product_id
GROUP BY o.id;

SELECT customer, COUNT(*) AS orders, SUM(total) AS spent, MAX(total) AS biggest
FROM order_totals
GROUP BY customer
ORDER BY spent DESC
LIMIT 3;

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which command adds a new column?
2. What does DROP TABLE products; do?
3. What is an index for?
4. What is a view?

By Sir Reyel · Last updated 2026-12-04