33
Module Title? DBMS E-R Model to Relational Model

Module Title? DBMS E-R Model to Relational Model

Embed Size (px)

Citation preview

Page 1: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

E-R Model to Relational Model

Page 2: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

WHY

 Relational model was first proposed in 1970 by Dr E F (Ted) Codd.

 Purpose:

Achieve program/data independence

The structure of the database is stored separately in the system

catalog from the access of the application programs.

Treat data in a disciplined way (data is ordered rows and column)

Apply rigor of mathematics (sum: total marks)

manipulating a set of records at a time

Use of set theory Improve programmer productivity (join, union, cross

product)

Page 3: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

What is a Relational Database?

 A relational database is made up of tables (relations) in which

data are stored.

 A table (relation) is a 2-dimensional structure made up of

columns (attributes) and rows (tuples).

Page 4: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

table

1. A relation is a table of values

row

The "ENum" column

column

relationtuple

Employee ENum EName Dept Hire_date

204 R. B. Jones E21 11/01/19991917 M. R. Brent E21 10/10/2003

TABLE **

Page 5: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

FORMAL DEFINITIONS

Each row in the EMPLOYEE table may be referred to as a tuple in the table and

would consist of four values, e.g.:

<1917, "M. R. Brent", "E21", "10/10/2003"> .

A relation (table) may be regarded as a set of tuples (rows).

Employee ENum EName Dept Hire_date

204 R. B. Jones E21 11/01/19991917 M. R. Brent E21 10/10/2003

Page 6: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

table

