Sunteți pe pagina 1din 59

Relational Database Model

Table and their Characteristics


Relation : The creation of data relationships based on a
logical construct is know as a Relation
Table :
A table is a two dimensional structure composed of
rows and columns . A table is also known as a relation
Table

is a relation whose contents can be


permanently saved for future use
Table

contains a group of related entity occurrences


called Entity set
For

example : A STUDENT table contains a collection


of entity occurrences , each representing a student

Characteristics of Table
A

table is two dimensional structure


composed of rows and columns
Each table row (tuple) represents a
single entity occurrence of rows and
columns
Each table column represents an
attribute and each column has distinct
name
Each row/column intersection
represents a single data value
All values in a column must be in same
data format

Each

column has a specific range


of values known as attribute
domain

The

order of rows and columns is


immaterial to the DBMS

Each

table must have an attribute


or a combination of attributes that
uniquely identifies each row

Table: STUDENT
Rollno Sname

SDOB

Sclass GPA

100

Praveen

12-feb- BSC
1995

4.5

CS

Yes

101

Rajesh

18mar1996

3.5

CS

No

103

Kiran sri

12-jan- Bcom
1999

4.8

CA

NO

104

priyanka

18-jun- BSC
1999

4.2

CS

NO

BSC

Dep_cod Stu_transf
e
er

DBMS Supports different data types


Numeric

: Numeric data are data on which you


can perform meaningful arithmetic operations or
procedures
Eg: Rollno and GPA are numeric attributes
Character

: Character data also known as text


data or string data can contain any character or
symbol.
Eg: Sname, dept_code
Date

: Date attributes contains calender dates


stored in a special format known as Julian date
format
Eg: SDOB
Logical

: Logical data can have only a true or


false (yes/no ) condition
Eg: stu_transfer

Keys
In

the relational model keys are


important because they are used to
ensure that each row in a table is
uniquely identifiable
They are also used to establish
relationships among tables and to
ensure data integrity
Keys consists of one or more attributes
that determines other attribute
For example , a student hall ticket
number identifies all of the student
attributes such as name and class etc

Various keys used in


DBMS are
1.
2.
3.
4.
5.

Primary key
Super key
Candidate key
Foreign key
Secondary key

Primary key
Primary

key is an attribute that


uniquely identifies a given row
For example , eid is PK for
EMPLOYEE entity set . Similarly
regno is primary key for STUDENT
entity set
(OR )
DBA selects one of the candidate
key as a primary key that uniquely
identifies any given row

Super Key

super key is an attribute or a combination of


attributes that uniquely identifies each row in a
table

For example , each entity has the following


attributes eid,ename, dob, designation, basic ,
street , city etc.
In the above example , eid is enough to identify
one particular Employee entity . Hence eid is a
super key
Similarly using the following combination of
attributes are valid super keys
{eno,ename}, {ename,dob, street}
Here ename is not a super key , because several
employees may have same name

Candidate key
A

super key becomes a candidate key


when no proper subset of the super
key is again a super key
(or)
A candidate key can be described as
a super key without unnecessary
attributes, i.e., a minimal super key
For

example , {eid}, {eid, ename,


dob, city } are candidate keys where
as {eid, ename} is not a candidate
key because {eno} subset of
{eno,ename}.

Foreign key
A

primary key in one table will


become a foreign key in the second
table(when reference is used).
The foreign key sets a relationship
between two tables . For example,
student information is maintained in
two tables as follows
In table 1 stu_details regno is a
primary key , In table 2 stu_marks
regno is a foreign key

STU_DETAILS
Sname

Dob

Addr

regno

Sashank

24-jun1999

kurnool

100

sashidhar

14-nov1999

anantapur

110

STU_MARKS
rahul
16-oct2001

Anantapur

120

Regno

M1

M2

M3

TOTAL

AVG

100

90

80

75

245

81.67

110

75

80

90

245

81.67

120

80

90

90

260

86.66

Referential

Integrity : means that if


the foreign key contains a value , that
value refers to an existing valid
tuple(row) in another relation. For
example, referential integrity is
maintained between the tables
Stu_details and Stu_Marks.

Secondary

Key : it is defined as a key


that is used strictly for data retrieval
purposes.
Note : secondary key does not
necessary yield a unique outcome

Key

attribute : Any attribute that


is part of a key is known as a Key
attribute

Composite

key : A key may be


