Module 2 - Data Modeling
Module 2 - Data Modeling
Module 2
Data Modeling – ER Model and
Relational Model
Topics to be Covered
Entity-Relationship Model
Types of Attributes
Relationship
Structural Constraints
Relational Model
Relational Model Constraints
Mapping E-R Model to Relational Schema
Integrity Constraints
Entity-Relationship(ER) Model
Student Attribute
Attribute Value
Entity Sets
instructor_ID instructor_name student-ID student_name
Attributes can be
composite / simple (atomic)
single-valued / multivalued
stored / derived
descriptive
key / nonkey.
Composite Attributes
Composite
Attribute
Address
ZipCode
District Street name
Single valued Vs. Multi valued
Attribute
Age Degree
A : 24 Years A : B.Sc.
B : 27 Years B : B.Sc, M.Sc., M.Phil., Ph.D.
Derived Attribute
Entity with a multivalued attribute (Skill) and derived
attribute (Years_Employed)
ER Diagram with Composite, Multi-valued and
Derived Attributes
Aggregation
Aggregation represents a ‘has-a’ relationship between entity types, where
one represents the ‘whole’ and the other the ‘part’
Composition
Composition is a form of aggregation that represents an association between
entities, where there is a strong ownership between the ‘whole’ and the ‘part’.
For example, a tree and a branch have a composition relationship. A branch is
‘part’ of a ‘whole’ tree - we cannot cut the branch and add it to another tree
Summary of Symbols
Key Constraints
Primary Key (1 Key)
Foreign Key (Referential Integrity)
Candidate key (Minimal Super Key)
Super Key (1 or more Keys)
When a ‘Not null’ constraints is enforced through either on a column or a set of columns in
a table, it will not allow Null values. The user has to provide a value for the column.
Syntax:
Create table table_name (columnname datatype constraint constraint_name not null);
Check constraints:
Check constraints specify conditions that each row must satisfy. These are rules governed by
logical expressions or Boolean expressions. Check conditions cannot contain subqueries.
Syntax:
Create table table_name (columnname datatype constraint constraint_name check
(condition));
Entity Integrity constraints
Each entity represents a table and each row of a table represents
an instance of that entity. Each row in a table can be uniquely
identified using the entity constraint. There are basically two types
of Entity Integrity constraints.
Unique constraints
Primary key constraints
Unique constraints
Usage of the unique key constraint is to prevent the duplication of values within the rows of a
specified columns or a set of columns in a table. Columns defined with this constraint can also allow null
values.
Syntax:
Create table table_name (columnname datatype… constraint constraint_name unique (colname)) ;
Primary key constraints
The primary key constraint avoids duplication of rows and does not allow Null values, when enforced
in a column or set of columns. A table can have only one primary key.
Syntax:
Create table table_name (columnname datatype constraint constraint_name primary key);
Referential integrity constraints
(Not Null)
SQL> CREATE TABLE STUD(ROLLNO NUMBER(6) NOT NULL,NAME VARCHAR2(10),BRANCH VARCHAR2(6));
Table created.
SQL> DESC STUD;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLLNO NOT NULL NUMBER(6)
NAME VARCHAR2(10)
BRANCH VARCHAR2(6)
(UNIQUE)& primary key)
SQL> create table stud(rno number(5),name varchar2(10),sal number(10) constraint no_ck check(sal between 10000 and
30000));
Example
SQL> /
Enter value for rno: 565
Enter value for name: rohit
Enter value for sal: 35000
old 1: insert into stud values(&rno,'&name',&sal)
new 1: insert into stud values(565,'rohit',35000)
insert into stud values(565,'rohit',35000)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.NO_CK) violated
Foreign Key
SOL>create table adm(stuid number(6) constraint stuid_pk
primary key,sname varchar2(15),per number(5));
SQL> create table course(stuid number(6) constraint sid_fk
references adm(stuid),branch varchar2(5),sec varchar2(10));
Operations in Relational Model
Insert, update, delete and select.
Insert is used to insert data into the relation
Delete is used to delete tuples from the table.
Modify allows you to change the values of some attributes in
existing tuples.
Select allows you to choose a specific range of data.
implicity: A Relational data model in DBMS is simpler than the hierarchical and network model.
Structural Independence: The relational database is only concerned with data and not with a
structure. This can improve the performance of the model.
Easy to use: The Relational model in DBMS is easy as tables consisting of rows and columns are
quite natural and simple to understand
Query capability: It makes possible for a high-level query language like SQL to avoid complex
database navigation.
Data independence: The Structure of Relational database can be changed without having to
change any application.
Scalable: Regarding a number of records, or rows, and the number of fields, a database should
be enlarged to enhance its usability.
Dr. M. Premalatha, VIT Chennai
Disadvantages of using Relational Model