Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Relation
Definition: A relation is a named, twodimensional table of data
Table is made up of rows (records), and columns (attribute or field)
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
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)
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.
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 are drawn via arrows from dependent to parent table
10
11
12
(b)
Country
Management Information and Control Systems
14
15
16
NOTE: the domain constraint for the foreign key should NOT allow null value if DEPENDENT is a weak entity
Foreign key
18
19
Again, no null value in the foreign keythis is because of the mandatory minimum cardinality
Foreign key
20
TheSuppliesrelationshipwillneedtobecomeaseparaterelation...
Management Information and Control Systems
21
22
23
Resulting relations
24
25
26
(b) EMPLOYEE relation with recursive foreign key Management Information and Control Systems
27
28
29
30
31
33
Supertype/subtype relationships
34
35
ID Content Course
Term
takes Part-time Full-time Student manages Hours advises Grade Number Name
36
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
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)
38
Normal Forms
1st normal form
each attribute has single values, does not allow duplicate rows all relations are in 1st normal form
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)
40
Normal Forms
Exercise
Entity and variable vs. relationship. Which ERD could be right (think about FDs)?
Account
Number Currency
Account
Account
in
41
42
44