Solved

Creating a database

Posted on 2003-11-12
9
262 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 500 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with form project 66 314
C++ vs C compilers 13 155
Detect CR LF to each line 12 137
How to programmatically differentiate between C and Java 10 175
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now