DEV Community

Cover image for From SQL to Object-Oriented Databases: Navigating the Evolution of Database Models
Kaweesha Marasinghe
Kaweesha Marasinghe

Posted on

From SQL to Object-Oriented Databases: Navigating the Evolution of Database Models

When we talk about databases, SQL often comes to mind. Most people are familiar with SQL and relational databases, which use tables to organize and store data. But have you ever heard of Object-Oriented Databases? ๐Ÿค”

At first glance, the term may seem unfamiliar. We all know about Object-Oriented Programming (OOP), but a database based on OOP concepts? Sounds new, right? ๐Ÿคทโ€โ™‚๏ธ

Well, you're partially right! Object-oriented databases do indeed borrow principles from OOP, enhancing the capabilities of traditional databases and significantly boosting their performance. But how? Letโ€™s dive in! ๐Ÿš€

What are Relational Databases? ๐Ÿ“Š

Relational databases store data in tables, similar to how you would organize information in an Excel spreadsheet. Each column holds a specific type of data (like numbers, text, or dates), and each row represents a unique record.

Limitations of Relational Databases โš ๏ธ

However, as Benjamin says in "Who Am I" (2016): "No system is perfect." Relational databases have their own set of issues:

  • Struggle with Multimedia Data:
    Relational databases find it difficult to handle large, unstructured data like images and videos, often storing them as files and referencing them with keys, which can slow down performance.

  • Set-Value Attributes:
    When you need to store multiple values in one field (e.g., a list of skills for an employee), relational databases struggle. This requires creating extra tables or complex structures to manage such data.

  • Hierarchical Data:
    Data with parent-child relationships (like organizational charts or family trees) doesn't fit neatly into tables, making it hard to represent complex structures efficiently.

To tackle these limitations, object-oriented databases emerged as a new breed. ๐Ÿงฌ

What are Object-Oriented Databases? ๐Ÿ› ๏ธ

An Object-Oriented Database (OODB) is a database system that stores data in the form of objects, similar to how objects are handled in Object-Oriented Programming (OOP) languages like Java or C++. This makes OODBs ideal for applications that work with complex data and relationships.

A Relational Database (RDBMS) stores data in tables with rows and columns, and managing complex or hierarchical relationships often requires creating multiple tables, foreign keys, and applying normalization. This can make the system more complex, especially when dealing with data that doesnโ€™t naturally fit into tables, such as nested objects or relationships like many-to-many.

For example, imagine you have a Student table with attributes like name, age, multiple phone numbers, and a list of certificates the student is enrolled in. In a relational database, you would need to create multiple columns for each attribute (including handling lists like phone numbers and certificates by normalizing the data into separate tables). You would also need to set up complex relationships to link these tables.

However, with an Object-Oriented Database, you can store the entire Student object โ€” including its name, age, phone numbers, and certificates โ€” as a single object within one column. This simplifies the process significantly, as you no longer need to manage multiple tables and relationships for the data. Instead, the complex data is encapsulated into a single object that can be stored and retrieved easily. ๐Ÿงฉ

However, object-oriented databases were also replaced by another breed:

Object-Relational Databases โšก

Why Object-Relational Databases Replaced Object-Oriented Databases (OODB)

  • Limited Adoption of OOP:
    OODB worked well with object-oriented languages but didnโ€™t work well with traditional procedural languages.

  • SQL Dependency:
    Many organizations still rely on SQL because moving from SQL to OODBs was challenging.

  • Performance Issues:
    While OODBs handled complex objects well, they sometimes struggled with high-performance needs in applications requiring simple, structured data.

  • Integration Challenges:
    Many organizations already had relational databases in place. Completely replacing these systems with OODBs was impractical and expensive.

These issues led to the emergence of a new database technology: Object-Relational Databases. ๐ŸŽฏ

What are Object-Relational Databases? ๐ŸŒ€

Object-relational databases were developed as a mixture of relational and object-oriented databases. They combined the best features of relational databases and OODBs.

Features of Object-Relational Databases

  • Maintain the Standard Relational Model:
    They allow the creation of custom data types, nested data, and user-defined functions while still being compatible with SQL.

  • SQL Compatibility:
    This extension of SQL provides the ability to query both complex data as well as traditional data with a well-established standard.

  • Gradual Adoption:
    Organizations could keep their relational databases while gradually introducing object-oriented features.


Examples of Database Models ๐Ÿ“š

1. Relational Model (RDBMS)

Scenario: Storing information about a Student and their Courses.

Example: Relational Model

Storing information about Students and Courses.

Students Table

StudentID Name Age
1 Alice 21
2 Bob 22

Courses Table

CourseID CourseName
101 Mathematics
102 Computer Science

StudentCourses (Join Table)

StudentID CourseID
1 101
1 102
2 101

Characteristics:

  • Data is stored in flat, structured tables.
  • Relationships are managed using foreign keys and joins.
  • Querying is done using SQL.

2. Object-Oriented Model (OODB)

Scenario: Representing a Student as an object with nested attributes.

Object Representation:

{
  "StudentID": 1,
  "Name": "Alice",
  "Age": 21,
  "Courses": [
    {"CourseID": 101, "CourseName": "Mathematics"},
    {"CourseID": 102, "CourseName": "Computer Science"}
  ]
}
Enter fullscreen mode Exit fullscreen mode

Characteristics:

  • Data is stored as objects, similar to programming languages like Java or Python.
  • No need for separate tables or joins.
  • Methods (e.g., getCourseList() or calculateAge()) can be attached to objects.
  • Querying is done using object-oriented APIs instead of SQL.

3. Object-Relational Model (ORD)

Scenario: Combining relational structure with object-oriented features.

PostgreSQL Example:

  • Define a custom type for Courses:
CREATE TYPE Course AS (
    CourseID INT,
    CourseName TEXT
);
Enter fullscreen mode Exit fullscreen mode
  • Create a Students table with a column for nested courses:
CREATE TABLE Students (
    StudentID SERIAL PRIMARY KEY,
    Name TEXT,
    Age INT,
    Courses Course[]
);
Enter fullscreen mode Exit fullscreen mode
  • Insert data:
INSERT INTO Students (Name, Age, Courses)
VALUES (
    'Alice', 
    21, 
    ARRAY[ROW(101, 'Mathematics')::Course, ROW(102, 'Computer Science')::Course]
);
Enter fullscreen mode Exit fullscreen mode
  • Query nested data:
SELECT Name, Age, Courses FROM Students WHERE Name = 'Alice';
Enter fullscreen mode Exit fullscreen mode

Characteristics:

  • Combines relational structure with object-oriented features like nested types and arrays.
  • Uses extended SQL to handle complex data types.
  • Provides a middle ground between RDBMS and OODB.

Summary Table

Model Structure Example Data
Relational Model Flat tables with relationships Students, Courses, Join tables
Object-Oriented Hierarchical, nested objects JSON-like objects or OOP classes
Object-Relational Tables with nested data and objects Arrays, custom types in tables

๐ŸŽ‰ Hope this helps clarify the different database models! Let me know if you have questions! ๐Ÿ˜Š

Top comments (0)