Usha Guduri

MYSQL Effects on Views from Changes to Table Structure

Au contraire, there are no effects of changing a table’s structure on a dependent view in MySQL! I’ve heard that other databases like Oracle automatically pick up changes on the table’s structure, but in MySQL the view is frozen upon creation.

1
2
CREATE VIEW my_view AS SELECT * from my_table;
ALTER TABLE my_table ADD COLUMN my_new_column <data and constraints>;

If you expect to do

1
SELECT my_new_column FROM my_view;

it sure is going to fail.

Solution: Either drop the view and recreate it:

1
2
DROP VIEW my_view;
CREATE VIEW my_view AS SELECT * from my_table;

or alter the view:

1
ALTER VIEW my_view (<list of all columns>) SELECT * FROM my_table;

Lesson: Do not forget about the dependent views when you change a table’s structure!

Comments