NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Update ID in MySQL?

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
5 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 11-03-2009, 04:10 PM THREAD STARTER               #1 (permalink)
NamePros Regular
 
snike's Avatar
Join Date: Mar 2006
Location: USA
Posts: 497
snike has a spectacular aura aboutsnike has a spectacular aura about
 


Save a Life

Update ID in MySQL?


Let's say I have this column in a database:

ID | Name
2 | Snike

Is it possible to have a php script update the ID number to the next available ID number? If in the example above there were 2 more column and the largest ID being 4, the script would update the ID from 2 to 5.

Can I do this? Thanks in advance.
Last edited by snike; 11-03-2009 at 05:44 PM.
snike is offline   Reply With Quote
Old 11-03-2009, 05:18 PM   #2 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
MySQL can do this if you set the ID column to AUTO_INCREMENT PRIMARY KEY
Eric is offline   Reply With Quote
Old 11-03-2009, 05:44 PM THREAD STARTER               #3 (permalink)
NamePros Regular
 
snike's Avatar
Join Date: Mar 2006
Location: USA
Posts: 497
snike has a spectacular aura aboutsnike has a spectacular aura about
 


Save a Life
Yes, it is set like that. So what would the query look like?
snike is offline   Reply With Quote
Old 11-03-2009, 05:50 PM   #4 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
you don't need to set the ID if it's auto_increment just do a regular insert statement

Code:
INSERT INTO table (name) VALUES ('Blah');
Eric is offline   Reply With Quote
Old 11-03-2009, 07:45 PM THREAD STARTER               #5 (permalink)
NamePros Regular
 
snike's Avatar
Join Date: Mar 2006
Location: USA
Posts: 497
snike has a spectacular aura aboutsnike has a spectacular aura about
 


Save a Life
But I don't want to add data into the database. I want to change the id of a column. from like 2 to whatever is next.
snike is offline   Reply With Quote
Old 11-04-2009, 04:26 AM   #6 (permalink)
NamePros Regular
 
qbert220's Avatar
Join Date: Jul 2007
Location: UK
Posts: 394
qbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to behold
 


Protect Our Planet
The following might work:

Code:
UPDATE table_name SET id=0 WHERE id=2
ALTER TABLE table_name AUTO_INCREMENT
qbert220 is offline   Reply With Quote
Old 11-04-2009, 04:57 AM   #7 (permalink)
Forum Moderator
 
enlytend's Avatar
Join Date: Aug 2006
Location: USA
Posts: 2,152
enlytend has a reputation beyond reputeenlytend has a reputation beyond reputeenlytend has a reputation beyond reputeenlytend has a reputation beyond reputeenlytend has a reputation beyond reputeenlytend has a reputation beyond reputeenlytend has a reputation beyond reputeenlytend has a reputation beyond reputeenlytend has a reputation beyond reputeenlytend has a reputation beyond reputeenlytend has a reputation beyond repute
 



Cancer Survivorship
You should be able to do this as a subquery (mysql 5.0 and higher) but you have to do some extra work because you can't use a subquery on the same table to provide a value to an update statement:

Something like this might work ... ( WARNING: not tested and since I rarely write complex sql statements any more there's a good chance it's not quite right or there's something stupidly easy that I'm not thinking of !)
????: NamePros.com http://www.namepros.com/programming/620935-update-id-in-mysql.html

Code:
Update table set id = (select * from (select (max(id) + 1) from table group by id) as i)  WHERE id = 2;
The additional "select" should store the value in a temporary table - since that's processed before beginning to execute the update, it should be OK.

Or here's a really easy alternative:

Get all the values from the old record (except ID)
Insert as a new record
Delete the record with the old ID
__________________
Enlytend Solutions - Internet marketing and web development
Was my advice helpful? Please consider a small donation to the National Canine Cancer Foundation - a tax deductible 501(c)(3)that directly funds cancer research
Last edited by enlytend; 11-06-2009 at 05:52 AM.
enlytend is offline   Reply With Quote
Reply


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


 
All times are GMT -7. The time now is 02:32 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger