Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Understanding Primary vs Secondary Indexes
📖 Scenario: You are managing a small library database. You want to organize the books so you can find them quickly by their unique ID and also by their author names.
🎯 Goal: Build a simple example that shows the difference between a primary index (on book ID) and a secondary index (on author name).
📋 What You'll Learn
Create a data structure to hold book records with IDs and authors
Define a primary index on the book ID
Define a secondary index on the author name
Show how to use both indexes to find books
💡 Why This Matters
🌍 Real World
Databases use primary and secondary indexes to quickly find data without scanning everything.
💼 Career
Understanding indexes is important for database administrators, developers, and data analysts to optimize data retrieval.
Progress0 / 4 steps
1
Create the book records data structure
Create a dictionary called books with these exact entries: 101: {'title': 'Python Basics', 'author': 'Alice'}, 102: {'title': 'Data Science', 'author': 'Bob'}, and 103: {'title': 'Web Development', 'author': 'Alice'}.
DBMS Theory
Hint
Use a dictionary with book IDs as keys and another dictionary for title and author as values.
2
Define the primary index on book ID
Create a variable called primary_index that holds the list of book IDs from the books dictionary keys.
DBMS Theory
Hint
Use the keys() method of the dictionary and convert it to a list.
3
Define the secondary index on author name
Create a dictionary called secondary_index where each author name maps to a list of book IDs they wrote, using the books dictionary.
DBMS Theory
Hint
Loop over books.items(), get the author, and add the book ID to the list for that author in secondary_index.
4
Use both indexes to find books
Create a variable book_by_id_102 that gets the book info for ID 102 using books. Then create a variable books_by_alice that gets the list of book IDs for author 'Alice' from secondary_index.
DBMS Theory
Hint
Use dictionary access with the key 102 for books and key 'Alice' for secondary_index.
Practice
(1/5)
1. What is the main purpose of a primary index in a database?
easy
A. To provide unique and fast access to records using the primary key
B. To speed up searches on non-key columns
C. To store duplicate values for faster retrieval
D. To backup the database automatically
Solution
Step 1: Understand the role of primary index
A primary index is created on the primary key of a table, which uniquely identifies each record.
Step 2: Identify its main function
It ensures fast and unique access to records based on the primary key values.
Final Answer:
To provide unique and fast access to records using the primary key -> Option A
Quick Check:
Primary index = unique fast access [OK]
Hint: Primary index = unique key fast access [OK]
Common Mistakes:
Confusing primary index with secondary index
Thinking primary index allows duplicates
Assuming primary index is for backup
2. Which of the following is the correct statement about creating a secondary index in SQL?
easy
A. CREATE INDEX idx_name ON table(column);
B. CREATE UNIQUE INDEX idx_name ON table(column);
C. CREATE PRIMARY INDEX idx_name ON table(column);
D. CREATE SECONDARY INDEX idx_name ON table(column);
Solution
Step 1: Recall SQL syntax for indexes
Secondary indexes are created using the standard CREATE INDEX statement without the PRIMARY keyword.
Step 2: Identify the correct syntax
CREATE INDEX idx_name ON table(column); uses the correct syntax: CREATE INDEX idx_name ON table(column);
Final Answer:
CREATE INDEX idx_name ON table(column); -> Option A
Quick Check:
Secondary index syntax = CREATE INDEX [OK]
Hint: Secondary index uses CREATE INDEX without PRIMARY [OK]
Common Mistakes:
Using CREATE SECONDARY INDEX which is invalid
Confusing with CREATE PRIMARY INDEX syntax
Using UNIQUE keyword incorrectly for secondary index
3. Consider a table Employees(emp_id, name, department) where emp_id is the primary key. Which index type would speed up a query filtering by department?
medium
A. Primary index on department
B. Primary index on emp_id
C. Secondary index on department
D. No index needed
Solution
Step 1: Identify the primary key and its index
The primary key is emp_id, so the primary index is on emp_id.
Step 2: Determine which index helps filter by department
Since department is not the primary key, a secondary index on department speeds up queries filtering by it.
Final Answer:
Secondary index on department -> Option C
Quick Check:
Filter by non-key column = secondary index [OK]
Hint: Use secondary index for non-primary key columns [OK]
Common Mistakes:
Assuming primary index helps filter by any column
Trying to create primary index on non-key column
Ignoring the benefit of secondary indexes
4. A developer created a secondary index on a column that contains many duplicate values. What is the likely problem?
medium
A. The database will reject the index creation
B. The primary index will be corrupted
C. The secondary index will enforce uniqueness
D. The secondary index will be inefficient due to low uniqueness
Solution
Step 1: Understand secondary index behavior with duplicates
Secondary indexes can be created on columns with duplicates but may become less efficient because many records share the same key.
Step 2: Identify the impact on performance
Low uniqueness means the index has many entries pointing to multiple rows, slowing down search performance.
Final Answer:
The secondary index will be inefficient due to low uniqueness -> Option D
Quick Check:
Duplicates in secondary index = inefficiency [OK]
Hint: Secondary index on duplicates slows searches [OK]
Common Mistakes:
Thinking secondary index enforces uniqueness
Believing primary index gets corrupted
Expecting index creation to fail
5. You have a large table with a primary index on customer_id and a secondary index on city. You want to optimize queries filtering by both customer_id and city. What is the best indexing strategy?
hard
A. Drop the secondary index and rely only on primary index
B. Create a composite index on (customer_id, city)
C. Create a secondary index on customer_id only
D. Create two separate secondary indexes on customer_id and city
Solution
Step 1: Analyze current indexes and query filters
Primary index exists on customer_id, secondary index on city. Queries filter by both columns.
Step 2: Understand composite index benefits
A composite index on (customer_id, city) allows efficient filtering on both columns together, improving query speed.
Step 3: Evaluate other options
Dropping indexes or creating separate secondary indexes won't optimize combined filtering as well as a composite index.
Final Answer:
Create a composite index on (customer_id, city) -> Option B
Quick Check:
Combined filter = composite index [OK]
Hint: Use composite index for multi-column filters [OK]