User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,444 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,617 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1358 | Replies: 15 | Solved
Reply
Join Date: Nov 2007
Posts: 40
Reputation: jay64 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
jay64's Avatar
jay64 jay64 is offline Offline
Light Poster

How to properly use tables in db?

  #1  
Dec 1st, 2007
I don't really know how to explain this easily. I guess I will layout the project I am trying to do, and the problem I am having with it, and maybe someone could explain it better to me.
I am trying to build an online food journal for a fitness/nutrition company. I have a form page made out that will allow their clients to input their eating habits for the day.
http://befit4riding.com/food_journal.html
I was thinking that I would have a db for each client, and each day would be a new table. I need to be able to store each day of data and keep it accecible for a later time. I also need to email the data to the company each time new data is submitted. Turns out, the server I am working on only allows 1 db. So how do I organize the tables so that they are all under the same client, but I can have multiple clients? Is there some kind of 'sub-table' (db/table/sub-table)? If I am not explaining myself adequately, please ask some specifics and I will be happy to try again.

notes on the page. I didn't have it automatically input the date, because sometimes people don't update everyday, so they need to be able to come in and put in multiple days at once, but have the correct date for each one. Eventually I might put in a calendar function, but I would have to learn that one and trying to just get this going first.

Thanks.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Posts: 40
Reputation: jay64 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
jay64's Avatar
jay64 jay64 is offline Offline
Light Poster

Re: How to properly use tables in db?

  #2  
Dec 1st, 2007
oops. I just read the announcement that daniweb doesn't want us linking to our sites because of spam reasons. I in no way was trying to promote the site, I just linked it to give a reference for what I was trying to do. If this is a violation, could one of the mods please remove the link from my post? I can't seem to find an edit post feature here.
Reply With Quote  
Join Date: Sep 2005
Posts: 709
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 43
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Master Poster

Help Re: How to properly use tables in db?

  #3  
Dec 1st, 2007
Originally Posted by jay64 View Post
I don't really know how to explain this easily. I guess I will layout the project I am trying to do, and the problem I am having with it, and maybe someone could explain it better to me.
I am trying to build an online food journal for a fitness/nutrition company. I have a form page made out that will allow their clients to input their eating habits for the day.
http://befit4riding.com/food_journal.html
I was thinking that I would have a db for each client, and each day would be a new table. I need to be able to store each day of data and keep it accecible for a later time. I also need to email the data to the company each time new data is submitted. Turns out, the server I am working on only allows 1 db. So how do I organize the tables so that they are all under the same client, but I can have multiple clients? Is there some kind of 'sub-table' (db/table/sub-table)? If I am not explaining myself adequately, please ask some specifics and I will be happy to try again.

notes on the page. I didn't have it automatically input the date, because sometimes people don't update everyday, so they need to be able to come in and put in multiple days at once, but have the correct date for each one. Eventually I might put in a calendar function, but I would have to learn that one and trying to just get this going first.

Thanks.


Unless you have two separate applications, you don't need two databases. You can even have different applications share 1 db by prefixing tables. eg: app1_table, app2_table

I think what you want to do is create relationships between your tables. (no db's don't have sub tables). Since your working with a relational database, you can create relationships between tables that simulates "sub-tables".

You don't need a db for every client, or table for every client. This is very inefficient.

What you want is a single table for your clients. Each client should have a unique index.
Then you want another table storing information about clients. Each info has a unique index, and another index with a value that corresponds to the index in the client table. This lets you know that this data is for that client.
If you need different data for different days stored, just create a column where you save a timestamp in your client data table.

It would be easier to understand if you have a good book on relational databases to guide you.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Join Date: Nov 2007
Posts: 40
Reputation: jay64 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
jay64's Avatar
jay64 jay64 is offline Offline
Light Poster

Re: How to properly use tables in db?

  #4  
Dec 1st, 2007
digital-ether, thank you very much for breaking it down like that for me. I figured it was something like that, but it wasn't showing any of that on the sites I was studying and I didn't know the terminology to search for it. Thanks. I'll look into the relational databases and unique indexes.
Reply With Quote  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 11,019
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 33
Solved Threads: 116
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: How to properly use tables in db?

  #5  
Dec 1st, 2007
> I in no way was trying to promote the site, I just linked it to give a reference for what I was trying to do. If this is a violation, could one of the mods please remove the link from my post?

I cannot speak for the other mods, but I am more lax about this rule in the web development forums. What really pisses me off is when people post in the Internet marketing forums questions like, "Please give me suggestions on how I can promote my website www.mysite.com". It's also very hard to have candid discussions related to Internet marketing with the constant fear that everyone knows who you are and your competitors are there to see what you're doing and how you're doing it ... a general rule of not mentioning your site allows much more unbiased conversations because people are free to share their experiences about what worked and what didn't with regards to making money, advertising, etc,.
Reply With Quote  
Join Date: Nov 2007
Posts: 40
Reputation: jay64 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
jay64's Avatar
jay64 jay64 is offline Offline
Light Poster

