Intermediate SQL Server Concepts

Working with Views

Views are virtual tables based on the result-set of a SQL statement. They can simplify complex queries, enhance security by restricting data access, and provide a consistent interface to data.

  • Creating and managing views
  • Understanding indexed views
  • Using views for data abstraction

Example: Creating a simple view

CREATE VIEW CustomerOrderSummary AS
SELECT
    c.CustomerID,
    c.CompanyName,
    COUNT(o.OrderID) AS TotalOrders,
    SUM(od.Quantity * od.UnitPrice) AS TotalAmount
FROM
    Customers c
JOIN
    Orders o ON c.CustomerID = o.CustomerID
JOIN
    "Order Details" od ON o.OrderID = od.OrderID
GROUP BY
    c.CustomerID, c.CompanyName;
Explore View Tutorials

Stored Procedures

Stored procedures are precompiled SQL code that can be executed on a database server. They offer benefits such as improved performance, enhanced security, and code reusability.

  • Defining and executing stored procedures
  • Parameters and return values
  • Error handling within stored procedures

Example: A stored procedure to get customer orders

CREATE PROCEDURE GetCustomerOrders (@CustomerID INT)
AS
BEGIN
    SELECT
        o.OrderID,
        o.OrderDate,
        SUM(od.Quantity * od.UnitPrice) AS OrderTotal
    FROM
        Orders o
    JOIN
        "Order Details" od ON o.OrderID = od.OrderID
    WHERE
        o.CustomerID = @CustomerID
    GROUP BY
        o.OrderID, o.OrderDate;
END;
Learn More About Stored Procedures

Triggers

Triggers are special types of stored procedures that automatically execute or fire in response to certain events on a particular table or view in a database.

  • Understanding DML and DDL triggers
  • Writing trigger logic for data integrity
  • Managing trigger performance

Example: An AFTER INSERT trigger to log changes

CREATE TRIGGER LogProductPriceChange
ON Products
AFTER UPDATE OF UnitPrice
AS
BEGIN
    IF UPDATE(UnitPrice)
    BEGIN
        INSERT INTO ProductPriceLog (ProductID, OldPrice, NewPrice, ChangeDate)
        SELECT
            i.ProductID,
            d.UnitPrice,
            i.UnitPrice,
            GETDATE()
        FROM
            inserted i
        JOIN
            deleted d ON i.ProductID = d.ProductID
        WHERE
            i.UnitPrice <> d.UnitPrice;
    END
END;
Discover Trigger Functionality

Transactions and Concurrency Control

Understanding how to manage database transactions ensures data integrity and consistency, especially in multi-user environments. This includes concepts like ACID properties and locking mechanisms.

  • ACID properties (Atomicity, Consistency, Isolation, Durability)
  • Transaction isolation levels
  • Locking and deadlocks
Dive into Transactions

Indexes and Query Optimization

Proper indexing is crucial for fast query execution. This section covers different types of indexes and how the query optimizer uses them to retrieve data efficiently.

  • Clustered vs. Non-Clustered Indexes
  • Index maintenance
  • Understanding execution plans
Optimize Your Queries