10
Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model

Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model

Embed Size (px)

Citation preview

Page 1: Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model

Advanced DatabaseCS-426Week 2 – Logic Query Languages, Object Model

Page 2: Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model

Object Models An object typically has two components: state (value) and behavior (operations). One goal of an ODMS (Object Data Management System) is to maintain a direct correspondence between real-world and database objects so that objects do not lose their integrity and identity and can easily be identified and operated upon.define type EMPLOYEEtuple ( Fname: string;Minit: char;Lname: string;Ssn: string;Birth_date: DATE;Address: string;Sex: char;Salary: float;Supervisor: EMPLOYEE;Dept: DEPARTMENT;);

Page 3: Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model

Object Modelsdefine type DATE

tuple ( Year: integer;

Month: integer;

Day: integer; );

Page 4: Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model

Object Data Modeling Group (ODMG)• The ODMG object model is the data model upon which the

object definition language (ODL) and object query language (OQL) are based. It is meant to provide a standard data model for object databases, just as SQL describes a standard data model for relational databases. It also provides a standard terminology in a field where the same terms were sometimes used to describe different concepts.

Page 5: Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model

Object Definition Language (ODL)

• The ODL is designed to support the semantic constructs of the ODMG object model and is independent of any particular programming language. Its main use is to create object specifications—that is, classes and interfaces. Hence, ODL is not a full programming language. A user can specify a database schema in ODL independently of any programming language, and then use the specific language bindings to specify how ODL constructs can be mapped to constructs in specific programming languages, such as C++, Smalltalk, and Java.

Page 6: Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model

Object Query Language (OQL) • The object query language OQL is the query language

proposed for the ODMG object model. It is designed to work closely with the programming languages for which an ODMG binding is defined, such as C++, Smalltalk, and Java. Hence, an OQL query embedded into one of these programming languages can return objects that match the type system of that language. Additionally, the implementations of class operations in an ODMG schema can have their code written in these programming languages. The OQL syntax for queries is similar to the syntax of the relational standard query language SQL, with additional features for ODMG concepts, such as object identity, complex objects, operations, inheritance, polymorphism, and relationships.

Page 7: Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model

Advanced SQL 

1. Accessing SQL From a Programming Language

There are two approaches to accessing SQL from a general-purpose programming language:

• Dynamic SQL: A general-purpose program can connect to and communicate with a database server using a collection of functions (for procedural languages)or methods (for object-oriented languages). Dynamic SQL allows the program to construct an SQL query as a character string at runtime, submit the query, and then retrieve the result into program variables a tuple at a time. The dynamic SQL component of SQL allows programs to construct and submit SQL queries at runtime.

Page 8: Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model

Advanced SQL• Embedded SQL: Like dynamic SQL, embedded SQL provides

a means by which a program can interact with a database server. However, under embedded SQL, the SQL statements are identified at compile time using a preprocessor. The preprocessor submits the SQL statements to the database system for precompilation and optimization; then it replaces the SQL statements in the application program with appropriate code and function calls before invoking the programming-language compiler.

Page 9: Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model

2. Functions and ProceduresProcedures and functions allow “business logic” to be stored in the database, and executed from SQL statements.

Example:create function dept count(dept name varchar(20))returns integerbegindeclare d count integer;select count(*) into d countfrom instructorwhere instructor.dept name= dept namereturn d count;end

Page 10: Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model

3. TriggersA trigger is a statement that the system executes automatically as a side effect of a modification to the database. To design a trigger mechanism, we must meet two requirements:

1. Specify when a trigger is to be executed. This is broken up into an event that causes the trigger to be checked and a condition that must be satisfied for trigger execution to proceed.

2. Specify the actions to be taken when the trigger executes.