0% found this document useful (0 votes)
17 views

K Dms Chapter2

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

K Dms Chapter2

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

K-Scheme DBM -22319 Chapter -2

Unit - II Relational Data Model


2.1 Relational Structure :- Tables (Relations), Rows (Tuples), Domains, Attributes, Entities
2.2 Keys :- Super Keys, Candidate Key, Primary Key, Foreign Key.
2.3 Data Constraints :- Domain Constraints ,Referential Integrity Constraints
2.4 Entity Relationship Model : - Strong Entity set, Weak
Entity set, Types of Attributes, Symbols for ER diagram,
ER Diagrams
2.5 Normalization:- Functional dependencies, Normal
forms: 1NF, 2NF, 3NF

SuperKey :
Superkey is a set of one or more attributes which are taken collectively used to
identify uniquely an entity in the entity set.
Superkey may have extra attribute i.e if k is a superkey then any superset of k is also a super key.
Here Cust_id is a superkey. Also (Cust_id,Cust_name) is a superkey but Cust_name is not a
superkey.

Disadvantage: SuperKey may have extra attribute

Candidate Key:
The candidate key can be defined as when two or more attributes uniquely identifies an instance
of entity set. These attributes or combination of attributes are called as candidate key.

Candidate Keys in Customer Relation


1) {Soc_security}
2) {customer_name,Cust_add}

Primary key:
Within a relation there is always one attribute which has values that are unique in a relation also
not null, thus can be used to identify tuple of that relation. Such a unique identifier is called the
primary key.
E.g. 1) In the Student relation RNO is the primary key.
2) In Customer relation Cust_id is a primary key

Foreign Key:
Foreign key is a field in database table that is Primary key in another table.
It can accept multiple null, duplicate value
Consider two schemas Student and Course as follows:

Student_schema={stud_id,course_id,firstname,lastname} ===> course_id is foreign key

Course_schema={course_id,courseName} ===> course_id is primary key


A student table that contains the course_id the student is attending. Another table lists the
course on offer with course_id being primary key. The 2 tables are linked through
course_id and as such course_id would be a foreign key in the student table.

Integrity constraint ? Explain the three types of integrity constraints.


Definition: an integrity constraint is a mechanism to prevent invalid data entry into
database/table.
Types of Constraint
Explanation:
1) Domain integrity constraint: It is used to maintain the value according to the user
specification. It has two types :
a) Not null constraint: not null constraint is assigned on a column or set of column
to avoid null values.
Syntax at Table Creation:
Create table <table_name>
(column_name1 datatype(size),
column_name2 datatype(size) not null,
---
column_name n datatype(size) );
Example:
create table emp(
empno number(5),
ename varchar2(25) not null,
job varchar2(15)
);
After table creation
alter table <table_name> modify <column_name> not null;
e.g
alter table emp modify mgr not null;
b) Check constraint: it defines a condition that each row must satisfy. A single
column can have multiple check constraints that reference the column in its
definition.
Syntax at table creation:
Create table <table_name>
(column_name1 datatype(size) constraint <constraint_name> check
<condition or logical expression>,
---
column_name n datatype(size)
);
Example:
create table emp(
empno number(5),
ename varchar2(25),
salary number(7,2) constraint emp_sal_ck check(salary > 5000),
job varchar2(15)
);
After table creation
Syntax:
Alter table <table_name> add constraint<constraint_name> check <condition>;
Example:
alter table emp
add constraint emp_deptno_ck check(deptno between 10 to 99);

2) Entity integrity constraint:


Entity is any data recorded in database.
It has two types:
a) Unique key constraint and
b) Primary key constraint
a) Unique constraint: It avoids the duplication of values within the rows in table.
It allows null values.
Syntax at table creation:
Create table <table_name>
(column name1 datatype(size),
column_name2 datatype(size) constraint <constraint_name> unique,
---
column_name n datatype(size)
);
Example Column level constraint
create table dept
(deptno number(5) constraint dept_deptno_uk unique,
dname varchar2(20),
loc varchar2(20));
Example table level constraint
create table dept
(deptno number(5),
dname varchar2(20),
loc varchar2(20) ,
constraint dept_deptno_uk unique(deptno)
);
After table creation :
Syntax:
alter table <table_name> add constraint <constraint_name>
unique(column_name);
Example:
alter table dept add constraint dept_deptno_uk unique(deptno);
b) Primary key constraint: Pk constraint can be assigned on one or more
columns in a table used to uniquely identifies the each row in table. It avoids
duplication of rows and do not allow null values.

