Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INSERT a row when an auto_increment field exists

Status
Not open for further replies.

mellenburg

Programmer
Aug 27, 2001
77
US
I'm trying to insert a row into a table where I have an id field that is an auto_increment and primary key.

When trying to do an insert, what do I put for the id field in the values statement?

If I put nothing, the record does not get inserted.
 
It depends. Do you want MySQL to provide the value for id, or do you want to do it?

If the former, don't reference the field id at all:
INSERT INTO tablename (column2, column3) values ('a','b')

MySQL will generate a value for id automatically.

If the latter, just reference the column and pass in a value. Keep in mind that primary key columns must be unique -- duplicate a key and the insert will barf:

INSERT INTO tablename (id, column2, column3) values (4, 'a', 'b')

Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top