简体   繁体   中英

Change Column to Auto_Increment

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?

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

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.

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.

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.

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.

ALTER TABLE <table name> <column name> NOT NULL AUTO_INCREMENT

More info: AUTO_INCREMENT Handling in InnoDB

Server SQL Modes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM