23
Relational Database and Constraints Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

Embed Size (px)

Citation preview

Page 1: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

Relational Database and Constraints

Content Resource-Elamsari and Navathe, Fundamentals of Database Management systems

Page 2: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

The relational Model of Data is based on the concept of a Relation.

A Relation is a mathematical concept based on the ideas of sets.

Similar to set theory of relations.

Relational Model Concepts

Page 3: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

Used by all major commercial database systems

Very simple model

Query with high-level languages: simple yet expressive

Efficient implementations

Relational model

Page 4: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

Schema = structural description of relations in database

Instance = actual contents at given point in time

Page 5: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

RELATION: A table of values

◦ A relation may be thought of as a set of rows and columns.

◦ Each row represents a fact that corresponds to a real-world entity or relationship.

◦ Each row has a value of an item or set of items that uniquely identifies that row in the table.

◦ Sometimes row-ids or sequential numbers are assigned to identify the rows in the table.

◦ Each column typically is called by its column name or column header or attribute name.

INFORMAL DEFINITIONS

Page 6: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

Schema of a Relation: R (A1, A2, .....An)Relation schema R is defined over attributes A1, A2, .....An

For Example - CUSTOMER (Cust-id, Cust-name, Address, Phone)

Here, CUSTOMER is a relation defined over the four attributes Cust-id, Cust-name, Address, Phone,each of which has a domain or a set of valid values.

For example, the domain of Cust-id is 6 digit numbers.

FORMAL DEFINITIONS

Page 7: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

Domain –Range to which a particular column or attribute must agree .

Data type –Built in type of the data Example- Customer phone number,home

phone, office phone , mobile phone

Domain and data type

Domain- 10 digit number containing no decimalData type-Integer

Page 8: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

A tuple is an ordered set of values Each value is derived from an appropriate

domain.

FORMAL DEFINITIONS

Page 9: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

The relation is formed over the cartesian product of the sets;

Each set has values from a domain; And, that domain is used in a specific role which

is conveyed by the attribute name. For example, attribute Cust-name is defined over

the domain of strings of 25 characters. Formally,

Given R(A1, A2, .........., An) r(R) dom (A1) X dom (A2) X ....X dom(An)

R: schema of the relation r of R: a specific "value" or population of R.

FORMAL DEFINITIONS

Page 10: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

DEFINITION SUMMARYInformal Terms Formal Terms

Table Relation

Column Attribute/Domain

Row Tuple

Values in a column Domain

Table Definition Schema of a Relation

Populated Table Extension

Page 11: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

Example

Page 12: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

Ordering of tuples in a relation r(R): The tuples are not considered to be ordered, even though they appear to be in the tabular form.

 Ordering of attributes in a relation schema R (and of values within each tuple): We will consider the attributes in R(A1, A2, ..., An) and the values in t=<v1, v2, ..., vn> to be ordered .(However, a more general alternative definition of relation does not require this ordering).

 Values in a tuple: All values are considered atomic (indivisible). A special null value is used to represent values that are unknown or inapplicable to certain tuples.

CHARACTERISTICS OF RELATIONS

Page 13: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

Constraints are conditions that must hold on all valid relation instances. There are three main types of constraints:

1. Key constraints2. Entity integrity constraints3. Referential integrity constraints

Relational Integrity Constraints

Page 14: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

Superkey of R: A set of attributes SK of R such that no two tuples in any valid relation instance r(R) will have the same value for SK. That is, for any distinct tuples t1 and t2 in r(R), t1[SK] t2[SK].

Super key stands for superset of a key.A Super Key is a set of one or more attributes that are taken collectively and can identify all other attributes uniquely.

Key Constraints

Page 15: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

A Super key is any combination of fields within a table that uniquely identifies each record within that table.

SK1:R.No+Name SK2:R.No+Mobile No SK3 :Mobile no + Name

Superkey

R.No Name Class Mobile no

Page 16: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

Key which is unique and irreducible but not necessarily a primary key.

A minimal superkey is considered a candidate key. 

Candidate Key

R.No Name Class Mobile no

Various Candidate Keys

If a relation has several candidate keys, one is chosen arbitrarily to be the primary key. The primary key attributes are underlined.

CK1:R.No CK2:Mobile No

Page 17: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

Primary key is a candidate key which is selected by designer to uniquely specify the record of a relation.

Characteristics:1. Column in relational database table whose value must

be unique for each row2. Serves to identify individual occurrence of entity3. Every row must have a primary key4. Cannot be NULL5. It is irreducibleThe candidate key that is selected to identify tuples uniquely within

the relation, the candidate keys which are not selected as PKs are called "Alternate keys“

We choose PK : Rollno

Primary key

Page 18: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

18

Composite Keys Unique key that is created by combining two

or more columns Usually comprises fields that are primary

keys in other tables

ORDER_ID PRODUCT_ID ORDER_QUANTITY

100 1 2

100 2 2

200 2 2

200 1 1

Composite Key

Page 19: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

Another example in Key Constraints

Page 20: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

A constraint involving two relations (the previous constraints involve a single relation).

Used to specify a relationship among tuples in two relations: the referencing relation and the referenced relation.

Tuples in the referencing relation R1 have attributes FK (called foreign key attributes) that reference the primary key attributes PK of the referenced relation R2. A tuple t1 in R1 is said to reference a tuple t2 in R2 if t1[FK] = t2[PK].

A referential integrity constraint can be displayed in a relational database schema as a directed arc from R1.FK to R2.

Referential Integrity

Page 21: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems

Statement of the constraintThe value in the foreign key column (or

columns) FK of the the referencing relation R1 can be either: (1) a value of an existing primary key value of the corresponding primary key PK in the referenced relation R2,, or..

(2) a null.In case (2), the FK in R1 should not be a part

of its own primary key.

Referential Integrity Constraint

Page 22: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems
Page 23: Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems