Relational and Object-Oriented Databases
Total Page:16
File Type:pdf, Size:1020Kb
Relational and Object-Oriented Databases by Willi-Hans Steeb International School for Scientific Computing Contents 1 What is a table? 1 1.1 Introduction . 1 1.2 Examples . 5 1.3 Tables in Programs . 8 1.4 Table and Relation . 33 2 Structured Query Language 35 2.1 Introduction . 35 2.2 Integrity Rules . 38 2.3 SQL Commands . 39 2.3.1 Introduction . 39 2.3.2 Aggregate Function . 40 2.3.3 Arithmetic Operators . 40 2.3.4 Logical Operators . 40 2.3.5 SELECT Statement . 41 2.3.6 INSERT Command . 45 2.3.7 DELETE Command . 46 2.3.8 UPDATE Command . 47 2.3.9 CREATE TABLE Command . 48 2.3.10 DROP TABLE Command . 51 2.3.11 ALTER TABLE Command . 52 2.4 Set Operators . 53 2.5 Views . 60 2.6 Primary and Foreign Keys . 62 2.7 Datatypes in SQL . 63 2.8 Joins . 66 2.9 Stored Procedure . 71 2.10 MySQL Commands . 72 2.11 Cursors . 73 2.12 PL and SQL . 75 2.13 ABAP/4 and SQL . 76 2.14 Query Processing and Optimization . 77 i 3 Normal Forms 83 3.1 Introduction . 83 3.2 Anomalies . 87 3.3 Example . 89 3.4 Fourth and Fifth Normal Forms . 93 4 Transaction 101 4.1 Introduction . 101 4.2 Data Replication . 107 4.3 Locks . 108 4.4 Deadlocking . 111 4.5 Threads . 117 4.5.1 Introduction . 117 4.5.2 Thread Class . 119 4.5.3 Example . 121 4.5.4 Priorities . 123 4.5.5 Synchronization and Locks . 126 4.5.6 Producer Consumer Problem . 131 4.6 Locking Files for Shared Access . 134 5 JDBC 137 5.1 Introduction . 137 5.2 Classes for JDBC . 140 5.2.1 Introduction . 140 5.2.2 Classes DriverManager and Connection . 141 5.2.3 Class Statement . 144 5.2.4 Class PreparedStatement . 147 5.2.5 Class CallableStatement . 149 5.2.6 Class ResultSet . 151 5.2.7 Class SQLException . 154 5.2.8 Classes Date, Time and TimeStamp . 155 5.3 Data Types in SQL . 156 5.4 Example . 158 5.5 Programs . 159 5.6 Metadata . 173 5.7 JDBC 3.0 . 173 6 Object-Oriented Databases 177 6.1 Introduction . 177 6.2 Object-Oriented Properties . 181 6.3 Terms Glossary . 183 6.4 Properties of an Object-Oriented Database . 186 6.5 Example . 188 6.6 C++ . 192 6.7 The Object Query Language . 194 ii 6.8 SQL3 Object Model . 195 6.8.1 Basic Concepts . 195 6.8.2 Objects . 197 6.8.3 Operations . 198 6.8.4 Methods . 199 6.8.5 Events . 201 6.8.6 Binding and Polymorphism . 202 6.8.7 Types and Classes . 203 6.8.8 Inheritance and Delegation . 208 6.8.9 Noteworthy Objects . 210 6.8.10 Extensibility . 212 6.9 SQL3 Datatypes and Java . 214 6.10 Evaluations of OODBMSs . 219 6.11 Summary . 222 7 Versant 225 7.1 Introduction . 225 8 FastObjects 233 8.1 Introduction . 233 9 Data Mining 235 9.1 Introduction . 235 9.2 Example . 242 Bibliography 243 Index 243 iii Preface This book explores the use of databases and related tools in the various applications. Both relational and object-oriented databases are coverd. An introduction to JDBC is also given. It also includes C++ and Java programs relevant in databases. Without doubt, this book can be extended. If you have comments or suggestions, we would be pleased to have them. The email addresses of the author are: [email protected] [email protected] [email protected] The web sites of the author are: http://www.fhso.ch/~steeb http://issc.rau.ac.za iv Chapter 1 What is a table? 1.1 Introduction What is a table? As a definition for a table in the Oxford dictionary we find "orderly arrangment of facts, information etc (usually as in columns)" For a database we find the definition A database is a means of storing information in such a way that information can be retrieved from it. Thus a database is typically a repository for heterogeneous but interrelated pieces of information. Often a database contains more than one table. Codebooks and dictionaries can also be considered as tables. A dictionary is a reference book on any subject, the items of which are arranged in alphabetical order. A codebook is a list of replacements for words or phrases in the original message. A code is a system for hiding the meaning of a message by replacing each word or phrase in the original message with another character or set of characters. The list of replacements is contained in a codebook. An alternative definition of a code is any form of encryption which has no built-in flexbility, i.e. there is only one key, namely the codebook. Databases contain organized data. A database can be as simple as a flat file (a single computer file with data usually in a tabular form) containing names and telephone numbers of one’s friends, or as elaborate as the worldwide reservation system of a major airline. Many of the principles discussed in this book are applicable to a wide variety of database systems. 1 2 CHAPTER 1. WHAT IS A TABLE? Structurally, there are three major types of databases: Hierarchical Relational Network During the 1970s and 1980s, the hierarchical scheme was very popular. This scheme treats data as a tree-structured system with data records forming the leaves. Ex- amples of the hierarchical implementations are schemes like b-tree and multi-tree data access. In the hierarchical scheme, to get to data, users need to traverse up and down the tree structure. XML (Extensible Markup Language) is based on a tree structure. The most common relationship in a hierarchical structure is a one- to-many relationship between the data records, and it is difficult to implement a many-to-many relationship without data redundancy. The network data model solved this problem by assuming a multi-relationship be- tween data elements. In contrast to the hierarchical scheme where there is a parent- child relationship, in the network scheme, there is a peer-to-peer relationship. Most of the programs developed during those days used a combination of the hierarchical and network data storage and access model. During the 90s, the relational data access scheme came to the forefront. The re- lational scheme views data as rows of information. Each row contains columns of data, called fields. The main concept in the relational scheme is that the data is uniform. Each row contains the same number of columns. One such collection of rows and columns is called a table. Many such tables (which can be structurally different) form a relational database. A relational database is accessed and admin- istered using Structured Query Language (SQL) statements. SQL is a command language for performing operations on databases and their component parts. Ta- bles are the component parts we are dealing with most often. Their column and row structure makes them look a great deal like spreadsheets, but the resemblance is only surface-level. Table elements are not used to represent relationships to other elements - that is, table elements do not hold formulas, they just hold data. Most SQL statements are devoted to working with the data in these rows and columns, allowing the user to add, delete, sort, and relate it between tables. 1.1. INTRODUCTION 3 There are many ways to approach the design of a database and tables. The database layout is the most important part of an information system. The more design and thought put into a database, the better the database will be in the long run. We should gather information about the user’s requirement, data objects, and data def- initions before creating a database layout. The first step we take when determining the database layout is to build a data model that defines how the data is to be stored. For most relational databases, we create an entity-relationship diagram or model. The steps to create this model are as follows: 1. Identify and define the data objects (entities, relationship, and attributes) 2. Diagram the objects and relationship 3. Translate the objects into relational constructs (such as tables) 4. Resolve the data model 5. Perform the normalization process First, define the entities and the relationships between them. An entity is something that can be distinctively identified. An example of an entity is a specific person, an element in the periodic table, a specific.