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 455,985 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 3,772 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: 1352 | Replies: 15 | Solved
Reply
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?

  #11  
Dec 5th, 2007
For clients I recommend to start with id, fname (first name), lname, username, email

For journal, id, id_client, title, body, date

You could use text type for any field except id (I suggest int, 11 chars length for this)
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?

  #12  
Dec 5th, 2007
Thanks. I have found this code below for creating a table. I edited it to incorporate what was mentioned here previously. At the end, it has PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id), what exactly is this for? There was an short explanation, but it didn't make any sense to me. Also, there is no space after the ',' in the code. Is that correct? I think I would have a habit to put in a space if I wasn't just copying and pasting. Would that cause a problem?
$query="CREATE TABLE clients (id int(11) NOT NULL auto_increment,fname varchar(15) NOT NULL,lname varchar(15) NOT NULL,username varchar(20),email varchar(20),PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
mysql_query($query);
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?

  #13  
Dec 5th, 2007
UNIQUE and KEY id_2 isn't necessary, let alone primary key (id), indexes are meant to use a basic field for lookups, you could use more fields like fname and lname to perform special full-text lookups using MATCH().

And yes, you could avoid spaces or make carriage returns between commas to make the code more readable.
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?

  #14  
Dec 6th, 2007
Would this be the correct way to make the second table?

$query="CREATE TABLE food_journal (id int(11) NOT NULL auto_increment, id_client(11) NOT NULL, timestamp varchar(8) NOT NULL,water varchar(8) NOT NULL, breakfast varchar(8) NOT NULL, breakfast_calories int(8) NOT NULL, breakfast_time varchar(8) NOT NULL, snack_1 varchar(8) NOT NULL, snack_1_calories varchar(8) NOT NULL, snack_1_time varchar(8) NOT NULL, lunch varchar(8) NOT NULL, lunch_calories varchar(8) NOT NULL, lunch _time varchar(8) NOT NULL, snack_2 varchar(8) NOT NULL, snack_2_calories varchar(8) NOT NULL, snack_2_time varchar(8) NOT NULL, dinner varchar(8) NOT NULL, dinner_calories varchar(8) NOT NULL, dinner_time varchar(8) NOT NULL, snack_3 varchar(8) NOT NULL, snack_3_calories varchar(8) NOT NULL, snack_3_time varchar(8) NOT NULL";
mysql_query($query);
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?

  #15  
Dec 6th, 2007
There is some little mistakes, like missing type on id_client field, a UNIX timestamp normally takes ten positions and lunch_time have one space inside (table names can't have any space). I recommend make the first field (id) as primary key usually.
The resultant SQL would be:

CREATE TABLE food_journal(
id INT( 11 ) NOT NULL AUTO_INCREMENT ,
id_client INT( 11 ) NOT NULL ,
TIMESTAMP INT( 11 ) NOT NULL ,
water VARCHAR( 8 ) NOT NULL ,
breakfast VARCHAR( 8 ) NOT NULL ,
breakfast_calories INT( 8 ) NOT NULL ,
breakfast_time VARCHAR( 8 ) NOT NULL ,
snack_1 VARCHAR( 8 ) NOT NULL ,
snack_1_calories VARCHAR( 8 ) NOT NULL ,
snack_1_time VARCHAR( 8 ) NOT NULL ,
lunch VARCHAR( 8 ) NOT NULL ,
lunch_calories VARCHAR( 8 ) NOT NULL ,
lunch_time VARCHAR( 8 ) NOT NULL ,
snack_2 VARCHAR( 8 ) NOT NULL ,
snack_2_calories VARCHAR( 8 ) NOT NULL ,
snack_2_time VARCHAR( 8 ) NOT NULL ,
dinner VARCHAR( 8 ) NOT NULL ,
dinner_calories VARCHAR( 8 ) NOT NULL ,
dinner_time VARCHAR( 8 ) NOT NULL ,
snack_3 VARCHAR( 8 ) NOT NULL ,
snack_3_calories VARCHAR( 8 ) NOT NULL ,
snack_3_time VARCHAR( 8 ) NOT NULL ,
PRIMARY KEY ( id )
)
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?

  #16  
Dec 6th, 2007
Originally Posted by martin5211 View Post
I recommend make the first field (id) as primary key usually.


I thought you stated that the primary key (id) wasn't necessary??

Originally Posted by martin5211 View Post
UNIQUE and KEY id_2 isn't necessary, let alone primary key (id), indexes are meant to use a basic field for lookups, you could use more fields like fname and lname to perform special full-text lookups using MATCH().
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 9:23 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC