Changing tables, dropping them, and indexes
Tables change over time. The business wants to record something new, a column was badly named, an old table is no longer needed. ALTER TABLE changes the structure of a table without touching the rest of its data. DROP TABLE removes a table. This lesson covers both, and then indexes, the main tool for making queries fast.
Adding a column
SELECT id, name, phone FROM customers LIMIT 3;Result
| id | name | phone |
|---|---|---|
| 1 | Asha Verma | NULL |
| 2 | Ravi Kumar | NULL |
| 3 | Meena Gill | NULL |
3 rows
The new column exists in every row, and it is empty: all the existing rows hold NULL in it. Fill it with UPDATE.
Give a default, and existing rows get that value instead of NULL.
SELECT id, name, active FROM products LIMIT 3;Result
| id | name | active |
|---|---|---|
| 1 | Basmati Rice 5 kg | 1 |
| 2 | Toor Dal 1 kg | 1 |
| 3 | Sunflower Oil 1 L | 1 |
3 rows
A new column that is NOT NULL needs a default. Otherwise the database would not know what to put in the rows that already exist.
Renaming
SELECT name, department FROM staff LIMIT 2;Result
| name | department |
|---|---|
| Anita Kapoor | Management |
| Rohit Bansal | Sales |
2 rows
Renaming is quick for the database and can be slow for people: every saved query, report and program that used the old name stops working. On a shared database, agree a rename with everyone who uses the table first. We rename the table back so that later examples work.
Removing a column
The column and everything in it are gone for good. Check first that nothing still uses it.
What ALTER TABLE can do differs by database
This is one of the least uniform parts of SQL.
| Change | SQLite | MySQL | PostgreSQL | SQL Server |
|---|---|---|---|---|
| Add a column | ADD COLUMN | ADD COLUMN | ADD COLUMN | ADD |
| Rename a column | RENAME COLUMN a TO b | RENAME COLUMN a TO b | RENAME COLUMN a TO b | sp_rename procedure |
| Change a column's type | not supported | MODIFY COLUMN | ALTER COLUMN ... TYPE | ALTER COLUMN |
| Drop a column | DROP COLUMN | DROP COLUMN | DROP COLUMN | DROP COLUMN |
SQLite cannot change the type of a column or add most constraints to an existing table. The usual way round is to create a new table with the right design, copy the data across with INSERT ... SELECT, drop the old table and rename the new one.
DROP TABLE
DROP TABLE deletes a table: its structure, its data, everything.
SELECT * FROM scratch;What the database says
Error: no such table: scratchThere is no confirmation and, outside a transaction, no undo. Dropping a table that does not exist is an error; DROP TABLE IF EXISTS name avoids that in scripts.
DELETE, TRUNCATE or DROP?
| Statement | Removes | Table still exists? |
|---|---|---|
DELETE FROM t WHERE ... | the rows that match | yes |
DELETE FROM t | all rows | yes, empty |
TRUNCATE TABLE t | all rows, quickly | yes, empty |
DROP TABLE t | rows and the table itself | no |
Interviewers like this question. DELETE is a data statement and can be filtered. TRUNCATE and DROP are structure statements and cannot.
Indexes: why some searches are fast
Think of the index at the back of a textbook. To find every page that mentions "photosynthesis", you do not read the whole book. You look the word up in the index, which is in alphabetical order, and go straight to the pages.
A database index works the same way. Without one, a search such as WHERE city = 'Delhi' makes the database read every row of the table. That is called a full table scan. With an index on city, it can jump to the matching rows. On twelve rows nobody can tell the difference. On twelve million, it is the difference between a blink and a long wait.
Creating an index
Give the index a name, the table, and the column. Nothing looks different afterwards. Queries are written exactly as before and return exactly the same rows. The database simply uses the index when it helps.
SELECT name, city FROM customers WHERE city = 'Delhi';Result
| name | city |
|---|---|
| Ravi Kumar | Delhi |
| Imran Ali | Delhi |
2 rows
An index can cover two columns. The order matters: an index on (status, order_date) helps searches on status alone, or on status and date together, but not on date alone, in the same way that a phone book sorted by surname and then first name is no help in finding everyone called Ravi.
A unique index also enforces that no two rows share a value.
What to index
- Primary keys are indexed automatically. You never need to do it.
- Foreign key columns, because joins search on them:
orders.customer_id,order_items.order_id,order_items.product_id. - Columns that appear often in
WHERE. - Columns used often in
ORDER BY.
What indexes cost
Indexes are not free, which is why you do not index everything.
- Each index takes disk space.
- Every
INSERT,UPDATEandDELETEmust update the indexes as well as the table, so writing gets slower as indexes are added. - An index on a column with only a few different values, such as a yes or no flag, rarely helps.
- As earlier lessons noted, an index on a column is usually ignored when the query wraps the column in a function, or when a
LIKEpattern starts with%.
The sensible approach is to add indexes for the queries you really run, and to measure.
Asking the database what it will do
Every database can explain how it plans to run a query. In SQLite, put EXPLAIN QUERY PLAN in front.
EXPLAIN QUERY PLAN
SELECT name FROM customers WHERE city = 'Delhi';Result
| id | parent | notused | detail |
|---|---|---|---|
| 3 | 0 | 62 | SEARCH customers USING INDEX idx_customers_city (city=?) |
1 row
The answer mentions the index made above, which shows it is being used. MySQL and PostgreSQL use the word EXPLAIN alone. Reading plans is a skill of its own. For now it is enough to know that the tool exists and that the word "scan" on a big table is the thing to look for.
Removing an index
Dropping an index loses no data. Only the speed-up goes. MySQL and SQL Server need the table name too: DROP INDEX idx_products_name ON products.
Try it yourself
In the practice box, press Reset the database first.
Exercise 1. Add a text column called notes to the orders table.
Show one answer
ALTER TABLE orders ADD COLUMN notes TEXT;Done.
Exercise 2. Create an index called idx_products_category on the category column of products.
Show one answer
CREATE INDEX idx_products_category ON products (category);Done.
Exercise 3. Create a table called temp_list with one text column item, and then drop it. Two statements.
Show one answer
CREATE TABLE temp_list (item TEXT);
DROP TABLE temp_list;Done.
Examples on this page were run with SQLite 3.50.4 on 12 November 2026. The results shown are the real results.
This is an independent learning site. It is not connected with the makers of any database product. MySQL, PostgreSQL, SQL Server, Oracle and SQLite are named only so that you know where a piece of SQL works. The shop, its customers and its staff in the practice database are made up.