Object Oriented Databases (OODBs) • Relational and OO data models. • Advantages and Disadvantages of OO as compared with relational databases. 1 A Database of Students and Modules Student 0..* Student Number {PK} takes 0..* Module Module Code {PK} 2 Relational Model Student Module Student Number {PK} Student Name Module Code {PK} Module Title 1..1 1..1 Takes Student Number {PK} 0..* Module Code {PK} 0..* 3 Relational Schema Student {Student Number, Student Name} Module {Module Code, Module Title} Takes {Student Number∗, Module Code∗} 4 Relational Model Each of these relations is implemented as a two dimensional table, whose columns are called attributes, and whose rows are called tuples or instances. Student Number 12486 43292 42403 52212 77123 Name Fred Jones Clare Adams Priscilla White J. Bigshott Clare Adams 5 Relational Model Module Code C66 C80 C89 ... ... ... Title Computing is Fun Extremely Difficult Computing Practical Business Computing ... ... ... 6 Relational Model Takes Code C66 C89 C66 C80 C40 C89 C66 C40 C42 C35 C42 C89 Number 43292 52212 12486 52212 43292 12486 52212 52212 77123 52212 52212 43292 7 Relational Model Queries can be written using relational algebra, relational calculus or SQL. Suppose mynumber is my student number. Then the query ”list the codes and titles of my modules” might look like this in SQL: SELECT FROM WHERE AND Module.code, Module.title Takes, Module Takes.number = mynumber Module.code = Takes.code 8 Relational Model or like this: SELECT Module.code, Module.title FROM Modules WHERE Module.code IN SELECT Takes.code FROM Takes WHERE Takes.number = mynumber 9 Relational Model – Terminology • Entity instance: represents an object of interest. For example, a student record is an entity instance that represents an individual student. • Entity class: a collection of entity instances with a common structure. For example, a whole collection of student records forms an entity class. • Attribute: a piece of interesting information, about the instances of an entitity class. For example, ’name’ is a fact about students that is represented as an attribute of all the instances of the class Student. 10 • In a relational data model, the identity of an entity instance is defined by the values of its attributes; no two entity instances can have the same attribute values. • Relationship: an association between entities. Entities are often identified by nouns in a requirements specification, and relationships by verbs. For example, ’takes’ indicates a relationship between Student and Module. Relationships can be between entity classes, or between entity instances. • Mathematically, a relation has a heading, which is a subset of the Cartesian product of a set of (attribute name, domain) pairs, and a body, which contains (attribute name, value) pairs. For example, Student could be represented as a heading, (student number, integer), (student name, text) and a body containing values like (student number, 12486),(student name, Fred Jones) • A relation is implemented as a table in a relational database. Object Model Student 0..* takes 0..* Module Student Number {PK} Student Name Module Code {PK} Module Title set Student Name get Student Name take Module lookup Modules set Title get Title print Register 11 Classes and Instances in the Object Data Model • An object oriented data model (OODM) is a logical data model that captures the meaning of objects of the kind used in object oriented programming. • It defines object classes, like Student and Module, and object instances that have the attributes defined by the object class but with particular values for each instance. • Instances of Student have instance variables studentNumber and studentName, with values like 12486 and Fred Jones respectively. 12 Object Identity • The object instances in an OODM have an identity that is independent of the values of their attributes (contrast with the relational model). 13 Complex Objects, Implementing relationships • The relationship, takes, could be represented as an instance variable of Student. This could be a set, bag, list or other container of references to instances of Module. The container would then be accessed using the message (method invocation) lookupModules. • Notice how an object oriented data model allows complex objects (contrast with the relational model). 14 Bidirectional relationships • Instances of Module have state variables moduleCode and moduleTitle. • Instances of Module could also implement taken by, the reverse of the relationship Takes, by means of a container of references to instances of student. • ObjectStore, an OODBMS described in C&B, implements relationships like Takes by bidirectional links. The DBMS automatically maintains referential integrity between the two. 15 Queries in an Object Oriented Data Model • Queries are responses to messages, and are implemented as method invocations. For example, if aStudent is an instance of Student, then an invocation like aStudent.lookupModules() queries the modules taken by Student. • Queries are intimately bound up with object classes; object instances have their own queries, which are defined by their class. (Contrast with SQL queries and the relational model). • The object query language (OQL) uses an SQL-like syntax to access the database; updates are performed by the methods defined in the classes. 16 Encapsulation and Inheritance • The object classes in the OODM allow for encapsulation. • For example, the implementation of takes is hidden by Student and Module, and can only be accessed by invoking the relevant methods. • The object classes in the OODM allow for inheritance. • This means that attributes that are shared by different kinds of entity can be represented once, using superclasses or generalization. Subclasses, or specializations of a superclass can inherit these shared attributes. 17 • For example, we could define a class Person, of which Student and e.g. staff member were specializations. • This reduces redundancy in the database schema (contrast with the relational model . . . though table inheritance is supported by modern DBMSs). Object Oriented Database • An object oriented database (OODB) is a persistent and shareable collection of objects modelled by an OODM. • An object oriented database management system (OODBMS) is a software system that provides the functionality that is normally required of a database management system, but whose underlying model is an OODM, rather than, for example, the relational data model. 18 Applications • Object oriented database management systems were developed (largely) in response to engineering needs. • Applications include computer aided design, and integrated software development environments (IDEs). • Relational database systems were developed (largely) in response to commercial needs, 19 Requirements on Commercial Databases • information can largely be described before the database is implemented • infrequent schema update, controlled by DBA • atomic, fixed-length data • few entity types, with many instances • large initial data load, with slow constant growth thereafter 20 • single value for each data item; updates in situ • short transactions can be used as a basis for concurrency control Requirements on Engineering Databases • continuous changes to the schema, made by different users • large, complex, variable length data items • many entity types, few instances, complex relationships • small initial data load, rapid early growth, slowing after design • need for configuration and version management 21 • long transactions, periods of inconsistency, problems with concurrency control Advantages of Object Oriented compared with Relational Databases • Object model intuitively closer to real world • Extensibility – inheritance • Complex values • Removal of impedance mismatch • More expressive query language 22 • Tight coupling between data and applications allows schema to capture more of the meaning of applications • Support for long transactions • Better support for applications like software engineering or computer aided design (CAD) • Arguably better performance, though benchmarks have mainly been applied in areas like engineering support to which OODBMS are better suited. Disadvantages of Object Oriented compared with Relational Databases • Lack of a theoretical foundation, so exact meaning of OODM is not well defined (though this is mitigated by the ODMG model) • More difficult to get staff experienced with OODBMSs • Lack of standards (mitigated by ODMG’s OQL) • Competition from relational and object-relational DBMS’s 23 • Encapsulation compromised to optimize queries (but in RDBMS’s, normalization may be compromised to improve performance). • OODBMS’s usually control concurrency by locking; locking an inheritance hierarchy is difficult and may affect performance. • OODM is inherently more complex than relational data model; OODBMS provides more complex functionality than RDBMS; complexity leads to higher implementation and maintenance costs. • Lack of views; but are views necessary with an object model? • OODBMS’s usually provide for coarse-grained access control; finer grained security mechanisms are needed for most commercial applications. Trends • Some OODBMS have emerged and seem likely to continue in use (e.g. ObjectStore) • The Object Data Management Group have proposed a standard covering an obect data model, an object data definition language (ODL) an object query language (OQL) an an object manipulation language (OML) • Currently, though the object relational data model seems to be more widely used 24 • and for the future, it looks as though some form of e.g. Java persistence will become the norm for persistent object oriented data.