relational database model & database development process

30
Relational Database Model & Database Development Process IST359 M005 Yang Wang [email protected] 342 Hinds http://blackboard.sy r.edu

Upload: ewan

Post on 21-Feb-2016

57 views

Category:

Documents


0 download

DESCRIPTION

Relational Database Model & Database Development Process. IST359 M005. Yang Wang [email protected] 342 Hinds http://blackboard.syr.edu. Acknowledgements and caveat. These slides draw liberally, with permission, from the following sources: IST359 materials by Prof. Michael Fudge Jr. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Relational  Database Model &  Database  Development Process

Relational Database Model & Database Development Process

IST359 M005

Yang [email protected] Hindshttp://blackboard.syr.edu

Page 2: Relational  Database Model &  Database  Development Process

Acknowledgements and caveat

These slides draw liberally, with permission, from the following sources:

• IST359 materials by Prof. Michael Fudge Jr.

Caveat (beware): At best, PowerPoint slides are only a pale limitation of the entirety of a class meeting. In IST359 in particular, the lectures will cover topics beyond what appears in these slides. Don’t rely on them as a substitute for attending class.

Page 3: Relational  Database Model &  Database  Development Process

Learning Objectives

• Describe the Relational Model• Define relational terms and understand the

terminology in practice• Understand these relational terms through

practice• Explain the System Development Life Cycle • Explain the Database Life Cycle

Page 4: Relational  Database Model &  Database  Development Process

Learning Objectives

• Describe the Relational Model• Define relational terms and understand the

terminology in practice• Understand these relational terms through

practice• Explain the System Development Life Cycle • Explain the Database Life Cycle

Page 5: Relational  Database Model &  Database  Development Process

The Relational Database Model

Many DBMS products implement the Relational Model, but none of them enforce it. This permits rookie database designers to shoot themselves in the foot. And many have (including yours truly).

The Relational Model has: (Codd’s 3 rules)1)Data Independence

Clear separation between data and metadata2) Data Consistency

Minimal redundancy; the data adopts the “DRY” principle3) Easy to use - abstracted

You don’t have to understand the implementation to use it.

Page 6: Relational  Database Model &  Database  Development Process

You too can make a crappy DB like this one!Do you see problems with this database design?

Think Codd: Independence? Consistency? Ease-of-Use?

Change Sally Jones’s phone #? Add New Employee Bob Smith?

Page 7: Relational  Database Model &  Database  Development Process

Activity: Relational Terminology

Identify each of these :• Table• Relation• Row• Column• Tuple• Attribute• Atomic (Attrib.)• Logical Domain• Physical Domain

Item Category Asset#The ABC's of Excel Book 1InFocus Projector Hardware 2Prog. for Dummies Book 3Learning Perl Book 4Dell Laptop #1 Hardware 5Windows 2000 Server Software 6Office 2000 Premium Software 7Dell Laptop #2 Hardware 8

Asset

Page 8: Relational  Database Model &  Database  Development Process

Tables are “Buckets for your data”

Customers

Orders

Products

“Customers Place

Orders”

“Products Appear on

Orders”

Tables:• Specialized• Order doesn’t matter• Contain real itemsRelationships among tables:• Enforce business rules• Apply to real items

Page 9: Relational  Database Model &  Database  Development Process

What makes a table a relation?

A column or a set of columns

uniquely identify each row

Page 10: Relational  Database Model &  Database  Development Process

DBMS : Physical DomainSQL Server MySQL What Is It?

int int Signed Integer values -2G +2G

int identity serial Auto-incrementing integer (surrogate keys)

bit bit Used for true / false yes/no values.

decimal(n,d) decimal(n,d) A fixed-point signed decimal of n digits with d decimal places.

char(n) char(n) Exactly n characters, useful for fixed-length data.

varchar(n) varchar(n) Variable length of no more than n characters.

text text Variable length of 2G characters; not index able

datetime datetime For storing dates and or times.

Notes:• Different “flavors” of DBMSs use different data types.• It’s not part of the SQL Spec, but part of the DBMS implementation.

Page 11: Relational  Database Model &  Database  Development Process

DBMS: Logical Domain / Constraints

• Default Value – a value entered into an attribute for a row when one isn’t specified.

• Check Constraint – an expression which must be evaluated prior to the insertion of a row. E.g., employee_hourly_wage >= 0

• Unique Constraint – ensures duplicate values are not inserted into a column.

• Lookup table – a separate table containing all of the acceptable values for a given column.

Page 12: Relational  Database Model &  Database  Development Process

Keys• Candidate Key – any attribute or combination of attributes

that can uniquely identify each row • Primary Key - A candidate key which has been chosen by the

database designer to uniquely identify each• Surrogate Key - A primary key whose values are automatically

generated by the DBMS• Secondary Key - An attribute or combination of attributes

used for row retrieval from a table. In practice, secondary keys are index candidates in the table design.

• Foreign Key – an attribute or combination of attributes in one table whose values either match those of the primary key in another table or are null. Used to link relations.

Page 13: Relational  Database Model &  Database  Development Process

Activity: The Relational Table

Relation Name?Attributes?

