0% found this document useful (0 votes)
16 views88 pages

Chapter 2

Chapter 2 diploma information technology.

Uploaded by

zeeshansari.390
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views88 pages

Chapter 2

Chapter 2 diploma information technology.

Uploaded by

zeeshansari.390
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 88

Chapter 2

Relational Data Model (12M)


Relational Structure
Relation
A Table in a relational database, also
known as a Relation, is a two
dimensional structure used to hold related
information.
A database consists of one or more related
tables.
 Relations are a logical structure which is
collection of table consisting rows and
columns.
Example of Employee table

ID Name Age Salary

1 Adam 34 13000

2 Alex 28 15000

3 Smith 20 18000

4 John 42 19020

Fig : Employee Table


Tuples / What is a Record?

A Record in a table represents set of


related data.
 A single entry in a table is called a
Record or Row.
 Records also kwon as Rows and Tuples.
Following is an example of single record.

1 Adam 34 13000

Rows/Records/Tuples
Attributes / What is Field?
In Relational table, a column is a set of value
of a particular type.
Fields also kwon as Columns and Attributes.
 The above Employee table consists of four
fields, ID, Name, Age and Salary.
For example, in Employee table, Name is a
column that represents names of employee.
Name

Adam

Alex

Smith

John

Column/ Fields/Attributes
Domain
A domain is defined as the set of all
unique values permitted for an attribute.
 In a relational table a domain can have a
Single value or no (Null) value.
Database Keys
Database Keys
Keys are very important part of Relational
database. They are used to establish and identify
relation between tables.

Types:

 Super Key
 Candidate Key
 Primary Key
 Foreign Key
Super Key
Super Key is defined as a set of attributes within a
table that uniquely identifies each record within a table.
Candidate Key
A Candidate Key is a set of one or more
fields/columns that can identify a record uniquely in a
table.
Primary Key
Primary Key is a one fields/columns of a table that
uniquely identify a record in database table.
Foreign Key
Primary Key of one table become a attributes of
another table is called Foreign Key.
Super Key

Super Key is defined as a set of attributes within a


table that uniquely identifies each record within a
table.

A combination of one or more columns in a table


which can be used to identify a record in a table
uniquely, a table can have any number of super
keys.

Primary key, Unique key, Alternate key, Candidate


Key are subset of Super Keys.
Cust-ID Cust_name Cust_street Cust_city account_no

Customer

Example:

In the Customer table Cust_ID, Cust_name,Cust_street ,Cust_city and account_no


are Super key.

Customer{ Cust_ID,Cust_name,Cust_street,Cust_city, account_no}


Candidate Key
A Candidate Key is a set of one or more
fields/columns that can identify a record uniquely
in a table.

Candidate keys are defined as the set of fields from


which primary key can be selected. There can be
multiple Candidate Keys in one table.

Candidate Key are subset of Super Keys.


Cust-ID Cust_name Cust_city account_no

Customer

Example:

In Customer table Cust_ID,Cust_name,Cust_city ,account_no are Candidate Key.

Customer{ Cust_ID,Cust_name,Cust_city, account_no}


Primary Key
Primary key is a one fields/columns of a table that
uniquely identify a record in database table.

It can not accept null, duplicate values.

Only one Candidate Key can be Primary Key.

Primary key that uniquely identify each record in a


table.
Cust-ID

Customer

Example:

In Customer table Cust_ID is Primary Key.

Customer{ Cust_ID},
Composite Key / Compound Key
Composite Key that consists of two or more
attributes that uniquely identify an entity is called
Composite key.
Cust-ID account_no

Customer

Example:

In Customer table Cust_ID and account_no are is Composite Key.

Customer{ Cust_ID,account_no.},
Foreign Key
Foreign key represents relationship between tables.

A Foreign key is a column(or a group of columns)


whose values are derived from the ‘Primary Key’
of other table.

One or more columns can be defined as Foreign


key.
Cust-ID Cust_name Cust_city account_no

Customer

Relationship

Account_no Bname Balance

Account

Example:

We can have account_no column in the Customer table which is pointing to


account_no column in a account table where it a primary key.
Integrity Constraint
Integrity Constraint

An Integrity Constraint is a mechanism used


to prevent invalid data entry into the table.

OR

Integrity constraints are a set of rules.


It is used to maintain the quality of
information.
Types of Integrity Constraint

Integrity Constraints

Domain Entity Referential


Integrity Constraints Integrity Constraints Integrity Constraints

Primary Foreign
Not Null Check Unique Reference
Key Key
1) Domain Integrity Constraints

It is used to maintain value according to


the user specifications.

Types:
a) NOT NULL
b) Check
a) NOT NULL

Case A: At the time of table creation

