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
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)
| Feature | View | Index | Stored Procedure |
|---|---|---|---|
| What it is | A saved query (virtual table) | A lookup structure for fast searches | A saved block of SQL logic |
| Stores data? | No β runs the query each time | Yes β a copy of indexed columns | No β stores code, not data |
| Main benefit | Simplify and secure queries | Speed up data retrieval | Encapsulate reusable logic |
| Example use | Show active drivers with stats | Find deliveries by date faster | Assign 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
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?
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.