Logical Domain of Columns?Physical Domain of Columns?Candidate keys?

Name Team Total Yds TDs

Emmitt Smith Cowboys 17162 153

Walter Payton Bears 16726 110

Barry Sanders Lions 15269 99

Emmitt Smith Cardinals 937 9

Page 14: Relational  Database Model &  Database  Development Process

The Primary Key Constraint

• Special selected constraint (you choose it)• Enforces entity integrity on the table.• Must be data unique for each row• Should be a candidate key

Page 15: Relational  Database Model &  Database  Development Process

Good PK … Bad PK

Good candidate key choices?• Customer Name?• Email Address?• Name and DOB?• SSN?• Customer Selected value?• Random Unique #?• Sequential Unique #? • Last two are examples of

surrogate keys

• The best PK’s– Don’t change …

ever!– Have no external

meaning– Do not

compromise security…

– Do not hinder performance …

Page 16: Relational  Database Model &  Database  Development Process

Activity: Which of these is a good PK?

Candidate keys? Best primary key? Why? Should a Surrogate key be used?

Page 17: Relational  Database Model &  Database  Development Process

The Foreign Key constraint

• The foreign key is a constraint on a column of one relation so that it can be associated with another relation.

• Foreign keys must have referential integrity – their values must come from the corresponding Primary Key column in the relation.

Page 18: Relational  Database Model &  Database  Development Process

Example of FK: The Lookup Table

Item Category Asset#The ABC's of Excel Book 1InFocus Projector Hardware 2Prog. for Dummies Book 3Learning Perl Book 4Dell Laptop #1 Hardware 5Windows 2000 Server Software 6Office 2000 Premium Software 7Dell Laptop #2 Hardware 8

Asset

CategoryIDBook

Hardware

SoftwareForeign Key

Page 19: Relational  Database Model &  Database  Development Process

Activity: Find the keys• Candidate? Primary? Foreign? Secondary? Surrogate?

Page 20: Relational  Database Model &  Database  Development Process

One more time.

Rule for joining tables

Tables

Columns: Physical Domain? Logical Domain? Allow Null?

Keys: Candidate? Primary? Foreign, Surrogate, Secondary?

Page 21: Relational  Database Model &  Database  Development Process

The Equi-Join of PK-FK at Work

EquiJoin

Page 22: Relational  Database Model &  Database  Development Process

FK Example: Implementation of a 1-M Relationship

Page 23: Relational  Database Model &  Database  Development Process

FK Example: Implementation of a M-N Relationship

This M-M Relationship has been resolved intotwo 1-M relationships

Page 24: Relational  Database Model &  Database  Development Process

Example:Null and Flags

EID Ename ETermDate EBenefitPlan101 Willie Survive BluePoint102 Mike Rophone 01/01/04103 Curt Tens 02/13/04104 Sara Doctorintahaus OrangePoint105 Dustin Dawind

Employee

EID Ename ETermDate EBenefitPlan101 Willie Survive BluePoint102 Mike Rophone 01/01/04 Term103 Curt Tens 02/13/04 Term104 Sara Doctorintahaus OrangePoint105 Dustin Dawind Op-Out

Employee

Null makes sense for this

column

103 and 105 are null for different reasons!

Flags used to represent

different status

Nulls typically cause problems in Varchar andNumeric, and bit fields

Page 25: Relational  Database Model &  Database  Development Process

Data Models: Abstraction Levels

Conceptual

Internal

External

Physical

Logical

Highly AbstractHardware and Software

Independent

Somewhat Abstract Hardware IndependentSoftware Dependent

Not Abstract (Concrete)Hardware and Software Dependent

Page 26: Relational  Database Model &  Database  Development Process

An Concrete Example• SU administration asks us to build a database for class enrollment

o E.g., student info, class info, a student enrolls in a class• Logical model: what database model to use? Relational, object-

oriented, object-relational, etc. o E.g., we use relational model (entities, relations).

• Internal model: use a DBMS to implement our relational modelo E.g., what do the student, class tables look like?

• External model: what the internal model looks like to end users (hide details of the implementation, e.g., tables)o E.g., you can see a student enrolls in a class

• Physical model: how database is implemented by the DBMSo E.g., how is the database physically stored? In a single file?

• Conceptual model: represent requirements, what needs to be represent in the database?

Page 27: Relational  Database Model &  Database  Development Process

Systems Development Lifecycle

IPlanning

IIAnalysis

IIIDesign

IVImplementation

VMaintenance &

Support

Time

Res

ourc

es

Conceptual Model

LogicalModel

Internal / ExternalModel

SDLC / DBLC

PhysicalModel

Page 28: Relational  Database Model &  Database  Development Process

Alternatives

• Fast prototyping • Agile development• eXtreme programming• Hackathon

Page 29: Relational  Database Model &  Database  Development Process

Exercise

• Design a database for on-campus parking info– Car– Parking lots– A parking permit allows a car to park in a certain

parking lot• Tables and keys

Page 30: Relational  Database Model &  Database  Development Process

Permit_ID SUID Car License Lot num Price

Permits (table)

SUID Last name First name Gender School

Persons (table)

Car License Brand Model Color

Cars (table)