Page is a not externally linkable
- Code, Content, and Presentation
-- Databases
---- adding an id field to a database after content


rocknbil - 5:19 pm on Jul 27, 2010 (gmt 0)


You'd add an auto_increment field:

alter table mytable add id int(11) primary key auto_increment;

Then you'd really want it to be the first column on the left, not that it has to be, it's just a bit of a standard. Simply put, you create a temporary table, select the fields in the order you want them, insert, drop old table, rename temp table. From the documentation,

1. Create a new table with the columns in the new order.
2. Execute this statement:
mysql> INSERT INTO new_table SELECT columns-in-new-order FROM old_table;
3. Drop or rename old_table.
4.Rename the new table to the original name:
mysql> ALTER TABLE new_table RENAME old_table;

If you select data by column-name, everything will function as it did, if you select anything by array index you will have to shift your array over by one.

old:
title|content
$row=mysql_fetch_array($result);
// These are equivalent
$title = $row[0];
$title = $row['title'];

new:
id|title|content
$row=mysql_fetch_array($result);
// These are equivalent
$title = $row[1]; // id is now 0
$title = $row['title'];


Thread source:: http://www.webmasterworld.com/databases_sql_mysql/4177228.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com