Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I wrote a bus ticket reservation system, in c. The admin could add buses, and number of seats, then for each bus, the admin could book seats with using the user details, cancel reservation. Adding a bus, created a new .DAT file where there would be the bus id, stop names, fares , total seats and occupied seats. And the reservation.DAT would contain all the booking details. but I have lost the whole project. Thinking of rebuilding it, I wanted to add a little touch of SQL to it.

I could use an existing library like sqlite3 , but for the sake of knowing, I am trying to implement a simple sql engine logic on my own : I would pass the table name, the column names with the type of data to be stored, and maybe a primary key. It would generate a file by the table name with those fields in it. Now I am also learning c++ (I want to make this my pet-project) and I haven't found much on dynamic indexing , all i see is using B+ trees.

(I want to implement the database system rather than the bus reservation system as for now)

I have seen this link but as far as I understand , it has only 3 specific fields, I have tried to implement my own logic here , I can pass the names of column and table name,

struct Table {
char *tableName;
char *columns[];
};

class DB {
    private :
       //something later
    public :
        Table createTable(char*);
        void createColumns(Table *,int, char**);
        void displayTable(Table *);
};
Table DB::createTable(char *name){
    Table t;
    t.tableName = tname;
    return t;
 }
 //other functions..
int main() {
     char *params[] = {"id", "name", "fare"};
     DB d;
     Table bus;
     bus = d.createTable("buses");
     d.createColumns(&bus,3,params);
     //although I created the table with column names, I can't give them the type. I am not sure, even if its done this way.
     return 0; }

but I cannot determine how to specify the type of data it would hold.

And, if I were to implement a b+tree, how would I do it, I mean, in C , a particular struct was specified, from the beginning, but now, I would not have any prior knowledge as to what the fields in the struct will be, without the data type, space allocation will be difficult or impossible I guess. Everything is so blurry.

It would be great to get a little direction as how to proceed the right way.

Edit 1
For those who think it may be too broad, I wish to implement five, simple queries, create, insert, update,delete and select, and maybe indexing . Meanwhile I was reading more, I found that these databases use parsers and etc.. Which I am not quite expecting to do right now. I am also saving the optimizations, concurrency, locking protocols and everything a good/awsome programmer can think of to make it a huge prôject , for later, simple functions that take arguments might do the job.

Edit 2
I have learnt that std::map is used to create associative arrays in c++ . For example:

std::map<int, string> id;
id[1] = "bus_name";

But still the problem of determine the field name id remains, which I cannot do at runtime. Can associative arrays be used to make a document based storage or do I need json libraries?

share|improve this question

put on hold as too broad by Blrfl, MichaelT, Wayne M, david.pfx, mattnz Jul 16 at 4:14

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
"How do I implement a SQL database" is a very, very broad question. –  Blrfl Jul 15 at 17:36
    
I could start with a simple create , update , insert and delete .. Then progress to optimizations .. But that's for later @Blrfl –  blackbee Jul 15 at 23:35
add comment

Browse other questions tagged or ask your own question.