114532 Views
101685 Views
86270 Views
54891 Views
51137 Views
49962 Views
Level Up your CAD Skills
Operation Pico
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Robot Makers Almanac
High Five Bot
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
KevsRobots Learning Platform
84% Percent Complete
By Kevin McAleer, 6 Minutes
In this lesson, we’ll explore views and indexes, two powerful tools that improve database usability and performance. Views allow you to simplify complex queries by saving them as virtual tables, while indexes optimize query speed by improving data retrieval efficiency.
A view is a virtual table in SQL that is defined by a query. It doesn’t store data itself but provides a simplified representation of data from one or more tables. Views are useful for creating reusable query results, simplifying complex queries, and improving data security by limiting access to specific data.
You create a view using the CREATE VIEW statement. A view can be as simple or complex as necessary, depending on the query used to define it.
CREATE VIEW
CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;
Suppose we have an employees table with employee details:
employees
| emp_id | name | salary | dept_id | |--------|----------|--------|---------| | 1 | Alice | 50000 | 1 | | 2 | Bob | 60000 | 2 | | 3 | Charlie | 70000 | 1 |
Example: Create a view called high_earners for employees earning over $60,000.
high_earners
CREATE VIEW high_earners AS SELECT name, salary FROM employees WHERE salary > 60000;
Now, you can retrieve high earners using a simple SELECT on the high_earners view:
SELECT
SELECT * FROM high_earners;
To update a view’s definition, use the CREATE OR REPLACE VIEW statement. This will redefine the view without requiring you to drop it first.
CREATE OR REPLACE VIEW
Example: Update the high_earners view to include only employees earning over $65,000.
CREATE OR REPLACE VIEW high_earners AS SELECT name, salary FROM employees WHERE salary > 65000;
This command updates the high_earners view with the new salary threshold.
To remove a view from the database, use the DROP VIEW statement.
DROP VIEW
DROP VIEW view_name;
Example: Drop the high_earners view.
DROP VIEW high_earners;
An index is a database structure that improves the speed of data retrieval. It works like an index in a book, allowing the database to locate data more quickly. Indexes are particularly useful for large tables or columns frequently used in WHERE, JOIN, or ORDER BY clauses.
WHERE
JOIN
ORDER BY
Note: While indexes improve read performance, they can slow down write operations (e.g., INSERT, UPDATE, DELETE) because the index needs updating whenever the data changes.
INSERT
UPDATE
DELETE
To create an index, use the CREATE INDEX statement. You can create indexes on one or more columns to optimize queries on those columns.
CREATE INDEX
CREATE INDEX index_name ON table_name (column1, column2, ...);
Example: Create an index on the salary column of the employees table to speed up queries that filter by salary.
salary
CREATE INDEX idx_salary ON employees (salary);
This index helps optimize queries such as:
SELECT * FROM employees WHERE salary > 60000;
A composite index is an index on multiple columns. It’s useful when queries frequently use multiple columns in the WHERE clause.
Example: Create a composite index on dept_id and salary in the employees table.
dept_id
CREATE INDEX idx_dept_salary ON employees (dept_id, salary);
This index will optimize queries that filter by both dept_id and salary.
To remove an index from the database, use the DROP INDEX statement.
DROP INDEX
DROP INDEX index_name;
Example: Drop the idx_salary index.
idx_salary
DROP INDEX idx_salary;
Create a View: Create a view called engineering_staff that shows employees in the Engineering department.
engineering_staff
CREATE VIEW engineering_staff AS SELECT name, salary FROM employees WHERE dept_id = 1;
Query the View: Retrieve all data from the engineering_staff view.
SELECT * FROM engineering_staff;
Create an Index: Create an index on the dept_id column of the employees table.
CREATE INDEX idx_dept_id ON employees (dept_id);
Test the Index: Run a query that filters by dept_id and observe the improved performance.
SELECT * FROM employees WHERE dept_id = 1;
Drop the View: Drop the engineering_staff view.
DROP VIEW engineering_staff;
Here’s a quick summary of the key concepts and commands related to views and indexes:
CREATE VIEW view_name AS SELECT ...
CREATE INDEX index_name ON table (col)
CREATE INDEX index_name ON table (col1, col2)
Views and indexes are valuable tools for optimizing and simplifying your SQL workflows. In the next lesson, we’ll dive into SQL functions and expressions, which will further enhance your ability to manipulate and analyze data.
< Previous Next >