You are on page 1of 23

Introduction to

Relational Model
and
Database Integrity
DBMS Models and
Database Integrity

• Objectives -
– List & describe DBMS models
– Describe Relational model
– Explain Database Integrity & it’s key
definitions
– List & explain Relational operators

2
DBMS Models

• Hierarchical
• Network
• Relational
• Object Oriented
• Object Relational

3
Relational Model
• Relational Model
– Data is organized in terms of rows and columns
in a table
– Does not maintain physical relation (pre-
defined relation) between databases
– Terms
• Table/Relation
• Row/Tuple
• Column/Attribute

4
Relational Model
• Employee Table
EmpNo Ename Job Hiredate Mgr Sal Comm Deptno

5
Relational Model

• Database
Dept
Emp Incr

Bonus SalGrade

6
Relational Model

P.K. F.K. F.K. P.K.

Empno Ename …. Mgr ….. Deptno Deptno Dname

7369 SMITH 7902 20 10 ACCOUNTIN


G
: : : : : : 20 RESEARCH

7902 FORD 7566 20 30 SALES

Emp Table Dept Table

7
Relational Model

• Features
– Data independence
– Supports many-to-many relationships
– Operates on a set of data at a time rather than a
record at a time
– Simple, interactive query language
– An RDBMS has to satisfy the 12 rules (Codd’s
rules) to be accepted as a full-fledged RDBMS
8
Database Integrity
• Database Integrity refers to the wholeness and
correctness of the database
– Key Definitions
• Primary Key
• Foreign Key
– Integrity Rules
• Entity Integrity
• Referential Integrity

9
Key Definitions
• Primary Key
– Primary Key is a column or a set of columns in
a table which uniquely identifies a row in a
table
– One of the candidate keys is chosen as the
primary key
– There can be only one primary key per table
– Primary key may be a compound key

10
Key Definitions

• Foreign Key
– If a key in a given table has a reference to the
value of a primary key of some other relation
then it is called as a foreign key
– A table can have more than one foreign keys

11
Integrity Rules
• Entity Integrity Rule
– No primary key in the given table can be null
– No two rows have the same value for primary key

• Referential Integrity Rule


– In a given table if a foreign key references to a primary
key of some other table then every value of the foreign
key in the given table must be either null or be
available in the other table

12
Relational Operators
• Relational Operators are
– Restrict (Select)
– Project
– (Cartesian) Product
– Join
– Union
– Intersection
– Difference

13
Relational Operators
• Restrict / Select
– Retrieves a set of rows from a relational table based on
the condition given (for the values in a column or set of
columns)

– To specify condition where clause is used with select


operator

14
Emp Table
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMA 7698 20-FEB-81 1600 300 30
7521 WARD N
SALESMA 7698 22-FEB-81 1250 500 30
7566 JONES N
MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMA 7698 28-SEP-81 1250 1400 30
7698 BLAKE N
MANAGER 7839 01-MAY-81 2850 30

Result of RESTRICT / SELECT on JOB column (i.e. JOB=‘SALESMAN’)

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


7499 ALLEN SALESMA 7698 20-FEB-81 1600 300 30
7521 WARD N
SALESMA 7698 22-FEB-81 1250 500 30
7654 MARTIN N
SALESMA 7698 28-SEP-81 1250 1400 30
N
15
Relational Operators
• Project
– Retrieves a set of columns from a table, creating the
result table after removing duplicate rows

16
EMP Table
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMA 7698 20-FEB-81 1600 300 30
7521 WARD N
SALESMA 7698 22-FEB-81 1250 500 30
7566 JONES N
MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMA 7698 28-SEP-81 1250 1400 30
7698 BLAKE NMANAGER 7839 01-MAY-81 2850 30

Result of PROJECT on JOB column


JOB
CLERK
SALESMAN
MANAGER

17
Relational Operators
• (Cartesian) Product
– Produces a result table whose rows are a
concatenation of every row from one table with
every row from another table
– Though Cartesian product is not useful itself, it
is important as an intermediate step in
obtaining more meaningful result

18
EMP table DEPT table
EMPNO ENAME DEPTNO DEPTN DNAME LOC
7369 SMITH 20 O
10 ACCOUNTING NEW
7499 ALLEN 30 20 RESEARCH YORK
DALLAS
30 SALES CHICAGO

Data from EMP table Data from DEPT table


EMPNO ENAME DEPTNO DEPTNO DNAME LOC
7369 SMITH 20 10 ACCOUNTING NEW YORK
7369 SMITH 20 20 RESEARCH DALLAS
7369 SMITH 20 30 SALES CHICAGO
7499 ALLEN 30 10 ACCOUNTING NEW YORK
7499 ALLEN 30 20 RESEARCH DALLAS
7499 ALLEN 30 30 SALES CHICAGO

Resulted (Cartesian) Product of emp and dept


tables
19
Relational Operators
• Join
– It is a combination of the (Cartesian) product,
select and (possibly) projection operation
– Types of join
• Equi-Join
• Outer-Join
– One Way Outer Join
• Left Outer Join
• Right Outer Join
– Two Way Outer Join

20
Result of JOIN combination of the (Cartesian) product, select
and projection operation
EMPNO ENAME DEPTNO DEPTNO DNAME LOC

7369 SMITH 20 20 RESEARCH DALLAS

7499 ALLEN 30 30 SALES CHICAGO

21
Relational Operators
• Union
– Combines rows from one table with rows from another
table
– Duplicate rows are eliminated
– Tables must be union compatible

• Intersection
– Produces a result table with rows common to both
tables

22
Relational Operators
• Difference
– Produces a result table that contains rows that
occur in first table, but not in the second

23

You might also like