The ANSI/SPARC Three-Level Schema: A Pillar of Database Design and Flexibility

Satria Aluh Perwira Nusa
7 min readJul 30, 2023

--

In the world of database management, the ANSI/SPARC three-level schema, also known as the ANSI/SPARC architecture, stands as a foundational concept that empowers database designers and developers to create robust, adaptable, and efficient database systems. The architecture divides a database into three distinct layers — the external schema (user view), the conceptual schema (logical view), and the internal schema (physical view). Each layer serves a specific purpose and brings unique benefits to the table, allowing for data abstraction, data independence, and enhanced flexibility in the face of evolving requirements.

  1. External Schema (User View)

The external schema represents the user view of the database, offering tailored perspectives of the data for various user groups or applications. It acts as an intermediary between the database and the end-users, concealing underlying complexities and presenting relevant information in a user-friendly manner. By creating external schemas, developers can control user access to specific data elements, ensuring data security and privacy.

Imagine an e-commerce platform with diverse users, such as customers, employees, and managers. The external schemas for each group can display distinct data subsets that cater to their specific needs. For instance, customers may only see product names and prices, while employees may view additional details like cost prices and suppliers. This separation of concerns enhances data security and ensures a smooth user experience.

2. Conceptual Schema (Logical View)

The conceptual schema represents the logical or abstract view of the entire database, independent of any specific database management system or physical storage considerations. It serves as the blueprint for the database design, defining entities, attributes, and relationships between data elements. By creating this logical representation, the conceptual schema enables data independence, allowing changes to be made without affecting the external views or physical storage.

In our e-commerce example, the conceptual schema defines tables for products, customers, and orders, along with their relationships. This abstraction permits modifications to the database structure without disrupting the end-users or the underlying physical implementation. Thus, developers can make changes to the conceptual schema, such as adding new tables or altering relationships, while keeping the application layer unaffected.

3. Internal Schema (Physical View)

The internal schema represents the physical storage and implementation details of the database managed by the database management system (DBMS). It includes decisions related to data storage formats, indexing strategies, and data partitioning to optimize performance and efficiency.

Database administrators work closely with the DBMS to fine-tune the internal schema, optimizing data retrieval and storage. For instance, the internal schema may dictate the use of columnar storage for analytical queries, while employing row-oriented storage for transactional processing. Additionally, caching mechanisms and query optimization algorithms are implemented at this level to enhance system performance.

Example of ANSI/SPARC Three-Level Schema Using PostgreSQL:

To demonstrate the ANSI/SPARC three-level schema architecture. We’ll create a database for a fictional bookstore with three different user views: one for customers, one for employees, and one for managers.

Step 1: Create the Database and Tables

First, you’ll need to have PostgreSQL installed and running on your system. Then, using a PostgreSQL client (such as pgAdmin or psql), create a new database called “bookstore.”

CREATE DATABASE bookstore;

Next, we’ll create three tables: one for books, one for customers, and one for employees.

-- Table for books
CREATE TABLE books (
book_id SERIAL PRIMARY KEY,
title VARCHAR(100) NOT NULL,
author VARCHAR(100) NOT NULL,
price NUMERIC(8, 2) NOT NULL
);

-- Table for customers
CREATE TABLE customers (
customer_id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL
);
-- Table for employees
CREATE TABLE employees (
employee_id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
position VARCHAR(100) NOT NULL
);

Step 2: Define External Schemas (User Views)

Now, let’s define the external schemas to provide specific views for different user groups.

-- External schema for customers
CREATE VIEW customer_view AS
SELECT book_id, title, author, price
FROM books;

-- External schema for employees
CREATE VIEW employee_view AS
SELECT book_id, title, author, price
FROM books;

-- External schema for managers
CREATE VIEW manager_view AS
SELECT book_id, title, author, price, name AS customer_name, email AS customer_email
FROM books
JOIN customers ON books.book_id = customers.customer_id;

Step 3: Define Conceptual Schema

We already have the conceptual schema represented by the tables created in Step 1.

