Sei sulla pagina 1di 44

Logical Database Design and the Relational Model

Management Information and Control Systems

Relation
Definition: A relation is a named, twodimensional table of data
Table is made up of rows (records), and columns (attribute or field)

Not all tables qualify as relations Requirements:


Every relation has a unique name Every attribute value is atomic (not multivalued, not composite) Every row is unique (cant have two rows with exactly the same values for all their fields) Attributes (columns) in tables have unique names The order of the columns is irrelevant The order of the rows is irrelevant
Management Information and Control Systems
2

Correspondence with ER model

Relational model is one logical data model


Forms the base for a DBMS DBMS then stores, manages etc. data

ERDs can be transferred into a corresponding set of relations


Guidelines allow for almost automatical translation

Next step in database design process

Management Information and Control Systems

Correspondence with ER model

Relations (tables) correspond with entity types (and with many-to-many relationship types) Columns correspond with attributes Rows correspond with entity instances (and with many-to-many relationship instances) NOTE: The word relation (in relational database) is NOT the same as the word relationship (in ER model)
Management Information and Control Systems
4

Key fields
Keys are special fields that serve two main purposes:
Primary keys are unique identifiers of the relation in question. Examples include employee numbers, social security numbers, etc. This is how we can guarantee that all rows are unique. Foreign keys are identifiers that enable a dependent relation (on the many side of a relationship) to refer to its parent relation (on the one side of the relationship).

Keys can be simple (a single field) or composite (more than one field). Keys usually are used as indexes to speed up the response to user queries.
Management Information and Control Systems
5

Schema for four relations (Pine Valley Furniture)

Primary Key Foreign Key


(implements 1:N relationship between customer and order)

Combined, these are a composite primary key (uniquely identifies the order line)individually they are foreign keys (implement M:N relationship between order and product)

Management Information and Control Systems

Integrity constraints

Domain Constraints
Allowable values for an attribute (e.g. Social Security Number, dates,...).

Entity Integrity
No primary key attribute may be null. All primary key fields MUST have data.

Action Assertions
Business rules.

Management Information and Control Systems

Integrity constraints
Referential Integrity rule that states that any foreign key value (on the relation of the many side) MUST match a primary key value in the relation of the one side (or the foreign key can be null).
For example: Delete Rules
Restrict dont allow delete of parent side if related rows exist in dependent side (do not delete customer if there are orders) Cascade automatically delete dependent side rows that correspond with the parent side row to be deleted (if a customer is delete, delete all orders) Set-to-Null set the foreign key in the dependent side to null if deleting from the parent side not allowed for weak entities
Management Information and Control Systems
8

Referential integrity constraints (Pine Valley Furniture)

Referential integrity constraints are drawn via arrows from dependent to parent table

Management Information and Control Systems

Transforming ER diagrams into relations


Mapping Regular Entities to Relations
1. 2. 3. Simple attributes: E-R attributes map directly onto the relation Composite attributes: Use only their simple, component attributes Multi-valued Attribute: Becomes a separate relation with a foreign key taken from the superior entity

Management Information and Control Systems

10

Mapping a regular entity


(a) CUSTOMER entity type with simple attributes

(b) CUSTOMER relation

Management Information and Control Systems

11

Mapping a composite attribute


(a) CUSTOMER entity type with composite attribute

(b) CUSTOMER relation with address detail

Management Information and Control Systems

12

Mapping a multivalued attribute (a)

Multivalued attribute becomes a separate relation with foreign key

(b)

1 to many relationship between original entity and new relation


Management Information and Control Systems
13

Transforming ER diagrams into relations


Exercise
Provide the relation(s) for the following ERD.

Name Professor Age Languages Adress Street

Country
Management Information and Control Systems
14

Transforming ER diagrams into relations


Mapping Weak Entities
Becomes a separate relation with a foreign key taken from the superior entity Primary key composed of: Partial identifier of weak entity Primary key of identifying relation (strong entity)

Management Information and Control Systems

15

Example of mapping a weak entity


Weak entity DEPENDENT

Management Information and Control Systems

16

Relations resulting from weak entity

NOTE: the domain constraint for the foreign key should NOT allow null value if DEPENDENT is a weak entity

Foreign key

Composite primary key


Management Information and Control Systems
17

Transforming ER diagrams into relations


Mapping Binary Relationships
1. One-to-Many - Primary key on the one side becomes a foreign key on the many side 2. Many-to-Many - Create a new relation with the primary keys of the two entities as its (composite) primary key 3. One-to-One - Primary key on the mandatory side becomes a foreign key on the optional side

Management Information and Control Systems

18

Example of mapping a 1: M relationship Relationship between customers and orders

Note the mandatory one

Management Information and Control Systems

19

Mapping the relationship

Again, no null value in the foreign keythis is because of the mandatory minimum cardinality

Foreign key

Management Information and Control Systems

20

Example of mapping an M : N relationship ER diagram (M:N)

TheSuppliesrelationshipwillneedtobecomeaseparaterelation...
Management Information and Control Systems
21

Three resulting relations

Composite primary key

Foreign key Foreign key

New intersection relation

Management Information and Control Systems

22

Mapping a binary 1:1 relationship ER diagram (1:1)

Management Information and Control Systems

23

Resulting relations

Management Information and Control Systems

24

Transforming ER diagrams into relations


Exercise
Provide the relation(s) for the following ERD.

P_ID Price Product

Customer C_ID Name Adress Date

buys Invoice supplied ID Name Country Supplier