Syntax at table creation:


Create table <table_name>
(column name1 datatype(size),
column_name2 datatype(size) constraint <constraint_name> primary key,
---
column_name n datatype(size)
);
Example Column level constraint
create table dept
(deptno number(5) constraint dept_deptno_pk primary
key,
dname varchar2(20),
loc varchar2(20));
Example table level constraint
create table dept
(deptno number(5),
dname varchar2(20),
loc varchar2(20) ,
constraint dept_deptno_pk primary key(deptno)
);
After table creation :
Syntax:
alter table <table_name> add constraint <constraint_name> primary
key(column_name);

Example:
alter table dept add constraint dept_deptno_pk primary key(deptno);
3) Referential integrity constraint:
It is used to establish the parent child relation between two tables having common
column. Value of foreign key is derived from primary key.
We should define the column in the parent table as a primary key and same column
in the child table as a foreign key referring to the corresponding parent key
dept={deptno, dname}
emp={empid,ename,deptno,salary
}
In table dept , deptno is a primary key containing unique values for deptnos.
To set the relationship between these two tables , we can define emp.deptno as
a foreign key as
At table creation :
1. create table dept
(deptno number(5) constraint dept_deptno_pk primary key,
dname varchar2(20));
2 Create table emp
( empid number(4),
ename varchar2(25),
deptno number(5) constraint emp_deptno_fk references dept(deptno),
salary number(10,2));
After table creation
alter table emp add constraint emp_deptno_fk foreign key(deptno)references
dept(deptno);

E-R Model
1. The Entity-Relationship model (E-R Model) is a high level conceptual model developed
by Chen in 1976 to facilitate database design. It is object based logical model
2. E-R model is based on a perception of a real world that consists of a set of basic
objects called entities and of the relationship among these objects.
3. The basic component of the model is the Entity Relationship diagram which is used to
visually represent data objects. Entities are real world objects like person, things,
organization etc. The relationship is how one entity is related with the other entity as 1:1,
1: N, M: N.
4. Main advantage of E-R model is that, once it is drawn it can be easily convert. The
constructs used in ER model can be easily be transformed into relational table.
5. This model is used by database designer to communicate the design to the end user.
ER diagram is build up of following components

Sample E-R diagram


E-R dig for Customer, Branch& Account:

Components of ER Model
Entitiy: Entity is a thing or object in the real world that is distinguishable from all other objects.
e.g. Student of a university is entity or
Customer of a bank is an entity.
Entity are represented by rectangles.
Student
Entity Set:
Entity set is a set of entities of the same type that share the same properties or attributes.
E.g. The set of all students
Attributes: Attributes are various descriptive properties possessed by an entity.
Books relation attributes are {Title, ISBN, Price}
Attributes are denoted by Ellipse

Attributes are classified as :


1)Simple attributes : Attributes that cannot be subdivided (i.e are atomic) into subparts are
called as simple attributes.
e.g Enroll_no, RollNo

2)Composite Attributes :The attributes which can be divided into subparts are called composite
attributes.
e.g attribute name could be structured as a composite attribute consisting of
first_name,middle_name and last_name

3)Single Valued Attributes:


The attribute has single value for a particular entity called as single valued attribute.
e.g Student_id

4)Multivalued Attributes:
The attribute has set of values for a specific entity called as multi valued attribute.
e.g Phone_no is multivalued attribute because employee may have zero,one or several phone no.

5) Derived Attribute:
The value for this type of attribute can be derived from the values of other related attributes or
entities.
e.g Customer entity has attribute age and date_of_birth. We calculate age from date_of_birth
and current_date.
Here age is derived attribute and date_of_birth is base or stored attribute
6)Null Arrtibute:
An attribute takes a null value when an entity does not have a value for it. Null can indicate “not
applicable”- that is value does not exist for the entity.
e.g apartment_no

Roles in E-R Diagrams


