28

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?

Reporter
  • 3,897
  • 5
  • 33
  • 47
Shane
  • 1,015
  • 2
  • 12
  • 31

5 Answers5

47
INSERT INTO `table` (`primary_key`) VALUES (NULL);
Dan Simon
  • 12,891
  • 3
  • 49
  • 55
45
INSERT INTO `table` () VALUES();
Joe Stefanelli
  • 132,803
  • 19
  • 237
  • 235
35

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

Community
  • 1
  • 1
Linus Unnebäck
  • 23,234
  • 15
  • 74
  • 89
  • 5
    Only answer here that worked for me (Postgres 12). – Colin McDonnell Jun 12 '20 at 06:19
  • 1
    Tried `INSERT INTO table_name DEFAULT VALUES;` with MySQL Version 10.3.32-MariaDB-cll-lve and got `SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DEFAULT VALUES' at line 1` – Andris Feb 14 '22 at 05:10
7

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.

Alex
  • 1,223
  • 1
  • 19
  • 31
3

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.

Stephane Gosselin
  • 9,030
  • 5
  • 42
  • 65