(c) Addison Wesley Chapter 8 - Macquarie...

18
(c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design 1 MACIASZEK, L.A. (2001): Requirements Analysis and System Design. Developing Information Systems with UML, Addison Wesley Chapter 8 Database Design Copyright © 2000 by Addison Wesley Version 1.0 (c) Addison Wesley (c) Addison Wesley Chapter 8 Chapter 8 2 Topics Topics Database Design Design for Object Databases Design for Object-Relational Databases Design for Relational Databases (c) Addison Wesley (c) Addison Wesley Chapter 8 Chapter 8 3 Persistent database layer Persistent database layer Relational Oracle, Sybase, DB2, SQL Server Object-relational Oracle8, UniSQL Object ObjectStore, Versant Older models Hierarchical (IMS) Network (IDMS) Inverted or similar (Total, Adabas) Flat file

Transcript of (c) Addison Wesley Chapter 8 - Macquarie...

Page 1: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 1

MACIASZEK, L.A. (2001): Requirements Analysis and System Design. Developing Information Systems with UML, Addison Wesley

Chapter 8 Database Design

Copyright © 2000 by Addison WesleyVersion 1.0

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 22

TopicsTopics

Database Design

Design for Object Databases

Design for Object-Relational Databases

Design for Relational Databases

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 33

Persistent database layerPersistent database layerRelational• Oracle, Sybase, DB2, SQL Server

Object-relational• Oracle8, UniSQL

Object• ObjectStore, Versant

Older models• Hierarchical (IMS)• Network (IDMS)• Inverted or similar (Total, Adabas)

Flat file

Page 2: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 2

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 44

Data modelsData modelsAbstraction that presents the database structures in more understandable terms than as raw bits and bytesExternal (conceptual) data model• a high-level conceptual data model required by a

single applicationLogical (global conceptual) data model• a global integrated model to support any current

and expected applications that need access to the information stored in the database

Physical data model• defines how data is actually stored on persistent

storage devices

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 55

Application model & persistent modelApplication model & persistent model

Boundary Package

Control Package

Entity PackageDatabase

ConceptualModel

Database PackageApp-DB

InterfaceModel

Application UML Model

DatabaseLogical Model

DatabasePhysical Model

Persistent Database Layer

Boundary Package

Control Package

Entity PackageDatabase

ConceptualModel

Database PackageApp-DB

InterfaceModel

Application UML Model

DatabaseLogical Model

DatabasePhysical Model

Persistent Database Layer

Transient classes

Persistent classes or other data structures

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 66

Mapping objects to databaseMapping objects to databaseMapping difficulties• Storage structures of the database may have

little to do with the object-oriented paradigm–– Requires the conversion of classes in the Entity Requires the conversion of classes in the Entity

Package to Package to non objectnon object--oriented structuresoriented structures

• Database is almost never designed for a single application

–– Requires an optimal database Requires an optimal database design for all design for all applicationsapplications, not just the one under consideration, not just the one under consideration

Page 3: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 3

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 77

ODB modelODB modelODMG standards

The ODMG 3.0 standard (January 2000) –mapping objects to relational and other databases• Object Storage API that

–– works with any persistent data sourceworks with any persistent data source

–– implements the Database Packageimplements the Database Package

ODBMS• May not provide a separate DML language (such as SQL)

within a PL environment – PL extended with database objects, including navigational queries.

• However, Object SQL (OSQL) to access DB interactively

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 88

ODB modeling primitivesODB modeling primitivesObject has an OID

Literal has no OID – its value is like its identifier

Distinction between class (implementation) and type (specification) • Type can have multiple classes

Class has properties and operations• Property can be an attribute or a relationship

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 99

ODB literal typesODB literal typesAtomic (simple)• Numeric• Alphanumeric and special characters

Structured• Date• Time

Collection (template) – parameterized type• Set<t>• List<t>

Null• Specifies that a NULL value is allowed in an

atomic, structured or collection literal

Page 4: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 4

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 1010

ODB object ODB object yypesyypesAtomic object• e.g. DeptName

Structured object• Contains component object and/or literal types• e.g. Dept

Collection • The same set of possibilities as for the collection

literal but with parameters taking on object values

• e.g. set<Dept>

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 1111

Type declarations in ODBType declarations in ODB

Employeeemp_id : stringemp_name : PersonNamedate_of_birth : dategender : enum{M,F}phone_num : array<string>salary : float

<<ODB>>emp_name is structured object if the class PersonNameconsists of attributes such as family_name, first_name, middle_initial

Other attributes in Employeeare literal types

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 1212

Relationships and inverses in ODBRelationships and inverses in ODBODB model supports • Association

–– Implemented with collection object types, in particular Implemented with collection object types, in particular Set<> and List<>Set<> and List<>

–– The The inverseinverse keyword, explicit in the schema definition, keyword, explicit in the schema definition, enforces the referential integrity and eliminates the enforces the referential integrity and eliminates the possibility of dangling pointerspossibility of dangling pointers

• Generalization• Aggregation is only supported by constraining

an association• Keys – unique identifying values for objects of

the class–– However, However, keykey is not the principal way of object is not the principal way of object

identification (the identification (the OIDOID values serve this purpose)values serve this purpose)–– Key can be Key can be simplesimple or or compoundcompound

Page 5: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 5

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 1313

Associations in ODBAssociations in ODBStudent

<<at tribute>> name : string<<at tribute>> stud_id : string<<relationship>> crs_off : Set<CourseOffering>

<<ODB>>CourseOffering

<<attribute>> crs_name : string<<attribute>> semester : string<<relationship>> std : List<Student>

<<ODB>>

class Student{

attribute string name;attribute string stud_id;relationship Set<CourseOffering> crs_off

inverse CourseOffering::std;};

class CourseOffering{

attribute string crs_name;attribute string semester;relationship List<Student> std

inverse Student::crs_off;};

Student<<at tribute>> name : string<<at tribute>> stud_id : string<<relationship>> crs_off : Set<CourseOffering>

<<ODB>>CourseOffering

<<attribute>> crs_name : string<<attribute>> semester : string<<relationship>> std : List<Student>

<<ODB>>

class Student{

attribute string name;attribute string stud_id;relationship Set<CourseOffering> crs_off

inverse CourseOffering::std;};

class CourseOffering{

attribute string crs_name;attribute string semester;relationship List<Student> std

inverse Student::crs_off;};

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 1414

ISA and EXTENDS inheritanceISA and EXTENDS inheritanceISA relationship ≈ interface inheritance EXTENDS relationship ≈ implementation inheritance

The ODMG standard uses • Keyword interface when defining an abstract

class

• Keyword class is used only for the definition of concrete classes that can be instantiated

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 1515

Inheritance in ODBInheritance in ODB

PersonInterface<<ODB Interface>>

ManagerClassmanagerial_position : stringsalary_supplement : float

<<ODB Class>>

EmployeeClassemp_id : stringdob : date

age(in dob)

<<ODB Class>>

EXTENDS

ISA

Page 6: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 6

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 1616

From UML entity classes …From UML entity classes …In UML modeling • Attributes in classes are defined on

atomic data types and on few built-in structured data types (Date, Currency)

• Association roles imply the collections (templates)

But what about questions like:• “What if an employee has many

phone numbers? How should I model this during analysis? Do I really need to have a separate class of phone numbers?”

• “Can I model an employee name as a single attribute but with the internal structure recognizing that the name consists of the family name, first name and middle initial? Do I really need to have a separate class of employee names?”

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 1717

… To ODB… To ODB

Employee<<key>> employee_id : stringemployee_name : PersonLongName

<<ODB Class>>

PersonLongNamemiddle_initial : char

<<ODB Interface>>

PersonShortNamefamily_name : stringfirst_name : string

<<ODB Interface>>

ISA

ISA

Contact<<key>> contact_id : unsigned shortcontact_name : PersonShortNamephone : set<string>fax : set<st ring>email : set<string>

<<ODB Class>>

ISA

Not needed,Just to visualize the issue

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 1818

From UML associations …From UML associations …

Page 7: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 7

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 1919

… To ODB… To ODBOrganization

organization_id : unsigned shortphone : set<string>fax : set<string>email : stringis_current : booleanpostal_address : PostalAddresscourier_address : CourierAddresscontact : Set<Contact>task : Set<Task>

<<ODB Class>>

Taskdescription : stringcreated_dt : datevalue : floatcreated_emp : Employeeevent : List<Event>organization : Organizationcontact : Set<Contact>

<<ODB Class>>

Contact<<key>> contact_id : unsigned shortcontact_name : PersonShortNamephone : set<string>fax : set<string>email : set<string>postal_address : PostalAddresscourier_address : CourierAddressorganization : Organizationtask : Set<Task>

<<ODB Class>>

Employee<<key>> employee_id : stringemployee_name : PersonLongNamecreated_task : List<Task>created_event : Set<Event>due_event : List<Event>completed_event : Set<Event>

<<ODB Class>>

Eventdescription : stringtask : Taskcreated_dt : datecreated_emp : Employeedue_dt : datedue_emp : Employeecompleted_dt : datecompleted_emp : Employeepriority : char

<<ODB Class>>

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 2020

From UML aggregations …From UML aggregations …

AcademicRecordcourse_code : Stringyear : Datesemester : Integergrade : String

Course<<PK>> course_code : String<<CK>> course_name : Stringcredit_points : Integer

AcademicInCharge

Student<<PK>> s tudent_id : St ringstudent_name : Stringcurrent_fees : Money

0..*0..*CourseOffering

year : Datesemester : Integerenrolment_quota : Integer

0..*0..*

0..*

0..1

0..*

0..1

*

* takes

*

*

takes_crsoff

has_stud

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 2121

… To ODB… To ODB

Studentstudent_id : stringstudent_name : stringcurrent_fees : floatcourse_off : list<CourseOffering>academic_record : set<AcademicRecord>

<<ODB Class>>

YearSemesteryear : datesemester : unsigned short

<<ODB Interface>> Coursecourse_code : stringcourse_name : st ringcredit_points : unsigned shortcourse_offering : list<CourseOffering>

<<ODB Class>>

CourseOfferingyear_sem : YearSemesterenrolment_quota : unsigned shortcourse : Coursestudent : list<Student>academic_in_charge : AcademicInCharge

<<ODB Class>>AcademicRecordcourse_code : stringyear_sem : YearSemestergrade : st ring

<<ODB Interface>>

AcademicInChargecourse_off : list<CourseOffering>

<<ODB Class>>

Page 8: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 8

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 2222

From UML generalizations …From UML generalizations …

BetaTape VHSTape DVDDisk

VideoTapeis_taped_over : Boolean

VideoDiskdifferent_languages : Booleandifferent_endings : Boolean

MovieTit le<<PK>> movie_code : Stringmovie_t itle : Stringdirector : String/ is_in_stock : Boolean

VideoMediumvideo_condition : Byte$ number_currently_available : Integer0..*1 0..*1

available

RentalConditionsrental_period_in_days : Integerrental_charge_per_period : Currency

1..*

1

1..*

1apply

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 2323

… To ODB… To ODB

VideoMediumvideo_condition : char$ number_currently_available : unsigned shortrental_cond : RentalCondit ionsmovie_t it le : MovieTitle

<<ODB Interface>>

VideoTapeis_taped_over : boolean

<<ODB Interface>> VideoDiskdifferent_langages : booleandifferent_endings : boolean

<<ODB Interface>>

BetaTape<<ODB Class>>

VHSTape<<ODB Class>>

DVDDisk<<ODB Class>>

ISA ISA

ISA ISA ISA

MovieTitle<<key>> movie_code : stringmovie_title : stringdirector : string/ is_in_stock : booleanvideo_medium : set<VideoMedium>

<<ODB Class>>

RentalConditionsrental_period_in_days : unsigned shortrental_charge_per_period : float

<<ODB Interface>>

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 2424

ORDB ModelORDB ModelANSI and ISO standardsSQL:1999 standard• Many ORDB issues unresolved • Expected to be revised roughly every three years

The “next great wave”Upward compatible with RDB• Capable of processing relational data structures (relational

tables) and object data structures (object tables)• User-defined types extended with arbitrary complex

structured types

Migration from RDB to ORDB not really addressed by the standard

Page 9: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 9

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 2525

ORDB modeling primitivesORDB modeling primitivesStructured type ≈ ODB interface ≈ UML classMultiple interface inheritanceTable – the storage mechanismObject table – has columns defined as structured typesRow type• To specify complex internal structure without

even using structured types• Row type column implies nested table

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 2626

ORDB typesORDB typesUser-defined types• Distinct type ≈ ODB atomic object type• Structured type ≈ ODB structured object type

–– Attributes (state)Attributes (state)–– Operations (behavior)Operations (behavior)–– Operations to define equality/inequality, ordering, and Operations to define equality/inequality, ordering, and

conversion of structured type objectsconversion of structured type objects

Structured type attribute can be a collection• Set• List• Multiset• Array

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 2727

Type declarations in ORDBType declarations in ORDB

EmployeeTYemp_id : char(7)emp_name : PersonNamedate_of_birth : dategender : charphone_num : set(varchar(12))salary : money

<<structured type>>

Page 10: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 10

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 2828

ORDB object tablesORDB object tablesTable (object table) - a set of rows having one or more columnsObject - a row in object table (the smallest unit of data that can be inserted or deleted from a table)To store objects (instances of a type), a table has to be createdcreate table Employee of EmployeeTY;

SQL:1999 has not defined encapsulation of attributes of a structured type by operations observer (get) and mutator (set) operations generated automatically by ORDB for each attribute

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 2929

ORDB row typesORDB row types

create table Contact(contact_id integer,contact_name row

(family_name varchar(30),first_name varchar(20)),

postal_address row(po_box varchar(10),post_code varchar(10),address row

(street varchar(30),city varchar(20),state varchar(20),country varchar(25))));

Sequence of fields (<field name><data type> pairs)Column in a table can contain row values

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 3030

ORDB reference typesORDB reference types

Studentname : varchar(60)stud_id : char(8)crs_off : set(ref(CourseOffering))

<<object table>>CourseOffering

crs_name : varchar(40)semester : charstd : list (ref(Student))

<<object table>>

Keyword ref is used to define referencesemp ref(EmployeeTY)

In SQL:1999, reference type must be known at compilation type (i.e. dynamic classification not supported)Value of a reference type is an OIDCollections of references - to implement many-to-many associations

Page 11: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 11

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 3131

ORDB columns, fields, attributesORDB columns, fields, attributesColumn - structural component of a table• Can be nullable

• Can be an identity column (taking OID values)

• Can be a reference type

Field - structural component of a row type• Can be a reference type

Attribute - structural component of a structured type• Can be a reference type

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 3232

ORDB OF and UNDER inheritanceORDB OF and UNDER inheritance

Single implementation inheritance

Table hierarchycan correspond to type hierarchy(but a type can be “skipped” in table hierarchy)

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 3333

Mapping entity classes to ORDBMapping entity classes to ORDB

create table Contact of ContactTY(person_short_name row

(family_name varchar(30)first_name varchar(20)));

EmployeeTYemployee_id : char(8)employee_name : PersonLongNameTY

<<structured type>>

PersonLongNameTYfamily_name : varchar(30)first_name : varchar(20)middle_initial : char

<<structured type>>

person_short_namefamily_name : varchar(30)first_name : varchar(20)

<<row type>>ContactTY

contact_id : integerphone : set(varchar(15))fax : set(varchar(15))email : set(varchar(50))

<<structured type>>

Employee<<object table>>

Contactperson_short_name : row type

<<object table>>

OF

OF

create table Contact of ContactTY(person_short_name row

(family_name varchar(30)first_name varchar(20)));

EmployeeTYemployee_id : char(8)employee_name : PersonLongNameTY

<<structured type>>

PersonLongNameTYfamily_name : varchar(30)first_name : varchar(20)middle_initial : char

<<structured type>>

person_short_namefamily_name : varchar(30)first_name : varchar(20)

<<row type>>ContactTY

contact_id : integerphone : set(varchar(15))fax : set(varchar(15))email : set(varchar(50))

<<structured type>>

Employee<<object table>>

Contactperson_short_name : row type

<<object table>>

OF

OF Dependency relationship

Row type is NOT an OO concept!

Page 12: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 12

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 3434

Mapping associations to ORDBMapping associations to ORDB

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 3535

Mapping aggregations to ORDBMapping aggregations to ORDB

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 3636

Mapping generalizations to ORDBMapping generalizations to ORDBVideoMediumTY

video_condition : charrental_cond : RentalConditionsTY

<<structured type>>

VideoTapeTYis_taped_over : boolean

<<struc tured type>>VideoDiskTY

different_langages : booleandifferent_endings : boolean

<<structured type>>

BetaTapemovie_title : ref(MovieTitle)

<<object table>>

VHSTapemovie_title : ref(MovieTitle)

<<object table>>

DVDDiskmovie_title : ref(MovieTitle)

<<object table>>

MovieTitlebeta_tape : set(ref(BetaTape))vhs_tape : set(ref(VHSTape))dvd_disk : set(ref(DVDDisk))

<<object table>>

RentalConditionsTYrental_period_in_days : integerrental_charge_per_period : money

<<structured type>>

OF

MovieTitleTYmovie_code : char(10)movie_title : varchar(100)director : varchar(30)

<<structured type>>

OF

OF

OF

Page 13: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 13

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 3737

RDB modelRDB modelANSI and ISO standards

SQL92 – the current (and the last) standard

Replaced hierarchical and network database models

Will be replaced by ORDB model

ODB model provides Object Storage API to interface applications to RDB

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 3838

RDB modeling primitivesRDB modeling primitivesRelational table

• Columns with atomic values only

RDB model does not support:

• Object types and associated concepts

(inheritance, methods, etc.)

• Structured types

• Collections

• References

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 3939

RDB modeling primitivesRDB modeling primitives

Table

ViewReferential Integrity

Index

Stored procedure

Domain

Trigger

RuleColumn

defined on

defined on

applies to

consists ofdefined on

defined on

can call

can call

applies to

applies to

defined ondefined on

defined on

defined on

Key

defined on

Page 14: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 14

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 4040

RDB columns, domains, rulesRDB columns, domains, rulesColumn – atomic data typesDomain – legal set of values for a column

create domain Gender char(1);

Rule can define• default value (e.g. if no value is provided for city, assume

“Sydney”)• range of values (e.g. the allowed age is in the range 18 to

80)• list of values (e.g. the allowed color is “green”, “yellow” or

“red”)• case of value (e.g. the value must be in uppercase or

lowercase)• format of value (e.g. the value must start with letter “K”)

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 4141

RDB relational tablesRDB relational tablesFixed set of columnsAny number of rowsMathematical set → no duplicate rows → key

Primary keyCandidate (alternate) keys

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 4242

Referential integrityReferential integrityPrimary to foreign key correspondencePrimary and foreign keys defined on the same domainDeclarative or procedural (triggers)

Page 15: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 15

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 4343

Referential integrityReferential integrity

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 4444

TriggersTriggerscreate trigger keepdpt

on Departmentfor deleteasif @@rowcount = 0

return /* avoid firing trigger if no rowsaffected */

if exists(select * from Employee, deletedwhere Employee.dept_id =

deleted.dept_id)begin

print "Test for RESTRICT DELETE failed. Nodeletion"

rollback transactionreturn

endreturn

go

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 4545

Mapping entity classes to RDBMapping entity classes to RDB

Page 16: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 16

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 4646

Mapping associations to RDBMapping associations to RDB

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 4747

Mapping aggregations to RDBMapping aggregations to RDB

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 4848

From UML generalization …From UML generalization …

Person

Employee Student

StudentEmployee

Page 17: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 17

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 4949

… To RDB model… To RDB modelMap each class to a table

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 5050

… To RDB model… To RDB modelMap the entire class hierarchy to a single “superclass” table

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 5151

… To RDB model… To RDB modelMap each concrete class to a table

Page 18: (c) Addison Wesley Chapter 8 - Macquarie Universitycomp.mq.edu.au/books/maciaszek/student_resources/... · (c) Addison Wesley Chapter 8 MACIASZEK (2001): Req Analysis & Syst Design

(c) Addison Wesley Chapter 8

MACIASZEK (2001): Req Analysis & Syst Design 18

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 5252

… To RDB model… To RDB modelMap each disjoint concrete class to a table

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 5353

Mapping generalizations to RDBMapping generalizations to RDB

(c) Addison Wesley(c) Addison Wesley Chapter 8Chapter 8 5454

SummarySummaryThree levels of database models – external, logical and physical Mapping of objects to databases = mapping of a UML class model to a database logical modelThe mapping to the ODB logical model is the easiest The ORDB logical model is more complex than the ODB modelThe mapping to the RDB logical model is the most cumbersome