Impact of Table Definition Changes on “Views”

Look at the below statement. 

CREATE VIEW vwTest
AS
SELECT * FROM TestTable;

A simple statement to create a View, right?

Assume that “TestTable” has just three columns on which a View is built. What happens if we add one more column to the base table, i.e. TestTable? Is it feasible that the newly inserted column will appear in the view? Will changes to the database schema affect the View definition?

The answer is, No.

Let’s check it out.

Creating a table
CREATE TABLE TestTable (id INT,col1 STRING, col2 STRING);

Inserting few rows

INSERT INTO TestTable VALUES (1,’text1′,’data1′), (2,’text2′,’data2′);

Creating a view

CREATE VIEW vwTest AS SELECT * FROM TestTable;

Altering table definition

ALTER TABLE TestTable ADD COLUMNS (col3 STRING);

Check the view definition and data. 

DESC vwTest;
DESC TestTable;

This slideshow requires JavaScript.


So, modifications to the table’s definition will have no effect on Views. Hence, an ALTER VIEW statement must be issued in order for the table definition changes to be reflected in the View.

Altering the view definition.
ALTER VIEW vwTest AS SELECT * FROM TestTable;

Verification:
DESC vwTest;

Hope you find this article helpful.

Please subscribe for more interesting updates.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s