hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : How to change a tables next autoincrement...
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

How to change a tables next autoincrement...

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 08-27-2002, 10:42 PM
mAgz mAgz is offline
Newbie
 
Join Date: Aug 2002
Location: San Jose, California
Posts: 16
Post

How to change a tables next autoincrement...


see subject... let's say I have a few rows in a table and have 1 field (id) acting as the primary and index with autoincrement and the default set to 1... what is the query to change the next autoincrement in case i were to allow rows to be deleted from a table or something like that... because if i were to delete a row at the end or in the middle the autoincrement would stay at the exact same number and when i next add a row it will be added in the next autoincrement leaving a blank where the deleted row is...

__________________
-magz

Reply With Quote


Sponsored Links
  #2  
Old 08-28-2002, 04:30 AM
MMD MMD is offline
Junior Guru Wannabe
 
Join Date: Feb 2002
Posts: 48
*

I never found the answer to this, apparently there is a MySQL query to run but I haven't found it yet :|

top!

Reply With Quote
  #3  
Old 08-28-2002, 09:45 AM
Ahmad Ahmad is offline
Web Hosting Master
 
Join Date: Jan 2002
Location: Kuwait
Posts: 679
Insert a row with the ID you want MySQL to start at minus one. Say you want to start at 512, so you insert a row with ID 511. Then delete that row. That's it. Next row will be 512, then 513, .. etc.

Example:

Code:
mysql> CREATE TABLE mytest (
    -> testid integer not null auto_increment primary key );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO mytest (testid) VALUES(511);
Query OK, 1 row affected (0.01 sec)

mysql> DELETE FROM mytest WHERE testid = 511;
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO mytest VALUES();
Query OK, 1 row affected (0.01 sec)

mysql> SELECT testid FROM mytest;
+--------+
| testid |
+--------+
|    512 |
+--------+
1 row in set (0.00 sec)

__________________
Ahmad Alhashemi
PHP, Apache, C, Python, Perl, SQL
18 related BrainBench certificates

Reply With Quote
Sponsored Links
  #4  
Old 08-28-2002, 09:51 AM
Ahmad Ahmad is offline
Web Hosting Master
 
Join Date: Jan 2002
Location: Kuwait
Posts: 679
Ops. I haven't read your post correctly. Sorry.

As you can see, the problem you are haveing is used as a solution to another problem. Some people won't like it if it gets solved

__________________
Ahmad Alhashemi
PHP, Apache, C, Python, Perl, SQL
18 related BrainBench certificates

Reply With Quote
  #5  
Old 08-28-2002, 12:37 PM
benoire benoire is offline
Registered User
 
Join Date: Jul 2001
Location: Coventry, England
Posts: 130
ALTER TABLE tablename AUTO_INCREMENT = 1

This will reset the next auto_increment to be used to the highest available, so if you had 100 records and deleted the last 50, it would reset from 101 to 51. If you deleted the first 50, however, the auto_increment would stay at 101 and not go to 1

Reply With Quote
  #6  
Old 08-28-2002, 10:26 PM
mAgz mAgz is offline
Newbie
 
Join Date: Aug 2002
Location: San Jose, California
Posts: 16
Talking

aite... thx guys... the alter table query is what i was lookin for

__________________
-magz

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Important changes to WHIR Networking Events Europe schedule Blog 2013-04-15 16:00:16
Navigating Your Business Through Change with Elliot Noss - HostingCon 2012 Web Hosting News 2012-07-17 09:51:18
Hackers Post 450,000 Yahoo! Voices User Login Credentials Online Web Hosting News 2012-07-12 10:38:07
New on WHIR TV: Open-Xchange CEO Rafael Laguna at the OX Partner Summit Blog 2011-12-02 16:10:28
Telecom Telefonica Acquires Spanish Web Host acens Web Hosting News 2011-06-08 18:17:56


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?