Re: How to properly use tables in db?

  #6  
Dec 4th, 2007
Ok, after doing some reading, I think I got an idea of how to structure the table, client_logs. I can have a row for the client, with a specific date, and then all the fields from the page for that date. Then when I want to pull up a specific day of log entry, I can query the db with

SELECT * FROM client_logs WHERE client='value', date='value'

That is what I got from my research on this, but it looks like digital-ether, has a slightly different method, which sounds like it might save time in the long run. Any suggestions/critiques/ways to do it better on this approach?
Reply With Quote  
Join Date: Sep 2005
Posts: 709
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 43
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Master Poster

Help Re: How to properly use tables in db?

  #7  
Dec 5th, 2007
Originally Posted by jay64 View Post
Ok, after doing some reading, I think I got an idea of how to structure the table, client_logs. I can have a row for the client, with a specific date, and then all the fields from the page for that date. Then when I want to pull up a specific day of log entry, I can query the db with

SELECT * FROM client_logs WHERE client='value', date='value'

That is what I got from my research on this, but it looks like digital-ether, has a slightly different method, which sounds like it might save time in the long run. Any suggestions/critiques/ways to do it better on this approach?


Thats pretty much the same as what I mentioned.
Do you have a separate table for clients?

So you're query would be like:

SELECT * FROM client_logs WHERE client={clientid}, date='{timestamp}' LIMIT 1

where the {clientid} is the index of the client's row in the clients table and {timestamp} is any date/time format.

Note the LIMIT 1. It's good practice to let the DB know that you only want a single entry. The reason is that the DB stops iterating through its index as soon as it reaches the first valid row. Otherwise, it goes through all rows. If you have something like 100 000 rows, this makes a huge difference.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Join Date: Nov 2007
Posts: 40
Reputation: jay64 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
jay64's Avatar
jay64 jay64 is offline Offline
Light Poster

Re: How to properly use tables in db?

  #8  
Dec 5th, 2007
Originally Posted by digital-ether View Post
Thats pretty much the same as what I mentioned.
Do you have a separate table for clients?

So you're query would be like:

SELECT * FROM client_logs WHERE client={clientid}, date='{timestamp}' LIMIT 1

where the {clientid} is the index of the client's row in the clients table and {timestamp} is any date/time format.

Note the LIMIT 1. It's good practice to let the DB know that you only want a single entry. The reason is that the DB stops iterating through its index as soon as it reaches the first valid row. Otherwise, it goes through all rows. If you have something like 100 000 rows, this makes a huge difference.


Well, actually, I don't have any tables made yet, still trying to understand it before I start making things. I don't want to do a lot of work and then have to go back and change it all around. I am having a hard time understanding why I would use two tables. I am trying to make it automated, that the client can go in and choose their own username and then start logging in to their food journal. If I have them automatically added to the first table, clients, then how does the second table know which cliented they were assigned?

Thanks for the note on the LIMIT 1.
Reply With Quote  
Join Date: Aug 2007
Location: Argentina
Posts: 83
Reputation: martin5211 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
martin5211's Avatar
martin5211 martin5211 is offline Offline
Junior Poster in Training

Re: How to properly use tables in db?

  #9  
Dec 5th, 2007
I recommend every-time as possible the guideline use mentioned by digital-ether. I will try to make a more deep review.

Create one table for clients, first field called id_(table name), type Int, as primary key and with auto-increment extra feature. Then, when you add a record, a new auto-incremented unique id number will be generated for each client. That's the normal usage for tables.

You can translate the client id to another tables to maintain the relationships between tables (e.g. for posts table, I suggest to create the first field id_post, the second as id_client and then the additional ones - title, body, etc.).

When you add a new client the question is how to insert the resultant client id in another table? Well, you could do a lookup by client name to get the client id before, using a input field or a dropdown list with client names and id values, or use
SELECT id FROM clients ORDER BY id_client DESC LIMIT 1
same as mentioned by digital-ether again, in this case to get the latest inserted id from clients. When you make a insertion in another table do not forget to insert the client id.

You could make a lookup by logon name, e.g.
SELECT id FROM clients WHERE username = $logon_name

To make a single query through several tables with a same field in common (e.g. client id) you could perform a query like this:

SELECT *
FROM clients
INNER JOIN posts ON clients.id_client = posts.id_client
WHERE clients.name LIKE 'john smith'

That statement will display results from two tables like it was a single table.
Or you could do simple requests into each table and merge the resultants arrays in one.
Last edited by martin5211 : Dec 5th, 2007 at 1:18 pm.
Reply With Quote  
Join Date: Nov 2007
Posts: 40
Reputation: jay64 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
jay64's Avatar
jay64 jay64 is offline Offline
Light Poster

Re: How to properly use tables in db?

  #10  
Dec 5th, 2007
Digital-ether and martin5211, thanks for being patient and spelling it out for me. I think I'm starting to get a hang of it. Maybe I should start trying to make some tables and play with them. For the client table, do I only have the client names and unique ids? Or could I have other info in there as well, such as username?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 1:48 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC