Lab 09: Views

Time: 30 minutes | Level: Foundations | DB: MySQL 8 / PostgreSQL 15

Overview

Create, replace, and use views as virtual tables. Explore updatable views, WITH CHECK OPTION, and query the information schema for view metadata.


Step 1: Setup

docker run -d --name mysql-lab -e MYSQL_ROOT_PASSWORD=rootpass mysql:8.0
for i in $(seq 1 30); do docker exec mysql-lab mysql -uroot -prootpass -e "SELECT 1" 2>/dev/null && break || sleep 2; done

docker exec mysql-lab mysql -uroot -prootpass << 'EOF'
CREATE DATABASE viewlab;
USE viewlab;

CREATE TABLE employees (
    emp_id     INT NOT NULL AUTO_INCREMENT,
    first_name VARCHAR(50) NOT NULL,
    last_name  VARCHAR(50) NOT NULL,
    dept_name  VARCHAR(50) NOT NULL,
    salary     DECIMAL(10,2) NOT NULL,
    email      VARCHAR(100),
    is_active  BOOLEAN DEFAULT TRUE,
    hire_date  DATE,
    PRIMARY KEY (emp_id)
);

CREATE TABLE sales (
    sale_id    INT NOT NULL AUTO_INCREMENT,
    emp_id     INT NOT NULL,
    amount     DECIMAL(10,2) NOT NULL,
    sale_date  DATE NOT NULL,
    region     VARCHAR(30),
    PRIMARY KEY (sale_id),
    FOREIGN KEY (emp_id) REFERENCES employees(emp_id)
);

INSERT INTO employees (first_name, last_name, dept_name, salary, email, hire_date) VALUES
('Alice',  'Johnson',  'Engineering', 95000,  '[email protected]',  '2020-03-15'),
('Bob',    'Smith',    'Marketing',   72000,  '[email protected]',    '2019-07-22'),
('Carol',  'Davis',    'Engineering', 110000, '[email protected]',  '2018-01-10'),
('David',  'Wilson',   'HR',          65000,  '[email protected]',  '2021-09-01'),
('Eve',    'Martinez', 'Engineering', 98000,  '[email protected]',    '2020-11-30'),
('Frank',  'Anderson', 'Marketing',   68000,  NULL,            '2022-02-14'),
('Grace',  'Taylor',   'HR',          70000,  '[email protected]',  '2019-05-20'),
('Henry',  'Thomas',   'Engineering', 125000, '[email protected]',  '2017-08-05');

INSERT INTO sales (emp_id, amount, sale_date, region) VALUES
(1, 5000, '2024-01-10', 'North'),
(2, 3500, '2024-01-15', 'South'),
(1, 4200, '2024-02-01', 'North'),
(3, 8000, '2024-02-10', 'East'),
(5, 6100, '2024-02-20', 'West'),
(2, 2800, '2024-03-01', 'South'),
(1, 3900, '2024-03-10', 'North'),
(6, 4500, '2024-03-15', 'East');
EOF

Step 2: CREATE VIEW

📸 Verified Output:

💡 A view is a stored SELECT query. It doesn't store data — it runs the query every time you SELECT from it. Views are great for:

  • Simplifying complex queries

  • Hiding sensitive columns

  • Providing consistent reporting interfaces


Step 3: CREATE OR REPLACE VIEW

📸 Verified Output:


Step 4: View Joining Multiple Tables

📸 Verified Output:


Step 5: Updatable Views

📸 Verified Output:

⚠️ Views with GROUP BY, HAVING, DISTINCT, UNION, or aggregates are NOT updatable. Attempting to UPDATE/INSERT/DELETE against them will error.


Step 6: WITH CHECK OPTION

📸 Verified Output (failing insert):

💡 WITH CHECK OPTION ensures that INSERT/UPDATE operations through the view cannot create rows that the view wouldn't show. This maintains data consistency through view boundaries.


Step 7: INFORMATION_SCHEMA.VIEWS

📸 Verified Output:

PostgreSQL equivalent:


Step 8: Capstone — Security View (Hide Sensitive Columns)

📸 Verified Output (public view):

Cleanup:


Summary

Command
Description

CREATE VIEW v AS SELECT ...

Create a view

CREATE OR REPLACE VIEW v AS ...

Replace view definition

SELECT * FROM v

Query a view like a table

UPDATE v SET ... WHERE ...

Update through updatable view

WITH CHECK OPTION

Enforce view filter on inserts/updates

DROP VIEW IF EXISTS v

Remove a view

INFORMATION_SCHEMA.VIEWS

List views and metadata

pg_views

PostgreSQL view catalog

Next: Lab 10 — Database Design and Normalization

Last updated