Webmaster Forum

Advertise Here   High Bandwidth Dedicated Servers   V7N Directory
Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Coding Forum Problems with your code? Let's hear about it.

Reply
 
LinkBack Thread Tools Display Modes
Old 12-15-2007, 10:49 AM   #1 (permalink)
Contributing Member
 
Join Date: 06-11-07
Posts: 136
iTrader: 0 / 0%
Latest Blog:
None

Capo64 is on the right pathCapo64 is on the right path
MySQL Auto_increment

Is there a way to make auto_increment increment from the highest value in a column (not counting deleted entries)?

This is what happens now: I have a table with the ID column having auto_increment.

I create an entry. ID = 1.
I create another entry. ID = 2.
I delete entry 2.
I create another entry. ID = 3.
I end up with ID 1 and 3.

This is what I want to happen:

I create an entry. ID = 1.
I create another entry. ID = 2.
I delete entry 2.
I create another entry. ID = 2.
I end up with ID 1 and 2.

Is there something simple that will make this happen?
Capo64 is offline  
Add Post to del.icio.us
Reply With Quote
Sponsored Links
SEO Hosting by HostGator  Advertise Here  Buy Blog Links
Old 12-15-2007, 03:21 PM   #2 (permalink)
Contributing Member
 
exam's Avatar
 
Join Date: 04-20-06
Posts: 333
iTrader: 0 / 0%
Latest Blog:
None

exam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web pro
Run these queries:

Code:
SELECT MAX(`id`) FROM `tablename`; //$number gets the result of the first query plus 1. ALTER TABLE `tablename` AUTO_INCREMENT = $number;
But you haven't done a good job describing your problem. What you really should do, is in your code to create a new row, have it check the table to see if there are any unused ids, and give the new row that id instead of the auto increment value.
__________________
~exam~
exam is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-16-2007, 03:05 AM   #3 (permalink)
Contributing Member
 
Join Date: 09-03-07
Location: England
Posts: 358
iTrader: 0 / 0%
Latest Blog:
None

Boogle is liked by somebodyBoogle is liked by somebodyBoogle is liked by somebodyBoogle is liked by somebodyBoogle is liked by somebody
Quote:
But you haven't done a good job describing your problem. What you really should do, is in your code to create a new row, have it check the table to see if there are any unused ids, and give the new row that id instead of the auto increment value.
I agree, you don't need to use auto_increment. You should make your own ID column and run a query that checks for the highest value within it then use

Code:
ID = HIGHEST VALUE + 1
That's the way i would do it. But why do these gaps in the ID matter anyway?Surely people aren't seeing your primary key/mainID ? You should be using a seperate reference ID for the clients to see?

Boog's
__________________
Price is what you pay... Value is what you get.
Boogle is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-16-2007, 08:23 AM   #4 (permalink)
Contributing Member
 
Join Date: 06-11-07
Posts: 136
iTrader: 0 / 0%
Latest Blog:
None

Capo64 is on the right pathCapo64 is on the right path
Thanks, I won't use auto_increment. No, they don't see my primary key, and I guess you're right, it's not a huge deal, but I'd just prefer for there not to be gaps in the numbers
Capo64 is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-17-2007, 03:20 PM   #5 (permalink)
v7n Mentor
 
Join Date: 01-16-07
Location: Assen, the Netherlands
Posts: 1,379
iTrader: 1 / 100%
Latest Blog:
None

Jesse Vlasveld is a splendid one to beholdJesse Vlasveld is a splendid one to beholdJesse Vlasveld is a splendid one to beholdJesse Vlasveld is a splendid one to beholdJesse Vlasveld is a splendid one to beholdJesse Vlasveld is a splendid one to beholdJesse Vlasveld is a splendid one to beholdJesse Vlasveld is a splendid one to beholdJesse Vlasveld is a splendid one to beholdJesse Vlasveld is a splendid one to beholdJesse Vlasveld is a splendid one to behold
Send a message via MSN to Jesse Vlasveld Send a message via Yahoo to Jesse Vlasveld
I don't see the problem of skipping a number once in a time. It keeps nice track of you're total records you've ever had in that particular table row.
Jesse Vlasveld is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-17-2007, 04:15 PM   #6 (permalink)
Contributing Member
 
exam's Avatar
 
Join Date: 04-20-06
Posts: 333
iTrader: 0 / 0%
Latest Blog:
None

exam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web pro
Quote:
Originally Posted by Boogle View Post
I agree, you don't need to use auto_increment. You should make your own ID column and run a query that checks for the highest value within it then use

Code:
ID = HIGHEST VALUE + 1
That's the way i would do it. But why do these gaps in the ID matter anyway?Surely people aren't seeing your primary key/mainID ? You should be using a seperate reference ID for the clients to see?

Boog's
I never said not to use an auto_increment. In fact I think he should use auto_increment. Also, what you've described will work just like an auto incrementing column, with the exception that if the last record is deleted and then you add an new one the id will be reused. If you delete any but the last record, that ID won't be reused.


Quote:
Originally Posted by Capo64 View Post
Thanks, I won't use auto_increment. No, they don't see my primary key, and I guess you're right, it's not a huge deal, but I'd just prefer for there not to be gaps in the numbers
Preferring that there not be gaps in the numbers is a *bad* reason to do what you're thinking of doing. Use an auto incrementing column. For your own sanity, and for anyone else maintaining your code, do it.

Quote:
Originally Posted by Jesse Vlasveld View Post
I don't see the problem of skipping a number once in a time. It keeps nice track of you're total records you've ever had in that particular table row.
I agree.
__________________
~exam~
exam is offline  
Add Post to del.icio.us
Reply With Quote
Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum

Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with PhP MySQL JustRetarded Coding Forum 4 08-15-2007 11:12 PM
PHP and MySql help daniel0012 Coding Forum 4 06-28-2007 02:48 AM
MySQL help Limit Coding Forum 28 08-27-2004 05:39 AM
PHP/mySQL SN3 Coding Forum 5 02-03-2004 10:38 PM


Sponsor Links
Get exposure! Get exposure! Find Scripts Web Hosting Directory Get exposure! SEO Blog


All times are GMT -7. The time now is 08:06 AM.
© Copyright 2008 V7 Inc


Search Engine Optimization by vBSEO 3.1.0 ©2007, Crawlability, Inc.