Database Reference
In-Depth Information
Analysis
This statement creates a view named productcustomers , which joins three
tables to return a list of all customers who have ordered any product. If you
were to SELECT * FROM productcustomers , you'd list every customer
who ordered anything.
To retrieve a list of customers who ordered product TNT2 , you can do the
following:
Input
SELECT cust_name, cust_contact
FROM productcustomers
WHERE prod_id = 'TNT2';
Output
+----------------+--------------+
| cust_name | cust_contact |
+----------------+--------------+
| Coyote Inc. | Y Lee |
| Yosemite Place | Y Sam |
+----------------+--------------+
Analysis
This statement retrieves specific data from the view by issuing a WHERE clause.
When MariaDB processes the request, it adds the specified WHERE clause to any
existing WHERE clauses in the view query so the data is filtered correctly.
As you can see, views can greatly simplify the use of complex SQL statements.
Using views, you can write the underlying SQL once and then reuse it as
needed.
Tip
Creating Reusable Views It is a good idea to create views that are not tied to specific
data. For example, the view created in this example returns customers for all products,
not just product TNT2 (for which the view was first created). Expanding the scope of the
view enables it to be reused, making it even more useful. It also eliminates the need for
you to create and maintain multiple similar views.
 
Search WWH ::




Custom Search