Sei sulla pagina 1di 70

Relational Database Management Systems Practical Guide

Ali Adams
Abu Dhabi Mens College Higher Colleges of Technology

Information Management
 Data  Information  Knowledge  Decision
27 age = 27 if age>25 risk is low set premium to low

Decisions are made based on current information and existing knowledge.

Functions of DBMS
 Create, alter and delete tables  Add, update, and delete records  Extract information from data  Maintain data integrity and security  Add, update and delete users  Grant and revoke user privileges  Data sharing & concurrency control  Provide API for application builders

Database Development Cycle


 Prior Analysis
Examine the case for commissioning a new GIS database.

 Logical design
Build a conceptual data-model of the natural form of the data using dataEAR diagrams and normalize it.

 Physical Design
Map the conceptual model into tables, fields, keys, indexes, etc for a preferred DBMS.

 Testing
Test for functionality correctness and then for design efficiency under the predicted usage level.

 Implementation
Deploy the database within its target environment, write user documentation and train users.

 Maintenance
Iron out any bugs, and schema-evolve the database as the organization schemaevolves.

Types of databases
Hierarchical

 The hierarchical data-model is essentially a tree structure datathat allows each parent entity to own several child entities, but each child belongs to only one parent. parent.

Types of databases
Network

 The Network data-model is similar to the hierarchical data-

model except that child records are allowed to belong to many parent records, and thus any problem of multiple ownership is overcome.

Types of databases
Relational

 The Relational data-model is based on set-theory which datasetis tabular and alphanumeric in nature.

 Relationships are implemented using shared columns in


different tables.

 Data is independent of code.

ObjectObject-Oriented Databases

Types of databases
ObjectObject-Oriented

Departments Employees

 Nested structure of
objects maintained

 Objects have identity


Equipment

 Supports class
hierarchy, inheritance and aggregation

Types of databases
Object Relational

 ORDBMS are an evolution of RDBMS that add

complex data types to the simple alphanumeric types.

 It borrows ideas from Object-Oriented Databases Object-

Types of databases
Web Databases (XDBMS)

 Database needs to be Web-enabled and XDBMS is Webemerging to fulfill this need.

 Standard Web pages are written in HTML


(Hyper Text Markup Language).

 XML (Extendable Mark-up Language) allows database Mark-

designers to specify their own extensions to the language and define their own data-types. data-

 XML is the new way to transfer data across the Web.

The Relational Model


The three elements are:
 Structural Element (DDL)  Manipulation Element (DML)  Integrity Element (Triggers, Validation, Constraints)

The Structural Element


 Columns in a table must be given distinct names  Each row in a table must be distinct  All field values in a given column must be drawn
from the same domain significance

 The order of columns and rows has no  Primary keys used to directly access rows in
tables

 Foreign keys used to link rows in different tables

Entity relationships
 Primary Key (underlined) (underlined)  Foreign Keys (italic) (italic)
