[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Creating a database

Posted on 2003-11-12
9
Medium Priority
?
269 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:lwinkenb
  • 5
  • 4
9 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 2000 total points
ID: 9737229
>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
 
LVL 5

Author Comment

by:lwinkenb
ID: 9737441
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9737465
> 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 9737716
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
 
LVL 5

Author Comment

by:lwinkenb
ID: 9751410
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9753048
thanks for taking out the time to post .. I was beginning to think that question has been abandoned

good luck
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 9753329
abandon a question??
never =)
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 9758024
Thanks for the help Sunny, it looks like I'll be able to do this OK.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9758078
gr8... thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question