create table <table_name>


(column_name1 datatype(size),
column_name2 datatype(size) NOT NULL,
.
.
.
column_namen datatype(size));
Case B: After table Creation

alter table <table_name>


modify <column_name> NOT NULL;
b) Check

The Check Constraint defines a condition that each row must


satisfy.
Case A: At the time of table creation

create table <table_name>


(column_name1 datatype(size)
constraint <constraint_name>
check <condition>,
column_name2 datatype(size),
.
.
.
column_namen datatype(size));
Case B: After table Creation

alter table <table_name>


add constraint <constraint_name>
check <condition>;
2) Entity Integrity Constraints

An entity is any data recorded in a database.


Each database represents a table and each row
of a table represents an instance of that entity.

Types:
a) Primary Key
b) Unique
a) Primary Key

Case A: At the time of table creation

create table <table_name>


(column_name1 datatype(size)
constraint <constraint_name> primary key,
column_name2 datatype(size),
.
.
column_namen datatype(size));
Case B: After table Creation

alter table <table_name>


add constraint <constraint_name>
primary key <column_name>;
b) Unique

Case A: At the time of table creation

create table <table_name>


(column_name1 datatype(size)
constraint <constraint_name> unique,
column_name2 datatype(size),
.
.
column_namen datatype(size));
Case B: After table Creation

alter table <table_name>


add constraint <constraint_name>
unique <column_name>;
Primary Key Unique
1) The primary key does not accept any 1) The Unique key accept any NULL
duplicate and NULL values. values

2) Only one primary key in the table. 2) More than one Unique key in the table.

3) Syntax 3) Syntax
create table <table_name> create table <table_name>
(column_name1 datatype(size) (column_name1 datatype(size)
constraint <constraint_name> primary constraint <constraint_name> unique,
key, column_name2 datatype(size),
column_name2 datatype(size), .
. .
column_namen datatype(size)); column_namen datatype(size));

4) Example 4)Example
create table emp eg
(id number(6) primary key, create table emp
name char(25), (id number(6) unique,
age number(2), name char(25),
sal number(7,2)); age number(2),
sal number(7,2));
3) Referential Integrity Constraints

It is used to establish a ‘Parent-child’


relationship between two tables having a
common column.
Types:
a) Reference Key
b) Foreign Key
a) Reference Key

Case A: At the time of table creation

create table <table_name>


(column_name1 datatype(size),
column_name2 datatype(size)
constraint <constraint_name>
references <parent_table_name>
(parent_table_column_name)
[on delete cascade],
:
:
:
column_name n datatype(size));
b) Foreign Key

 Foreign key represents relationship between tables.


 A Foreign key is a column(or a group of columns) whose values
are derived from the ‘Primary Key’ of other table.

 One or more columns can be defined as Foreign key.


b) Foreign Key
Case A: At the time of table creation

create table <table_name>


(column_name1 datatype(size),
column_name2 datatype(size),
:
:
:
column_name n datatype(size),
constraint <constraint_name>
references <parent_table_name>
(parent_table_column_name)
[on delete cascade]);
Consider the following schemas:
(i) Dept (Dept_no, Dept_name, Dept-loc)
(ii) Staff (Staff_id, Staff_name, Dept_no, Joint_date)
Draw and explain parent-child relationship for above schemas
and find out foreign key with justification.(S-19)
Consider the following schemas:
(i) Dept (Dept_No, DName, LOC)
(ii) Emp (Emp_No, Ename, Job, Sal,
Dept_No)
Consider the following schema
Employee (id, name, age,sal).

Write procedure to manipulate given database


by adding, modifying and deleting records.(6M)
Example of Employee table

ID Name Age Salary

1 Adam 34 13000

2 Alex 28 15000

3 Smith 20 18000

4 John 42 19020

Fig : Employee Table


Entity Relationship Model
(ER-Model)
Types of Database Models
Entity Relationship Model(ER-Model)

The Entity Relationship Model(ER-Model) is a


high level conceptual data model developed by
Chen in 1976 to facilities database design.

The overall logical structure (Schema) of a


database can be expressed graphically by an ER
diagram.
The basic constructs of ER Model are
Entity, Attributes and Relationships.
Components
of ER Model
 Rectangle – It represents entity in the ER Model.
 Ellipse – It represents attribute in the ER Model.
 Diamond – It represents relationship between entity and attribute.
 Line –It links attribute to entity set and entity set to relationship
set.
 Doubles Ellipses – It represents multivalued attributes.
 Dashed Ellipses – It denotes derived attributes.
 Double lines – It indicates total participation of an entity in a
relationship set.
 Double Rectangle – It represents weak entity set.
 Double Diamonds – It represents weak relationships.
 Multiple ellipses connected to single ellipse using lines –

