Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Need Help In Database Auto_increment
Eggie
post Jan 20 2008, 09:24 AM
Post #1


Advanced Member
Group Icon

Group: [HOSTED]
Posts: 176
Joined: 19-January 08
From: Zagreb/Croatia
Member No.: 27,735



i am creating a game and i set when someone registers than he gets id...in "extra" i have set it to be "auto_increment" but whenever new player signs up he gets number bigger than 210...(my first id was 211,next was 212,third was 213,fourth got 214) how to set it to go from 1 to infinite? thnx

This post has been edited by Eggie: Jan 20 2008, 09:28 AM
Go to the top of the page
 
+Quote Post
turbopowerdmaxst...
post Jan 20 2008, 10:16 AM
Post #2


Premium Member
Group Icon

Group: [HOSTED]
Posts: 392
Joined: 16-February 06
From: Kolkata, India
Member No.: 11,322



You must have manually inserted the 211 ID. If you can afford to empty the table and re-enter the values the problem can be solved. In your insert query, leave the value for the AUTO_INCREMENT field to blank. For example, say you have a table with two fields ID (the auto number) and name (a varchar). Use the following query to insert a record with IDs starting from 1.

CODE
INSERT INTO MyTable VALUES('', "Partho")


You can set the initial number to a greater number but you can't get it back down (or so I think) unless you empty/re-create the table. Use the following query to start the numbers from 200.

CODE
INSERT INTO MyTable VALUES('200', "Partho")


From the next time, you can just use the previous query to continue with numbers 201, 202 and so on.
Go to the top of the page
 
+Quote Post
faulty.lee
post Jan 20 2008, 11:14 AM
Post #3


Premium Member
Group Icon

Group: [HOSTED]
Posts: 495
Joined: 5-November 06
Member No.: 17,016



If you had inserted some record before, and deleted them, the auto increment number will still remain from the last one that you inserted, e.g. 210 in this case, so the next data you insert will start from 211. For this you need to reset the auto_increment counter. Assuming you're using MySQL

CODE
ALTER TABLE your_table AUTO_INCREMENT = 0


Then your next inserted data will start with 0, then 1, 2, 3 .....

Hope that's what you're looking for

Good Luck

PS: For your information, the auto_increment is made to remain as last even though you've deleted your record, for the purpose of data integrity. It was done so that even you've deleted your old record, anything that refers to that ID won't accidentally belongs to another row of data. For example, you have your first row with ID=1, and another table that refer to this ID. That 2nd table referring to ID 1, and contain a bad comment for it. Later you deleted that row from the first table, and reset the auto_increment to 1, and insert another data. This time the new row, also has an ID=1, but the bad comment is automatically referred to it although it wasn't your intention to do so. You might not see the problem, maybe for now, but when you database grow bigger and bigger, it will get worse, and very hard to debug.
Go to the top of the page
 
+Quote Post
Eggie
post Jan 20 2008, 11:27 AM
Post #4


Advanced Member
Group Icon

Group: [HOSTED]
Posts: 176
Joined: 19-January 08
From: Zagreb/Croatia
Member No.: 27,735



CODE
ALTER TABLE table_name AUTO_INCREMENT = 1;


that is what i need but thanks anyway
that sets your auto_increment value to 1!
Go to the top of the page
 
+Quote Post
Eggie
post Jan 20 2008, 11:29 AM
Post #5


Advanced Member
Group Icon

Group: [HOSTED]
Posts: 176
Joined: 19-January 08
From: Zagreb/Croatia
Member No.: 27,735



faulty.lee...
i didn't even see your post...after i saw turbopowerdmaxsteel's post i leaved it here on the post and haven't refreshed...and i posted it
thanx anyways
Go to the top of the page
 
+Quote Post
faulty.lee
post Jan 20 2008, 12:40 PM
Post #6


Premium Member
Group Icon

Group: [HOSTED]
Posts: 495
Joined: 5-November 06
Member No.: 17,016



It's OK. In fact I would be happy if you can figure it out yourself. That shows your enthusiasm on what you wanted to do.

Go to the top of the page
 
+Quote Post
Eggie
post Jan 20 2008, 12:51 PM
Post #7


Advanced Member
Group Icon

Group: [HOSTED]
Posts: 176
Joined: 19-January 08
From: Zagreb/Croatia
Member No.: 27,735



QUOTE(faulty.lee @ Jan 20 2008, 01:40 PM) *
It's OK. In fact I would be happy if you can figure it out yourself. That shows your enthusiasm on what you wanted to do.

well..i didnt figure it out myself...i googled it ..thats how i figured it
Go to the top of the page
 
+Quote Post
sparkx
post Jan 20 2008, 03:32 PM
Post #8


