Author Topic: Update ID field of table  (Read 496 times)

0 Members and 1 Guest are viewing this topic.

Offline OmzyTopic starter

  • Enthusiast
  • Posts: 313
    • View Profile
Update ID field of table
« on: March 03, 2010, 02:51:01 PM »
I have field ID in my table (not auto-incremented), what I wish to do is set the ID field of the first row to 200, and then add 1 to the ID on every next row.

Offline Dennis1986

  • Enthusiast
  • Posts: 78
  • Gender: Male
  • PHP for life!
    • View Profile
    • DennisRasmussen.dk
Re: Update ID field of table
« Reply #1 on: March 03, 2010, 03:08:02 PM »
Here's how I would do it:
- Create a temptable with similar fields
- ALTER TABLE [temptable] AUTO_INCREMENT = 200
- INSERT data from original table into temptable
- Empty original table
- INSERT back from temptable to original table

The actual queries you need I'm not going to write right now (I'm sure someone else would want to) as I'm too tired right now.
Just ask and I'll answer your question(s)! ;)

Offline OmzyTopic starter

  • Enthusiast
  • Posts: 313
    • View Profile
Re: Update ID field of table
« Reply #2 on: March 03, 2010, 07:28:22 PM »
How do I insert data from one table to another?

Offline Dennis1986

  • Enthusiast
  • Posts: 78
  • Gender: Male
  • PHP for life!
    • View Profile
    • DennisRasmussen.dk
Re: Update ID field of table
« Reply #3 on: March 04, 2010, 05:46:01 AM »
You could also set a variable that increases by 1 each update.

Code: [Select]
SET @temp = 200;
UPDATE table_name SET ID = @temp := ( @temp +1 ) ORDER BY ID ASC;
Just ask and I'll answer your question(s)! ;)

Offline OmzyTopic starter

  • Enthusiast
  • Posts: 313
    • View Profile
Re: Update ID field of table
« Reply #4 on: March 04, 2010, 11:29:54 AM »
How do I insert data from one table to another?

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.