Management Information and Control Systems

25

Transforming ER diagrams into relations


Mapping Unary Relationships
One-to-Many - Recursive foreign key in the same relation Many-to-Many - Two relations: One for the entity type One for an associative relation in which the primary key has two attributes, both taken from the primary key of the entity

Management Information and Control Systems

26

Mapping a unary 1: N relationship

(a) EMPLOYEE entity with Manages relationship

(b) EMPLOYEE relation with recursive foreign key Management Information and Control Systems
27

Mapping an unary M : N relationship

(a) Bill-of-materials relationships (M:N)

(b) ITEM and COMPONENT relations

Management Information and Control Systems

28

Transforming ER diagrams into relations


Mapping Ternary (and n-ary) Relationships
One relation for each entity and one for the relationship Example: PATIENT TREATMENT relationship Assumption: The same patient may visit the same physican for the same treatment for more than once in single day.

Management Information and Control Systems

29

Mapping a ternary relationship

Management Information and Control Systems

30

Mapping the ternary relationship

Remember that the primary key MUST be unique

Management Information and Control Systems

31

Transforming ER diagrams into relations


Exercise
Provide the relation(s) for the following ERD.

Date Customer C_ID Name Adress buys Invoice Number Amount


32

Product P_ID Price

Management Information and Control Systems

Transforming ER diagrams into relations


Mapping Supertype/Subtype Relationships
One relation for supertype and for each subtype Supertype attributes (including identifier and subtype discriminator) go into supertype relation Subtype attributes go into each subtype; primary key of supertype relation also becomes primary key of subtype relation 1:1 relationship established between supertype and each subtype, with supertype as primary table

Management Information and Control Systems

33

Supertype/subtype relationships

Management Information and Control Systems

34

Mapping Supertype/subtype relationships to relations

Management Information and Control Systems

35

Transforming ER diagrams into relations


Exercise
Provide the relation(s) for the following ERD.

ID Content Course

Name Instructor teaches

Term

takes Part-time Full-time Student manages Hours advises Grade Number Name
36

Management Information and Control Systems

Normalization
Normalization: process which aims at minimizing duplication of information and safeguarding the database against certain types of logical or structural problems (data anomalies)
e.g., when multiple instances of a given piece of information occur in a table, these instances might not be kept consistent when data is updated

Based on concept of functional dependency between attributes


A -> B: attribute A determines B, B is functionally dependent on A (if value of A is known, value of B is known) e.g. Social_security_number -> Name A -> B, B -> C: C is transitively dependent on A
Management Information and Control Systems
37

Functional Dependencies
Functional dependencies and keys
for an attribute to be a candidate key, it has to determine (directly or transitively) all other attributes and needs to have minimal length e.g., in a relation (ABCD, A -> B, A -> C, B -> D), A is the only candiate key e.g., in a relation (ABCD, A -> B, B -> A, A -> C, A -> D), both A and B are candidate keys e.g., in a relation (ABC, A -> B), the only candidate keys is AC (a composite key)

Management Information and Control Systems

38

Normal Forms
1st normal form
each attribute has single values, does not allow duplicate rows all relations are in 1st normal form

2nd normal form


if in 1st normal form, and none of the non-prime attributes of the table are functionally dependent on a part (proper subset) of a candidate key e.g. (ABC, A -> B), or (Course, Room -> Seats)

3rd normal form


if in 2nd normal form, and there are no transitive dependencies e.g. (ABC, A -> B, B -> C), or (Course -> Lecturer -> Age)

Higher normal forms exist


Management Information and Control Systems
39

Normal Forms
Exercise
For each relation, give the set of candidate keys, and the normal form the relation is in. 1.(ABCD, A -> B) 2.(ABCDE, A -> C, B -> A, C -> D) 3.(ABCD, AB -> C, D -> C) 4.(ABCDE, A -> C, B -> C, C -> D, C -> E) 5.(ABCD, A -> B, A -> C, A -> D) 6.(ABCD, B -> A, A -> B, A -> C, B -> C, C -> D) 7.(ABCDE, A -> E, E -> B, B -> A, C -> D)

Management Information and Control Systems

40

Normal Forms
Exercise
Entity and variable vs. relationship. Which ERD could be right (think about FDs)?

Account

Number Currency

Account

Number Currency Conversion rate

Account

in

Currency Conversion rate

Management Information and Control Systems

41

Other Logical Data Models


Hierarchical model
data is organized into an inverted tree-like structure, implying a multiple downward link in each node to describe the nesting, and a sort field to keep the records in a particular order in each same-level list unit in the model is a record which is also known as a node, each record on one level can be related to multiple records on the next lower level (parent children) well suited for one-to-many relationships with other data elements in the database, advantageous when the data elements are inherently hierarchical

Management Information and Control Systems

42

Other Logical Data Models


Network model
records can participate in any number of named relationships, which associates a record of one type (owner) with multiple records of another type (member) sets difference to hierarchical model is that a child can have a number of parents

Object-relational database model


adds new object storage capabilities to relational databases attempt to bring the database world and the application programming world closer together key ideas of object programming, such as encapsulation and polymorphism
Management Information and Control Systems
43

Other Logical Data Models


Hypermedia database model
stores chunks of information in nodes that can contain data in a variety of media users can branch to related data in any kind of relationship WWW uses concepts of hypermedia

Specialized database models


Geographical databases: locational data and its handling Timed databases: special support for timestamps, changes in data, scope,... ...

Management Information and Control Systems

44

Potrebbero piacerti anche