Table Definitions:
PROPERTY (Uprn#, Name, Address, Value, ConArea# ) ( , CON_AREA (ConArea#, DateDec, Name ) (ConArea#,

Entity relationships
PROPERTY table
Uprn# Name Address Value ConArea# ConArea#

1 2 3 4 5

Mayes Jacobs St. Briggs Rural St. Reeves Canal St. Petch Peace Park Cornelius Hope Crescent

30000 60000 28000 55000 25000

1 null 2 1 2

Find conservation area of property 3 ? Find the row with Uprn#= 3 in the Property table Read the value of ConArea# field for this row Search the Con_Area table for a matching ConArea# value Read the name of the conservation area from Con_Area table.

CON_AREA table
ConArea# DateDec Name

1 2

1970 1975

Town Centre St. George

The Manipulation Element


 There are three main relational operators:
 SELECT  PROJECT  JOIN

 And few more:


   

UNION INTERSECT DIFFERENCE DIVIDE

SELECT
SELECT FROM Property WHERE Value >= 50,000 as Expensive_Property
EXPENSIVE_PROPERTY Uprn# Name Address Value 2 Briggs Rural St 60000 4 Petch Peace Park 55000

ConArea# ConArea# null 1

PROJECT
PROJECT Occupier, Address FROM Property as Address
ADDRESS
Occupier Mayes Briggs Reeve Petch Cornelius Address Jacobs St Rural St Canal St Peace Park Hope Crescent

JOIN
JOIN Property AND Con_Area ON ConArea# as Prop_Con
PROP_CON
Uprn# P.Name Address 1 3 4 5 Mayes Jacob's St Reeve Canal St Petch Peace Park CorneliusHope Cres. Value Con Area# 30000 1 28000 2 55000 1 25000 2 Date Dec 1970 1975 1970 1970 C.Name Town Centre St Georges Town Centre St Georges

More Relational Operations


 UNION
UNION Property AND New_Property as All_Property

 INTERSECT
New_Property INTERSECT 2bed_Property as New_2bed_Property

 DIFFERENCE
DIFFERENCE All_Property AND Council_Property as Private_Property

 DIVIDE
Divides a table of 2 columns with a table of 1 column.

The Integrity Element


Correctness and Accuracy of the data.   Entity Integrity  Primary key must be Unique and Not Null Referential Integrity. Integrity.
Foreign key must contain values that exist in the primary key of another table, or Null

1. Cascade Delete 2. 3.

Deleting the parent row will delete all rows in other tables that refer to it. Restrict Delete Cannot delete the parent row, unless no row is referring to it from other tables. Set to Null Upon deleting the parent row, its primary key is removed from all referencing rows in other tables and replaced by Null.

Structured Query Language


 Completeness
comprehensive database language  Simplicity less than 30 SQL commands  Declarative tell the computer what to do, not how to do it  Pseudo English verb (Create, Select, Grant, Drop, ), then object (database, table, record)

Database Definition Commands


SQL provides these data definition commands:
 CREATE TABLE  ALTER TABLE  DROP TABLE  CREATE VIEW  DROP VIEW  CREATE INDEX  DROP INDEX

CREATE TABLE
CREATE TABLE Property (Uprn# integer NOT NULL, Occupier char(20), Address char(30), Value decimal(10.2));

ALTER TABLE
ALTER TABLE ALTER TABLE Property ADD (ConArea# integer);

DROP TABLE
DROP TABLE DROP TABLE Property;

CREATE VIEW
CREATE VIEW ConProp (ConName, DateDec, Occupier, Address) AS SELECT Con_Area.Name, Con_Area.DateDec, Property.Name, Property.Address FROM Property, Con_Area WHERE Property.ConArea# = Con_Area.ConArea#;

DROP VIEW
DROP VIEW DROP VIEW ConProp;

CREATE INDEX
CREATE INDEX UNIQUE UprnIndex ON Property (Uprn# ASC);

DROP INDEX
DROP INDEX DROP INDEX UprnIndex;

Data Manipulation Commands


SQL provides four data manipulation commands:
    INSERT INTO UPDATE DELETE FROM SELECT

Examples of Manipulation Commands


 INSERT INTO
INSERT INTO Property (Address, Uprn#) VALUES (Smith St, 6);

 UPDATE
UPDATE Property SET Name = Heywood Value = 50000 WHERE Uprn# = 3;

 DELETE FROM
DELETE FROM Property WHERE Uprn# = 1;

SELECT
Selects all records in the Property table: SELECT * FROM Property; Selects all records in the Con_Area table: SELECT * FROM Con_Area;

SELECT
Selects all properties valued at more than 40000: SELECT * FROM Property WHERE Value > 40000;

SELECT
Selects the names of all properties valued more than the Heywoods property: SELECT Name FROM Property WHERE Value > (SELECT Value FROM Property WHERE Name = Heywood);

SELECT
Selects the id of all properties in the St. George conservation area that are valued less than 30000: SELECT Uprn# FROM Property, Con_Area WHERE Con_Area.Name = St. George AND Con_Area.ConArea# = Property.ConArea# AND Value < 30000;

SELECT
Using other keywords like:

   

DISTINCT HAVING ORDER BY GROUP BY

can further refine result of a SELECT statement

Data Access Control Commands


SQL provides two data access control commands:
 GRANT  REVOKE

GRANT
Grant the conservation officer the right to select, update, and alter the ConProp view: GRANT Select, Update, Alter ON ConProp TO ConOfficer;

REVOKE
Revoke the conservation officer the right to alter the ConProp view: REVOKE Alter ON ConProp FROM ConOfficer;

Logical Design
Entity-Attribute-Relationship (EAR)
EAR is used as a preliminary, broad brush technique, to develop an initial data model.

Normalization
Normalization can then be used to fine-tune the preliminary model into a conceptual data-model that is appropriate for use with the target relational database management system.

EAR Modeling
1. Identify entities
- nouns

2. Identify relationships between entities


- verbs

3. Identify attributes of entities


- adjectives

4. Defining tables

Identifying Entities
 Entities are objects or things, i.e.
anything that can be identified as having an independent existence and about which we needs to collect information.
 Examples:
 Student  Course  Subject

Identifying Relationships
 One to One:
 1 Husband is married to 1 Wife

 One to Many:
 1 Mother is parent of 0..m Children

 Many to Many:
 1 Student takes 1..m Courses  1 Course is taken by 1..m Students

Identifying Attributes
 Student entity attributes:
   

Student# Name Course#

 Course entity attributes:


   

Subject# Title Credits

Defining Tables
 STUDENT (Student#, Name, ...)  COURSE (Course#, Title, Credits, ...)  STUDENT-COURSE (Student#, Course#)
 We introduce this artificial link table to simplify a many-to-many relationship into two one-to-many relationships.

Student --> Student-Course <-- Course

Normalization
1. 2. 3. 4. 5. 6.
List all attributes Identify initial tables Apply 1st Normalization Apply 2nd Normalization Apply 3rd Normalization Check Relationships (in case any broke)

List all attributes


Identify all attributes associated with a system and list in one table.
Example:
FACTORY (FactId, Name, Address, DateEst, Area, AreaOfficer, ProdCode, ProdDesc, Brand, )

Identify initial tables


Group related attributes together

Example:
FACTORY (FactId, Name, Address, DateEst, Area, AreaOfficer, (ProdCode, ProdDesc, Brand), )

Apply

st 1

Normalization

 Factor out repeating groups to new tables.


FACTORY2 (FactId, Name, Address, DateEst, AreaNo, AreaOfficer) MADEBY (FactId, ProdCode, ProdDesc, Brand)

Apply 2nd Normalization


 If a table has a compound primary key then
remove all attributes that dont depend fully on the complete primary key.
FACTORY2 (FactId, Name, Address, DateEst, AreaNo, AreaOfficer) MADEBY2 (FactId, ProdCode, Brand) PRODUCT (ProdCode, ProdDesc)

A table is in 2nd normal form if it is in 1st normal form and all its non-key attributes are dependent upon the FULL compound primary key.

Apply 3rd Normalization


 The AreaOfficer is functionally dependent
not only upon FactId but also upon the value of AreaNo (a non-key attribute), so remove this transitive dependency by splitting the table further.
FACTORY3 (FactId, Name, Address, DateEst) AREA (FactId, AreaNo, AreaOfficer) A table is in third normal form if it is in second normal form, and each non-key attribute is not dependent on any other non-key attribute.

Normalization Summary
ONE FACT IN ONE PLACE
(avoid unnecessary duplications)

EACH NON-KEY ATTRIBUTE SHOULD NONBE DEPENDENT ON THE:


KEY (1NF), THE WHOLE KEY (2NF) and NOTHING BUT THE KEY (3NF)

Check the relationships


 In our case, we will find that we have not got the
necessary link in FACTORY to COMPANY, and so will need to insert CompId into FACTORY3.

 Eventually, we should end up with the following:


FACTORY (FactId, Name, Address, DateEst, CompId, AreaNo) COMPANY (CompId, Name, HQAddress) PRODUCT (ProdCode, ProductDescription) EMPLOYMENT (FactId, SurveyDate, EmpNo) MADEBY (FactId, ProdCode, Brand) AREA (FactId, AreaNo, AreaOfficer)

Physical Design
What are the most frequent queries
in the system?

What is the expected response time? How big will the database be? Whats the network bandwidth?

Response Time Factors


Indexes Choice of Primary Keys Table Splitting Table Combining (De-normalization) (De Query Optimization

Indexes
Indexes are applied on frequently used attributes to speed up access to the whole data in a record. An index creates a separate two-column table twowith attributes values sorted asc/desc and the primary key of their records. Subsequent access based on this attribute uses binary search to find the value and immediately follow the primary key into the main table.

Candidate Attributes for Indexing


the Primary key of any table should
always be uniquely indexed

all Foreign keys should be indexed as


these are the attributes which are used to join tables.

frequently used attributes should be


indexed, indexed, even though it may not be a key field (secondary or alternate key).

Choice of Primary Keys


SHORT: a short key is more efficient than a long
one

NUMERIC:numeric keys are preferred to


alphanumeric keys

MEANINGLESS: auto-numbered meaningless


attributes are well suited

UNCHANGING: the key attribute should be nonvolatile

Table Splitting
RDBMS process narrow tables more efficiently than wide tables.
 consider splitting the table into two tables, separating the frequently used attributes from the rarely used ones.  establish a one-to-one relationship between the two new tables.

Table Recombining
(De(De-normalization)

 Combine frequently used attributes

from separate tables to avoid having to perform repeated join operations.

 Recombining is de-normalization and may debegin to experience the update, insert and deletion anomalies which are designed out of databases in 3NF.

 Will introduce data duplication and


increase in database size.

Query Optimization
 Frequently used queries should be
optimized for an optimal response time.

 In a WHERE clause, the order of AND-ed ANDconditions can greatly influence the performance of the query:
Selecting a small subset and then joining it with another medium/large subset is much faster than the other way round.

SQL History and GIS


 SQL-86: First bare bones SQL standard published by ANSI (American SQLNational Standards Institute).

  SQL-89: Revised standard published by ISO (International Standards SQLOrganization).

 SQL- (SQLForward SQL-2 (SQL-92): Forward-looking revision specifying future development


of SQL.

 SQL-3 (SQL-99): Most significant revision for GIS was SQL- (SQL-

published in 1999. It accommodates ideas drawn from the Object Oriented database approach to provide a basis for an object-relational approach classes, objects, objectclasses, objects, complex data-types, inheritance, etc. data-types, inheritance,
SQLSQL-3 MM (Multi-Media) part, provides support for (Multimanipulating spatial data.

Spatial Capability
Oracle Spatial is built on SQL-3 MM SQL Network Data Model Topology Data Model Spatial Analytic Functions Geocoder (Universal coordinate system)

Oracle 10g spatial features


Network Data Model This model
explicitly stores and maintains connectivity within networks and provides network analysis capability such as shortest path and connectivity analysis. analysis.

The network data model also enables


Oracle's new Java-based geocoding Javaengine.

Oracle 10g spatial features


Topology Data Model This data model
and schema persistently store topology in the Oracle database. maintaining data integrity across maps and layers. involving relationships among features like adjacency, connectivity, and containment.

Persistent topology is a strong requirement for

Persistent topology also speeds queries

Oracle 10g spatial features


GeoRaster This new data type manages
georeferenced raster imagery (satellite imagery, remotely sensed data).

The GeoRaster feature includes an XML


schema for managing metadata and for basic operations like pyramiding, tiling pyramiding, and interleaving. interleaving.

Oracle 10g spatial features


Spatial Analytic Functions New spatial
analysis capabilities include classification, binning, association and spatial correlation.

These capabilities are essential for


business intelligence applications and are the foundation of spatially-enabled data spatiallymining.

Oracle 10g spatial features


Geocoder - Geocoding is the process of
associating geographic references like address and postal codes with latitude and longitude coordinates.

Oracle's geocoding engine is used with

3rd party data for functions like international address standardization and point of interest matching. matching.

Questions & Answers

Ali Adams
Higher Colleges of Technology www.geocities.com/aliadams ali.adams@hct.ac.uk

Potrebbero piacerti anche