Shane
Shane

Reputation: 1075

How do I insert an empty row, but have the autonumber update correctly?

I was hoping to be able to do something like

INSERT INTO `table`;

or maybe even a SET clause like

SET `primary_key` = null

where all the columns of the table aren't set (and are left to their default), with the exception of the autonumbered primary key which should be set.

Any suggestions?

Upvotes: 31

Views: 44090

Answers (6)

vnva
vnva

Reputation: 1

INSERT INTO table (id)
VALUES (default)

Upvotes: 0

Linus Unnebäck
Linus Unnebäck

Reputation: 24301

I believe that the more correct answer would be this:

INSERT INTO my_table DEFAULT VALUES

As suggested here: https://stackoverflow.com/a/13605273/148072

Upvotes: 39

Alex
Alex

Reputation: 1309

A little late.. But maybe handy for anyone looking for this answer.

INSERT INTO `table` () VALUES ()

You do not need any specification of the primary key.

Upvotes: 6

Stephane Gosselin
Stephane Gosselin

Reputation: 9148

Depends on your table. If it allows null values on every field, just do an insert with all values as null. If it does not allow null values for every field, you will need to insert a row with those fields having some value.

Upvotes: 3

Joe Stefanelli
Joe Stefanelli

Reputation: 135858

INSERT INTO `table` () VALUES();

Upvotes: 46

Dan Simon
Dan Simon

Reputation: 13137

INSERT INTO `table` (`primary_key`) VALUES (NULL);

Upvotes: 48

Related Questions