You are on page 1of 27

Chapter 2 : Entity-Relationship Modeling

Objectives :

Understand the different between spreadsheet and
Database

Understand the basic element of Relational
Database

Understand the characteristic of Object Relational
Database

Understand the meaning of the integrity rules for
relational databases


2.1 Spreadsheet Database
More than one datatype can be stored in a
spreadsheet column.
Usually, only one
datatype can be stored in
a database table column.
Cells in a spreadsheet can be defined as a
formula, making the contents variable
depending on other cells.
Columns in a database
table have a fixed value.
A spreadsheet has only the physical row number
to make it unique and no built-in way to enforce
uniqueness of a given spreadsheet row.
Single rows of a
database table are
uniquely identified by a
Primary Key
Usually, only one user can have write access to
the spreadsheet at any given time
Multiple users can
access a database table
at the same time
A corrupt spreadsheet cannot usually be
repaired!
There are many tools for
repairing and recovering
databases.
Relational Database
Relational Database is the relational model for any
relational database management system (RDBMS).
uses relations, or two-dimensional tables, to store the
information needed to support a business
A relational model has three core components:
a collection of objects or relations( it has a place to
store the data)
operators that act on the objects or relations (a way to
create and retrieve the data)
and data integrity method (a way to make sure that the
data is logically consistent)
2.2 Basic Element of Relational
Database
1. Table or entity
known as a relation, is a two-dimensional structure used to
hold related information.
Heading: table name and column names
Body: rows, occurrences of data
StdSSN StdLastName StdMajor StdClass StdGPA
123-45-6789 WELLS IS FR 3.00
124-56-7890 NORBERT FIN JR 2.70
234-56-7890 KENDALL ACCT JR 3.50

Table: Student
rows
column
2. Row
in a table is a collection or instance of one
thing, such as one employee or one line item
on an invoice.
3. Column
contains all the information of a single type,
and the piece of data at the intersection of a
row and a column
Basic Element of Relational
Databasecont
4. Primary Key
Is a column (or columns) in a table that
makes the row in the table distinguishable
from every other row in the same table.
PRIMARY KEY constraint will not allow
NULL values,
More than one PRIMARY KEY constraint
is allowed on a table.
Basic Element of Relational
Databasecont
5. Foreign Key
A column (or columns) in a table that
draws its values from a primary column
in another table.
The foreign key in the child table refers to
a primary or unique key in the parent
table.
Foreign keys must match candidate key of
source table
Foreign keys in some cases can be null



Basic Element of Relational
Databasecont
6. Data Type
Define a set of values and permissible
operations on the values.
Each column of a table is associate with a
data type
Oracle offers the following basic data types
Varchar2(n)
Number = >Number,Number(n) and Number(o,d)
Date => dd-mm-yy

Basic Element of Relational
Databasecont
7. Relationship
is a way to connect, join, or associate two
tables.
there are 3 types of relationships in a
relational database:
One-to-Many Relationship
one row in a given table is related to many other rows
in a child table
One-to-One Relationship
one row in a given table is related to only one or zero
rows in a second table
Many-to-Many Relationship
one row of a given table may be related to many rows
of another table, also known as associative table.
Basic Element of Relational Databasecont
Alternative Terminology
Table-oriented
(familiar)
Set-oriented
(mathematical)
Record-oriented
(IS Staff)
Table Relation Record-type, file
Row Tuple Record
Column Attribute Field
2.3 Object Relational Database
An object-relational database system supports
everything a relational database system
supports
as well as constructs for object-oriented
development and design techniques (e.g. Java
and C++. ).
Both Oracle9i and Oracle 10g fully support all
of the traditional object-oriented constructs and
methods.
2.3.1 Abstraction
Oracle also supports the object-relational
model is by using abstraction, method,
encapsulation and inheritance
By using abstraction we can define user-
defined objects as an aggregate of several
other datatypes called abstract datatypes.
Abstract Datatypes ->New datatypes,
usually user-created, that are based on one
or more built-in datatypes and can be
treated as a unit.
Methods define which operations can be
performed on an object.
Operations on an object that are exposed for use by
other objects or applications.
Encapsulation restricts access to the object other
than via the defined methods.
technique that may hide, or abstract, the inner
workings of an object and expose only the relevant
characteristics and operations on the object to other
objects.
2.3.2 Method
Inheritance allows objects that are derived
from other objects to use the methods
available in the parent object.

