Course contentsShow
← Databases & SQL
Lesson 463 of 1,9535. Relational Schema ArchitectureFree lesson

Database Security at the Schema Level

Implement least-privilege principles by designing view-based layers for database access.

In this lesson, you will learn how to implement the principle of least privilege by using database views to restrict exactly what users can see and do within your schema.

The Principle of Least Privilege

In a well-designed database, users should only have access to the specific data required for their job function—no more, no less. While you might be tempted to grant a user direct access to an underlying table (like Employees), this is often dangerous. If that table contains sensitive columns like Salary or SSN, every application or user with access to that table can see that private data.

Designing View-Based Security Layers

The most effective way to secure a schema is to "hide" the base tables from general users and expose only Views. A View acts as a virtual table that presents a filtered or limited version of the data.

Imagine we have an Employees table with columns: EmployeeID, Name, Department, and Salary. To support an internal directory service that should never see salaries, we create a specialized view:

CREATE VIEW PublicEmployeeDirectory AS
SELECT EmployeeID, Name, Department
FROM Employees;

Now, instead of granting the directory service SELECT permissions on the Employees table, you grant those permissions only on the PublicEmployeeDirectory view. Even if the application is compromised, the sensitive Salary data remains protected because the database engine literally does not allow the query to access columns outside of that view.

Why This Matters

By layering your schema with views, you create a security abstraction. This allows you to:

  1. Restrict Columns: Block access to PII (Personally Identifiable Information).
  2. Restrict Rows: Use a WHERE clause in your view (e.g., WHERE Department = 'Sales') so users only see data relevant to their region or team.
  3. Simplify Interfaces: Provide a cleaner, simplified version of complex joined tables, reducing the chance that an application developer writes an insecure or inefficient query.

Security should be baked into the schema structure itself, ensuring that your data stays protected regardless of how many different applications connect to your database.

Key Takeaway: Use views to act as security gateways that mask sensitive columns and filter rows, ensuring users interact only with the data they absolutely need.