DDM 5
DDM 5
To handle this type of class hierarchy, we have two basics tasks to perform:
• Design the relations to represent the class hierarchy.
• Design how objects will be accessed, which means:
– writing code to decompose the objects into tuples and store the decomposed objects in
relations;
– writing code to read tuples from the relations and reconstruct the objects.
Here we examine different approaches for declaring persistent classes. If we need to retrieve
some data from the relational database, the details for managers with a bonus in excess of £1000,
the code might look like the following:
On the other hand, to retrieve the same set of data in an OODBMS, we may write the following
code:
os_Set<Manager*> &highBonus
5 managerExtent->query(“Manager*”, “bonus > 1000”, db1);
Until recently, the choice of DBMS seemed to be between the relational DBMS and the
object-oriented DBMS. However, many vendors of RDBMS products are conscious of
the threat and promise of the OODBMS.
They agree that traditional relational DBMSs are not suited to the advanced applications
However, they reject the claim that extended RDBMSs will not provide sufficient
functionality or will be too slow to cope adequately with the new complexity.
4
If we examine the advanced database applications that are emerging, we find they make
extensive use of many object-oriented features such as a user-extensible type system,
encapsulation, inheritance, polymorphism, dynamic binding of methods, complex objects
including non-first normal form objects, and object identity.
The most obvious way to remedy the shortcomings of the relational model is to extend
the model with these types of features. This is the approach that has been taken by many
extended relational DBMSs, although each has implemented different combinations of
features.
Thus, there is no single extended relational model; rather, there are a variety of these
models, whose characteristics depend upon the way and the degree to which extensions
were made.
However, all the models do share the same basic relational tables and query language, all
incorporate some concept of “object,” and some have the ability to store methods (or
procedures or triggers) as well as data in the database.
Various terms have been used for systems that have extended the relational data model.
The original term that was used to describe such systems was the Extended Relational
DBMS (ERDBMS) and the term Universal Server or Universal DBMS (UDBMS) has
also been used.
However, in recent years the more descriptive term Object-Relational DBMS has been
used to indicate that the system incorporates some notion of “object.”
Here we use the term Object-Relational DBMS (ORDBMS). Three of the leading
RDBMS vendors—Oracle, Microsoft, and IBM—have all extended their systems into
ORDBMSs, although the functionality provided by each is slightly different.
The concept of the ORDBMS, as a hybrid of the RDBMS and the OODBMS, is very
appealing, preserving the wealth of knowledge and experience that has been acquired
with the RDBMS—so much so that some analysts predict the ORDBMS will have a 50%
larger share of the market than the RDBMS.
As might be expected, the standards activity in this area is based on extensions to the
SQL standard. The national standards bodies have been working on object extensions
to SQL since 1991.
These extensions have become part of the SQL standard, with releases in 1999,
referred to as SQL:1999, 2003, (SQL:2003), 2006 with extensions for XML
5
(SQL:2006), 2008 (SQL:2008) and 2011 (SQL:2011). These releases of the SQL
standard are an ongoing attempt to standardize extensions to the relational model and
query language.
STONEBRAKER’S VIEW:
Stonebraker (1996) proposed a four-quadrant view of the database world.
In the lower-left quadrant are applications that process simple data and have no
requirements for querying the data. These types of applications—for example, standard
text processing packages such as Microsoft Word—can use the underlying operating
system to obtain the essential DBMS functionality of persistence.
In the lower-right quadrant are applications that process complex data, but again have no
significant requirements for querying the data. For these types of applications—for
example, computer-aided design packages—an OODBMS may be an appropriate choice
of DBMS.
In the top-left quadrant are applications that process simple data and also have
requirements for complex querying. Many traditional business applications fall into this
quadrant and an RDBMS may be the most appropriate DBMS.
Finally, in the top-right quadrant are applications that process complex data and have
complex querying requirements. This represents many of the advanced database
applications and for these applications Stonebraker argues that an ORDBMS may be the
most appropriate choice of DBMS.
6
ADVANTAGES OF ORDBMSS:
1. Reusable and Sharable – able to reuse the hard-coded components. Through database
servers those components can be shared among available resources.
2. Ability of applying Objects with existing RDBMS models as it is – That is, RDBMS can
be extended with Object concepts without changing the underlying models. This leads the
organizations to switch over to ORDBMS concepts easily without performing bigger
migration or major changes.
3. It allows users and programmers to start using object-oriented systems in parallel.
4. Object Relational Database Management Systems ensures large storage capacity.
5. Supports rich data types by adding a new object-oriented layer.
6. Scalability
7. Relationships are represented explicitly, often supporting both navigational and
associative access to information.
8. Improved concurrency - concurrent users can safely query the same data.
9. Support for Composite data types - data is bundled with its metadata.
10. Improved integrity - ability to reject bad data before it is stored in an ORDBMS.
11. Database extensibility - easy addition of data types and operations.
12. Uniform treatment of data items - the SQL interface can perform complex queries
based on any of these data items, e.g., metadata as well as data; hence there is less need
for custom programming by users.
13. Custom data access methods - e.g., R-tree indexes.
14. Point-in-time recovery of data is possible.
7
15. Built-in complex SQL functions can be provided for data operations - e.g., aggregating,
slicing, subsetting, reprojecting, etc.
DISADVANTAGES OF ORDBMSS:
1. Complexity
2. Increased cost
3. Unclear if the ORDBMS will actually combine relationships and encapsulated objects to
correctly and completely mirror the ‘real world’.
4. Optimal Performance
Object Database
An object database stores data in the form of objects. These objects can contain both data
and methods for accessing and manipulating that data. Object databases work with object-
oriented programming languages, like Java and Python.
8
Relationships can be represented using single Relationships are limited to being single-
references or collections of references. valued in each record because multivalued
attributes are not permitted in the basic
relational model.
Inheritance is handled using the inheritance No built-in construct exists for inheritance in
constructs such as derived (:) and extends. the basic relational model. Several options are
available to choose from.
Operations are part of the class specifications Operations may be delayed until the
and need to be specified early on in the implementation phase.
design.
The object model mandates the predefinition The relational model does not mandate the
of a set of valid behaviors or operations. database designers to predefine a set of valid
behaviors or operations.
Mapping an EER schema to an ODB schema is a process of designing the type declarations of
object classes for an Object Database Management System (ODBMS) from an Entity-
Relationship (ER) diagram. Here are the stepwise instructions to perform this mapping.
Step-2
9
Add relationship properties or reference attributes for each binary relationship into the ODL
classes that participate in the relationship. Depending on the cardinality ratio of the binary
relationship, the relationship properties or reference attributes may be single-valued or collection
type.
Step-3
Include appropriate operations for each class. These are not available from the EER schema
and must be added to the database design by referring to the original requirements.
Step-4
An ODL class that corresponds to a subclass in the EER schema inherits the type and methods
of its superclass in the ODL schema. Its specific (non-inherited) attributes, relationship
references, and operations are specified.
Step-5
Weak entity types can be mapped in the same way as regular entity types. An alternative
mapping is possible for weak entity types that do not participate in any relationships except their
identifying relationship.
Step-6
Categories (union types) in an EER schema are difficult to map to ODL. One way is to create a
class to represent the category and define 1:1 relationship between the category and each of its
superclasses.
Step-7
An n-ary relationship with degree n > 2 can be mapped into a separate class, with appropriate
references to each participating class. An M: N binary relationship may also use this mapping
option if desired.
After completing the structural mapping, add operations for each class, including constructor
and destructor methods that check for constraints. The mapping can be applied to a subset of the
database schema in the context of the ODMG object database standard.
Conclusion:
When designing a database, it is important to consider the needs of the applications.
It is a unique way to database design that is more flexible and dynamic than traditional
relational models.
The conceptual design of object databases involves object-oriented modeling. Which
allows for inheritance, encapsulation, and polymorphism.
Mapping an EER schema to ODB schema involves several steps, including creating ODL
classes, adding relationship properties, and including appropriate operations for each
class.
However, there are also challenges in object database design. These are complexity,
integration with existing systems, and performance issues due to the complex
relationships between objects.
SQL:2011
An extensive tutorial on the features of the ISO SQL standard, concentrating mainly on
those features present in the 1992 version of the standard, commonly referred to as SQL2
or SQL-92.
ANSI (X3H2) and ISO (ISO/IEC JTC1/SC21/WG3) SQL standardization have added
features to the SQL specification to support object-oriented data management, the latest
release of which is SQL:2011 (ISO, 2011a).
As we mentioned earlier, the SQL:2011 standard is extremely large and comprehensive, and is
divided into the following parts:
(1) ISO/IEC 9075–1: SQL/Framework.
(2) ISO/IEC 9075–2: SQL/Foundation, which includes new data types, user-defined
types, rules and triggers, transactions, stored routines, and binding methods (embedded
SQL, dynamic SQL, and direct invocation of SQL).
(3) ISO/IEC 9075–3: SQL/CLI (Call-Level Interface), which specifies the provision of an
API interface to the database, as we discuss in Appendix I, based on the SQL Access
Group and X/Open’s CLI definitions.
(4) ISO/IEC 9075–4: SQL/PSM (Persistent Stored Modules), which allows procedures
and user-defined functions to be written in a 3GL or in SQL and stored in the database,
making SQL computationally complete.
(5) ISO/IEC 9075–9: SQL/MED (Management of External Data), which defines
extensions to SQL to support management of external data through the use of foreign
tables and datalink data types.
11
(6) ISO/IEC 9075–10: SQL/OLB (Object Language Bindings), which defines facilities
for embedding SQL statements in Java programs.
(8) ISO/IEC 9075–13: SQL/JRT (Java Routines and Types Using the Java Programming
Language), which defines extensions to SQL to allow the invocation of static methods
written in Java as SQL-invoked routines, and to use classes defined in Java as SQL
structured types.
In this section we examine some of these features, and cover these topics:
1. Type constructors for row types and reference types;
2. User-defined types (distinct types and structured types) that can participate in
Supertype/subtype relationships;
3. User-defined procedures, functions, methods, and operators;
4. Type constructors for collection types (arrays, sets, lists, and multisets);
5. Support for large objects—Binary Large Objects (BLOBs) and Character Large Objects
(CLOBs);
6. recursion.
If we now attempt to treat an instance of one type as an instance of the other type, an error would
be generated. UDT definition consists of one or more attribute definitions, zero or more routine
declarations (methods) and, in a subsequent release, operator declarations. We refer to routines
and operators generically as routines. In addition, we can also define the equality and ordering
relationships for the UDT using the CREATE ORDERING FOR statement. The value of an
attribute can be accessed using the common dot notation (.).
Example, assuming p is an instance of the UDT PersonType, which has an attribute fName of
type VARCHAR, we can access the fName attribute as:
p.fName
p.fName 5 ‘A. Smith’
For example, the observer function for the fName attribute of PersonType would be:
FUNCTION fName (p PersonType) RETURNS VARCHAR (15)
RETURN p. fName;
and the corresponding mutator function to set the value to newValue would be:
FUNCTION fName (p PersonType RESULT, newValue VARCHAR (15))
RETURNS PersonType
BEGIN
p. fName 5 newValue;
RETURN p;
END;
END;
The NEW expression can be used to invoke the system-supplied constructor function; for
example:
SET p 5 NEW PersonType ();
User-defined constructor methods must be invoked in the context of the NEW expression. For
example, we can create a new instance of PersonType and invoke the previous user-defined
constructor method as follows:
RELATIVE- The relative method is a function that returns a 0 for equals, a negative
value for less than, and a positive value for greater than.
MAP- The map method uses a function that takes a single argument of the UDT type and
returns a predefined data type. Comparing two UDTs is achieved by comparing the two
map values associated with them.
STATE- The state method compares the attributes of the operands to determine an order.
CAST functions can also be defined to provide user-specified conversion functions between
different UDTs. In a subsequent version of the standard, it may also be possible to override some
of the built-in operators.
15
An instance of a subtype is considered an instance of all its supertypes. SQL:2011 supports the
concept of substitutability: that is, whenever an instance of a supertype is expected an instance of
the subtype can be used in its place. The type of a UDT can be tested using the TYPE predicate.
For example, given a UDT, say Udt1, we can apply the following tests:
TYPE Udt1 IS OF (PersonType) // Check Udt1 is the PersonType or any of its subtypes
TYPE Udt1 IS OF (ONLY PersonType) // Check Udt1 is the PersonType
Privileges:
To create a subtype, a user must have the UNDER privilege on the user-defined type
specified as a supertype in the subtype definition.
In addition, a user must have USAGE privilege on any user-defined type referenced
within the new type.
Prior to SQL:1999, the SELECT privilege applied only to columns of tables and views.
From SQL:1999, the SELECT privilege also applies to structured types, but only when
instances of those types are stored in typed tables and only when the dereference operator
is used from a REF value to the referenced row and then invokes a method on that
referenced row.
When invoking a method on a structured value that is stored in a column of any ordinary
SQL table, SELECT privilege is required on that column.
If the method is a mutator function, UPDATE privilege is also required on the column. In
addition, EXECUTE privilege is required on all methods that are invoked.
.
An ORDBMS should provide significant flexibility in this area, such as allowing UDRs
to return complex values that can be further manipulated (such as tables), and support for
overloading of function names to simplify application development.
An SQL-invoke procedure is invoked from an SQL CALL statement. It may have zero or
more parameters, each of which may be an input parameter (IN), an output parameter
(OUT), or both an input and output parameter (INOUT), and it has a body if it is defined
fully within SQL. An SQL-invoked function returns a value; any specified parameters
must be input parameters. One input parameter can be designated as the result (using the
18
RESULT keyword), in which case the parameter’s data type must match the type of the
RETURNS type. Such a function is called type-preserving, because it always returns a
value whose runtime type is the same as the most specific type of the RETURN
parameter (not some subtype of that type). Mutator functions are always type-preserving.
In the first two cases, the methods include an additional implicit first parameter called SELF
whose data type is that of the associated UDT.A method can be invoked in one of three ways:
A constructor method is invoked using the NEW expression, as discussed previously;
An instance method is invoked using the standard dot notation; for example, p. fName, or
using the generalized invocation format, for example, (p AS StaffType). fName ();
A static method is invoked using: for example, if total Staff is a static method of
StaffType, we could invoke it as StaffType: total Staff ().
An external routine is defined by specifying an external clause that identifies the corresponding
“compiled code” in the operating system’s file storage.
For example, we need to use a function that creates a thumbnail image for an object stored in the
database. The functionality cannot be provided in SQL and so we have to use a function
provided externally, using the following CREATE FUNCTION statement with an EXTERNAL
clause:
CREATE FUNCTIONthumbnail(IN myImage ImageType) RETURNS BOOLEAN
EXTERNAL NAME ‘/usr/dreamhome/bin/images/thumbnail’
LANGUAGE C
PARAMETER STYLE GENERAL
DETERMINISTIC
NO SQL;
POLYMORPHISM:
19
The concepts of overriding, overloading, and more generally polymorphism. Different routines
may have the same name; that is, routine names may be overloaded, for example to allow a UDT
subtype to redefine a method inherited from a supertype, subject to the following constraints:
No two functions in the same schema are allowed to have the same signature, that is, the
same number of arguments, the same data types for each argument, and the same return
type.
No two procedures in the same schema are allowed to have the same name and the same
number of parameters.
For the remaining candidate methods, the second (runtime) phase proceeds as follows:
If the most specific type of the runtime value of the implicit argument to the method
invocation has a type definition that includes one of the candidate methods, then that
method is selected for execution.
If the most specific type of the runtime value of the implicit argument to the method
invocation has a type definition that does not include one of the candidate methods, then
the method selected for execution is the candidate method whose associated type is the
nearest supertype of all supertypes having such a method.
The argument values are converted to the parameter data types, if appropriate, and the
body of the method is executed.
Ideally, an object’s identity is independent of its name, structure, and location, and
persists even after the object has been deleted, so that it may never be confused with the
identity of any other object. Other objects can use an object’s identity as a unique way of
referencing it.
Before SQL:1999, the only way to define relationships between tables was using the
primary key/foreign key mechanism, which in SQL2 could be expressed using the
referential table constraint clause REFERENCES.
Since SQL:1999, reference types have been able to be used to define relationships
between row types and uniquely identify a row within a table. A reference type value can
be stored in one (typed) table and used as a direct reference to a specific row in some
base table that has been defined to be of this type (similar to the notion of a pointer type
in C or C++).
Thus, references allow a row to be shared among multiple tables and enable users to
replace complex join definitions in queries with much simpler path expressions.
References also give the optimizer an alternative way to navigate data instead of using
value-based joins.
REF IS SYSTEM GENERATED in a CREATE TYPE statement indicates that the actual
values of the associated REF type are provided by the system.
CREATING TABLES:
To maintain upwards compatibility with the SQL2 standard, it is still necessary to use the
CREATE TABLE statement to create a table, even if the table consists of a single UDT.
In other words, a UDT instance can persist only if it is stored as the column value in a
table. There are several variations of the CREATE TABLE statement.
In the first instance, we would access the columns of the Person table using a path
expression such as ‘Person.info.fName’; in the second version, we would access the
columns using a path expression such as ‘Person.fName’.
More importantly, tables constructed using the second version (CREATE TABLE . . . OF
statement) are called typed tables. The rows of a typed table are considered to be objects,
and the rows in the first version are not, even though the same UDT has been used in
both cases. A typed table has a column for every attribute of the structured type on which
it is based. In addition, a typed table has a self-referencing column that contains a unique
OID (known as a reference) for each row of the table.
Objects are inserted into a typed table using the normal INSERT statement as for any
relational table. Apart from the self-referencing column and the attributes of the
structured type, no additional columns can be added to the table definition.
The OID is generated automatically when a new row is inserted into the typed table.
However, to gain access to the OIDs stored in the self-referencing column, we have to
give the column name explicitly using the REF IS clause (PersonID in our example).
Note that a typed table definition must also repeat a reference generation specification
that is consistent with the corresponding specification in the UDT: system-generated,
user-generated, or derived.
For example, referring to Figure(b), if we insert a row into PTStudentStaff, then the values of the
inherited columns are inserted into Student and Staff, and then the values of the inherited
columns of Student/Staff are inserted into Person.
When a row is updated in a subtable, a procedure to the previous one is carried out to
update the values of inherited columns in the supertables.
When a row is updated in a supertable, then the values of all inherited columns in all
corresponding rows of its direct and indirect subtables are also updated accordingly. As
the supertable may itself be a subtable, the previous condition will also have to be applied
to ensure consistency.
When a row is deleted in a subtable/supertable, the corresponding rows in the table
hierarchy are deleted. For example, if we deleted a row of Student, the corresponding
rows of Person and Undergraduate/Postgraduate/PartTimeStudent/PTStudentStaffare
deleted.
SQL:2011 does not provide a mechanism to store all instances of a given UDT unless the user
explicitly creates a single table in which all instances are stored. Thus, in SQL:2011 it may not
be possible to apply an SQL query to all instances of a given UDT. For example, if we created
another table such as:
CREATE TABLE Client (
info PersonType,
prefType CHAR,
maxRent DECIMAL (6, 2),
branchNo VARCHAR (4) NOT NULL);
PRIVILEGES:
As with the privileges required to create a new subtype, a user must have the UNDER
privilege on the referenced supertable.
In addition, a user must have USAGE privilege on any user-defined type referenced
within the new table.
QUERYING DATA:
SQL:2011 provides the same syntax as SQL2 for querying and updating tables, with
various extensions to handle objects.
This query invokes the implicitly defined observer function position in the WHERE clause to
access the position column.
This alternative method of finding Managers uses the user-defined method isManager as a
predicate of the WHERE clause. This method returns the boolean value TRUE if the member of
staff is a manager. In addition, this query also invokes the inherited virtual (observer) function
age as an element of the SELECT list.
This query lists not only the details of rows that have been explicitly inserted into the Person
table, but also the names from any rows that have been inserted into any direct or indirect
subtables of Person, in this case, Staff and Client.
24
Suppose, however, that rather than wanting the details of all people, we want only the details
of the specific instances of the Person table, excluding any subtables. This can be achieved using
the ONLY keyword:
SELECT p.lName, p.fName
FROM ONLY (Person) p
WHERE p.age > 65;
To retrieve the member of staff for property PG4, rather than just the first and last names, we
would use the following query instead:
SELECT DEREF(p.staffID) AS Staff
FROM PropertyForRent p
WHERE p.propertyNo 5 ‘PG4’;
Although reference types are similar to foreign keys, there are significant differences. In
SQL:2011, referential integrity is maintained only by using a referential constraint definition
specified as part of the table definition. By themselves, reference types do not provide referential
integrity. Thus, the SQL reference type should not be confused with that provided in the ODMG
object model. In the ODMG model, OIDs are used to model relationships between types and
referential integrity is automatically defined.
SQL:1999 introduced an ARRAY collection type and SQL:2003 added the MULTISET
collection type, and a subsequent version of the standard may introduce parameterized
LIST and SET collection types.
In each case, the parameter, called the element type, may be a predefined type, a UDT, a
row type, or another collection, but cannot be a reference type or a UDT containing a
reference type. In addition, each collection must be homogeneous: all elements must be
of the same type, or at least from the same type hierarchy.
The collection types have the following meaning:
25
We could now retrieve the first and last telephone numbers at branch B003 using the following
query:
SELECT telNo[1], telNo[CARDINALITY (telNo)]
FROM Branch
WHERE branchNo 5 ‘B003’;
are analogous to tables, they are not regarded as the same as tables, and operators are
provided to convert a multiset to a table (UNNEST) and a table to a multiset
(MULTISET).
There is currently no separate type proposed for sets. Instead, a set is simply a special
kind of multiset: one that has no duplicate elements. A predicate is provided to check
whether a multiset is a set. Two multisets of comparable element types, A and B say, are
considered identical if and only if they have the same cardinality and for each element x
in A, the number of elements of A that are identical to x, including x itself, equals the
number of elements of B that are equal to x. Again, as with array types, a multiset type
constructor can be defined by enumerating their elements as a comma-separated list
enclosed in square brackets, or by using a query expression with degree 1, or by using a
table value constructor.
DISTINCT predicate;
MEMBER predicate;
SUBMULTISET predicate, which tests whether one multiset is a sub multiset of another;
IS A SET/IS NOT A SET predicate, which checks whether a multiset is a set.
TYPED VIEWS:
SQL:2011 also supports typed views, sometimes called object views or referenceable
views. A typed view is created based on a particular structured type and a sub view can
be created based on this typed view.
TRIGGERS:
A trigger is an SQL (compound) statement that is executed automatically by the DBMS
as a side effect of a modification to a named table.
It is similar to an SQL routine, in that it is a named SQL block with declarative,
executable, and condition handling sections.
However, unlike a routine, a trigger is executed implicitly whenever the triggering event
occurs, and a trigger does not have any arguments.
The act of executing a trigger is sometimes known as firing the trigger.
29
Triggering events include insertion, deletion, and update of rows in a table. In the latter case
only, a triggering event can also be set to cover specific named columns of a table. A trigger has
an associated timing of either BEFORE, AFTER, or INSTEAD OF. A BEFORE trigger is fired
before the associated event occurs, an AFTER trigger is fired after the associated event occurs,
and an INSTEAD OF trigger is fired in place of the trigger event.
The triggered action is an SQL procedure Statement, which can be executed in one of two ways:
For each row affected by the event (FOR EACH ROW). This is called a row-level
trigger;
Only once for the entire event (FOR EACH STATEMENT), which is the default. This is
called a statement-level trigger.
Clearly, old values are not applicable for insert events, and new values are not applicable for
delete events. The body of a trigger cannot contain any:
SQL transaction statements, such as COMMIT or ROLLBACK;
SQL connection statements, such as CONNECT or DISCONNECT;
SQL schema definition or manipulation statements, such as the creation or deletion of
tables, user-defined types, or other triggers;
SQL session statements, such as SET SESSION CHARACTERISTICS, SET ROLE,
SET TIME ZONE.
30
Furthermore, SQL does not allow mutating triggers, that is, triggers that cause a change resulting
in the same trigger to be invoked again, possibly in an endless loop. As more than one trigger can
be defined on a table, the order of firing of triggers are important. Triggers are fired as the trigger
event (INSERT, UPDATE, DELETE) is executed. The following order is observed:
BEGIN ATOMIC
DELETE FROM Mailshot WHERE maxRent > pfr.rent;
UPDATE Mailshot SET rent 5 pfr.rent
WHERE propertyNo 5 pfr.propertyNo;
END;
This trigger is executed after the rent field of a PropertyForRent row has been updated. The FOR
EACH ROW clause is specified, as all property rents may have been increased in one UPDATE
statement, for example due to a cost of living rise. The body of the trigger has two SQL
statements: a DELETE statement to delete those mailshot records where the new rental price is
outside the client’s price range, and an UPDATE statement to record the new rental price in all
rows relating to that property.
Triggers can be a very powerful mechanism if used appropriately. The major advantage is that
standard functions can be stored within the database and enforced consistently with each update
to the database. This can dramatically reduce the complexity of applications.
PRIVILEGES:
To create a trigger, a user must have TRIGGER privilege on the specified table, SELECT
privilege on any tables referenced in the triggerCondition of the WHEN clause, together
with any privileges required to execute the SQL statements in the trigger body.
LARGE OBJECTS
A large object is a data type that holds a large amount of data, such as a long text file or a
graphics file. Three different types of large object data types are defined in SQL:2011:
Binary Large Object (BLOB), a binary string that does not have a character set or
collation association;
Character Large Object (CLOB) and National Character Large Object (NCLOB), both
character strings.
The SQL large object is slightly different from the original type of BLOB that appears in some
database systems. In such systems, the BLOB is a noninterpreted byte stream, and the DBMS
does not have any knowledge concerning the content of the BLOB or its internal structure.
32
The standard string operators, which operate on characters strings and return character strings,
also operate on character large object strings.
such as:
1. The concatenation operator, (string1 i string2), which returns the character string formed
by joining the character string operands in the specified order.
2. The character substring function, SUBSTRING(string FROM startpos FOR length),
which returns a string extracted from a specified string from a start position for a given
length.
3. The character overlay function, OVERLAY (string1 PLACING string2 FROM startpos
FOR length), replaces a substring of string1, specified as a starting position and a length,
with string2. This is equivalent to: SUBSTRING (string1 FROM 1 FOR length − 1) i
string2 i SUBSTRING (string1 FROM startpos + length).
4. The fold functions, UPPER (string) and LOWER (string), which convert all characters in
a string to upper/lower case.
5. The trim function, TRIM([LEADING | TRAILING | BOTH string1 FROM] string2),
which returns string2 with leading and/or trailing string1 characters removed. If the
FROM clause is not specified, all leading and trailing spaces are removed from string2.
6. The length function, CHAR_LENGTH(string), which returns the length of the specified
string.
7. The position function, POSITION(string1 IN string2), which returns the start position of
string1 within string2.
A binary large object (BLOB) string is defined as a sequence of octets. All BLOB strings are
comparable by comparing octets with the same ordinal position. The following operators operate
on BLOB strings and return BLOB strings, and have similar functionality as those defined
previously:
• The BLOB concatenation operator (i);
• The BLOB substring function (SUBSTRING);
• The BLOB overlay function (OVERLAY);
• The BLOB trim function (TRIM).
In addition, the BLOB_LENGTH and POSITION functions and the LIKE predicate can also be
used with BLOB strings.
RECURSION:
A major new operation in SQL for specifying such queries is linear recursion.
A relationship between two entities of a similar entity type is called a recursive
relationship.
Atomicity of data means that repeating groups are not allowed in the relational model. As
a result, it is extremely difficult to handle recursive queries, that is, queries about
relationships that a relation has with itself (directly or indirectly).
WITH RECURSIVE
AllManagers (staffNo, managerStaffNo) AS
(SELECT staffNo, managerStaffNo
FROM Staff
UNION
SELECT in. staffNo, out. managerStaffNo
FROM AllManagers in, Staff out
WHERE in. managerStaffNo 5 out. StaffNo);
SELECT * FROM AllManagers
ORDER BY staffNo, managerStaffNo;
For example, at the end of the WITH RECURSIVE statement we could add the following clause:
SEARCH BREADTH FIRST BY staffNo, managerStaffNo
SET order Column
Object types
34
An object type is a schema object that has a name, a set of attributes based on the built-in
data types or possibly other object types, and a set of methods. similar to what we
discussed for an SQL:2011 object type.
For example, we could create Address, Staff, and Branch types as follows:
CREATE TYPE AddressType AS OBJECT (
street VARCHAR2(25),
city VARCHAR2(15),
postcode VARCHAR2(8));
We can then create a Branch (object) table using the following statement:
CREATE TABLE Branch OF BranchType (branchNo PRIMARY KEY);
METHODS:
The methods of an object type are classified as member, static, or explicit comparison. A
member method is a function or a procedure that always has an implicit or explicit SELF
parameter as its first parameter, whose type is the containing object type. Such methods
are useful as observer and mutator functions and are invoked in the selfish style.
A static method is a function or a procedure that does not have an implicit SELF
parameter. Such methods are useful for specifying user-defined constructors or cast
methods and may be invoked by qualifying the method with the type name, as in
typename.method().
35
A comparison method is used for comparing instances of object types. Oracle provides two ways
to define an order relationship among objects of a given type:
1. A map method uses Oracle’s ability to compare built-in types. In our example, we have
defined a map method for the new type BranchType, which compares two branch objects
based on the values in the branchNo attribute. We show an implementation of this
method shortly.
2. An order method uses its own internal logic to compare two objects of a given object
type. It returns a value that encodes the order relationship. For example, it may return −1
if the first is smaller, 0 if they are equal, and 1 if the first is larger.
For an object type, either a map method or an order method can be defined, but not both. If an
object type has no comparison method, Oracle cannot determine a greater than or less than
relationship between two objects of that type. However, it can attempt to determine whether two
objects of the type are equal using the following rules:
If all the attributes are nonnull and equal, the objects are considered equal;
If there is an attribute for which the two objects have unequal nonnull values, the objects
are considered unequal;
Otherwise, Oracle reports that the comparison is not available (null). Methods can be
implemented in PL/SQL, Java, and C, and overloading is supported provided their formal
parameters differ in number, order, or data type.
Example:
CREATE OR REPLACE TYPE BODY BranchType AS
MAP MEMBER FUNCTION getbranchNo RETURN VARCHAR2(4) IS
BEGIN
RETURN branchNo;
END;
END;
Oracle also allows user-defined operators to be created using the CREATE OPERATOR
statement. Like built-in operators, a user-defined operator takes a set of operands as input and
return a result. Once a new operator has been defined, it can be used in SQL statements like any
other built-in operator.
Constructor methods Every object type has a system-defined constructor method that makes a
new object according to the object type’s specification. The constructor method has the same
name as the object type and has parameters that have the same names and types as the object
type’s attributes. For example, to create a new instance of BranchType, we could use the
following expression:
BranchType (‘B003’, AddressType (‘163 Main St’, ‘Glasgow’, ‘G11 9QX’));
For example, we could restate the creation of the Branch table as:
CREATE TABLE Branch OF BranchType (branchNo PRIMARY KEY)
OBJECT IDENTIFIER IS PRIMARY KEY;
A REF can be used to examine or update the object it refers to and to obtain a copy of the
object it refers to. The only changes that can be made to a REF are to replace its contents
with a reference to a different object of the same object type or to assign it a null value.
At an implementation level, Oracle uses object identifiers to construct REFs.
Example:
CREATE TYPE BranchType AS OBJECT (
branchNo VARCHAR2(4),
address AddressType,
manager REF StaffType,
MAP MEMBER FUNCTION getbranchNo RETURN VARCHAR2(4),
PRAGMA RESTRICT_REFERENCES (getbranchNo, WNDS, WNPS,RNDS, RNPS));
TYPE INHERITANCE:
Oracle supports single inheritance allowing a subtype to be derived from a single parent
type.
The subtype inherits all the attributes and methods of the supertype and additionally can
add new attributes and methods, and it can override any of the inherited methods. As with
SQL:2011, the UNDER clause is used to specify the supertype.
For example, a branch office can have up to three telephone numbers, which we could model in
Oracle by declaring the following new type:
CREATE TYPE TelNoArrayType AS VARRAY (3) OF VARCHAR2(13);
The creation of an array type does not allocate space but instead defines a data type that can be
used as:
1. The data type of a column of a relational table;
2. An object type attribute;
38
For example, we could modify the type BranchType to include an attribute of this new type:
phoneList TelNoArrayType,
An array is normally stored inline, that is, in the same tablespace as the other data in its row. If it
is sufficiently large, however, Oracle stores it as a BLOB.
Nested tables A nested table is an unordered set of data elements that are all of the same data
type. It has a single column of a built-in type or an object type. If the column is an object type,
the table can also be viewed as a multicolumn table, with a column for each attribute of the
object type. For example, to model next-of-kin for members of staff, we may define a new type
as follows:
We can now modify the type StaffType to include this new type as a nested table:
nextOfKin NextOfKinNestedType,
INSERT INTO Staff VALUES (‘SG37’, ‘Ann’, ‘Beech’, ‘Assistant’, ‘F’, ‘10-Nov-
1960’, 12000, NextOfKinNestedType ());
INSERT INTO Staff VALUES (‘SG5’, ‘Susan’, ‘Brand’, ‘Manager’, ‘F’, ‘3-Jun-
1940’, 24000, NextOfKinNestedType ());
The expression NextOfKinNestedType () invokes the constructor method for this type to create
an empty nextOfKin attribute. We can insert data into the nested table using the following
statement:
This statement uses a TABLE expression to identify the nested table as the target for the
insertion, namely the nested table in the nextOfKin column of the row object in the Staff table
that has a staffNo of ‘SG5’. Finally, we can insert an object into the Branch table:
Querying object tables In Oracle, we can return an ordered list of branch numbers using the
following query:
SELECT b. branchNo
FROM Branch b
ORDER BY VALUE(b);
40
This query implicitly invokes the comparison method getbranchNo that we defined as a map
method for the type BranchType to order the data in ascending order of branchNo. We can return
all the data for each branch using the following query:
Note the use of the DEREF operator to access the manager object. This query writes out the
values for the branchNo column, all columns of an address, all columns of the manager object
(of type StaffType), and all relevant telephone numbers. We can retrieve next of kin data for all
staff at a specified branch using the following query:
Many applications are unable to handle collection types and instead require a flattened view of
the data. In this example, we have flattened (or unnested) the nested set using the TABLE
keyword.
OBJECT VIEWS
The way that a view is a virtual table, an object view is a virtual object table.
Object views allow the data to be customized for different users.
For example, assume that we have created the object types defined and assume that we have
created and populated the following relational schema for DreamHome with associated
structured types BranchType and StaffType:
We could create an object-relational schema using the object view mechanism as follows:
CREATE VIEW Staff View OF StaffType WITH OBJECT IDENTIFIER (staffNo) AS
SELECT s. staffNo, s. fName, s. lName, s.sex, s. position, s.DOB, s. salary,
CAST (MULTISET (SELECT n. fName, n. lName, n. telNo
FROM NextOfKin n WHERE n. staffNo 5 s. staffNo)
AS NextOfKinNestedType) AS nextOfKin
41
FROM Staff s;
Privileges:
Oracle defines the following system privileges for user-defined types:
1. CREATE TYPE – to create user-defined types in the user’s schema;
2. CREATE ANY TYPE – to create user-defined types in any schema;
3. ALTER ANY TYPE – to alter user-defined types in any schema;
4. DROP ANY TYPE – to drop named types in any schema;
5. EXECUTE ANY TYPE – to use and reference named types in any schema;
6. UNDER ANY TYPE – to create subtypes under any non-final object types;
7. UNDER ANY VIEW – to create sub views under any object view.
In addition, the EXECUTE schema object privilege allows a user to use the type to define a
table, define a column in a relational table, declare a variable or parameter of the named type,
and to invoke the type’s methods.
Use OQL to create new databases or insert data into existing databases (to configure the
operation of Network Manager components) by amending the component schema files. You can
also issue OQL statements using the OQL Service Provider, for example, to create or modify
databases, insert data into databases and retrieve data.
Example:
SELECT c. address
42
FROM Persons p,
p. children c
WHERE p. address. street="Main Street" AND
COUNT (p. children) >= 2 AND
c.address. City! = p. address. City;
OODBMS ORDBMS
It stands for Object Oriented Database It stands for Object Relational Database
Management System. Management System.
Every object-oriented system has a different Keys, entity integrity, and referential integrity
set of constraints that it can accommodate. are constraints of an object-oriented database.
FEATURES OF ODBMS:
Object-oriented data model: ODBMS uses an object-oriented data model to store and
manage data. This allows developers to work with data in a more natural way, as objects
are similar to the objects in the programming language they are using.
Complex data types: ODBMS supports complex data types such as arrays, lists, sets,
and graphs, allowing developers to store and manage complex data structures in the
database.
High performance: ODBMS can provide high performance, especially for applications
that require complex data access patterns, as objects can be retrieved with a single query.
Data integrity: ODBMS provides strong data integrity, as the relationships between
objects are maintained by the database. This ensures that data remains consistent and
correct, even in complex applications.
Scalability: ODBMS can scale horizontally by adding more servers to the database
cluster, allowing it to handle large volumes of data.
45
Support for transactions: ODBMS supports transactions, which ensure that multiple
operations on the database are atomic and consistent.
ADVANTAGES:
Supports Complex Data Structures
Improved Performance
Reduced Development Time
Supports Rich Data Types
Scalability
DISADVANTAGES:
Limited Adoption
Lack of Standardization
Cost
Integration with Other Systems
Scalability Challenges
*************************