If a new object is created with an existing
object as a base, all of the methods
available with the existing object will also
be available with the new object
2.3.3 Inheritance
2.3.3 Inheritancecont
For example:
if Scott were to implement a new EMPLOYEE
type and a new CUSTOMER type using the
PERSON type as the base

then any methods that already exist for PERSON
would be available when using one of the two
new types.

The method ChangeLastName, defined with the
PERSON type only once, can be used with
objects defined with the CUSTOMER or
EMPLOYEE type.
Data integrity allows to define certain data
quality requirements that the data in the
database needs to meet.
If a user try to insert data that doesn't meet
these requirements, Oracle will not allow
so.
Constraint types
There are five integrity constraints in Oracle.
Not Null, Unique Key, Primary Key, Foreign Key
and Check
2.3.4 Integrity Constraints in Oracle
2.3.4.1 Not Null
1. Not Null
A column in a table can be specified not
null.
It's not possible to insert a null in such a
column.
The default is null. So, in the following
create table statement, a null can be
inserted into the column named c.
2.3.4.1 Not Nullcont
create table example_not_null
( a number not null,
b number null,
c number );

insert into example_not_null values ( 1, null, null);
insert into example_not_null values ( 2, 3, 4);
insert into example_not_null values (null, 5, 6);

The first to second records can be inserted, the
third cannot.
2.3.4.2 Unique Key
2. Unique Key
The unique constraint doesn't allow
duplicate values in a column.
If the unique constraint encompasses two
or more columns, no two equal
combinations are allowed.
2.3.4.2 Unique Keycont
create table Example_unique
( a number unique,
b number
);

insert into Example_unique values (4, 5);
insert intoExample_unique values (2, 1);
insert into Example_unique values (null,9);
insert into Example_unique values (null,9);
insert intoExample_unique values (2, 1);

The first to fourth records can be inserted, the
fifth cannot.
2.3.4.3 Primary Key
3. Primary Key
Also called Entity integrity
a primary key combines a unique and a not
null constraint.
a table can have at least one primary key.
After creating a primary key, it can be
referenced by a foreign key.
Cannot contain null values

2.3.4.3 Primary Keycont
create table Example_PrimaryKey
( a number,
b number,
c number,
constraint pk_name primary key (a, b) );

Primary keys can explicitly be named.
The following create table statement creates a
table with a primary key whose name is
pk_name.
2.3.4.4 Foreign Key
4. Foreign Key
Also called referential integrity constraint)
To ensures that the value in that column is
found in the primary key of another table.
Ensures valid references among tables

create table Example_ForeignKey
( a number,
b number,
c number,
constraint fk_name foreign key (b) references Example_PrimaryKey
5. Check
A check constraint allows to state a minimum
requirement for the value in a column.

create table ri_check_1
( a number check (a between 0 and 10),
b number );
The following table allows only numbers that
are between 0 and 10 in the column a;

2.3.4.5 Check
2.3.4.5 Checkcont
It is also possible to state a check
constraint that check the value of more
than one column.
The following example makes sure that the
value of begin_ is smaller than the value of
end_.

create table check2
begin_ number,
end_ number,
value_ number,
check (begin_ < end_) );
Possible Actions
Restrict: do not permit action on the
referenced row
Cascade: perform related action on related
rows
Nullify: only valid if foreign keys accept
null values
Default: set foreign keys to a default value
2.3.5 Graphical Representation of
Referential Integrity
1 Table
M Table
Connection are from
The primary key (underline)
To the foreign key

You might also like