Class 1 | Week 12 | DAT601

Week twelve notes have been released which are summed up as:

Milestone Two feedback

SQL server permissions and views

Introduction to milestone Three, how dig is the database?

In SQL, a view is a virtual table based on the result-set of an SQL statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.

Views can also be thought of as virtual tables, they behave like tables but are derived from queries themselves, views are saved within databases and indexed. This allows them to be used much in the same way as regular tables.

How do I create a view?

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Once a view is created using this code then it is stored in the database for reuse.

Views may be updated to add additional columns as required.

A VIEW is generally used:

To present the information data from a different point of view;
To simplify the access to the information of a high-level complexity of the database schema;
To prevent direct access to database tables (for security purposes)(less popular these days because of ORM tools);
Some "reality" is just simpler with a VIEW.

If you alter a view you WILL lose all your indexes to this view in the process.

Updating through views is possible, derived columns cannot be updated, it is also important to note that you cant derive a column from another derived column.

UPDATE vwCategoriesProducts
 SET CategoryName = 'Drinks'
 WHERE ProductID = 1

Renaming views is possible although it should be approached with caution as anything dependant on knowing the name of this view may break considering that it wouldn't be able to find the view its dependant on, Renaming is essentially just dropping and creating again under a different name, this also means that indexes have to be done again.


Comments

Popular posts from this blog

Class 2 | Week 9 | DAT601

Class 2 | Week 2 | DAT601

Class 1 | Week 14 | DAT601