composed of more than one
attribute , such a multi attribute
key is known as Composite Key

Functional Dependency (FD)


The

attribute B is Functionally dependent on the


attribute A if each value in column A
determines one and only one value in column B.
(or)

Functional dependency is a constraint between


two attributes or two sets of attributes .
For any relation R, Attribute B is functionally
dependent on attribute A if for every valid
instance of A, that value of A uniquely
determines the value of B.
The

FD is represented with arrow from A to B i.e,


AB. It is read as B is functionally dependent
on A.

An Attribute may be FD on two or more attributes


also . Consider the FD diagram for EMPLOYEE
EMP_I
D

COURSE_Titl
e

Name

Dept_Nam
Date_complete
Salary
e
d

Primary Key {Emp_id, Course_Title}


Example1: Emp_id Name, Emp_id Salary
Example 2: (emp_id, Course_Title)
Date_Completed

Fully

Functional Dependency (FFD) :


A FFD is the functional dependency in
which one or more non-key attributes
are functionally dependent on the
combination of the primary key

Partial

functional dependency (PFD):


A PFD is the functional dependency in
which one or more non-key attributes
are functionally dependent on the part
of the primary key

Entity

Integrity :
With in a table, each primary key
value must be unique to ensure
that each row is uniquely
identified by a primary key . In that
case , the table is said to exhibit
entity integrity . To maintain Entity
integrity A NULL is not permitted
in the primary key

Integrity Rules :
Relational database integrity rules are
very important to good database
design. Many (but by no means all)
RDBMSs enforce integrity rules
automatically

Other

integrity rules that can be


enforced in the relational model are
the NOT NULL and UNIQUE
constraints.

NOT

NULL constraint can be placed


on a column to ensure that every row
in the table has a value for that
column.

The

UNIQUE constraint is a restriction


placed on a column to ensure that no
duplicate values exist for that column

Relational Set Operators


Relational algebra defines the
theoretical way of manipulating table
contents using the eight relational
operators . They are
1.UNION
2.INTERSECT
3.DIFFERENCE
4.PRODUCT
5.SELECT
6.PROJECT
7.JOIN
8.DIVIDE

UNION :
UNION combines all rows from two tables,
excluding duplicate rows.
The

tables must have the same attribute


characteristics (the columns and domains
must be identical) to be used in the UNION.
When

two or more tables share the same


number of columns, when the columns
have the same names, and when they
share the same (or
compatible) domains, they are said to be
union-compatible.

PART_1

PART_2

PCODE

PNAME

PRICE

1200

Cooker

1500

1300

Oven

9000

1400

Dish
washer

35000

UNION

PCODE

PNAME

PRICE

1600

Refrigerator

12000

1700

Food
processor

8000

1400

Dish washer

35000

PCODE

PNAME

PRICE

1200

Cooker

1500

1300

Oven

9000

1400

Dish washer

35000

1600

Refrigerator

12000

1700

Food
processor

8000

Yields

Intersect :
INTERSECT

yields only the rows


that appear in both tables.

The

tables must be unioncompatible to yield valid results.

PART_1

PART_2

PCODE

PNAME

PRICE

1200

Cooker

1500

1300

Oven

9000

1400

Dish
washer

35000

PCODE

PNAME

PRICE

1600

Refrigerator

12000

1700

Food
processor

8000

1400

Dish washer

35000

INTERSECT

Yields

PCODE

PNAME

PRICE

1400

Dish
washer

35000

Difference :
DIFFERENCE yields all rows in one
table that are not found in the other
table; that is, it subtracts one table
from the other.
note that subtracting the first table
from the
second table is not the same as
subtracting the second table from
the first table.

PRODUCT :
PRODUCT

yields all possible pairs


of rows from two tablesalso
known as the Cartesian product.

For

example , if one table has 3


rows and the other table has 2
rows, the PRODUCT yields a list
composed of 3 2= 6 rows.

PRODUCT
PCODE

PNAME

PRICE

1000

Oven

12000

1200

cooker

1400

Dish
Washer

PSTORE

STORE _NUM

SHELF_NU
M

Atp

100

12

Knl

200

24

1500
35000
Yields

PCODE

PNAME

PRICE

PSTORE

STORE _NUM

SHELF_NU
M

1000

Oven

12000

Atp

100

12

1000

Oven

12000

Knl

200

24

1200

cooker

1500

