Download Relational Model

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Concurrency control wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

SQL wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Clusterpoint wikipedia , lookup

Relational algebra wikipedia , lookup

Database wikipedia , lookup

Database model wikipedia , lookup

Relational model wikipedia , lookup

Transcript
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.
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.
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
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
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 metadata
2) Data Consistency
Minimal redundancy; the data adopts the “DRY” principle
3) Easy to use - abstracted
You don’t have to understand the implementation to use it.
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?
Activity: Relational Terminology
Identify each of these :
• Table
• Relation
• Row
• Column
• Tuple
• Attribute
• Atomic (Attrib.)
• Logical Domain
• Physical Domain
Asset
Item
Category Asset#
The ABC's of Excel Book
1
InFocus Projector
Hardware
2
Prog. for Dummies
Book
3
Learning Perl
Book
4
Dell Laptop #1
Hardware
5
Windows 2000 Server Software
6
Office 2000 Premium Software
7
Dell Laptop #2
Hardware
8
Tables are “Buckets for your data”
“Products
Appear on
Orders”
“Customers
Place
Orders”
Orders
Customers
Tables:
• Specialized
• Order doesn’t matter
• Contain real items
Relationships among tables:
• Enforce business rules
• Apply to real items
Products
What makes a table a relation?
A column or a set of columns
uniquely identify each row
DBMS : Physical Domain
SQL 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.
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.
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.
Activity: The Relational Table
Name
Team
Emmitt Smith
Cowboys
Total Yds TDs
17162
153
Walter Payton Bears
16726
110
Barry Sanders Lions
15269
99
937
9
Emmitt Smith
Relation Name?
Attributes?
Cardinals
Logical Domain of Columns?
Physical Domain of Columns?
Candidate keys?
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
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 …
Activity: Which of these is a good PK?
Candidate keys? Best primary key? Why? Should a Surrogate key be used?
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.
Example of FK: The Lookup Table
Asset
Item
Category Asset#
The ABC's of Excel
Book
1
InFocus Projector
Hardware
2
Prog. for Dummies
Book
3
Learning Perl
Book
4
Dell Laptop #1
Hardware
5
Windows 2000 Server Software
6
Office 2000 Premium Software
7
Dell Laptop #2
Hardware
8
CategoryID
Book
Hardware
Foreign Key
Software
Activity: Find the keys
• Candidate? Primary? Foreign? Secondary? Surrogate?
One more time.
Tables
Rule for joining tables
Columns: Physical Domain? Logical Domain? Allow Null?
Keys: Candidate? Primary? Foreign, Surrogate, Secondary?
The Equi-Join of PK-FK at Work
Equi
Join
FK Example: Implementation of a 1-M
Relationship
FK Example: Implementation of a M-N
Relationship
This M-M Relationship
has been resolved into
two 1-M relationships
Example:Null and Flags
Nulls typically cause
problems in Varchar and
Numeric, and bit fields
Employee
EID
101
102
103
104
105
Ename
ETermDate EBenefitPlan
Willie Survive
BluePoint
Mike Rophone
01/01/04
Curt Tens
02/13/04
Sara Doctorintahaus
OrangePoint
Dustin Dawind
103 and 105 are
null for different
reasons!
Null makes sense
for this column
Employee
EID
101
102
103
104
105
Ename
ETermDate EBenefitPlan
Willie Survive
BluePoint
Mike Rophone
01/01/04 Term
Curt Tens
02/13/04 Term
Sara Doctorintahaus
OrangePoint
Dustin Dawind
Op-Out
Flags used to
represent
different status
Data Models: Abstraction Levels
Conceptual
Logical
Internal
Highly Abstract
Hardware and Software
Independent
Somewhat Abstract
Hardware Independent
Software Dependent
External
Physical
Not Abstract (Concrete)
Hardware and Software Dependent
An Concrete Example
• SU administration asks us to build a database for class enrollment
• Conceptual model: represent requirements, what needs to be
represent in the database?
o E.g., student info, class info, a student enrolls in a class
• Logical model: what database model to use? Relational, objectoriented, object-relational, etc.
o E.g., we use relational model (entities, relations).
• Internal model: use a DBMS to implement our relational model
o 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 DBMS
o E.g., how is the database physically stored? In a single file?
Systems Development Lifecycle
SDLC / DBLC
II
Analysis
III
Design
IV
Implementation
Resources
I
Planning
Logical
Model
Conceptual
Model
Physical
Model
Time
Internal / External
Model
V
Maintenance &
Support
Alternatives
•
•
•
•
Fast prototyping
Agile development
eXtreme programming
Hackathon
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
Permits (table)
Permit_ID SUID
Car License
Lot num
Price
First name
Gender
School
Persons (table)
SUID Last name
Cars (table)
Car License
Brand Model Color