Changing and Deleting Tables
Tables change over time: the shop starts to collect a new piece of data, or a name turns out to be unclear. You don't have to delete the table with all its rows and create it again: ALTER TABLE changes a table that's already there. ADD COLUMN adds a column to it:
ALTER TABLE customers
ADD COLUMN phone TEXT;
SELECT name, phone
FROM customers
LIMIT 2;| name | phone |
|---|---|
| Emma Clark | NULL |
| Liam Turner | NULL |
The new column is added at the end of the table. The rows that are already there get NULL in it, or the column's DEFAULT. That's why a new column with NOT NULL needs a DEFAULT: without one, the rows that are already there would break the rule:
ALTER TABLE customers
ADD COLUMN newsletter INTEGER NOT NULL;Error on line 1: Cannot add a NOT NULL column with default value NULLWith DEFAULT 0, it works, and every customer gets 0. RENAME COLUMN renames a column, and RENAME TO the whole table:
ALTER TABLE customers
RENAME COLUMN joined TO signed_up;
ALTER TABLE order_items
RENAME TO order_lines;SQLite changes the name everywhere in the database, for example in the foreign keys of other tables. But the queries in an app's code still use the old name, and they'd stop working, so in a real app, tables and columns are renamed rarely and carefully. Besides this, SQLite's ALTER TABLE can only delete a column (DROP COLUMN). It can't change a column's type or add NOT NULL, UNIQUE or CHECK to a column that's already there. For that, you create a new table as it should be, copy the rows into it with INSERT … SELECT, delete the old table and give the new one its name. Other databases can do more.
DROP TABLE reviews; deletes a table with all its rows. There's no “Are you sure?” and no undo; only inside a transaction can ROLLBACK bring the table back. A table that other tables point at with their foreign keys can't be deleted while they have rows pointing at it, so the tables that point at it have to go first:
DROP TABLE games;Error on line 1: FOREIGN KEY constraint failedDROP TABLE of a table that doesn't exist is an error, and so is CREATE TABLE of a table that already exists. In scripts that may run more than once, programmers write DROP TABLE IF EXISTS and CREATE TABLE IF NOT EXISTS: when the table isn't there, or already is, the statement simply does nothing. The Tables tab keeps showing the tables as the exercise starts with them; your changes are seen only by the statements in your file.
Typical mistake: mixing up DROP TABLE and DELETE FROM. DELETE FROM reviews; deletes all the rows, but the empty table stays, ready for new ones. DROP TABLE reviews; deletes the table itself, with its columns and rules.
Instructions
The shop is planning a sale. Add to the table games a column discount: a whole number, always filled in, 0 by default. Then give the Racing games a discount of 5 (with UPDATE). Finally, show the title, the genre and the discount of the Racing and Puzzle games, sorted by title:
| title | genre | discount |
|---|---|---|
| Cube Logic | Puzzle | 0 |
| Pixel Racer | Racing | 5 |
| Turbo Karts | Racing | 5 |
| Word Quest | Puzzle | 0 |
Below it, rename the table reviews to game_reviews, and its column stars to rating. Then show how many reviews each rating has, in the columns rating and reviews, from the highest rating:
| rating | reviews |
|---|---|
| 5 | 8 |
| 4 | 10 |
| 3 | 5 |
| 2 | 2 |
The shop now takes its orders through an online payment service, which keeps them in its own database, so this database doesn't need them any more. At the end of the file, delete the tables orders and order_items. Write both with IF EXISTS (DROP TABLE IF EXISTS …), so that the file would still run on a database where they're already gone. One of the two tables points at the other: think about which of them has to go first.
Start programming for free
8/10