2. No two rows in a table can be the same (i.e., they can't have identical values)

relationtuple

Employee ENum EName Dept Hire_date

204 R. B. Jones E21 11/01/19991917 M. R. Brent E21 10/10/2003

The ROWS of a TABLE

row

Page 7: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS The columns of a table are also called attributes of the relation.

The attribute help in interpreting the meaning of the values

The attribute names will be useful in searching the table.

For example: Find all EName where Dept=E21.

Employee ENum EName Dept Hire_date

204 R. B. Jones E21 11/01/19991917 M. R. Brent E21 10/10/2003

The COLUMNS of a TABLE

In plain English, that would be ________________________

Attributes

Page 8: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS A domain is a set of values.

The COLUMNS of a TABLE

Employee ENum EName Dept Hire_date

204 R. B. Jones E21 11/01/19991917 M. R. Brent E21 10/10/2003

... ... ... ... Domains

Attributesp. 127

Page 9: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS A domain is a set of values.

The COLUMNS of a TABLE

Employee ENum EName Dept Hire_date

204 R. B. Jones E21 11/01/19991917 M. R. Brent E21 10/10/2003

• A domain for Enum is all positive integers greater than 0 and less than 10000.• A domain for EName is all stings less than 26 characters.•

Example domains

Domains are established in accordance with business rules.

Page 10: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS A domain has a name, data type, and a format.

It also has constraints and a set of permissible operators

The COLUMNS of a TABLE

Employee ENum EName Dept Hire_date

204 R. B. Jones E21 11/01/19991917 M. R. Brent E21 10/10/2003

Consider the Hire_date column of the Employee table:The domain name is denoted dom(Hire_date) and is the set of all valid hire dates.The domain data type is, say, character stringThe domain format is mm/dd/yyyy

Page 11: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Relational Database Terminology

Record or Tuple: Each row in the relation.

Attribute: Each column in the relation.

Domain: The set of allowable values for a column.

Degree: The number of columns in a relation.

Cardinality: The number of rows in a relation.

Page 12: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Schema

Relation schema = relation name + attributes, in order (+ types of attributes).

E.g. Student( rollno, stname,gsm,addr)

Database = collection of relations.

Database schema = set of all relation schemas in the database.

Page 13: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Properties of Relations

There are no duplicate tuples.

This property follows from the fact that the body of the relation is a

mathematical set (i.e., a set of tuples), and sets in mathematics by

definition do not include duplicate elements.

If a "relation" contains duplicate tuples, then it is not a relation.

Page 14: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Tuples are unordered (top to bottom).

Sets in mathematics are not ordered. So, even if a relation A's

tuples are reversely ordered, it is still the same relation.

Thus, there is no such thing as "the 5th tuple" or the last tuple. In

other words, there is no concept of positional addressing.

Page 15: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Attributes are unordered (left to right).

This property follows from the fact that the heading of a relation is

also defined as a set.

There is no such thing as "5th attribute" or the last attribute.

Page 16: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

All attribute values are atomic.

At every row-and-column position within the table, there always

exists precisely one value, never a list of values.

Or equivalently, relations do not contain repeating groups.

A relation satisfying this condition is said to be in First Normal

Form.

Page 17: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

 Rules to convert E-R to Relational model:

An entity type turns into a relation table:

Give it a sensible name – avoid reserved words and no spaces in

attribute names.

Choose appropriate Names, which are self-explanatory.

Each attribute turns into a column/field in the relation:

Keep the attribute domains – you will need them later when you

implement the schema in Oracle.

The primary key of the entity is the primary key of the table:

It can be composite if required. It can never be null.

Page 18: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Transforming Entity Sets and Their Attributes

EMPLOYEE

EMPID[PK]NAMESALARY

Page 19: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

The above ER Model is translated into the following relational

design:

EMPLOYEE (empid, name, salary)

Page 20: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Transforming 1:1 Relationships

1:1EMPLOYEE AUTO

Page 21: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Transforming 1:1 Relationships

EMPLOYEE( EmpNum, Name, Phone, ...)

AUTO( AutoId, Model, Year, Color, ..., EmpNum)

or

EMPLOYEE( EmpNum, Name, Phone,..., AutoID)

AUTO( AutoID, Model, Year, Color,...)

In both cases, we would want to enforce referential integrity by

requiring that the value of a foreign key in one relation/table

appear as the value of a primary key in the other relation/table.

Page 22: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Transforming One to Many Relationships

1:NTEACHER STUDENT

Page 23: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Transforming One to Many Relationships

TEACHER( TeacherName, Phone_no, Dept)

STUDENT( StudentId, StuName, ..., TeacherName)

Transformation Rule: The primary key of the entity set on the

"One" side of the relationship becomes a foreign key of the entity

set on the "Many" side of the relationship.

Page 24: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Transforming Many to Many E-R Relationships

M:NSTUDENT CLASS

Page 25: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Transforming Many to Many E-R Relationships

STUDENT( StudentNum, StuName, Phone, ...)

CLASS( ClassNum, ClassName, ...)

STU-CLASS( StudentNum, ClassNum)

RULE: Create a new relation, called an Intersection relation, whose

only (unless there is also an attribute belonging to the

relationship) attributes will be a composite primary key consisting

of the primary keys of the entity set participants in the

relationship.

Page 26: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Keys in DBMS

A simple key contains a single attribute.

A composite key is a key that contains more than one attribute.

A candidate key is an attribute (or set of attributes) that

uniquely identifies a row.

A candidate key must possess the following properties:

Unique identification - For every row the value of the key must

uniquely identify that row.

Non redundancy - No attribute in the key can be discarded without

destroying the property of unique identification.

Page 27: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Keys in DBMS

A primary key is the candidate key which is selected as the

principal unique identifier.

Every relation must contain a primary key. The primary key is

usually the key selected to identify a row when the database is

physically implemented.

For example, a part number is selected instead of a part

description.

Page 28: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Keys in DBMS

A super key is any set of attributes that uniquely identifies a row. A super key

differs from a candidate key in that it does not require the non redundancy

property.

Imagine a table with the fields <Name>, <Age>, <SSN> and <Phone Extension>.

This table has many possible superkeys. Three of these are <SSN>, <Phone

Extension, Name> and <SSN, Name>.

Of those listed, only <SSN> is a candidate key, as the others contain

information not necessary to uniquely identify records.

Page 29: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Keys in DBMS

A foreign key is an attribute (or set of attributes) that appears (usually)

as a non key attribute in one relation and as a primary key attribute in

another relation. I say usually because it is possible for a foreign key

to also be the whole or part of a primary key:

A many-to-many relationship can only be implemented by introducing an

intersection or link table which then becomes the child in two one-to-many

relationships. The intersection table therefore has a foreign key for each of

its parents, and its primary key is a composite of both foreign keys.

A one-to-one relationship requires that the child table has no more than

one occurrence for each parent, which can only be enforced by letting the

foreign key also serve as the primary key.

Page 30: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

DATA INTEGRITY CONSTRAINTS:

A constraint helps us to implement business rules in the

database.

Type of constraints:

Not Null: Attribute value is must. E.g. Studentname, address.

Unique: Attribute value should be different or not same. E.g.

Rollno.

Primary Key: Attribute which is Unique + Not null. E.g. Rollno.

It is an attribute or set of attributes, which uniquely identifies

the record in a relation.

This is also known as the entity integrity rule.

Page 31: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

DATA INTEGRITY CONSTRAINTS:

Foreign Key:The attribute, which is primary in other table.

Whenever we enter value in foreign key attribute it will

check with the primary key value.

So, this is also called Referential Integrity.

Check : Data has to follow some condition or rule.

E.g. bank balance amount should not be less than 5

Student marks not greater than 100

Page 32: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Primary Key

COURSEID NAME  

COMP COMPUTING

BIT BUSINESS INFORMATION TECHNOLOGY

PBIT PART TIME BIT

PHBA PART TIME BUSINESS

HBA BUSINESS

HACC ACCOUNTING

Primary key

Page 33: Module Title? DBMS E-R Model to Relational Model

Module Title?

DBMS

Foreign Key- REFRENTIAL INTEGRITY

STUDENT

 STUDID STUDNAME COURSEID

99222 RAMLA COMP

20029 REHAB HBA

20020 RASHID PHBA

99149 ZAHRA COMP

99090 AHMED BIT

Primary key Foreign key