DBMS
DBMS
What is Indexing ?
Types of Indexing and the difference between them.
2-3 SQL queries to find the nth largest salary , maximum salary of each department etc.
Difference between primary and unique constraints in SQL.
What is Normalization ?
Why it is needed ?
Types of Normalizations and there conditions.
What is Indexing?
There are several types of indexing in DBMS, each suited to different scenarios and
types of queries. The main types are:
1. Primary Index
2. Secondary Index
3. Clustered Index
4. Non-clustered Index
5. Unique Index
6. Bitmap Index
7. Hash Index
1. Primary Index
Definition: A primary index is created on the primary key of a table. The primary
key is a unique identifier for each record in the table.
Characteristics:
Unique: No two rows can have the same primary key value.
Automatically created when the primary key is defined.
Usually, the primary index is stored in a sorted order.
Use Case: Efficiently retrieving rows based on the primary key.
2. Secondary Index
Definition: A secondary index is created on non-primary key columns. It is used
to improve the performance of queries that do not involve the primary key.
Characteristics:
Can be created on multiple columns.
Not necessarily unique.
Used in addition to the primary index.
Use Case: Improving query performance on non-primary key columns, such as
searching by name or date.
3. Clustered Index
Definition: In a clustered index, the table rows are physically stored in the order
of the indexed column(s). A table can have only one clustered index.
Characteristics:
Sorts the actual data rows.
Often the primary key index is clustered.
Faster retrieval of range-based queries due to physical ordering.
Use Case: When data retrieval based on a range of values is frequent, such as
date ranges.
4. Non-clustered Index
Definition: A non-clustered index does not alter the physical order of the rows.
Instead, it creates a separate object within the table that points back to the
original table rows after sorting.
Characteristics:
Can be multiple non-clustered indexes on a table.
Contains pointers to the actual data rows.
Slower than clustered indexes for retrieving ranges.
Use Case: When searching for specific values rather than ranges, like finding a
specific email address.
5. Unique Index
Definition: A unique index ensures that the indexed column(s) contain unique
values. It is similar to a primary key but can be created on any column(s).
Characteristics:
Enforces uniqueness constraint on the column(s).
Can be created on multiple columns.
Prevents duplicate values.
Use Case: Enforcing uniqueness on columns other than the primary key, like an
email or social security number.
6. Bitmap Index
Definition: A bitmap index uses bitmaps (arrays of bits) and is very efficient for
columns with a low cardinality (few distinct values).
Characteristics:
Efficient storage and retrieval for low cardinality columns.
Bitwise operations allow for fast query processing.
Not suitable for columns with high cardinality.
Use Case: Ideal for columns like gender, marital status, or boolean values.
7. Hash Index
Definition: A hash index uses a hash function to convert column values into a
hash code, which points to the location of the data.
Characteristics:
Efficient for equality searches.
Not suitable for range queries as hash codes do not preserve order.
Collisions can occur and need to be handled.
Use Case: Fast lookups for equality conditions, like searching by exact user ID.
Summary
Each type of index serves specific needs and use cases, and the choice of which to use
depends on the nature of the data and the types of queries performed.