Full answer
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');Renaming is handled:
CREATE VIEW pay AS SELECT name, salary FROM employees;
ALTER TABLE employees RENAME COLUMN salary TO base_salary;
SELECT * FROM pay WHERE name = 'Asha';| name | base_salary |
|---|---|
| Asha | 120000 |
The view still works, but its output column is now called base_salary, because SQLite rewrote the view text. Queries that select salary from the view would fail. Giving view columns explicit aliases (base_salary AS salary) keeps the view's interface stable across renames.
Dropping a column the view uses is refused in SQLite 3.35+, because the view would break:
CREATE VIEW pay2 AS SELECT name, hire_date FROM employees;
ALTER TABLE employees DROP COLUMN hire_date;
-- error on purposeError: error in view pay2 after drop column: no such column: hire_datePostgreSQL is stricter in the same situation and names the dependent view:
CREATE VIEW pay AS SELECT name, salary FROM employees;
ALTER TABLE employees DROP COLUMN salary;
-- error on purposeError: cannot drop column salary of table employees because other objects depend on itWhen views break after a migration, list them with SELECT name, sql FROM sqlite_schema WHERE type = 'view', or in PostgreSQL use the dependency views, and recreate them as part of the migration.
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');
CREATE VIEW pay AS SELECT name, salary FROM employees;
ALTER TABLE employees RENAME COLUMN salary TO base_salary;
SELECT * FROM pay WHERE name = 'Asha';
-- error on purpose
CREATE VIEW pay2 AS SELECT name, hire_date FROM employees;
ALTER TABLE employees DROP COLUMN hire_date;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- SQLite 3.25+ RENAME COLUMN updates views and triggers
- Dropping a used column is refused
- PostgreSQL tracks dependencies and needs CASCADE
- Recreate dependent views in migrations
Common mistakes
- Discovering broken views only when a report fails
- Dropping columns with CASCADE without checking what it removes
Follow-ups
- How would you find all objects that depend on a column?
- How would you change a column used by many views?
Tips
- Check dependencies before migrations