5. Views
Views are virtual tables that are created from some filters or relationships. They save us time and simplify certain queries.
To create a view we will use:
CREATE VIEW [nombre de vista] AS SELECT [Columnas] FROM [tabla] WHERE [condicionales] ORDER BY [columna] ASC/DESC LIMIT [posición] OFFSET [número de filas];
And to delete it.
DROP VIEW [nombre de vista];
In the following example I am going to create a view of the songs (Track) of the Jazz genre.
CREATE VIEW tracks_jazz AS SELECT * FROM Track WHERE GenreId = (SELECT GenreId FROM Genre WHERE Name = 'Jazz');
Now I can run queries.
SELECT * FROM tracks_jazz;
63 Desafinado 8 1 2
64 Garota De Ipanema 8 1 2
65 Samba De Uma Nota Só (One Note Samba) 8 1 2
66 Por Causa De Você 8 1 2
...
Or even applying filters. Let's show all the songs (Track) that start with S.
SELECT * FROM tracks_jazz WHERE Name LIKE 'S%';
65 Samba De Uma Nota Só (One Note Samba) 8 1 2 137273 4535401
70 Se Todos Fossem Iguais A Você (Instrumental) 8 1 2 134948 4393377
124 Snoopy's search-Red baron 13 1 2 Billy Cobham 456071 15075616
125 Spanish moss-"A sound portrait"-Spanish moss 13 1 2 Billy Cobham 248084 8217867
127 Stratus 13 1 2 Billy Cobham 582086 19115680
...
Activity 1
From the Customer table create a view called Customer_with_companies, which will include all the results except when Company is NULL. From the view, perform the following actions.
- Order the results by alphabetical order of
Company. - Show which companies are from
Brazil.
This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.
Building SPAs with Django and HTML Over the Wire: Learn to build real-time single page applications with Python
The HTML over WebSockets approach simplifies single-page application (SPA) development and lets you bypass learning a JavaScript rendering framework such as React, Vue, or Angular, moving the logic to Python. This web application development book provides you with all the Django tools you need to simplify your developments with real-time results.
Buy the bookHelp me keep writing
Every coffee gives me a push toward the next article.
Sure, it's on me!
Comments
There are no comments yet.