Database Schemas, Instances and Data Models
Understand database schemas, instances, and data models, including hierarchical, network, relational, ER, object-oriented, document, and graph models.
Introduction
In the previous article, we studied the Three-Schema Architecture and Data Independence, which explained how a database is organized at different levels.
Now, we need to understand three fundamental concepts used to describe databases: Schema, Instance, and Data Model.
These concepts help us distinguish between the structure of a database, the actual data stored in it at a particular time, and the methods used to organize that data.
1. Database Schema
A Database Schema is the overall structure or design of a database.
It defines how data is organized, including:
- Tables
- Attributes (columns)
- Data types
- Relationships
- Constraints
A schema describes the design of the database rather than the actual records stored inside it.
Example
Consider a student database with the following table structure:
Students
| Attribute | Data Type |
|---|---|
| Student_ID | Integer |
| Name | Varchar |
| Age | Integer |
| Department | Varchar |
This structure represents the schema of the Students table.
The schema defines what information can be stored and how it is organized.
Schema Definition Using SQL
CREATE TABLE Students (
Student_ID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Department VARCHAR(50)
);
This SQL statement defines the structure of the Students table.
It specifies the columns, their data types, and the primary key constraint.
The schema generally changes less frequently than the actual data stored in the database.
2. Database Instance
A Database Instance refers to the actual data stored in a database at a particular moment in time.
While the schema defines the structure, the instance represents the current contents of that structure.
Example
Using the Students table defined above:
| Student_ID | Name | Age | Department |
|---|---|---|---|
| 101 | Aamir | 20 | Computer Science |
| 102 | Sara | 21 | Mathematics |
| 103 | Umar | 19 | Physics |
These records represent the current instance of the Students table.
If another student is added, or if an existing student’s age is updated, the database instance changes.
However, the schema may remain exactly the same.
How an Instance Changes
Initially:
| Student_ID | Name | Age |
|---|---|---|
| 101 | Aamir | 20 |
| 102 | Sara | 21 |
After inserting another student:
| Student_ID | Name | Age |
|---|---|---|
| 101 | Aamir | 20 |
| 102 | Sara | 21 |
| 103 | Umar | 19 |
The database instance has changed because the actual records have changed.
The schema remains unchanged because the table structure is still the same.
3. Difference Between Schema and Instance
| Database Schema | Database Instance |
|---|---|
| Defines the structure of a database | Contains the actual data |
| Represents database design | Represents database contents at a particular time |
| Changes relatively less frequently | Changes frequently |
| Defines tables, attributes, constraints, etc. | Contains records stored in those tables |
Example: Students(Student_ID, Name, Age) |
Example: (101, Aamir, 20) |
Easy Way to Remember
Think of a database schema as the design of a form.
The instance is the information filled into that form.
The design may remain unchanged while the information entered into it changes every day.
4. What Is a Data Model?
A Data Model is a collection of concepts used to describe the structure of a database, the relationships between data, the constraints on data, and sometimes the operations used to manipulate it.
It provides a way to represent data and design databases.
Different data models organize information in different ways.
For example, some models organize data into tables, while others represent data using documents, graphs, or hierarchical structures.
5. Types of Data Models
Several data models are used in database systems.
5.1 Hierarchical Data Model
The Hierarchical Data Model organizes data in a tree-like structure.
Each parent can have multiple children, but each child generally has only one parent.
It represents relationships through parent-child connections.
Example:
University
|
+-- Department
| |
| +-- Student
| +-- Student
|
+-- Department
|
+-- Student
+-- Student
This model is suitable for representing hierarchical relationships.
5.2 Network Data Model
The Network Data Model represents data using a network of interconnected records.
Unlike the hierarchical model, a record can have multiple parent records, allowing more complex relationships.
For example, a student may enroll in multiple courses, and each course may contain multiple students.
This allows many-to-many relationships to be represented.
5.3 Relational Data Model
The Relational Data Model organizes data into tables consisting of rows and columns.
Each table represents an entity or a related collection of data.
Relationships between tables are commonly established using keys.
Example:
Students table:
| Student_ID | Name |
|---|---|
| 101 | Aamir |
| 102 | Sara |
Courses table:
| Course_ID | Course_Name |
|---|---|
| C01 | DBMS |
| C02 | Mathematics |
The relational model is one of the most widely used data models in modern database systems.
Examples of relational DBMS include MySQL, PostgreSQL, Oracle Database, and Microsoft SQL Server.
5.4 Entity-Relationship (ER) Model
The Entity-Relationship Model is used to represent database design conceptually.
It describes:
- Entities
- Attributes
- Relationships
For example, a university database may contain entities such as Student, Teacher, and Course.
A student may have a relationship with a course through enrollment.
ER diagrams help database designers visualize the structure before implementing it.
5.5 Object-Oriented Data Model
The Object-Oriented Data Model represents data as objects.
Objects contain data and may also contain associated methods or behaviors.
It uses concepts such as:
- Objects
- Classes
- Inheritance
- Encapsulation
This model is useful in systems where complex objects and object-oriented programming concepts are important.
5.6 Document Data Model
The Document Data Model stores data in document-like structures, often using formats such as JSON or BSON.
Documents can contain fields, nested objects, and arrays.
Example:
{
"student_id": 101,
"name": "Aamir",
"department": "Computer Science",
"courses": ["DBMS", "Python"]
}
This approach allows flexible data structures.
MongoDB is a popular database system that uses a document-oriented model.
5.7 Graph Data Model
The Graph Data Model represents data using nodes and edges.
- Nodes represent entities.
- Edges represent relationships between entities.
For example, in a social network:
- A person is represented as a node.
- A friendship is represented as an edge.
Graph databases are particularly useful when relationships between data are important.
6. Classification of Data Models
Data models are often classified into different categories based on their level of abstraction.
Conceptual or High-Level Data Models
These models describe data from the perspective of users and database designers.
They focus on entities, attributes, relationships, and requirements.
Example: Entity-Relationship (ER) Model.
Logical or Representational Data Models
These models describe data in a form that can be implemented in a DBMS, while hiding many physical storage details.
Examples:
- Relational Model
- Hierarchical Model
- Network Model
Physical or Low-Level Data Models
These models describe how data is physically stored in the computer system.
They include details such as:
- File structures
- Storage formats
- Indexes
- Record placement
- Access paths
They focus on the physical organization of data.
Article in a Nutshell
A Database Schema defines the structure of a database, while a Database Instance represents the actual data stored at a particular moment.
A Data Model provides concepts for describing how data is organized, related, constrained, and managed.
Major data models include hierarchical, network, relational, entity-relationship, object-oriented, document, and graph models.
Data models can also be classified into conceptual, logical, and physical levels based on their abstraction.
In the next article, we will study the Entity-Relationship Model: Entities, Attributes and Relationships, which will help us understand how to design databases before implementing them.
Quick quiz
5 questionsOriginally published at farhanpaal.com — https://farhanpaal.com/articles/database-schemas-instances-and-data-models/
Keep a copy
Read offline as a printout or PDF.
farhanpaal.com