What are different hashing methods in DBMS?

DBMSDatabaseBig Data Analytics

DBMS for GATE Exams

178 Lectures 14.5 hours

DBMS in Simple Steps

194 Lectures 16 hours

Hashed file organisation is also called a direct file organisation.

In this method, for storing the records a hash function is calculated, which provides the address of the block to store the record. Any type of mathematical function can be used as a hash function. It can be simple or complex.

Hash function is applied to columns or attributes to get the block address. The records are stored randomly. So, it is also known as Direct or Random file organization.

If the generated hash function is on the column which is considered as key, then the column can be called as hash key and if the generated hash function is on the column which is considered as non-key, then the column can be called as hash column.

Suppose, a file has n records, each record has a key, which uniquely determines the record. Let K is the key and L is the memory location. Hash function is defined as H: K->L

Example

Given below is an example of hashed file organisation −

 0 1 2 011 Delhi 3 4 022 Calcutta 5 6 033 Chennai 7 8 044 Mumbai 9

Here STD codes of cities are keys. Hash function maps these keys into the addresses 2,4,6,8 by just adding the digits of the keys.

Methods of Hash function

The methods of Hash function H(k) are explained below −

• Division Method

The Hash function H(k)=k (mod m), where m is a prime method which is greater than the number of keys.

Example

Let 76 students have a unique 10 digit regno. Here regno is key.

m=79 (greater than 76)

L consists of 100 memory locations 00,01,02,…..99

For the regno 0148105102, 0148105124, 0148105405 H(k) is shown below −

H(0148105102) = 0148105102 % 79 = 10
H(0148105124) = 0148105124 % 79 = 32
H(0148105405) = 0148105405 % 79 = 76
• Mid -square Method

The key k is squared, and then the address is obtained by taking middle digits.

Example

Let 200 employees have a unique 3 digit empid. Here, empid is key.

H(k) = 2nd and 3rd digit of k2
K: 067 048 146
K2: 4489 2304 21316
H(k): 48 30 13
• Folding method

The key is portioned into number of parts k1,k2,…….kn then the parts are added together by ignoring the carry, that is H(k) = k1+ k2+ ……..+ kn

Example

Let 2000 employees have a unique 4 digit empid. Here, empid is key.

H(1643) =16+43 =57
H(1999) =19+99 = 18 (The leading digit 1 is ignored)
H(1176) = 11 + 76 = 87
H( 1374) = 13+ 74 =87

When more than one key refers to the same memory location that problem is called collision.

Updated on 08-Jul-2021 07:49:10