Link to home
Start Free TrialLog in
Avatar of lwinkenb
lwinkenb

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lwinkenb
lwinkenb

ASKER

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.
> 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

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
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.
thanks for taking out the time to post .. I was beginning to think that question has been abandoned

good luck
abandon a question??
never =)
Thanks for the help Sunny, it looks like I'll be able to do this OK.
gr8... thanks