Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Part 5.3Views in SQL

Part 5.3Views in SQL

SQL offers the option of views. Views are virtual tables.

Creating a view

It's very easy to create a view in SQL, since the syntax is very similar to selection but with an additional part:

SQL
CREATE VIEW test_view AS
	SELECT surname, SHA1(surname) FROM `Attendees`
		

It's common to indent the statement under a view as above, but it is still one single SQL query.

Updating a view

Views can also be updated. Assuming the original data has changed and the view needs to be recreated based on the new data, the following SQL command will replace the current view with the updated information:

SQL
CREATE OR REPLACE VIEW test_view AS
	SELECT surname, SHA1(surname) FROM `Attendees`
		

The CREATE OR REPLACE VIEW will create a view or, if it already exists, replace it.

Removing a view

Views can, obviously, be deleted. Deleting is done with the DROP command.

SQL
DROP VIEW test_view
		
Feedback 👍
Comments are sent via email to me.