1. The function that an entity plays in a relationship is called its role. Roles are normally
explicit and not specified.
2. They are useful when the meaning of a relationship set needs clarification.
3. For example, the entity sets of a relationship may not be distinct. The relationship works-
for might be ordered pairs of employees (first is manager, second is worker).
4. In the E-R diagram, this can be shown by labelling the lines connecting entities
(rectangles) to relationships (diamonds).

State weak and strong entity set.


(Weak entity set- 1 Mark, Strong entity set- 1 Mark)
Ans: Weak entity sets:
Entity sets that does not have sufficient attributes to define the primary key are called as weak
entity sets.
It is represented by double rectangle.

Strong entity set:


Entity sets that have sufficient attributes to define the primary key are called as strong entity
sets.
It is represented by rectangle.

What is difference between weak entity set and strong entity set ?
(Any 4 difference points : 1 Marks each)
Ans :

Weak Entity Set Strong Entity set

It does not have sufficient attribute to The relationship between two strong entity
form a primary key on its own. set is represented by a diamond symbol

The Member of weak entity set is called The member of strong entity set is called
as subordinate entity set. as dominant entity set.

It is represented by double rectangle. It is represented by rectangle.

The line connecting weak entity set with The line connecting weak entity set with
the identifying relationship is double. the identifying relationship is double.
Relationship

A relationship is a n association among two or more entities.

Binary Relationship

Binary Relationship means relation between two Entities. This is further divided into three types.
1. One to One : This type of relationship is rarely seen in real world.

The above example describes that one student can enroll only for one course and a course will
also have only one Student. This is not what you will usually see in relationship.
2. One to Many : It reflects business rule that one entity is associated with many number of
same entity. For example, Student enrolls for only one Course but a Course can have many
Students.

The arrows in the diagram describes that one student can enroll for only one course.

3 Many to Many

The above diagram represents that many students can enroll for more than one courses.
Explain generalization of ER with suitable diagram.
(Explanation- 2 Marks, Diagram- 2 Marks)
1. Generalization is abstracting process of viewing set of object as a single general class.
2. It means Generalization is the result of taking union of two or more entity set to produce a
higher level entity set.
3. Generalization is a bottom-up

Explain specialization of ER with suitable diagram.


(Explanation- 2 Marks, Diagram- 2 Marks)

Ans: 1.Specialization is opposite to Generalization. It is a top-down approach in which one


higher level entity can be broken down into two lower level entity.
2. In specialization, some higher level entities may not have lower-level entity sets at
all.
3. It is top-down approach.
Notations For ER diagram
a) Compare E-R model with Hierarchical model.
[1 Mark for each point, 4 points should be mentioned]

E-R Model Hierarchical Model


1. It is object based logical model 1.It is record based model
2. Information is represented as E-R diagram
2. It is a tree like structure. It consists of a
having different symbols like rectangle for
collection of records that are connected
entity, diamond for relationship set ,ellipse for
to each other through links.
attributes etc.

3. It has many to many relationships. 3. It has one to many relationships.

4. It is used by database designer to communicate 4. It is mainly used in mainframe


the design to the end user. computers.

Draw ER diagram for library Management system


(suitable entities:1 M, Proper Attributes -1 M, Correct relationship showing basic activities of
library- 1M, Proper symbols -1 M)

● Book -the set all the books in the library. Each book has a Book-id, Title, Author, Price, and
Available (y or n) as its attributes.
● Member-the set all the library members. The member is described by the attributes Member_id,
Name, Street, City, Zip_code, Mem_type, Mem_date (date of membership), Expiry_date.
● Publisher-the set of all the publishers of the books. Attributes of this entity are Pub_id, Name,
Street, City, and Zip_code.
● Supplier-the set of all the Suppliers of the books. Attributes of this entity are Sup_id, Name,
Street, City, and Zip_code.

Assumptions: a publisher publishes a book. Supplier supplies book to library. Members borrow
the book (only issue).
Return of book is not taken into account
Sketch E-R diagram for customer and loan. Assume suitable attributes.
[Correct shape 1mark, correct entity 1 Mark, correct attributes 1 Mark, proper
relationship set 1 Mark]
Cust_id Name L_no Amoun

Customer Borrow Loan


s

Addres Phone

You might also like