20
Relational database model Lecture 3 Chapter 3

Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Relational database model

Lecture 3 Chapter 3

Page 2: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Relational database modelThe relational model represents the database as a collection of relations.

Each relation resembles a table of values

Each row in the table represents a collection of related data values.

A row represents a fact that typically corresponds to a real-world entity or relationship.

The table name and column names are used to help to interpret the meaning of the values in each row.

!

Page 3: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Relational database model

ID Name Address Class

10 Ahmed Ism 1

20 Mohamed Cairo 1

30 Aly Cairo 2

STUDENT TABLE

This table called Student table because • Each row represents facts about a

particular student

• The column names specify how to interpret the data values in each row,

• All values in a column are of the same data type.

Relation

Attributes within a Domain

Row OR tuples

Page 4: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Relational database model

ID Name Address GPA

10 Ahmed Ism 1.8

20 Mohamed Cairo 1.5

30 Aly Cairo 3.5

Domain (D) of attribute has: • Set of all possible values (GPA (0-4)) • Data type such as character, int, etc.. A relation schema R, denoted by

• R(A1, A2, ..., An), is made up of:

• A relation name R and a list of attributes, (A1, A2, ..., An)

The degree of a relation is the number of attributes n of its relation schema. R(4) dom(Ai): The domain of the attribute Ai

!!!!!

Page 5: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Relational database model

• A relation (or relation state) r of the relation schema R(A1, A2, ..., An),

• Denoted by r(R),

• Is a set of n-tuples r = {t1, t2, ..., tm}.

• Each n-tuple t is an ordered list of n values t =<v1, v2, ..., vn>,

• Where each value vi, 1 ≤ i ≤ n, is an element of dom (Ai) or

• Is a special NULL value.

• The ith value in tuple t, which corresponds to the attribute Ai, is referred to as t[Ai] or t.Ai (or t[i] if we use the positional notation).

Page 6: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Relational database model

Page 7: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Characteristics of Relations

• Ordering of Tuples in a Relation • A set of tuples: Set don't care about element orders

• Tuples in a relation do not care about particular order.

• Ordering of Values within a Tuple: • Ordering of attributes is not important

• Values and NULLs in the Tuples. • Values are not divisible (no multi-value attribute)

• Null values is used when missing (Office phone in previous table) !

!!!

Page 8: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Relations Model Notations

•A relation schema R of degree n is denoted by R(A1, A2, ..., An).

•The uppercase letters Q, R, S denote relation names.

•The lowercase letters q, r, s denote relation states.

•The letters t, u, v denote tuples.

•Attributes used with relation as STUDENT.name

•An n-tuple t in a relation r(R) is denoted by t = <v1, v2, ..., vn>, where vi is the value corresponding to attribute Ai.

•The following notation refers to component values of tuples:

•Both t[Ai] and t.Ai (and sometimes t[i]) refer to the value vi in t for attribute Ai.

•Both t[Au, Aw, ..., Az] and t.(Au, Aw, ..., Az), where Au, Aw, ..., Az is a list of attributes from R, refer to the sub-tuple of values <vu, vw, ..., vz> from t cor- responding to the attributes specified in the list.

Page 9: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Relations Model Constrains

1. Domain constrain: Be sure the value inserted into the attribute A, is within the domain range and type

2. Key Constraints and Constraints on NULL Values:

1. Super-key: Set of attribute, no two tuples at a relation could have the same combination of values.

2. Candidate key: More than one key that are candidate to be primary key.

3. Primary key: Selected to distinguish tuples

1. How to choose primary key from different candidate?

4. Null Constrain Allowed or not.

Page 10: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Relations Model Constrains

Page 11: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Relations Model Constrains

3. A relational database schema S is a set of relation schemas S = {R1, R2, ..., Rm} and a set of integrity constraints IC

COMPANY(S) = {EMPLOYEE, DEPARTMENT,

DEPT_LOCATIONS, PROJECT, WORKS_ON, DEPENDENT}

• A database state that does not obey all the integrity constraints is called an invalid state, and ,

•A state that satisfies all the constraints in the defined set of integrity constraints IC is called a valid state. !

!!!

Page 12: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Create Databaserelational database

RelationRelationRelationRelationRelation

Attributes Id

Name Address

Attributes Attributes Attributes Attributes

Attributes Attributes Attributes Attributes

Attributes Attributes Attributes Attributes

Attributes Attributes Attributes Attributes

Attribute characteristics

Page 13: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be
Page 14: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

A possible database state

Page 15: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Relations Model Constrains

4. Integrity, Referential Integrity, and Foreign Keys:

1. The entity integrity constraint states that no primary key value can be Null

2. The referential integrity constraint: is specified between two relations:

1. A tuple in one relation that refers to another relation must refer to an existing tuple in that relation, (Dno of EMPLOYEE)

3. Foreign key:

1. Link two relations (related to PK)

2. Must have the same domain like primary ket

3. The value of FK must be exist as PK at the other location or NULL

1. t1[FK] = t2[PK], and we say that the tuple t1 references or refers to the tuple t2.

Page 16: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be
Page 17: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Operations & Dealing with Constraint Violations

Insert: Add a list of values to the attributes:

Domain constraints can be violated if an attribute value is given that does not appear in the corresponding domain or is not of the appropriate data type.

Key constraints can be violated if a key value in the new tuple t already exists in another tuple in the relation r(R).

Entity integrity can be violated if any part of the primary key of the new tuple t is NULL.

Referential integrity can be violated if the value of any foreign key in t refers to a tuple that does not exist in the referenced relation. Here are some examples to illustrate this discussion.

Page 18: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Operations & Dealing with Constraint Violations

Delete: Only violate referential as deletion here cut relation with other tuple

What to do:

Restrict deletion

Cascade: Deleter other reffered tuple

Set other attribute either to NULL or change to refer to other existing tuple

If PK no way to set to NULL

Page 19: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Operations & Dealing with Constraint Violations

Update: No problem if updated field is not PK or FK

If it is: Review what was in Insert and delete

Page 20: Relational database model - DR. Ahmed Eltahawey...4. Integrity, Referential Integrity, and Foreign Keys:! 1. The entity integrity constraint states that no primary key value can be

Operations & Dealing with Constraint Violations

A transaction is an executing program that includes some database operations, such as reading from the database, or applying inser- tions, deletions, or updates to the database.

At the end of the transaction, it must leave the database in a valid or consistent state that satisfies all the constraints specified on the database schema.