Create an Database Management System in C++ with Free Codes

Starting – Intro

A Database Management System is a software system which provides efficient, reliable, convenient, and secure data storage and retrieval. Building a DBMS in C++ involves creating data structures to store data, implementing algorithms for data manipulation, and developing user interface with the database.

Features include in DBMS Project

  1. Data Storage and Retrieval
  2. Data Manipulation
  3. Data Integrity
  4. Concurrency Control
  5. Transaction Management
  6. User Interface

Project Codes

Here is the complete project codes,

1. Data Structure for Records

#include <iostream>
#include <vector>
#include <string>

struct Record {
    int id;
    std::string name;
    std::string email;

    Record(int i, std::string n, std::string e) : id(i), name(n), email(e) {}
};

2. DBMS Class

class DBMS {
private:
    std::vector<Record> records;

public:
    void insertRecord(int id, const std::string& name, const std::string& email) {
        records.emplace_back(id, name, email);
        std::cout << "Record inserted successfully.\n";
    }

    void updateRecord(int id, const std::string& name, const std::string& email) {
        for (auto& record : records) {
            if (record.id == id) {
                record.name = name;
                record.email = email;
                std::cout << "Record updated successfully.\n";
                return;
            }
        }
        std::cout << "Record not found.\n";
    }

    void deleteRecord(int id) {
        auto it = std::remove_if(records.begin(), records.end(),
            [id](const Record& record) { return record.id == id; });
        if (it != records.end()) {
            records.erase(it, records.end());
            std::cout << "Record deleted successfully.\n";
        } else {
            std::cout << "Record not found.\n";
        }
    }

    void searchRecord(int id) const {
        for (const auto& record : records) {
            if (record.id == id) {
                std::cout << "Record found: " << record.id << ", " << record.name << ", " << record.email << "\n";
                return;
            }
        }
        std::cout << "Record not found.\n";
    }

    void displayAllRecords() const {
        std::cout << "All records:\n";
        for (const auto& record : records) {
            std::cout << record.id << ", " << record.name << ", " << record.email << "\n";
        }
    }
};

3. Main Function

int main() {
    DBMS dbms;
    int choice, id;
    std::string name, email;

    while (true) {
        std::cout << "\nDatabase Management System Menu\n";
        std::cout << "1. Insert Record\n";
        std::cout << "2. Update Record\n";
        std::cout << "3. Delete Record\n";
        std::cout << "4. Search Record\n";
        std::cout << "5. Display All Records\n";
        std::cout << "6. Exit\n";
        std::cout << "Enter your choice: ";
        std::cin >> choice;

        switch (choice) {
            case 1:
                std::cout << "Enter id, name, email: ";
                std::cin >> id >> name >> email;
                dbms.insertRecord(id, name, email);
                break;
            case 2:
                std::cout << "Enter id, new name, new email: ";
                std::cin >> id >> name >> email;
                dbms.updateRecord(id, name, email);
                break;
            case 3:
                std::cout << "Enter id to delete: ";
                std::cin >> id;
                dbms.deleteRecord(id);
                break;
            case 4:
                std::cout << "Enter id to search: ";
                std::cin >> id;
                dbms.searchRecord(id);
                break;
            case 5:
                dbms.displayAllRecords();
                break;
            case 6:
                std::cout << "Exiting...\n";
                return 0;
            default:
                std::cout << "Invalid choice. Please try again.\n";
        }
    }
    return 0;
}

Database Management System - E-Books

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top