Click to See Complete Forum and Search --> : Auto Increment and Primary Key Question


Joseph Witchard
06-21-2008, 12:51 AM
Let's say you had a table for the users of a message board. The fields would be user_id, user_name, user_email, etc. User_id would be numeric and set to auto increment, because it's each user's identification number. I know that auto increment adds one to each new value every time something new is inserted. How would you make it where auto increment was the table's primary key, and it incremented every time a new user was added to the users table? I'm very new to MySQL, so tell me if I'm not making sense, or if this isn't even possible.

Thanks!

Suhas Dhoke
06-21-2008, 02:19 AM
For Auto-Increment, the field should be the Primary-Key. You can use it through sql statement or by selecting the "auto_increment" from the "EXTRA" drop-down.
You can also set the starting number (of your primary-key) by entering the number in "DEFAULT".

Here is an example of that.

CREATE TABLE `users` (
`user_id` int(11) NOT NULL auto_increment,
`user_name` varchar(255) NOT NULL,
`user_email` varchar(255) NOT NULL,
PRIMARY KEY (`user_id`)
)

Joseph Witchard
06-21-2008, 02:20 PM
So with Auto Increment, would that mean it's the primary key for the whole table? Like, if I had a user_post field in another table set to the foreign key (the same as user_id), I could use the keys to figure which user did what?