Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… ·...

42
Systems Design DATABASE DESIGN

Transcript of Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… ·...

Page 1: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Systems DesignDATABASE DESIGN

Page 2: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose
Page 3: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Today’s planDatabase reviewRolesRelational databases

Converting domain model class diagram to relational databaseNormalization

Page 4: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Database DesignDefinition: a database is an integrated collection of stored data that is centrally managed and controlled

Definition: a database management system (DBMS) is a system software component that manages and controls one or more databases

Page 5: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Benefits of using a database/DBMSMany users can access the data at the same timeUse SQL processor to access data instead of having to write a specialized program

Uniform and consistent content controls and accessMakes it possible to distribute data across multiple servers and/or locations

Page 6: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Database Design and AdministrationTechnology EnvironmentProject Plan and ScheduleDatabase Design Team

Page 7: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Technology EnvironmentThe data and DBMS environment◦ Existing databases

◦ Upgrade or replace?◦ How many◦ Different types◦ Integrate with them

Hardware and Network Architecture◦ Organizations have an existing architecture:

◦ How will you integrate with it?

Page 8: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Project Plan and ScheduleWhen should you design your database?Factors that come into play:◦ Can it be designed in iterations?◦ Can it be designed early?◦ Does it need to be designed early to support other design?

Page 9: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Database Design TeamWhat needs to be considered?◦ Data standards◦ Data use◦ Data Quality

These are all the domain of the Data Administrator

Page 10: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Database Design TeamWhat else needs to be considered?◦ Managing the db after the system is deployed◦ Ensure the data and DBMS are:

◦ Configured correctly◦ Performing properly◦ Efficient

These are the responsibility of the Database Administrator

Page 11: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Relational DatabasesDefinition: a Relational Database Management System (RDBMS) is a dbms that organizes data in tables or relations

Page 12: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

RDBMS LingoTableTuple (row)Attribute (field)

Attribute value (field value)

Page 13: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

RDBMS LingoUnique KeyCandidate KeyPrimary Key

Foreign Key

Page 14: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Primary and Foreign KeysProductItemId Gender Description

1244 Man Casual Chino Trousers

1245 Man Fleece Crew Sweatshirt

1246 Man Fleece Crew V-neck

1247 Man Fleece Crew Zippered

InventoryItemID ProductItemId Size Colour

86779 1244 30/30 khaki

86780 1244 30/30 slate

86781 1244 30/30 tan

86782 1244 30/31 khaki

Page 15: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Designing Relational Databases1. Create table for each class in domain model2. Choose primary key3. Add foreign keys to represent 1---0..* relationships4. Create new tables to represent many to many relationships5. Represent and adjust classification hierarchies6. Define referential integrity constraints7. Evaluate schema quality and fix8. Choose appropriate data types9. Incorporate integrity and security controls

Page 16: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Create TablesTake all classes and make into tables◦ Use the same attributes◦ Use same names

Generalized and Specialized classes:◦ Treat as single class and collapse

Page 17: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose
Page 18: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Choose Primary KeysUse existing Unique ID◦ Make sure it is unique!

Make one if none existGive it a meaningful name◦ Include “ID” or “Code”

Page 19: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose
Page 20: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Represent AssociationsUse Foreign keysOne to many:◦ Add attribute of the “one” side to the “many” class’s table

Many to many:◦ A) have association class: add primary key of endpoint classes as foreign key

in table for association class◦ B) no association class: create new table to represent association

◦ Add pk from endpoint classes as fk of new table◦ Treat the concatenation of these pks as unique id -> candidate key◦ Add invented pk if desired

Page 21: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose
Page 22: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose
Page 23: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

ProductID

Page 24: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Representing Classification HierarchiesDeal with Specialization/Generalization classes◦ Special case in relational database design

Options:◦ Combine all tables into one table

◦ Superset of all classes

◦ Separate tables for child classes◦ Primary key of parent class as primary key of child class table

◦ Some combination of other two options

Page 25: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

ProductID

Page 26: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

ProductID

Page 27: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Enforcing Referential IntegrityReferential Integrity:◦ A constraint on database content◦ Every value as a foreign key in one table must have an equivalent value as a

primary key in associated table

Usually checked and maintained by RDMS

Page 28: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Database NormalizationSaleID Quantity SoldPrice ShipStatus Customer

NameCustomer Address

Customer phone

1234 2 4.99 Shipped Bob Geldof 1234 Street 250-222-3434

1235 3 99.99 Shipped Mary Lamb 12 Road rd 250-234-2345

1236 1 8.55 Shipped Mary Lamb 12 road rd 250-333-3434

Page 29: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Database NormalizationSIN Name Customer Address Customer phone Friends

987654321 Bob Geldof 1234 Street 250-222-3434 Mary Lamb, Bobby Tables

987612345 Mary Lamb 12 Road rd 250-234-2345 Bob Geldof, Bobby Tables

678912345 Bobby Tables 12 Road road 250-333-2323 Mary Lamb, Bob Geldof

