• C

Creating a database

Im trying to create a database for my program, and I have some questions.  Basically, the way I was thinking of setting it up was as follows:
[first 4 bytes to serve as a file signature][next 2 bytes say how many tables][list of table headers (1 per table)]
A table header being 24 bytes as follows:
[first 16 byes being a table name][next 4 bytes saying how many bytes per record][next 4 bytes says how many records in table]

Now my question is this:
First of all, is my above approach correct, and if not what suggestion do you have?
Also, what is the best way to interact with the database (should I read the whole thing into memory when I start my program, and then write it all back to the file when I close?).  

The program that the database is for is rather simple, but I was my database to be flexible so that I could reuse it in other programs.  All suggestions are very welcome.
LVL 5
lwinkenbAsked:
Who is Participating?
 
sunnycoderCommented:
>First of all, is my above approach correct, and if not what suggestion do you have?
It all depends on how big your database is going to be... For small academic teerm project, your approach looks impressive ...
Your approach is indeed good for a small and fast database...
>next 4 bytes says how many records in table
will limit your number of records to 2^32 and that should suffice for most purposes...
how do you propose to handle variable length fields and records? Or is it that you intend to support only fixed length fields ?

>Also, what is the best way to interact with the database (should I read the whole thing into memory when I start my program, >and then write it all back to the file when I close?).
If your database is full to capacity, I would not advise that ... best way would be to build an index for the database, may be multi level index if it happens to be a big one
for little more information on multilevel indices, refer to this link
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20751026.html

you can store this index on persistent storage and retrieve this index when user opens the database
0
 
lwinkenbAuthor Commented:
Thanks for the comments sunny.

I dont think I will worry about variable length fields.  Fixed length should work fine for me.  What I do have a question about though is how to store more than one table in the file:

Let's say my file has two tables (table1 and table2).  Table1 has record sizes of 100 bytes, and table2 has record sizes of 200 bytes.  My file would look like:

[file sig][number of tables(2)][table1 header][table2 header]
[table1 record]
[table1 record]
[table1 record]
[table1 record]
[table2 record]
[table2 record]
[table2 record]

Now let's say I want to store a new record in table1.  I can find the end of table1 by multiplying the number of records in the table by the record size.  But how would I add the record in without overwriting the data in table2.  I'm guessing there is something wrong with my methodology here, but I'm not sure what it is.
0
 
sunnycoderCommented:
> I'm guessing there is something wrong with my methodology here,
you are almost there but not quite... you do not have to store the tables consecutively in a single file ... you can store each table in a different file and let table name be the filename ... databases are persistent !!
or you can keep a table of filenames to which table header points ... the entry pointed to by the table header will give the filename

0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
sunnycoderCommented:
databases are generally closely asociated with memory management...
as you have rightly observed the problem
>But how would I add the record in without overwriting the data in table2.
a more generic problem is, how to insert in the midle of the file ...

there are a number of work arounds... one of the approaches is, to keep track of your database in terms of number of pages ( a linked list of pages ... you can organize them similar to inode structures in *nix)
that way you need to rewrite only a page back to the memory... if it is full and overflows, allocate another page and insert it in the middle of the list....
Since access to linked list is sequential, indexing in such an implementation becomes extremely important ...

i would recommend reading books on databases there was a book by ramakrishna gerkhe
there was another book by navathe

both have some useful information
0
 
lwinkenbAuthor Commented:
Hey sunny, just wanted to post here so you know I havnt abandoned the question.  I will be implementing the DB this weekend, and I want to keep this Q open just in case I have some questions on the specifics.
0
 
sunnycoderCommented:
thanks for taking out the time to post .. I was beginning to think that question has been abandoned

good luck
0
 
lwinkenbAuthor Commented:
abandon a question??
never =)
0
 
lwinkenbAuthor Commented:
Thanks for the help Sunny, it looks like I'll be able to do this OK.
0
 
sunnycoderCommented:
gr8... thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.