+-------+---------+Using CONCAT we can combine the first and last names together:
| first | last |
+-------+---------+
| John | Citizen |
| Jane | Smith |
+-------+---------+
SELECT CONCAT(first,' ',last) FROM names;
which gives, for example:
+------------------------+If we frequently need to do such queries, we can create a view:
| CONCAT(first,' ',last) |
+------------------------+
| John Citizen |
| Jane Smith |
+------------------------+
CREATE VIEW full_names AS SELECT CONCAT(first,' ',last) FROM names;
This creates a virtual table called full_names (it's called a virtual table because it's defined in terms of another table through a query). The view can which can be used in the following way:
SELECT * FROM full_names;
which gives:
+------------------------+We therefore no longer need to perform the SELECT statement using CONCAT.
| CONCAT(first,' ',last) |
+------------------------+
| John Citizen |
| Jane Smith |
+------------------------+
To obtain information about a view:
SHOW CREATE VIEW view_name;
A view can be deleted in the following way:
DROP VIEW view_name;
We'll now consider a slightly more complicated example. Consider two tables, books:
+--------------------------------+--------------+------+and publishers:
| title | author | id |
+--------------------------------+--------------+------+
| Elementary Mechanics | Bob Smith | 1 |
| Popular Science | Reg Hammer | 2 |
| Beginner's Guide to Mechanics | R. G. Wesley | 3 |
| Advanced Statistical Mechanics | O. R. Ange | 4 |
+--------------------------------+--------------+------+
+---------------+------+We can use a join to obtain a list of book titles, authors and publishers:
| publisher | id |
+---------------+------+
| Smith & Smith | 1 |
| Davidson's | 2 |
| Davidson's | 3 |
| Smith & Smith | 4 |
+---------------+------+
SELECT title, author, publisher FROM books, publishers WHERE books.id = publishers.id;
which gives:
+--------------------------------+--------------+---------------+Instead of performing this join whenever we want to obtain this data, we could create a view:
| title | author | publisher |
+--------------------------------+--------------+---------------+
| Elementary Mechanics | Bob Smith | Smith & Smith |
| Popular Science | Reg Hammer | Davidson's |
| Beginner's Guide to Mechanics | R. G. Wesley | Davidson's |
| Advanced Statistical Mechanics | O. R. Ange | Smith & Smith |
+--------------------------------+--------------+---------------+
CREATE VIEW bookshop AS SELECT title, author, publisher FROM books, publishers WHERE books.id = publishers.id;
Now we can just do this:
SELECT * FROM bookshop;
which gives the same set of results:
+--------------------------------+--------------+---------------+As expected, we can use a WHERE clause in the normal way:
| title | author | publisher |
+--------------------------------+--------------+---------------+
| Elementary Mechanics | Bob Smith | Smith & Smith |
| Popular Science | Reg Hammer | Davidson's |
| Beginner's Guide to Mechanics | R. G. Wesley | Davidson's |
| Advanced Statistical Mechanics | O. R. Ange | Smith & Smith |
+--------------------------------+--------------+---------------+
SELECT * from bookshop WHERE publisher = "Davidson's";
which would give:
+-------------------------------+--------------+------------+It's important to note that if the data in the original tables changes, the data in the view changes as well. Views can of course be much more complicated, and include calculations and aggregate functions.
| title | author | publisher |
+-------------------------------+--------------+------------+
| Popular Science | Reg Hammer | Davidson's |
| Beginner's Guide to Mechanics | R. G. Wesley | Davidson's |
+-------------------------------+--------------+------------+
It should be clear how useful views are. One of which is that users can perform simple SELECT queries on a view, rather than having to perform joins on multiple tables. A user can even be given permission only to have access to a view and not the underlying tables.
No comments:
Post a Comment