Notes
Chapter 11: Views

1. What is a View?

In Microsoft SQL Server, a view is a virtual table created by defining a query in the database. Unlike physical tables, views do not store data themselves; instead, they display data from one or more underlying tables. Views can be queried like regular tables, making them a powerful tool for data manipulation and security.

2. Creating and Using Views

Creating a Simple View:

Example :

Creating a View
CREATE VIEW ActiveEmployees
AS SELECT EmployeeID, FirstName, LastName
FROM Employees WHERE IsActive = 1;

In this example, a view named "ActiveEmployees" is created to display active employees' data from the "Employees" table.

Using a View:

Example :

querying a view
SELECT * FROM ActiveEmployees;

You can query the "ActiveEmployees" view just like a regular table, simplifying complex queries and improving query readability.

3. Advantages of Views

Benefits of Using Views:

  • Simplified Queries: Views abstract the underlying complexity of SQL queries, making them easier to read and write.
  • Data Security: Views can restrict access to specific columns, providing data security by exposing only relevant information to users.
  • Data Abstraction: Views allow you to present data in a more logical or business-oriented manner, abstracting away complex joins or transformations.
  • Consistency: Views ensure consistent access to data, reducing the risk of errors in queries.

Example :

Using a View to restrict access
CREATE VIEW CustomerData
AS SELECT CustomerID, FirstName, LastName,Email
FROM Customers;

4. Real-world View Usage:

  • Order History: Create a view that combines data from "Orders," "Products," and "Customers" tables to provide a comprehensive order history for customers.
  • Sales Dashboard: Build views to summarize and visualize sales data, making it easier for managers to track performance.
  • Employee Directory: Use views to present employee details while hiding sensitive information like Social Security numbers.
  • Salary Reports: Create views for generating salary reports, ensuring that only authorized personnel can access compensation data.

5. Key Takeaways:

  • A view is a virtual table that displays data from one or more underlying tables.
  • Views simplify complex queries, enhance data security, and provide data abstraction.
  • Real-world scenarios benefit from views for presenting data logically and ensuring consistent access.

6. Exercise Questions:

  1. Create a view named "HighValueProducts" that displays products with prices greater than $100 from a "Products" table.
  2. Explain how views can enhance data security in a healthcare database by hiding sensitive patient information.
  3. Design a view to summarize monthly sales data from an "Orders" table, showing total sales for each month.
  4. Describe the advantages of using views to provide an employee directory in a large organization.
  5. In a library database, create a view that combines data from "Books," "Authors," and "Borrowers" tables to show a list of borrowed books along with borrower details.
  6. Discuss the performance implications of using views, especially when dealing with large datasets and complex queries.
Naming Conventions and Their Impact

Naming conventions in Microsoft SQL Server are essential for creating and maintaining a well-organized and understandable database. Following best practices for naming can improve collaboration among database developers, administrators, and application developers.

Read More
Data Insertion Techniques

The INSERT statement in Microsoft SQL Server is used to add new rows to a table. There are several variations of the INSERT statement to accommodate different data insertion scenarios.

Read More