To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the use of functional cookies. For more information read this page.

Views 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
		
Provide feedback on this page
Comments are sent via email to me.