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 (predefined 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
Emp Dept Incr

Bonus

SalGrade

6

Relational Model
P.K. Empno 7369 : 7902 Ename SMITH : FORD : …. F.K. Mgr 7902 : 7566 : 20 ….. F.K. Deptno 20 : P.K. Deptno 10 20 30 Dname ACCOUNTIN G RESEARCH 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 7369 7499 7521 7566 7654 7698 SMITH ALLEN WARD JONES JOB CLERK SALESMA N SALESMA N MANAGER MGR 7902 7698 7698 7839 7698 7839 HIREDATE 17-DEC-80 20-FEB-81 22-FEB-81 02-APR-81 28-SEP-81 01-MAY-81 SAL 800 1600 1250 2975 1250 2850 1400 300 500 COMM DEPTNO 20 30 30 20 30 30

MARTIN SALESMA N BLAKE MANAGER

Result of RESTRICT / SELECT on JOB column (i.e. JOB=‘SALESMAN’)
EMPNO ENAME 7499 7521 7654 ALLEN WARD JOB SALESMA N SALESMA MGR 7698 7698 7698 HIREDATE 20-FEB-81 22-FEB-81 28-SEP-81 SAL 1600 1250 1250 COMM 300 500 1400
15

DEPTNO 30 30 30

MARTIN N SALESMA N

Relational Operators
• Project
– Retrieves a set of columns from a table, creating the result table after removing duplicate rows

16

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

N MANAGER 7839

MARTIN SALESMA 7698 BLAKE N MANAGER 7839

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 EMPNO 7369 7499 ENAME SMITH ALLEN DEPTNO 20 30 DEPTN O 10 20 30

DEPT table DNAME LOC ACCOUNTING NEW YORK RESEARCH DALLAS SALES CHICAGO

Data from EMP table EMPNO 7369 7369 7369 7499 7499 7499 ENAME SMITH SMITH SMITH ALLEN ALLEN ALLEN DEPTNO DEPTNO 20 20 20 30 30 30 10 20 30 10 20 30

Data from DEPT table DNAME ACCOUNTING RESEARCH SALES ACCOUNTING RESEARCH SALES LOC NEW YORK DALLAS CHICAGO NEW YORK DALLAS 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 7369 7499 ENAME SMITH ALLEN DEPTNO 20 30 DEPTNO 20 30 DNAME LOC RESEARCH DALLAS 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

Sign up to vote on this title
UsefulNot useful