Page 30: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Database NormalizationSIN Name Customer Address Customer phone Friends

987654321 Bob Geldof 1234 Street 250-222-3434 Mary Lamb

987654321 Bob Geldof 1234 Street 250-222-3434 Bobby Tables

987654321 Mary Lamb 12 Road rd 250-234-2345 Bob Geldof

987612345 Mary Lamb 12 Road rd 250-234-2345 Bobby Tables

678912345 Bobby Tables 12 Road road 250-333-2323 Mary Lamb

678912345 Bobby Tables 12 Road road 250-333-2323 Bob Geldof

Page 31: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

NormalizationIs your database:◦ Well-formatted?◦ Correct?

Normalization:◦ Allows flexibility◦ Minimizes redundancy◦ Prevents insertion/deletion/update anomalies◦ A formal technique of structuring the database

Page 32: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

1st Normal Form (1NF)Every field contains one value◦ All attributes are atomic

SIN Name Customer Address Customer phone Friends

987654321 Bob Geldof 1234 Street 250-222-3434 Mary Lamb, Bobby Tables

987612345 Mary Lamb 12 Road rd 250-234-2345 Bob Geldof, Bobby Tables

678912345 Bobby Tables 12 Road road 250-333-2323 Mary Lamb, Bob Geldof

Page 33: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

1st Normal Form (1NF)Every field contains one value◦ All attributes are atomic

SIN Name Customer Address Customer phone

987654321 Bob Geldof 1234 Street 250-222-3434

987612345 Mary Lamb 12 Road rd 250-234-2345

678912345 Bobby Tables 12 Road road 250-333-2323

FriendshipID SIN Friend

1 987654321 Mary Lamb

2 987654321 Bobby Tables

3 987612345 Bob Geldof

4 987612345 Bobby Tables

5 678912345 Mary Lamb

6 678912345 Bob Geldof

Page 34: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Aside: Functional DependencyA relationship between columnsValues in 1 or more column determine the values in anotherDefinition: Attribute B is functionally dependent on attribute A if for each value of A there is only one corresponding value of B

Page 35: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

Functional DependencyAre ProductItemId and Description functionally dependent?To Check:◦ Description is functionally dependent on

ProductItemId if for each value of ProductItemId there is only one corresponding value of Description

Is it true for all rows?

ProductItemId

Gender Description Supplier

1244 Man Casual Chino Trousers

West Coast

1245 Man Fleece Crew Sweatshirt

West Coast

1246 Man Fleece Crew V-neck

RMO

1247 Man Fleece Crew Zippered

Colorado Supply

Page 36: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

2nd Normal Form (2NF)In 2NF if:◦ In 1NF and◦ Each non-key is functionally dependent on entire primary key

Violates 2NF if ◦ Primary key of a table has > 1 attributes, and a non-key is functionally

dependent on only part of the primary key

Page 37: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

2nd Normal Form (2NF)Test Regular Price:

◦ RegularPrice is functionally dependent on PromoID if for each value of PromoID there is only one corresponding value of RegularPrice

◦ RegularPrice is functionally dependent on ProductItemID if for each value of ProductItemID there is only one corresponding value of RegularPrice

PromoID ProductItemID

RegularPrice PromoPrice

1 1234 599.99 529.99

1 9876 399.99 339.99

2 1234 599.99 449.00

2 9876 399.99 299.00

Page 38: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

2nd Normal Form (2NF)Test Regular Price:

◦ RegularPrice is functionally dependent on PromoID if for each value of PromoID there is only one corresponding value of RegularPrice

◦ RegularPrice is functionally dependent on ProductItemID if for each value of ProductItemID there is only one corresponding value of RegularPrice

PromoID ProductItemID

RegularPrice PromoPrice

1 1234 599.99 529.99

1 9876 399.99 339.99

2 1234 599.99 449.00

2 9876 399.99 299.00

Page 39: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

2nd Normal Form (2NF)

PromoID ProductItemID

PromoPrice

1 1234 529.99

1 9876 339.99

2 1234 449.00

2 9876 299.00

ProductItemID

Gender Description Price

1234 man Sweet pants 599.99

9876 woman Cool hat 399.99

5555 woman Lame hat 44.44

4444 woman Business socks 199.99

Page 40: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

ActivityCreate a schema:

Page 41: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

SummaryWe’ve seen:◦ Basic definitions of databases and database management systems◦ Seen how to create a database from domain model class diagram◦ How to ensure data is flexible, not redundant, and safe◦ How to normalize data

Page 42: Systems Design - Vancouver Island Universitycarruths/Courses/CSCI375/Lectures/10DatabaseD… · Designing Relational Databases 1. Create table for each class in domain model 2. Choose

ReadingsText Chapter 13 OO Design: Use Case RealizationText Chapter 7 Defining the System Architecturehttps://en.wikipedia.org/wiki/Systems_architecture

https://en.wikipedia.org/wiki/Deployment_diagram