Domain 2 β€” Module 4 of 7 57%
11 of 27 overall
Domain 2: Relational Data on Azure Free ⏱ ~10 min read

Database Objects: Views, Indexes & More

Tables are just the beginning. Relational databases contain views, indexes, stored procedures, and more β€” each serving a specific purpose.

Beyond tables

Simple explanation

A database isn’t just tables. It has extra objects that make it faster, safer, and easier to use.

Think of a library: the books are the data (tables). But the library also has a catalogue (index) for finding books quickly, a reading list (view) that shows a curated selection, and standard procedures (stored procedures) that librarians follow for common tasks.

These extra objects don’t store new data β€” they help you work with existing data more effectively.

Common database objects

Views

A view is a saved SQL query that acts like a virtual table. It doesn’t store data β€” it runs the query every time you access it.

-- Create a view showing active drivers with their delivery count
CREATE VIEW ActiveDriverSummary AS
SELECT d.Name, d.LicenceClass, COUNT(del.DeliveryID) AS TotalDeliveries
FROM Drivers d
JOIN Deliveries del ON d.DriverID = del.DriverID
WHERE d.IsActive = 1
GROUP BY d.Name, d.LicenceClass;

-- Use the view like a table
SELECT * FROM ActiveDriverSummary;

Why use views?

  • Simplify complex queries β€” write the join once, reuse it as a simple table name
  • Security β€” show users specific columns without exposing the full table
  • Consistency β€” everyone uses the same query logic

Indexes

An index is a data structure that speeds up data retrieval β€” like the index at the back of a textbook. Instead of reading every page to find a topic, you look it up in the index and go directly to the right page.

-- Create an index on the Deliveries table for faster date lookups
CREATE INDEX idx_delivery_date ON Deliveries (Date);

Key points:

  • Indexes speed up SELECT queries dramatically
  • They slow down INSERT/UPDATE/DELETE slightly (the index must be updated too)
  • The database automatically creates an index on the primary key
  • Add indexes on columns frequently used in WHERE and JOIN clauses

Stored procedures

A stored procedure is a saved block of SQL that you can call by name. Think of it as a reusable function.

-- Create a stored procedure
CREATE PROCEDURE AssignDriver
  @DeliveryID VARCHAR(10),
  @DriverID VARCHAR(10)
AS
BEGIN
  UPDATE Deliveries SET DriverID = @DriverID WHERE DeliveryID = @DeliveryID;
  UPDATE Drivers SET IsActive = 0 WHERE DriverID = @DriverID;
END;

-- Call it
EXEC AssignDriver @DeliveryID = 'DEL-504', @DriverID = 'D001';

Why use stored procedures?

  • Encapsulate logic β€” complex operations in one callable unit
  • Performance β€” precompiled and optimised by the database
  • Security β€” users can run the procedure without direct table access
  • Consistency β€” the same business logic every time

Functions

Functions are similar to stored procedures but return a value. They can be used inside SQL queries.

  • Scalar functions β€” return a single value
  • Table-valued functions β€” return a result set (like a view with parameters)
Views vs indexes vs stored procedures
FeatureViewIndexStored Procedure
What it isA saved query (virtual table)A lookup structure for fast searchesA saved block of SQL logic
Stores data?No β€” runs the query each timeYes β€” a copy of indexed columnsNo β€” stores code, not data
Main benefitSimplify and secure queriesSpeed up data retrievalEncapsulate reusable logic
Example useShow active drivers with statsFind deliveries by date fasterAssign a driver to a delivery
Exam tip: object identification

The exam describes a scenario and asks which database object to use:

  • β€œSpeed up queries that filter by date” β†’ Index
  • β€œGive analysts a simplified table without exposing raw data” β†’ View
  • β€œAutomate a multi-step business process in the database” β†’ Stored procedure
  • β€œReuse a calculation across multiple queries” β†’ Function

Flashcards

Question

What is a database view?

Click or press Enter to reveal answer

Question

What does a database index do?

Click or press Enter to reveal answer

Question

What is a stored procedure?

Click or press Enter to reveal answer

Knowledge check

Knowledge Check

Jake's CloudPulse database has a query that joins 5 tables to show customer usage reports. Multiple team members need to run this query daily. What should Jake create?

Knowledge Check

Tom's Deliveries table has 2 million rows. Queries that filter by delivery date are taking 15 seconds. What should the DBA create to improve performance?

Next up: Azure SQL: Your Database in the Cloud β€” let’s move from database concepts to real Azure services.