views:

206

answers:

7

Hi,

I asked this question a little earlier today but am not sure as to how clear I was.

I have a MySQL column filled with ordered numbers 1-56. These numbers were generated by my PHP script, not by auto_increment.

What I'd like to do is make this column auto_incrementing after the PHP script sets the proper numbers. The PHP script works hand in hand with a jQuery interface that allows me to reorder a list of items using jQuery's UI plugin.

Once I decide what order I'd like the entries in, I'd like for the column to be set to auto increment, such that if i were to insert a new entry, it would recognize the highest number already existing in the column and set its own id number to be one higher than what's already existing.

Does anyone have any suggestions on how to approach this scenario?

A: 

You can change that with a query, issued through php, using the mysql console interface or (easiest) using phpmyadmin.

ALTER TABLE table_name CHANGE old_column_name new_column_name column_definition;
ALTER TABLE table_name AUTO_INCREMENT = highest_current_index + 1

column_definiton:

old_column_definition AUTO_INCREMENT 

More info:

http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

http://dev.mysql.com/doc/refman/5.1/en/create-table.html

EDIT Always use mysql_insert_id or the appropiate function of your abstraction layer to get the last created id, as LAST_INSERT_ID may lead to wrong results.

PvB
A: 

First you will need to mark that particular column as a primary key.

ALTER TABLE mytable ADD PRIMARY KEY(incrementField);

then ALTER TABLE mytable MODIFY COLUMN incrementField INTEGER NOT NULL DEFAULT 0 AUTO_INCREMENT;

e4c5
A: 

You can switch it to MySQL's auto_increment implementation, but it'll take 3 queries to do it:

a) ALTER TABLE to add the auto_increment to the field in question

b) SELECT MAX(id) + 1 to find out what you need to set the ID to

c) ALTER TABLE table AUTO_INCREMENT =result from (b)

MySQL considers altering the AUTO_INCREMENT value a table-level action, so you can't do it in (a), and it doesn't allow you to do MAX(id) in (c), so 3 queries.

Brock Batsell
Do you have any suggestions as to how the syntax might appear using php's mysql_query()?
Dave Kiss
A: 

In MySQL you can set a custom value for an auto_increment field. MySQL will then use the highest auto_increment column value for new rows, essentially MAX(id)+1. This means you can effectively reserve a range of IDs for custom use. For instance:

CREATE TABLE mytable (
 id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
 col1 VARCHAR(256)
); 
ALTER TABLE mytable AUTO_INCREMENT = 5001;

In this schema all ids < 5001 are reserved for use by your system. So, your PHP script can auto-generate values:

for ($i=1; $i<=56; $i++)
 mysql_query("INSERT INTO mytable SET id = $i, col1= 'whatevers'");

New entries will use the non-reserved range by not specifying id or setting it to null:

INSERT INTO mytable SET id = NULL, col1 = 'whatevers2';
-- The id of the new row will be 5001

Reserving a range like this is key - in case you need more than 56 special/system rows in the future.

pygorex1
Interesting solution, I may consider something like this.... though it would be nice to not have a limitation
Dave Kiss
A: 
ALTER TABLE <table name> <column name> NOT NULL AUTO_INCREMENT

More info: AUTO_INCREMENT Handling in InnoDB

Server SQL Modes

Chaitanya
+2  A: 

I'd suggest creating the table with your auto_increment already in place. You can specify a value for the auto_inc column, and mysql will use it, and still the next insert to specify a NULL or 0 value for the auto_inc column will magically get $highest + 1 assigned to it.

example:

mysql> create table foobar (i int auto_increment primary key);

mysql> insert into foobar values (10),(25);

mysql> insert into foobar values (null);

mysql> select * from foobar;

# returns 10,25,26

ggiroux
The problem I have with this is the fact that when using the jQuery UI to sort the DB entries, if I resubmit, I get the error "duplicate entry 0 for key 1"... this is because while trying to redefine the id value for each entry, it runs into the values for entries that existed before submit. Can you think of a way around this?
Dave Kiss
+1  A: 

No, stop it. This isn't the point of auto_increment. If you aren't going to make them ordered by the id then don't make them auto_increment, just add a column onto the end of the table for ordering and enjoy the added flexibility it gives you. It seems like you're trying to pack two different sets of information into one column and it's really only going to bite you in the ass despite all the well-meaning people in this thread telling you how to go about shooting yourself in the foot.

Chuck Vose
I am going to make them ordered by the id.... i just want that id number to be initially customized by my php script and then if I insert an entry, MySQL picks up where the php script left off with numbering
Dave Kiss