Atp

100

12

1200

cooker

1500

Knl

200

24

1400

Dish
Washer

35000

Atp

100

12

1400

Dish
Washer

35000

Knl

200

24

SELECT :
SELECT, also known as RESTRICT,
yields values for all rows found in a
table that satisfy a given condition.
SELECT

can be used to list all of


the row values, or it can yield only
those row values that match a
specified criterion.

In

other words, SELECT yields a


horizontal subset of a table.

STUDENT
SID

SNAME

ADDRESS

Rahul

Atp

Sashank

Knl

Sri

Atp

Bhargav

Atp

SELECT ALL yields New STUDENT table :


SID

SNAME

ADDRESS

Rahul

Atp

Sashank

Knl

Sri

Atp

Bhargav

Atp

SELECT only address = atp yields

SID

SNAME

ADDRESS

Rahul

Atp

Sri

Atp

Bhargav

Atp

PROJECT
PROJECT

yields all values for selected


attributes.
In other words, PROJECT yields a
vertical subset of a table.
STUDENT
SID

SNAME

ADDRESS

Rahul

Atp

Sashank

Knl

Sri

Atp

Bhargav

Atp

PROJECT SNAME yields


SNAME
Rahul
Sashank
Sri
Bhargav

DIVIDE
The

DIVIDE operation uses one single-column table


(i.e. column a) as the divisor and one 2-column
table (i.e. columns a and b) as the dividend.
The tables must have a common column (i.e.
column a.)
The output of the DIVIDE operation is a single
column with the values of column a from the
dividend table rows where the value of the common
column (i.e. column a) in both tables match .

note that:
a. Table 1 is divided by Table 2 to produce Table 3.
Tables 1 and 2 both contain the column CODE but
do not share LOC.
b. To be included in the resulting Table 3, a value in
the unshared column (LOC) must be associated (in
the dividing Table 2) with every value in Table 1.
c. The only value associated with both A and B is 5.

JOINS
JOIN

allows information to be combined from two


or more tables.
JOIN allows the use of independent tables linked by
common attributes.
A natural join links tables by selecting only the
rows with common values in their common
attribute(s).
A natural join is the result of a three-stage process:
a. First, a PRODUCT of the tables is created
b. Second, a SELECT is performed on the output of
Step a to yield only the rows for which the column
values are equal. The common columns are
referred to as the join columns.
c. A PROJECT is performed on the results of Step b to
yield a single copy of each attribute, thereby
eliminating duplicate columns.

CUSTOMER
CUST_CODE

CUST_NAME

CUST_ZIP

AGENT_CODE

1000

Vijay

515001

231

2000

Adams

515210

125

3000

Anil

515001

167

4000

Sri

515120

125

AGENT
AGENT_CODE

AGENT_PHONE

231

9945012354

125

9550866409

167

9959847219

Step 1: PRODUCT of the tables is created


CUST_CODE

CUST_NAME

CUST_ZIP

AGENT_COD
E

AGENT_CODE

AGENT_PHONE

1000

Vijay

515001

231

231

994501235
4

1000

Vijay

515001

231

125

955086640
9

1000

Vijay

515001

231

167

995984721
9

2000

Adams

515210

125

231

994501235
4

2000

Adams

515210

125

125

955086640
9

2000

Adams

515210

125

167

995984721
9

3000

Anil

515001

167

231

994501235
4

3000

Anil

515001

167

125

955086640
9

STEP 2 : SELECT is performed on the output of


Step 1
CUST_CODE

CUST_NAME

CUST_ZIP

AGENT_COD
E

AGENT_CODE

AGENT_PHONE

1000

Vijay

515001

231

231

994501235
4

2000

Adams

515210

125

125

955086640
9

3000

Anil

515001

167

167

995984721
9 of
output

STEP 3: PROJECT is performed on the


Step 2 Sri
4000
515120
125
125
CUST_CODE

CUST_NAME

CUST_ZIP

AGENT_COD
E

955086640
9
AGENT_PHONE

1000

Vijay

515001

231

994501235
4

2000

Adams

515210

125

955086640
9

3000

Anil

515001

167

995984721
9
955086640

Data Dictionary and System


Catalog

Data Dictionary :
The data dictionary provides a detailed
description of all tables found within
database.
The data dictionary contains at least all of
the attribute names and characteristics for
each table in the system.
In short, the data dictionary contains
metadata.
Metadata : data about data. Meta data
describes the properties or characteristics
of other data