Sparkx
Group Icon

Group: [HOSTED]
Posts: 354
Joined: 11-October 06
From: Dana Point, CA, USA
Member No.: 16,496



The fact that you even Googled it puts you in front of about half the programmer wanabies on this forum as it is. On my website I do the following to increment things:
-Run While. If a constant added variable is equal to the ID it repeats the while. If failed found first "skipped" number.
-If no skipped number + 1 to the last number.
-Double Check if number exists (useless unless two entries and being inserted at the exact same time).
-Insert entry.

You probably wont understand what I just said until you get better at programming, but that’s how I check. I don't know if the code you found included skipped number or not but that is always nice to have unless you want deleted ID's to stay deleted. I made up this method and am not exactly sure how secure it is but it may be a good idea to do if you really get serious about your game.

Good Luck,
Sparkx
Go to the top of the page
 
+Quote Post
Eggie
post Jan 20 2008, 03:37 PM
Post #9


Advanced Member
Group Icon

Group: [HOSTED]
Posts: 176
Joined: 19-January 08
From: Zagreb/Croatia
Member No.: 27,735



good code but i dont want that a guy who registers 210th get id 2
so that code is better in my case
Go to the top of the page
 
+Quote Post
TavoxPeru
post Jan 21 2008, 05:18 AM
Post #10


Super Member
Group Icon

Group: [HOSTED]
Posts: 763
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579



Another way to reset your auto increment id field is by truncate the table so the ID of your first new inserted record will get as Id the value 1, TRUNCATE TABLE is a data manipulation statement that empties completely a table and also resets any auto_increment counter of the table, sounds like this is similar to perform a DELETE FROM TABLE statement, but it is not absolutely true, because it depends on the version of your database, also, if you perform the DELETE statement this will not reset the auto_increment counter of the table, so, if you insert a record to your table after this the auto_increment counter will be setting up with the next value.

But be carefull with this method, if you have data on your table and you want to preserve it, well, what i generally do in this case is to first delete the primary index of the table, then i delete the auto_increment field, after that i generate an sql file of the table by exporting it, then i proceed to delete the table and finally i import the generated sql file.

Sounds like there are too much tasks to perform a simple task like this, well, if you use MySql and phpMyAdmin it is very easy and simple, you simply go to the export tab of your table, select the options you need and press a button. There are a lot of options that you can select here, for this case the options i select are:
  • Export: Sql.
  • Structure: Add DROP TABLE, Add IF NOT EXISTS, Enclose table and field names with backquotes.
  • DATA: Complete inserts, Extended inserts.
  • Export type: INSERT
  • Save as file: This is optional because it depends on the size of the table, if the table have few records -less than 100- i do not use it.
Then, phpMyAdmin proceeds to generate the sql code, if i dont use the last option, it shows up the code on a textarea field, where i simply select all the generated sql code and copy it to the clipboard, or, in the other case, save the file to disk.

After that, if i use the Save as file option i go to the Import tab, open the FILE input control, browse my disk and select the just generated sql file, finally press the GO button. In the other case, i only need to go to the SQL tab of the table, and simply paste the just generated sql code inside the textarea field and finaly press the GO button.

Best regards,
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Stop AUTO_INCREMENT From Jumping(13)
  2. Grand Theft Auto: San Andreas(109)
  3. Creating A Game In Rpg Maker 2000/2003(18)
  4. Mirror My MySQL Database To Another Mysql Server(4)
  5. VB: Howto Auto-select Last Item In Listbox(5)
  6. How To: Connect, Read, Write, Close A Database(4)
  7. Need Help With A PHP - MySQL Registration Script(13)
  8. MySQL Output Database Question(18)
  9. Orkut.com Auto Scrapper - Scrap Ur Friend List In One Go(12)
  10. Permission Problem With Mysql Database Creation(8)
  11. Auto-click Script(7)
  12. Laptop Auto Starts After Turning It Off(8)
  13. Auto_increment Jumping About(3)
  14. Api And Http Or Database?(0)
  15. Connecting To A Remote Database(9)
  1. Database(1)
  2. Grand Theft Auto Iv(10)
  3. Integrate Access Database Onto Intranet Site(5)
  4. Auto Volume Controller For Windows(0)
  5. Accessing Ms Access Database From A Centralized Location?(5)
  6. Mysql Database Management(1)
  7. Mysql Database Entry By Excel Sheets(2)
  8. Space Needed For Database(10)
  9. Database Access On Remote Server W/jsp(0)
  10. Some Useful Database Links.(7)
  11. Database(7)
  12. Best Database(7)
  13. How To Understand A Database Schema(4)


 



- Lo-Fi Version Time is now: 12th October 2008 - 02:09 AM