'Security Cameras' (Views)

The "Security Cameras" (Views)
Doña Pepa has hired her first employee: Carlos, the cashier.
For Carlos to be able to ring up items, he needs access to the database to see how much Bread and Milk cost. However, in the Products table, Doña Pepa also has written down how much she pays the suppliers (her secret costs), and in the Cash table she has all her accumulated money.
The problem: If she gives Carlos access to the database, he could see all of the store's financial secrets!
The One-Way Mirror (What is a View?)
Instead of giving him access to the real table (which is locked behind a security vault), Doña Pepa creates a one-way mirror for Carlos. This mirror is a screen that ONLY shows the data Pepa wants him to see, hiding the rest.
In SQL, this is called a View. It is a virtual table formed from a predefined query.
sql-- 1. Doña Pepa (the boss) creates the View: CREATE VIEW Cashier_View AS SELECT product_name, retail_price, stock FROM Products;
What happened here?
The view Cashier_View now exists as if it were a normal table, but it DOES NOT contain the supplier cost price.
Using the View
Now, Carlos's permission to read the Products table is revoked, and he is only given permission to read the Cashier_View. When Carlos wants to look up the price of milk, he simply runs a normal query, but against the view:
sql-- 2. Carlos (the cashier) queries the view: SELECT * FROM Cashier_View WHERE product_name = 'Milk';
The database, in that millisecond, goes into the secure vault, extracts only the name, price, and stock of the Milk, and shows it to Carlos at the counter. Doña Pepa's secrets are 100% safe.
Another superpower of Views:
Do you remember those long and horrible JOINS from Lesson 6 that linked 3 tables at once? You can save them entirely inside a View called Complete_Receipts. That way, instead of writing 10 lines of code every time you want to see a receipt, you just write SELECT * FROM Complete_Receipts.
In the next lesson, we will move from the console to the web, understanding how to make a Counter connect to this Vault with Client-Server Architecture.
