Web Hosting Talk







View Full Version : Very simply mysql insert question


lexington
06-22-2007, 03:38 AM
Sometimes I like to restart the IDs that are assigned to rows stored in the database. I usually use the TRUNCATE TABLE command, then I import the original data and reinsert into the table. My problem is how can I insert the data and have it automatically assign the IDs? I tried to remove the ID part from the data I am inserting and using auto_inc value but it always errors. For example, if I want to insert this row and the ID is 4000:

INSERT INTO `blah_table` VALUES (4000, 18079, 'item name here', 1000, 100, 0, 1182203028, 1182635028, 'description here',

If I remove the 4000 ID from that insert it errors, so how can I make it automatically add the ID without me manually redoing it all? Thanks.

case
06-22-2007, 03:58 AM
Sometimes I like to restart the IDs that are assigned to rows stored in the database. I usually use the TRUNCATE TABLE command, then I import the original data and reinsert into the table. My problem is how can I insert the data and have it automatically assign the IDs? I tried to remove the ID part from the data I am inserting and using auto_inc value but it always errors. For example, if I want to insert this row and the ID is 4000:

INSERT INTO `blah_table` VALUES (4000, 18079, 'item name here', 1000, 100, 0, 1182203028, 1182635028, 'description here',

If I remove the 4000 ID from that insert it errors, so how can I make it automatically add the ID without me manually redoing it all? Thanks.

When you construct your create table statement its best to specify auto_increment...

Have you tried specifying column names without an id? id should be set to auto_increment.. it should increment without specifying the actual column name 'id'.

INSERT INTO table (column_name2, column_name3, column_name4, column_name5, column_name6 column_name7 column_name8 column_name9)
VALUES (18079, 'item name here', 1000, 100, 0, 1182203028, 1182635028, 'description here')

jdcdesigns
06-23-2007, 02:38 PM
You can also create an easier to read query to help you troubleshoot your problem such as:
INSERT INTO `blah_table` set col1='4000',
col2='18079',
col3='item name here',
col4=1000,
col5=100,
col6=0,
col7=1182203028,
col8=1182635028,
col9='description here'

Codelphious
06-23-2007, 03:46 PM
INSERT INTO tablename (id, stuff, morestuff, etc) VALUES('', 'this is stuff', 'this is more stuff', 'etc stuf')


Make sure you have auto_increment attribute set on the table, then the above query should work. Just leave your id section blank.