| | |
How to properly use tables in db?
Thread Solved |
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); •
•
Join Date: Aug 2007
Posts: 189
Reputation:
Solved Threads: 14
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.
And yes, you could avoid spaces or make carriage returns between commas to make the code more readable.
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);
•
•
Join Date: Aug 2007
Posts: 189
Reputation:
Solved Threads: 14
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:
The resultant SQL would be:
PHP Syntax (Toggle Plain Text)
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 ) )
![]() |
Similar Threads
- Hi, My Name Is Nubski (Visual Basic 4 / 5 / 6)
- php drop down menu to search multiple sql tables (PHP)
- Javascript/CSS Problem: Display is not properly hiding items (style.display="none") (JavaScript / DHTML / AJAX)
- IE works fine but Firefox doesn't display tables! (HTML and CSS)
- Why Use XHTML? (HTML and CSS)
- Large and tables/cells..how? (HTML and CSS)
- 200 GB Hard Drive Problems (Windows NT / 2000 / XP)
- C++ Relcat/Attrcat catalog help (C++)
Other Threads in the PHP Forum
- Previous Thread: js problem
- Next Thread: Sending a form via sendmail
Views: 2185 | Replies: 15
| Thread Tools | Search this Thread |
Tag cloud for PHP
access ajax apache array arrays beginner binary box broken buttons cakephp check checkbox class cms code cookies database date delete directory display download dropdown drupal dynamic echo email error file files form forms function functions header href htaccess html image images include insert ip java javascript joomla jquery limit link list login loop mail menu methods mlm mod_rewrite multiple mysql order output parse password paypal php problem query radio regex remote results script search security select server session soap sort sorting source sql string system table tutorial unicode update upload url user validation validator variable video web website wordpress xml





