Relational databases compared to object-oriented databases
We take a closer look at the comparison between relational and object-oriented databases and provide a summary.
Top!
Hello
We take a closer look at the comparison between relational and object-oriented databases and provide a summary.
Relational databases (RDB) are called traditional databases. The simple structuring in tables, also called relations, has made this type of database popular especially in administrative areas. This was achieved with a limited structuring of the data, as well as a limited number of operations. These limitations are prerequisites for processing the data with Structured Query Language (SQL). In SQL there are character strings as data type, numeric values, date and time. Nowadays also large binary data objects (BLOB) are supported as in MySQL.
In the early 80s appeared inaccessibility for complex applications, such as multimedia applications [1]. The problems were complex object structures or how to store multimedia data and define non-application specific operations. Object-oriented databases (OODB) were intended to solve these problems. The increasing use of object-oriented programming languages and the possibility to extend them with a DB have supported the development of OODB.
In OODB, attributes and operations can be encapsulated in the object class definition. This means that often operations that modify an object are encapsulated. In addition, inheritance and operator overloading are also supported.
Unlike tables in an RDB, objects are persisted. In order to store an object, it should have a unique object object identification and all referenced objects of a persistent object must also be persistent. The object identifier is similar to the primary key in relational databases. Even if an object ID, as in the OODB realm is not mandatory, it simplifies the search and is required when calling the Upsert method. The method Upsert method checks if an object exists and performs either an update or insert.
Another popular OODB is ObjectBox, which is also called a NoSQL database. This means a database that does not relational approach. In a simple example, the possible inheritance with the help of an OODB is demonstrated here.
Code snippet 1.1: Base class in object box:
@BaseEntity
abstract class Dinosaur {
@Id
var id: Long = 0
var name: String? = null
var length: Int? = null
var weight: Int? = null
constructor()
constructor(name: String?, length: Int?, weight: Int?) {
this.name = name
this.length = length
this.weight = weight
}
abstract fun getNutrition(): List<*>
}
In the above Code Snippet 1.1, in the Kotlin programming language, an Abstract Class "Dinosaur" is defined. From this base class can then inherit e.g. 'Herbivores' see Snippet 1.2 or 'Carnivores". The class "Herbivore" has an additional attribute "Nutrition" with "Plants".
Code snippet 1.2: object herbivore inheriting from base class dinosaur with a reference to a list of plants as food
@Entity
class Herbivore: Dinosaur {
lateinit var nutritionList: ToMany<Plant>
constructor()
constructor(name: String, laenge: Int, gewicht: Int)
: super(name, laenge, gewicht)
override fun getNutrition(): List<Plant> {
return nutritionList
}
}
@Entity
data class Plant(
@Id
var id: Long = 0,
var name: String? = null
)
An object can then be persisted with a box, see code snippet 1.3. A box is required in ObjectBox context to retrieve or persist objects of a type. Currently there is no support for querying with a base class to get inheriting classes. Relations in the base class are also not supported. In the example, the class "dinosaur" might not have a list for feeding.
Code snippet 1.3: Example persistence of an object in ObjectBox:
val dinosaursBox = ObjectBox.store.boxFor(Herbivore::class.java)
val brachiosaurus = Herbivore("Brachiosaurus", 23, 28)
brachiosaurus.nutritionList.addAll(listOf(Plant(name = "Farn"), Plant(name = "Moos")))
dinosaursBox.put(brachiosaurus)
In Room, a modern RDB, a mapping from a table to an object is used with an entity annotation. Room supports SQL queries, while ObjectBox uses its own query API. Inheritance is also possible in Room. It also makes sense to define an interface to support another database system later.
The decision which database to choose for an application depends among:
by Antonia
2024-07-18
by Karl
2020-03-19