Developing a Small Database Management System in C

Developing a Small Database Management System in C

Introduction

In this article, we will develop a simple database management system using the C programming language. This system will support basic functionalities such as inserting, querying, and deleting data. Even if you are a beginner in C, you can complete this project with the step-by-step guidance provided in this article.

Feature Overview

Our small database management system will include the following basic features:

  1. Add Record: Ability to add new records to the database.
  2. Query Record: Ability to query records based on specific conditions.
  3. Delete Record: Ability to delete specified records from the database.

To simplify our example, our database will only handle simple data structures, with each record containing two fields, such as <span>ID</span> and <span>Name</span>.

Data Structure Definition

First, we need to define our data structure. In this example, we will use a struct to represent each record in the database.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_RECORDS 100 // Maximum number of records
#define NAME_LENGTH 50   // Maximum length for names
// Structure definition for database entries
typedef struct {
    int id;                 // Unique identifier
    char name[NAME_LENGTH]; // Name field
} Record;
// Simple simulation of a DB-like array to store multiple Records
Record database[MAX_RECORDS];
int current_count = 0; // Counter for the current number of stored records

In the code above, we define the <span>Record</span> structure to hold each user’s information, while creating an array for our entire “database” that can hold up to 100 records.

Add Record Function

Next, we will build a method to add new records. In this method, the user will be prompted to enter the ID and Name, which will populate the respective fields and add them to the “database”.

void add_record() {
    if (current_count >= MAX_RECORDS) {
        printf("Database is full!\n");
        return;
    }
    Record new_record;
    printf("Enter ID: ");
    scanf("%d", &new_record.id);
    printf("Enter Name: ");
    scanf("%s", new_record.name);
    database[current_count] = new_record;
    current_count++;
    printf("Record added successfully!\n");
}

Implementation Logic Explanation

  • Check if the current count has reached the maximum capacity.
  • Use <span>scanf</span> to get user input and populate the new <span>Record</span>.
  • Add the new record to the “database” array and update the counter.

Query Record Function

Next, we will implement a query function that searches for and prints relevant information based on the given ID. If not found, an error message will be displayed.

void query_record() {
    int search_id;
    printf("Enter the ID to search for: ");
    scanf("%d", &search_id);
    for (int i = 0; i < current_count; i++) {
        if (database[i].id == search_id) {
            printf("Record Found: ID=%d, Name=%s\n", database[i].id, database[i].name);
            return;
        }
    }
    printf("No record found with ID %d\n", search_id);
}

Implementation Logic Explanation

  • Input the ID to search for.
  • Loop through all records stored in the array, comparing each with the search ID. If found, output the result; otherwise, handle the error.

Delete Record Function

Implement the delete operation, which also searches for the corresponding data item based on the provided ID and removes it. We will maintain order by overwriting, but it is acceptable for small examples not to keep empty slots contiguous. In actual applications, more complex methods (like linked lists) may be chosen depending on different design principles.

void delete_record() {
    int delete_id;
    printf("Enter the ID to delete: ");
    scanf("%d", &delete_id);
    for (int i = 0; i < current_count; i++) {
        if (database[i].id == delete_id) {
            for (int j = i; j < current_count - 1; j++) {
                database[j] = database[j + 1];
            }
            current_count--;
            printf("Record deleted successfully!\n");
            return;
        }
    }
    printf("No record found with ID %d\n", delete_id);
}

Implementation Logic Explanation

  • User re-enters the ID of the item to be deleted.
  • After retrieving the target element, loop to overwrite the subsequent data without changing the total count, then reduce the counter and provide timely feedback to enhance user experience.

Main Menu Driver Design

Finally, we will build a main menu to provide an interactive interface for users to select the desired operation:

void display_menu() {
   while(1) {
       int choice;
       printf("\n--- Simple Database Management System ---\n");
       printf("[1] Add Record\n");
       printf("[2] Query Record\n");
       printf("[3] Delete Record\n");
       printf("[4] Exit\n");
       scanf("%d", &choice);
       switch(choice) {
           case 1:
               add_record();
               break;
           case 2:
               query_record();
               break;
           case 3:
               delete_record();
               break;
           case 4:
               exit(0);
           default:
               printf("Invalid Selection!\n");
       }
   }
}
int main() {
   display_menu();
}

Leave a Comment