Step 4: Define Internal Schema

The internal schema, representing the physical storage, is handled by PostgreSQL itself, and we do not explicitly create it. PostgreSQL internally manages the physical organization of the tables and data.

Step 5: Test the User Views

Now, let’s test the user views to see how different user groups will interact with the database:

1 .Customers View:

SELECT * FROM customer_view;

2. Employees View:

SELECT * FROM employee_view;

3. Managers View:

SELECT * FROM manager_view;

The customers view and employees view will show the same data from the books table, while the managers view will include additional customer information.

This is a simplified example to illustrate the concept of the ANSI/SPARC three-level schema architecture using PostgreSQL. In real-world scenarios, we would have more complex data and application logic. The architecture helps manage the complexity by separating concerns and providing different views for different user groups without directly exposing the underlying database structure.

Two types of Independence provided by ANSI/SPARC three-level schema architecture

The ANSI/SPARC three-level schema architecture provides two types of independence: logical data independence and physical data independence. Let’s explore each type in detail:

Logical and Physical Data Independence
  1. Logical Data Independence: Logical data independence refers to the ability to make changes to the conceptual schema (logical level) without impacting the external schemas (user views) or the application layer. In other words, it allows modifications to the logical representation of the data without requiring changes to the way data is presented to end-users or applications.
    The goal of logical data independence is to ensure that the underlying data structure can evolve and adapt over time without disrupting the external views or applications that interact with the database. This independence is crucial for maintaining system flexibility and reducing the risk of cascading changes when the database schema is modified.
    For example, consider an e-commerce platform with an existing conceptual schema that includes a “products” table with attributes like “product_id,” “product_name,” and “price.” If the platform decides to introduce a new attribute, “discount_percentage,” to the “products” table to accommodate periodic discounts, this is a logical change. The addition of the new attribute can be implemented at the conceptual schema level without requiring any modifications to the existing external schemas (customer view, employee view, etc.). The external schemas will continue to function correctly, and the applications built on top of them will remain unaffected.
  2. Physical Data Independence: Physical data independence refers to the ability to make changes to the internal schema (physical level) without impacting the conceptual schema (logical level) or the external schemas (user views). It allows modifications to the physical storage and implementation details of the database without affecting the way data is logically represented or presented to end-users.
    The primary purpose of physical data independence is to enable database administrators and system architects to optimize the database performance and storage without disrupting the external interfaces or application layer. It provides the flexibility to choose different storage formats, indexing strategies, or caching mechanisms while maintaining data integrity and consistency.
    Continuing with the e-commerce example, suppose the platform decides to switch from a row-oriented storage format to a columnar storage format for the “products” table to improve analytical query performance. This change is a physical modification that can be made at the internal schema level without requiring any changes to the conceptual schema or the external schemas. The underlying data remains logically represented in the same way, and the external views will continue to function as expected.

By providing both logical data independence and physical data independence, the ANSI/SPARC three-level schema architecture enables a higher degree of flexibility, adaptability, and maintainability for complex database systems. It empowers database professionals to make changes and improvements at different levels of the architecture while minimizing the impact on the overall system and the applications that rely on it.

Conclusion

The ANSI/SPARC three-level schema architecture serves as a cornerstone of modern database design, enabling database systems to be flexible, scalable, and maintainable. By separating the user view (external schema), the logical representation (conceptual schema), and the physical storage (internal schema), the architecture empowers developers and administrators to make changes independently, while preserving data abstraction and independence.

Understanding the ANSI/SPARC three-level schema equips database professionals with the tools to design and manage complex database systems effectively. It fosters a systematic approach to database development, ensuring clear separation of concerns and providing a solid foundation for building resilient and user-friendly applications. As data continues to play a pivotal role in shaping industries, the ANSI/SPARC architecture will remain a vital concept, guiding the evolution of database management systems into the future.

--

--

Satria Aluh Perwira Nusa
0 Followers

Hi, I'm Satria Aluh Perwira Nusa, a backend and database engineer