System Catalog
The

system catalog contains metadata.


The system catalog can be described as a detailed
system data dictionary that describes all objects within
the database, including data about table names, the
tables creator and creation date, the number of
columns in each table, the data type corresponding to
each column, index filenames, index creators,
authorized users, and access privileges.
Because the system catalog contains all required data
dictionary information, the terms system catalog and
data dictionary are often used interchangeably.
System catalog automatically produces database
documentation. As new tables are added to the
database, that documentation also allows the RDBMS to
check for and eliminate homonyms and synonyms. In

HOMONYM : indicates the use of the same


attribute name to label different attributes.
For example, you might use C_NAME to
label a customer name attribute in a
CUSTOMER table and also use C_NAME to
label a consultant name attribute in a
CONSULTANT table.
SYNONYM: a synonym is the opposite of a
homonym and indicates the use of
different names to describe the same
attribute.
For example, car and auto refer to the
same object.
Hence homonyms and synonyms must be
avoided to lessen confusion

Relationships with in the Relational


Database
1:M Relationship : The 1:M relationship is the
relational database norm.
consider the PAINTER paints PAINTING example
As you examine the PAINTER and PAINTING table
contents
note the following features:

Each painting is painted by one and only one


painter, but each painter could have painted
many paintings

There is only one row in the PAINTER table for any


given row in the PAINTING table, but there may be
many rows in the PAINTING table for any given row
in the PAINTER table.

One-One Relationship
In

this relationship, one entity can be related


to only one other entity, and vice versa.
Example, Each Professor Chairs Department
Relationship is implemented as follows

Emp_num

Dept_code

Prof_name

Dept_name

100

1000

Anil

Computers

200

1500

Sunil

Accounting

300

2000

Swathi

HR

Dept_code

Dept_name

Emp_num

1000

Computers

100

1500

Accounting

200

2000

HR

300

Many-Many Relationship
A

many-to-many (M:N) relationship is


not supported directly in the relational
environment.
M:N relationships can be implemented
by creating a new entity in 1:M
relationships with the original entities.

STU_NUM

STU_NAME

CLASS_CODE

101

Vijay

1000

102

Swarna

1001

103

Krishna

1002

104

Aruna

1002

105

Vani

1001

CLASS_CODE

STU_NUM

COURSE_CODE CLASS_SECTIO
N

1000

101

Computers

1001

101

Accounts

1002

102

IT

1001

102

Accounts

In

M:M relationship each student


can take many classes and each
class can contain many students.
We can convert the M:M relationship
into 1:M relationship as follows

STU_NUM

STU_NAME

101

Vijay

102

swarna

CLASS_CODE

STU_NUM

ENROLL_GRADE

1001

101

AA

1002

102

AB

1001

102

AC

1000

101

AD

CLASS_CODE

CRS_CODE

CLASS_SECTIO
N

1000

COMPUTERS

1001

ACCOUNTANCY

1002

IT

EXPANDED ER MODEL IS

Data Redundancy
Revisited
Data
redundancy means repetition of

data unnecessarily . It leads to data


anomalies .
These anomalies can destroy the
effectiveness of the database. The
anomalies are of three types .
They are
1.Insertion anomaly
2.Deletion anomaly
3.Updation anomaly
The proper use of foreign key controls
data redundancy . It does not eliminate
data redundancy totally, but it
minimizes data redundancy.

Indexes
An

index is an orderly arrangement


used to logically access rows in a
table.
For example , to locate a particular
book in library we use library
catalog which is indexed by title,
topic, author etc
Indexes in the relational database
environment works like the indexes
in the catalog

With out an index we must read each row in


the PAINTING table and see if the Painter
number matches the required painter.
If we index the PAINTER table and use the
index key painter_num it will match the
pointers

An

index is composed of an index key and a


set of pointers. The index key is the indexs
reference point

An

index is ordered arrangement of keys


and pointers. Each key points to the
location of data identified by the key

DBMS

automatically creates a unique index


on the primary key column

unique index is an index in which the


index key can have only one pointer value
associated with it

table can have many indexes but each


index is associated with only one table .
The index can have multiple attributes

CODDS RELATIONAL DATABASE


RULES
In 1985, Dr. E. F. Codd published a
list of 12 rules to define a relational
database system

S-ar putea să vă placă și