It represents composite attribute.


 Ellipse with line inside it – It represents single values attributes
Entities
 An Entity is a thing or object in the real world that is distinguishable from all
other objects.
 For example 'Person' in an organization is an entity.
Customer in the bank.
Student in the college etc
Notation:
 Entities are represented by rectangles containing the entity name in ER
Model.

Entity Name

Example

Student
Attributes
An entity is represented by a set of attributes. It
corresponds to a field in a table.
Notation
Attributes are represented by ovals and are
connected to the entity with a line in ER model.
Each oval contains the name of the attribute it
represents.

Attribute Name
Example

Student
Roll no Year
name

Student
Example
Consider the following Student table-

Roll_no Name Age


1 John 20
2 Rahul 19
3 Smith 20
4 Ajay 19

Student

This complete table is referred to as “Student Entity Set” and each row
represents an “entity”.
Representation as ER Diagram-

The above table may be represented as ER


diagram as-
Domain or Value Set
For each attribute there is a set of permitted
values called the domain or value set of the
attribute.
Types of Attributes

In ER Model attributes can be classified into the


following types.

Simple and Composite Attribute


Single Valued and Multi Valued attribute
Stored and Derived Attributes
Null Attribute
1) Simple and Composite Attribute

Simple Attribute that consist of a single


atomic value. A simple attribute cannot be
subdivided. For example the attributes age,
roll_no etc are simple attributes.

A Composite Attribute is an attribute that can


be further subdivided. For example the
attribute ADDRESS can be subdivided into
street, city, state, and zip code.
Simple Attribute:
Attribute that consist of a single atomic value.
Example: Salary, age etc

Composite Attribute :
Attribute value not atomic.
Example :
Address : House_no:City:State
Name : First Name: Middle Name: Last Name
Example
2) Single Valued and Multi Valued attribute

A Single Valued attribute can have only a


single value. For example a person can have only
one 'date of birth', 'age' , Adhar card no.,Passport
no etc
Multivalued attributes can have multiple values.
For instance a person may have multiple phone
numbers, multiple degrees etc.
Notation:
Multivalued attributes are shown by a double line
connecting to the entity in the ER diagram
Single Valued Attribute:
Attribute that hold a single value.
Example1: Age
Exampe2: City
Example3:Customer id

Multi Valued Attribute:


Attribute that hold multiple values.
Example1: A customer can have multiple phone
numbers, email id's etc
Example2: A person may have several college
degrees
Example

Single Valued
Attribute

Multi Valued
Attribute
3) Null Attributes
Null value is used when an entity does not have
a value for an attributes.
Null can also designate that an attribute is
unknown i.e missing or not known.

Roll no

Student
4) Stored and Derived Attributes
 The value for the derived attribute is derived from the
other attribute.
 For example 'Date of birth' of a person is a attribute. The
value for the attribute 'AGE' can be derived by
subtracting the 'Date of Birth'(DOB) from the current
date.
 Stored attribute supplies a value to the related attribute.

Notation:
5) Derived Attribute:
An attribute that’s value is derived from a other
attribute.

Example: age, and it’s value is derived from the


attribute Date of Birth.
Example
Key Attribute
The attribute which uniquely identifies each
entity in the entity set is called key attribute. For
example, Roll_No will be unique for each
student.
Notation:
 In ER diagram, key attribute is represented by
an oval with underlying lines.
Relationships
A Relationship is an association between several entities.
 A relationship type represents the association between
entity types.

Notation:
Relationships are represented by a diamond connected to the
related entities in ER Model.
Example
For example, ‘Enrolled in’ is a relationship
type that exists between entity type Student and
Course.
Example
Relationship Set
 A set of relationships of same type is known as relationship set.
 The following relationship set depicts S1 is enrolled in C2, S2 is
enrolled in C1 and S3 is enrolled in C3.
Winter-18
1) Define the term Data Model.(2M)

2) Describe object oriented data models.(4M)

3) Explain advantages of centralized and distributed


databases.(4M)

4) Compare Hierarchical Database Model with


Network Model.(4M)

5) Explain client/server database system.(4M)


Summer-19
1) Define Domain and Attribute.(2M)

2) Define database model.(2M)

3) Distinguish between network database model and


relational database model.(4M)

4) Explain client/server database system.(4M)

5) Describe centralized database system with


example.(4M)
Winter-19
1) Define the term tuple and domain.(2M)

2) Describe client server system with example. (4M)


3) Differentiate between Hierarchical Database model
and network database model.(4M)

4) Explain merits and demerits of Object Oriented


Database model.(4M)

5) Explain distributed database system with example.


(4M)
Thanking You.

You might also like