Top Banner
Hamada Ghenniwa, ECE, UWO 1 Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 1 SE352b Software Engineering SE352b Software Engineering Design Tools Design Tools W6: W6: Database Technology Database Technology Operational Database Operational Database Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 2 Software Tools Software Tools Systems Programming Tools Programming Language Tools Database System
36

Software Tools - Instructinstruct.uwo.ca/engin-sc/se352b/notes/HG-SE352Lect-W06... · 2006-03-04 · SE352b 3-Mar-06 (c) Hamada Ghenniwa, ECE, UWO 3 Feb 24, 2006 SE352b, ECE,UWO,

Mar 14, 2020

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 1

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 1

    SE352b Software Engineering SE352b Software Engineering Design ToolsDesign Tools

    W6: W6: Database TechnologyDatabase Technology

    Operational DatabaseOperational Database

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 2

    Software ToolsSoftware Tools

    Source Procedure A

    Source Procedure B

    Source Procedure C

    Systems ProgrammingTools

    Programming LanguageTools

    DatabaseSystem

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 2

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 3

    DB Related IssuesDB Related Issues

    Data Models

    Schemas

    Schema Architecture

    DBMS Languages

    DBMS Component Modules

    Architecture of DBMS

    Quality of Design in Relational DB

    SQL in general context

    Query Processing

    Interacting with DBs

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 4

    Classification of Classification of DBMSsDBMSs

    Based on the data model used:

    Traditional: Relational, Network, Hierarchical

    Emerging: Object-oriented, Object-relational

    Based on the number of users support

    single-user (typically used with micro-computers)

    multi-user (most DBMSs).

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 3

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 5

    Classification of Classification of DBMSsDBMSs(Cont(Cont.).)

    Based on architecturecentralized

    consists of a single DBMS managing a single DB

    distributed consists of a single DBMS (calleddistributed DBMS, or DDBMS) that manages several DB that might be located at different computing systems

    DBMS

    DB

    Centralized DBS

    DDBMS

    DB DB

    Distributed DBS

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 6

    Classification of Classification of DBMSsDBMSs(Cont(Cont.).)

    A multi-database system (MDBS) supports operations on multiple DBSs,

    each consists of separate DBMS or DDBMS

    MDBMS

    DB DB

    DBMS DDBMS

    Multi-DBS

    DB

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 4

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 7

    33--Level SchemaLevel Schema

    Internal/Local

    External

    Conceptual

    MDBS: 5MDBS: 5--Level SchemaLevel Schema

    To support the main characteristics of MDBS, a 5-level schema architecture is required

    an extended version of the 3-level schema

    1. External schema

    2. Federated schema

    an integration of multiple export schemas

    3. Export schema

    a subset of the component schema that is available to the FDBS

    4. Component schema

    the conceptual schema of a component

    5. Local schema

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 8

    DBMS

    DB

    Centralized DBS

    MDBMS

    DB DB

    DBMS DDBMS

    DB

    MDBS: 5MDBS: 5--Level SchemaLevel Schema

    DBDB--Component 1Component 1 DBDB--Component 2Component 2

    MDBMS

    DB DB

    DBMS DDBMS

    DB

    33--Level SchemaLevel Schema

    Internal/Local

    External

    Conceptual

    55--Level SchemaLevel Schema

    ExternalExternal External External

    Federated

    Export

    Component1

    Local

    Federated Federated

    Export

    Component2

    Local

    Export

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 5

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 9

    ThreeThree--Schema ArchitectureSchema Architecture(Cont.)(Cont.)

    DATABASE

    End User End User

    External LevelExternal LevelExternal Schema

    VIEWExternal Schema

    VIEWExternal Schema

    VIEWExternal Schema

    VIEW

    Conceptual LevelConceptual Level Conceptual SchemaConceptual Schema

    Internal LevelInternal Level Internal SchemaInternal Schema

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 10

    Views Views Example…Example…

    Section Id

    YearSemesterGradeCourse Number

    Student TranscriptStudent NameTRANSCRIPT

    Database

    Data generated on the flyUsing subject areas entitiesData generated on the flyUsing subject areas entities

    Brown

    Smith

    13599FallACS3380

    10299SpringBCS3320

    9298FallACS1310

    8598FallAMATH2410

    11299FallBMATH2410

    11999FallCCS1310

    Database Definition

    (MetaMeta--DataData)

    DepartmentCreditCourse NumberCourse NameCourse NameCOURSE

    InstructorYearYearSemesterSemesterCourse NumberSection IdentifierSection IdentifierSECTION

    MajorClassStudent NumberNameNameSTUDENT

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 6

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 11

    DB Related IssuesDB Related Issues

    Data Models

    Schemas

    Schema Architecture

    DBMS Languages

    DBMS Component Modules

    Architecture of DBMS

    Business Data Types

    Quality of Design in Relational DB

    SQL in general context

    Query Processing

    Interacting with DBs

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 12

    Business Data TypesBusiness Data Types

    Operational Data

    Informational Data DatabaseSystem

    OperationalDatabase

    Tools

    InformationalDatabase

    Tools

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 7

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 13

    Operational DataOperational DataThe data used to run the day-to-day business

    Typically stored in a relational database

    possibly, in legacy hierarchical or flat file formats as well

    Some characteristicsUpdated often and through online transactions

    Non-historical data (not more than three to six months old)

    Optimized for transactional processing

    Highly normalized in the relational database for efficient update, maintenance, and integrity

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 14

    Informational DataInformational Data

    Or information is a representation of the data in a way makes analysis much easier

    Analysis can be in the form of decision support

    executive information systems

    report generation

    more in-depth statistical analysis

    Created from the wealth of existing operational data

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 8

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 15

    RelationalRelational Database DesignDatabase Design

    ∗ Conceptual design∗ ER Model is used at this stage to capture

    the core business/application concepts and rules∗ which is then translated to a relational schema

    • Schema refinement• Normalization: to tune the schema for efficiency

    • check relational schema for redundancies and related anomalies

    Physical database design tuning• Consider typical workloads and further refine the database design

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 16

    Conceptual DesignConceptual Design

    • What?• the entitiesentities and relationshipsrelationships in the enterprise

    •• informationinformation about these entities and relationships should we store in the database

    • the integrity constraintsintegrity constraints that hold

    • Represent this information pictorially in ER diagramsER diagrams,

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 9

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 17

    Conceptual Design Conceptual Design ER Diagrams ER Diagrams ---- the basicsthe basics

    Entitiesreal world “objects”,

    e.g. Students, Courses, Routes, Climbers. Entity sets are drawn as rectangles:

    Attributesthings typically used as column names,

    e.g. Name, Age, Height.

    Relationshipsrelationships between entities,

    e.g. a student enrolls in a course, a climber climbs a route, etc. :

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 18

    ER: ExampleER: Example

    IDEF1X Notationhttp://www.idef.com/IDEF1X.html

    Account ID

    ACCOUNT

    Account Type

    Own

    CLIENT

    Client Name

    Client IDClient ID

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 10

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 19

    Relational:Relational:DefinitionsDefinitions

    Keya set of attributes of a relation whose values are unique within the relation

    If there is more than one key, they are called Candidate KeysCandidate Keys

    Prime attributePrime attributeone which is a member of any key

    Nonprime attribute

    one which is not prime

    primary keyarbitrarily designated candidate keydesignated candidate key

    all other candidate keys are Secondary KeysSecondary Keys

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 20

    Client IDClient IDPrimary KeyNot Nullifyable

    ER: ER: Identity & Functional DependencyIdentity & Functional Dependency

    IDEF1X Notation

    Account ID

    ACCOUNT

    Account Type

    CLIENT

    Account ID

    ACCOUNT

    Client NameAccount Type

    Client IDOwn

    Client ID (FK)

    OwnClient ID (FK) Foreign Key

    CLIENT

    Nonprimeattributes

    Client Name

    Client ID

    Not Nullifyable

    Nullifyable

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 11

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 21

    Associative EntitiesAssociative Entities

    An associative entity is a relation that contains data, and so may be treated as an entity.

    Client ID

    CLIENT

    Client Name

    Account ID

    ACCOUNT

    Account Type

    Resolved many-to-many Relationship

    PP

    Role

    ROLEClient IDAccount ID

    Role Type

    P P

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 22

    Translating ER to Relational SchemaTranslating ER to Relational Schema

    Each entity becomes a a tabletable (relation)

    The attributes associated with each entity become attributesattributes of the relationThe key is nonnon--nullifiednullified attributesattributes

    The keys of the associated entities with dependency are foreign keysforeign keys

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 12

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 23

    ER: ER: Identity & Functional DependencyIdentity & Functional Dependency

    IDEF1X Notation

    Client IDClient IDPrimary KeyNot Nullifyable

    Account ID

    ACCOUNT

    Account Type

    OwnClient ID (FK) Foreign Key

    CLIENT

    Nonprimeattributes

    Client Name

    Client ID

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 24

    Translating to RelationsTranslating to Relations

    CREATE TABLE CLIENT (Client_Number VARCHAR2(20) NOT NULL,Client_Name VARCHAR2(20) NULL,

    );

    CREATE TABLE CLIENT CLIENT (Client_Number VARCHAR2(20) NOT NULL,Client_Number VARCHAR2(20) NOT NULL,Client_Name VARCHAR2(20) NULL,

    );

    CREATE TABLE ACCOUNT (Account_Number VARCHAR2(20) NOT NULL,Client_Number VARCHAR2(20) NOT NULL,Account_Type VARCHAR2(20) NULL,

    );

    CREATE TABLE ACCOUNTACCOUNT (Account_Number VARCHAR2(20) NOT NULL,Account_Number VARCHAR2(20) NOT NULL,Client_Number VARCHAR2(20) NOT NULL,Client_Number VARCHAR2(20) NOT NULL,

    Account_Type VARCHAR2(20) NULL,);

    Client IDClient IDPrimary KeyNot Nullifyable

    Account IDAccount ID

    ACCOUNT

    Account Type

    OwnClient ID (FK)Client ID (FK) Foreign Key

    CLIENT

    Nonprimeattributes

    Client Name

    Client ID

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 13

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 25

    Database Management

    System

    Database SystemDatabase System

    Database

    Application

    Data Design vs. ImplementationData Design vs. ImplementationER vs. Relational DatabaseER vs. Relational Database

    Entity

    Attribute

    Instances

    Relationship

    Data Architecture(Logical Model)

    Inv.Lee456

    Reg.Smith123

    Account Type

    Client IDAccount ID

    ImplementationImplementationDesignDesign

    ⇒ Table

    ⇒ Columns

    ⇒ Rows

    ⇒ Foreign Keys

    Column

    Row

    ACCOUNT Table

    Client ID

    CLIENT

    Client Name

    Account IDACCOUNT

    Account Type

    POwnClient ID

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 26

    DB Related IssuesDB Related Issues

    Data Models

    Schemas

    Schema Architecture

    DBMS Languages

    DBMS Component Modules

    Architecture of DBMS

    Business Data Types

    Quality of Design in Relational DB

    SQL in general context

    Query Processing

    Interacting with DBs

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 14

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 27

    Quality of DB DesignQuality of DB Design

    Begin by discussing good and bad relation schemasschemas

    Informal measures of the quality of relation schema design

    Semantics of the attributes

    Reducing the redundant values in tuples

    Reducing the null values in tuples

    Define normal forms formal measures of the quality of schemas

    restrictions on the form of relation schemas

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 28

    Manuf is attribute of Car and Not of OwnerCarManuf is attribute of Car and Not of OwnerCar

    Semantics of the Relation AttributesSemantics of the Relation Attributes

    The elements of the schema should be easy to explain

    Keep attributes from different entities and relationships distinctExample

    OwnerCar: (OId,Oname, DLNum, CarId, Make, Manuf)OIdOnameDLNumCarIdMakeManuf

    OwnerCar

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 15

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 29

    Irrelevant to the Owner

    Redundant InformationRedundant Information

    Consider the following :(123,Joe, 123456789,106, Dodge, Chrysler)

    (223,Moe, 223456789, 107, Dodge, Chrysler)

    The Manuf attribute is redundant!

    This leads to difficulty in updates (Update Anomalies)e.g. changing the Manuf for owner with OId 123 requires also changing it for OId 223.

    OwnerCar: (OId, Oname, DLNum, CarId, Make, Manuf)OwnerCar: (OId, Oname, DLNum, CarId, Make, Manuf)

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 30

    Update AnomaliesUpdate Anomalies

    Insertion anomaliesWhen inserting a new owner,

    we must correctly insert the Manuf, or will create inconsistenciesCannot create a car without an owner

    Deletion anomaliesDeletion of owner of a car

    also deletes make and manufacturer of car

    Deletion of owner of the last Dodgedeletes relationship between Dodge and Chrysler

    Modification anomaliesChanging the make of a car requires consistency checkCannot change so that a Dodge is made by Ford

    OwnerCar: (OId,Oname, DLNum, CarId, Make, Manuf)OwnerCar: (OId,Oname, DLNum, CarId, Make, Manuf)

    (123,Joe, 123456789,106, Dodge, Chrysler)

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 16

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 31

    X Y is a full functional dependencyif the removal of any attribute Ai from X removes the dependency

    Not (X - {Ai} Y)

    X Y is a partial dependencyif some attribute Ai may be removed without removing the dependency

    X - {Ai} Y

    X Y is a full functional dependencyif the removal of any attribute Ai from X removes the dependency

    Not (X - {Ai} Y)

    X Y is a partial dependencyif some attribute Ai may be removed without removing the dependency

    X - {Ai} Y

    Normalization:Normalization:Definition of Functional DependencyDefinition of Functional Dependency

    Functional dependency

    a constraint between 2 sets of attributes from the database For each value of the first set there is a unique value of the second set

    XX YY restricts the tuples that can be instances of the relation Rs.t. if t1 and t2 are instances of R

    t1(X) = t2(X) then t1(Y) = t2(Y)For example, Client(CId, CName, CAddress)

    {CId} {CName, CAddress}

    A1A2

    A3A4A5

    R

    X Y

    } X

    } YCId

    CNameCAddress

    Client

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 32

    Normalization FormsNormalization Forms

    A relation R (table) is in

    1NF: if all attribute values are atomic (or indivisible)A relation that contains no repeating groups

    2NF: If it is in 1NF and every nonprime attribute is fully functionally dependent on the primary key of R

    3NF: if it is in 2NF andthere is no nonprime attribute that is functionally dependenton a non-key set of attributes

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 17

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 33

    UnUn--normalized Tablenormalized Table

    Key List List List CompositeAccountant

    NumberSkill

    NumberSkill

    Category ProfAccountant

    NameAccountant

    AgeOffice

    NumberOffice City

    Office Supervisor

    21 113 Systems 3 Alice Adams 55 52 NYC Brown113 Systems 5179 Tax 1204 Audit 6

    50 179 Tax 2 Charlie Cody 40 44 LA Green

    148 Consulting 6179 Tax 6

    LA Green

    NYC Brown

    35 Doug Baker

    77 Doug Doe 52 52

    32 44

    Instances with Repeated GroupsInstances with Repeated Groups

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 34

    1NF Table1NF Table

    Accountant Number

    Skill Number

    Skill Category Prof

    First Name

    Last Name

    Accountant Age

    Group Number

    Group City

    Group Supervisor

    21 113 Systems 3 Alice Adams 55 52 NYC Brown35 113 Systems 5 Doug Baker 32 44 LA Green35 179 Tax 1 Doug Baker 32 44 LA Green35 204 Audit 6 Doug Baker 32 44 LA Green50 179 Tax 2 Charlie Cody 40 44 LA Green77 148 Consulting 6 Doug Doe 52 52 NYC Brown77 179 Tax 6 Doug Doe 52 52 NYC Brown

    Key

    1NF: if all attribute values are atomic (or indivisible)A relation that contains no repeating groups

    1NF: if all attribute values are atomic (or indivisible)A relation that contains no repeating groups

    2NF: If it is in 1NF and every nonprime attribute is fully functionally dependent on the primary key of R

    2NF: If it is in 1NF and every nonprime attribute is fully functionally dependent on the primary key of R

    {Account Number, Skill Number} {Skill Category, Prof, …}{Account Number, Skill Number} {Skill Category, Prof, …}X Y

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 18

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 35

    2NF Table2NF Table

    Key

    Accountant Number

    First Name Last Name

    Accountant Age

    Group Number

    Group City

    Group Supervisor

    21 Alice Adams 55 52 NYC Brown35 Doug Baker 32 44 LA Green50 Charlie Cody 40 44 LA Green77 Doug Doe 52 52 NYC Brown

    Accountant Table

    KeySkill

    NumberSkill

    Category113 Systems179 Tax204 Audit148 Consulting

    Skill Table

    Accountant Number

    Skill Number Prof

    21 113 335 113 535 179 135 204 650 179 277 148 677 179 6

    Proficiency TableKey

    2NF: If it is in 1NF and every nonprime attribute is fully functionally dependent on the primary key of R

    2NF: If it is in 1NF and every nonprime attribute is fully functionally dependent on the primary key of R

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 36

    2NF Table2NF Table

    Key

    Accountant Number

    First Name Last Name

    Accountant Age

    Group Number

    Group City

    Group Supervisor

    21 Alice Adams 55 52 NYC Brown35 Doug Baker 32 44 LA Green50 Charlie Cody 40 44 LA Green77 Doug Doe 52 52 NYC Brown

    Accountant Table

    2NF: If it is in 1NF and every nonprime attribute is fully functionally dependent on the primary key of R

    2NF: If it is in 1NF and every nonprime attribute is fully functionally dependent on the primary key of R

    3NF: if it is in 2NF and there is no nonprime attribute that is functionally dependent on a non-key set of attributes

    3NF: if it is in 2NF and there is no nonprime attribute that is functionally dependent on a non-key set of attributes

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 19

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 37

    3NF Table3NF Table

    KeyGroup

    NumberGroup

    CityGroup

    Supervisor44 LA Green52 NYC Brown

    Group TableKey

    Accountant Number

    First Name

    Last Name

    Accountant Age

    Group Number

    21 Alice Adams 55 5235 Doug Baker 32 4450 Charlie Cody 40 4477 Doug Doe 52 52

    Accountant Table

    KeySkill

    NumberSkill

    Category113 Systems179 Tax204 Audit148 Consulting

    Skill Table

    Accountant Number

    Skill Number Prof

    21 113 335 113 535 179 135 204 650 179 277 148 677 179 6

    Proficiency TableKey

    3NF: if it is in 2NF and there is no nonprime attribute that is functionally dependent on a non-key set of attributes

    3NF: if it is in 2NF and there is no nonprime attribute that is functionally dependent on a non-key set of attributes

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 38

    NormalizationNormalization

    Table with repeating groups

    Table with repeating groups

    1NF1NF

    Remove repeating groupsRemove repeating groups

    2NF2NF

    Remove partial dependencyRemove partial dependency

    3NF3NF

    Remove transitive dependencyRemove transitive dependency

    Each attribute relies on the key, the whole key, and nothing but the key

    so help me codd!”

    Each attribute relies on the Each attribute relies on the keykey, , the the whole keywhole key, and nothing but the , and nothing but the keykey

    so help me so help me coddcodd!”!”

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 20

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 39

    Referential IntegrityReferential IntegrityIntegrity Constraints:

    A set of (business) rules that address the validity of references by one object in DB to some other object(s) in the DB

    Insertion Rule

    A row should not be inserted in the referencing table unlessthere already exists a matching entry in the referenced table

    Deletion Rule

    A row should not be deleted from the referenced table if there is a matching row(s) in the referencing table

    Enforcing Referential Integrity:Referential integrity options are declared when relational tables are defined

    then, enforced by DBMS

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 40

    DB Related IssuesDB Related Issues

    Data Models

    Schemas

    Schema Architecture

    DBMS Languages

    DBMS Component Modules

    Architecture of DBMS

    Business Data Types

    Quality of Design in Relational DB

    SQL in general context

    Query Processing

    Interacting with DBs

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 21

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 41

    SQLSQLStructured Query Language

    ANSI and ISO standarde.g., SQL2 or SQL-92

    SQL isa data definition language (DDL), and

    a data manipulation language (DML), and

    a programming (4th generation) language

    We can use SQL forLogical database specification (database schema definitions)

    Physical database specifications (indexes, etc.)

    Querying database contents

    Modifying database contents

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 42

    SQL as SQL as DDLDDL::Create Table StatementCreate Table Statement

    create table Customer (accountId int,lastName varchar(32),firstName varchar(32),street varchar(100),city varchar(32),state char(2),zipcode varchar(9)

    );

    Note that SQL has specific types

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 22

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 43

    Data types in SQLData types in SQL

    integer integer, int, smallint, long floating point float, real, double precision

    Numeric types

    formatted decimal(i,j), dec(i,j)fixed length char(n), character(n) Character-string

    types varying length varchar(n), char varying(n), character varying(n)

    fixed length bit(n) Bit-string types varying length bit varying(n)

    Date and time types

    date, time, datetime, timestamp, time with time zone, interval

    character long varchar(n), clob, text Large types binary blob

    create table Customer (accountId int,lastName varchar(32),firstName varchar(32),street varchar(100),city varchar(32),state char(2),zipcode varchar(9)

    )

    create table Customer (accountId int,lastName varchar(32),firstName varchar(32),street varchar(100),city varchar(32),state char(2),zipcode varchar(9)

    )

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 44

    SQL as SQL as DMLDMLInsert Into values;

    Insert IntoInsert Into employee (first, last, age, address, city, state) valuesvalues('Luke', 'Duke', 45, '2130 Boars Nest', 'Hazard Co', 'Georgia');

    Delete from where Operator [and|or Operator ]; [ ] = optional

    Delete fromDelete from employee wherewhere lastname = 'May';

    Update set [

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 23

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 45

    Relational Operations in SQLRelational Operations in SQLSelect statement

    select from where

    Projection in SQL using select clause

    SelectSelect title fromfrom Movies

    Selection in SQL using where clause

    select * from Customer wherewhere lastName = 'Doe'

    select distinctdistinct lastName, firstName from Customer

    no duplicates with distinct

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 46

    SQL as SQL as VDLVDLProducts and Joins in SQL vs. Products and Joins in SQL vs. ViewsViews

    Cartesian product in SQL using from clauseSelect * from Employee, Timecard

    Join using from and where clauses

    Select * from Employee, Timecard whereEmployee.sin = Timecard.sin

    Create View Test As

    Create View EmployeeCard As

    Select * from Employee, Timecard where Employee.sin = Timecard.sin;

    Create View EmployeeCard As

    Select * from Employee, Timecard where Employee.sin = Timecard.sin;

    create table Employee (Sin int,lastName varchar(32),firstName varchar(32),street varchar(100),city varchar(32),state char(2),zipcode varchar(9)

    )

    create tabletable Employee (Sin int,lastName varchar(32),firstName varchar(32),street varchar(100),city varchar(32),state char(2),zipcode varchar(9)

    )

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 24

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 47

    ThreeThree--Schema ArchitectureSchema Architecture(Cont.)(Cont.)

    DATABASE

    End User End User

    External LevelExternal LevelExternal Schema

    VIEWExternal Schema

    VIEWExternal Schema

    VIEWExternal Schema

    VIEW

    Conceptual LevelConceptual Level Conceptual SchemaConceptual Schema

    Internal LevelInternal Level Internal SchemaInternal Schema

    EmployeeCard

    Employee

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 48

    Views Views Example…Example…

    Section Id

    YearSemesterGradeCourse Number

    Student TranscriptStudent NameTRANSCRIPT

    Database

    Data generated on the flyUsing subject areas entitiesData generated on the flyUsing subject areas entities

    Brown

    Smith

    13599FallACS3380

    10299SpringBCS3320

    9298FallACS1310

    8598FallAMATH2410

    11299FallBMATH2410

    11999FallCCS1310

    Database Definition

    (MetaMeta--DataData)

    DepartmentCreditCourse NumberCourse NameCourse NameCOURSE

    InstructorYearYearSemesterSemesterCourse NumberSection IdentifierSection IdentifierSECTION

    MajorClassStudent NumberNameNameSTUDENT

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 25

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 49

    SQL: 4SQL: 4thth Generation PLGeneration PLNested QueriesNested Queries

    Nested select query

    Select videoId, dateAcquiredfrom Videotape where videoId in (

    select videoId from Rental where dateRented=‘1/1/03’)

    compare with

    Select v.videoId, dateAcquiredfrom Videotape v, Rental rwhere v.videoId = r.videoId and

    dateRented=‘1/1/03’)

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 50

    Exists and Unique QueriesExists and Unique Queries

    Find employees who have no time cards

    Select firstName, lastName from Employee ewhere not exists

    (select * from TimeCard t where e.sin=t.sin)

    Find managers who have exactly one time card

    select firstName, lastName from Employee ewhere unique

    (select * from TimeCard t where t.sin=e.sin)and exists

    (select * from Store where sin=manager)

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 26

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 51

    Aggregate FunctionsAggregate Functions

    How many employees work at Store 3

    select countcount (*) from WorksAt

    where storeId=3

    What is the average hourly rate of all hourly employees

    select averageaverage(hourlyRate) from HourlyEmployees

    How many different salaries are there?

    select count (distinct salary) from SalariedEmployee

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 52

    DB Related IssuesDB Related Issues

    Data Models

    Schemas

    Schema Architecture

    DBMS Languages

    DBMS Component Modules

    Architecture of DBMS

    Business Data Types

    Quality of Design in Relational DB

    SQL in general context

    Query Processing

    Interacting with DBs

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 27

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 53

    Database Management

    System

    Database SystemDatabase System

    Data

    ApplicationQuery ProcessingQuery Processing

    How to represent relational DB?Each table is a file

    File is a random access collection of records

    Query is executed by reading records from files

    Read record, create object in memory

    Process object

    Write result as a file of records or

    keep in memory

    Data file

    4625

    Byte address0

    File contentsAnimal House&189&comedy&@Duck Soup&987&comedy&@Elizabeth&450&costume drama&@

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 54

    IndexesIndexes

    If selection queries are frequent, sorting the file or building indexes is important

    Hash-based indexes only good for equality search

    Sorted files and tree-based indexes best for range searchalso good for equality search

    Data file

    4625

    Byte address0

    File contentsAnimal House&189&comedy&@Duck Soup&987&comedy&@Elizabeth&450&costume drama&@

    Index to file

    KeymovieId

    Address

    189450987

    04625

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 28

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 55

    IndexesIndexes

    An index on a file speeds up selections on the search key fields for the index

    Any subset of the records of a relation can be the search key for an index on the relation

    Search key is not the same as the relation key

    An index contains a collection of data entries, and

    supports efficient retrievalof all data entries with a given key value

    Data file

    4625

    Byte address0

    File contentsAnimal House&189&comedy&@Duck Soup&987&comedy&@Elizabeth&450&costume drama&@

    Index to file

    KeymovieId

    Address

    189450987

    04625

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 56

    B+ TreeB+ Tree

    B+ tree index: Balanced treesThe Most Widely Used Index

    Ordered sequential file, linked list of ordered records

    Good for range queriesSearch index for first record in range

    Read data file sequentially

    (linked list order) for rest of records

    Insert/delete

    at log f N cost;

    (f = fanout, N = # leaf pages)

    keep tree height-balanced

    B+ treeP Y

    A B C

    · · · ·

    Q S U W

    · · · ·

    X Y

    · ·

    H K M P

    · · · ·

    E G

    · ·

    C G P W Y

    A ... B ... E ... G ...

    Q ... S ...

    C ... H ... P ...

    W ...X ... Y ...Data file

    First level

    Third level

    Second level

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 29

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 57

    Example: Processing a Range QueryExample: Processing a Range Query

    select * from Customer where accountId >= 101 and accountId < 300select * from Customer where accountId >= 101 and accountId < 300

    347 903

    10 24 35

    • • •

    700 767 876 901

    • • • •

    902 903

    • •

    123 200 246 347

    • • • •

    101 110

    • •

    35 110 347 901 903

    Data file

    10 ... 24 ... 101 ... 110 ...35 ...

    123 ... 200 ... 246 ... 347 ... 700 ... 767 ...

    902 ... 903 ...876 ... 901 ...

    B+ tree index for accountId

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 58

    B+ Trees in PracticeB+ Trees in Practice

    Typical order: 100. Typical fill-factor: 67%.average fanout = 133

    Typical capacities:Height 4: 1334 = 312,900,700 records

    Height 3: 1333 = 2,352,637 records

    Can often hold top levels in buffer pool:Level 1 = 1 page = 8 Kbytes

    Level 2 = 133 pages = 1 Mbyte

    Level 3 = 17,689 pages = 133 MBytes

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 30

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 59

    TransactionsTransactions

    A transaction is

    a sequence of read and write operations on data items Logically, functions are treated as one unit of work

    should either be done entirely or not at all

    if it succeeds,the effects of write operations persist (commit);

    if it fails,no effects of write operations persist (abort)

    these guarantees are made despite concurrent activity in the system, and despite failures that may occur

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 60

    Desirable Properties of Transactions: Desirable Properties of Transactions: ACIDACID

    Atomicitythe property of a transaction that all of the updates are successful,or there is no update at all.

    Consistencyeach transaction should leave the database in a consistent state

    Properties such as referential integrity must be preserved

    Isolationeach transaction when executed concurrently with other transactions should have the same affect as if it had been executed by itself

    Durabilityonce a transaction has completed successfully, its changes to the database should be permanent.

    Even serious failures should not affect the permanence of a transaction

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 31

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 61

    Transactions in SQLTransactions in SQL

    A transaction begins when any SQL statement that queries the DB begins

    To end a transaction,

    the user issues

    a Commit or

    Rollback statement

    OperationsBegin

    Read

    Write

    Commit

    Rollback

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 62

    Example of TransactionExample of Transaction

    Begin transaction

    videoId video1 = select id of a copy of "Star Wars"

    if (video1 == null) rollback transaction

    insert row into Reservation for video1

    videoId video2 = select id of a copy of "Return of the Jedi"

    if (video2 == null) rollback transaction

    insert row into Reservation for video2

    videoId video3 = select id of a copy of "The Empire Strikes Back"

    if (video3 == null) rollback transaction

    insert row into Reservation for video3

    commit transaction

    Begin transaction

    videoId video1 = select id of a copy of "Star Wars"

    if (video1 == null) rollback transaction

    insert row into Reservation for video1

    videoId video2 = select id of a copy of "Return of the Jedi"

    if (video2 == null) rollback transaction

    insert row into Reservation for video2

    videoId video3 = select id of a copy of "The Empire Strikes Back"

    if (video3 == null) rollback transaction

    insert row into Reservation for video3

    commit transaction

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 32

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 63

    DB Related IssuesDB Related Issues

    Data Models

    Schemas

    Schema Architecture

    DBMS Languages

    DBMS Component Modules

    Architecture of DBMS

    Business Data Types

    Quality of Design in Relational DB

    SQL in general context

    Query Processing

    Interacting with DBs

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 64

    Interacting with Interacting with DBsDBs::Client Server ArchitectureClient Server Architecture

    UserInterface Application

    MiddlewareMiddlewareMiddleware

    layer

    DatabaseServer Database

    Server

    DatabaseServer layer

    DatabaseServer

    User and ApplicationClient layer

    UserInterface Application

    DBApplication

    DBApplication

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 33

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 65

    Interacting with Interacting with DBsDBs::Java EnvironmentJava Environment

    MiddlewareLayerMiddlewareMiddlewareLayerLayer

    DatabaseServer LayerDatabaseDatabaseServer LayerServer Layer

    Client LayerClient LayerClient Layer JavaJavaProgramProgram

    java.sqljava.sqlpackagepackage

    JDBC-ODBCbridge

    ODBC databaseclient

    Oracle Databaseclient

    Middlewareclient

    Middlewareserver

    Oracleserver

    Accessserver

    Sybaseserver

    Oracle Databaseclient

    Sybase Databaseclient

    Oracle ODBCclient

    Access ODBCclient

    JDBC packageJDBC package JDBC packageJDBC package JDBC packageJDBC package

    Java Database Connectivity (JDBC)

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 66

    Interacting with Interacting with DBsDBs::Java EnvironmentJava Environment

    Package java.sql includes interfaces

    Driver: supports the creation of a data connection

    Connection: represents the connection between a Java client and an SQL database server

    DatabaseMetaData: contains information about the database server

    Statement: includes methods for executing text queries

    PreparedStatement: represents a pre-compiled and stored query

    CallableStatement: used to execute SQL stored procedures

    ResultSet: contains the results of the execution of a select query

    ResultSetMetaData: contains information about a ResultSet, including the attribute names and types.

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 34

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 67

    /* CIA.java/* CIA.javaFrom http://sqlzoo.net By Andrew CummingFrom http://sqlzoo.net By Andrew Cumming

    */*/import java.sql.*;import java.sql.*;public class CIA{public class CIA{public static void main(String[] args){public static void main(String[] args){Connection myCon;Connection myCon;Statement myStmt;Statement myStmt;try{try{Class.forName("com.mysql.jdbc.Driver").newInstance();Class.forName("com.mysql.jdbc.Driver").newInstance();# Connect to an instance of mysql with the follow details:# Connect to an instance of mysql with the follow details:# machine address: pc236nt.napier.ac.uk# machine address: pc236nt.napier.ac.uk# database : gisq# database : gisq# user name : scott# user name : scott# password : tiger# password : tigermyCon = DriverManager.getConnection(myCon = DriverManager.getConnection(

    "jdbc:mysql://pc236nt.napier.ac.uk/gisq""jdbc:mysql://pc236nt.napier.ac.uk/gisq""scott","tiger");"scott","tiger");

    myStmt = myCon.createStatement();myStmt = myCon.createStatement();ResultSet result = myStmt.executeQuery(ResultSet result = myStmt.executeQuery(

    "SELECT * FROM cia WHERE population>200000000");"SELECT * FROM cia WHERE population>200000000");while (result.next()){while (result.next()){System.out.println(result.getString("name"));System.out.println(result.getString("name"));

    }}myCon.close();myCon.close();

    }}catch (Exception sqlEx){catch (Exception sqlEx){System.err.println(sqlEx);System.err.println(sqlEx);

    }}}}

    }}

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 68

    #!C:/perl/bin/perl#!C:/perl/bin/perluse CGI qw(:standard);use CGI qw(:standard);use Mysql;use Mysql;

    print header();print header();

    #Import the CGI parameters into the $Q structure#Import the CGI parameters into the $Q structureimport_names();import_names();

    #Connect to Andrew's MySQL server at Napier University#Connect to Andrew's MySQL server at Napier Universitymy $dbh = DBI->connect("dbi:mysql:database=gisq;host=pc236nt.napier.ac.uk;port=3306",my $dbh = DBI->connect("dbi:mysql:database=gisq;host=pc236nt.napier.ac.uk;port=3306",

    "scott", "tiger");"scott", "tiger");

    my $sth = $dbh->prepare("SELECT DISTINCT region FROM cia");my $sth = $dbh->prepare("SELECT DISTINCT region FROM cia");$sth->execute;$sth->execute;print "Here are some regions\n";print "Here are some regions\n";while (my ($region) = $sth->fetchrow_array()){while (my ($region) = $sth->fetchrow_array()){

    print "$region
    \n";print "$region
    \n";}}

    if ($Q::region) {if ($Q::region) {print "Here are the countries of $Q::region";print "Here are the countries of $Q::region";my $sth = $dbh->prepare("SELECT name, population FROM cia where region=?");my $sth = $dbh->prepare("SELECT name, population FROM cia where region=?");$sth->execute($Q::region);$sth->execute($Q::region);while (my ($name,$pop) = $sth->fetchrow_array()){while (my ($name,$pop) = $sth->fetchrow_array()){

    print "$name $pop
    \n";print "$name $pop
    \n";}}

    }}

    $dbh->disconnect;$dbh->disconnect;

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 35

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 69

    Interacting with Interacting with DBsDBs::Web Server ArchitectureWeb Server Architecture

    Web ServerMiddleware

    layer

    DatabaseServer Database

    ServerDatabase

    Server

    User and ApplicationClient layer

    WebBrowser

    DBApplication

    WebBrowser

    WebBrowser

    WebBrowser

    DBApplication

    FileSystems

    DatabaseServer layer

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 70

    Web Server Web Server ArchitecturesArchitectures

    The architecture type depends on kind and number of servers involved

    Different archictures different advantages and disadvantages

    Generally we can distinguish between

    2-tier architecture

    3-tier architecture

    n-tier architecture

    What matters: SPEEDSPEED

  • SE352b 3-Mar-06

    (c) Hamada Ghenniwa, ECE, UWO 36

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 71

    Advantages:easy and fast to setup

    easy to administrate

    Disadvantages:not fail safe (single point of failure)

    scales badly on high loads

    22--tier Architecturetier ArchitectureWeb server + module connecting to database

    HTTP-Request

    HTML-File Module

    Directory

    1

    2

    Web-Server

    DB

    Feb 24, 2006 SE352b, ECE,UWO, Hamada Ghenniwa 72

    33--tier Architecturetier ArchitectureWeb server + application server connecting to database

    1

    3

    2

    Web Server[Cluster]

    Application Server[Cluster]

    Other Servers[Cluster]

    DBRepl.

    DBDB DB

    Advantages:better scalabilty

    more reliable

    through various mechanisms

    offers better load balancing

    Disadvantages:complicated to set up an maintain