Reputation: 1570
I need to do something simple - insert a row into MySQL (with PHP) but without values. The MySQL table will already have the default values it needs, so I don't need to insert any values. How would the insert statement look without values to insert?
Upvotes: 38
Views: 21123
Reputation: 385144
The proper way is to provide an empty values list and let the fields take their default values implicitly:
INSERT INTO `table` () VALUES();
Alternatively you can use the DEFAULT
keyword:
INSERT INTO `table` (`Col1`, `Col2`, ...) VALUES(DEFAULT, DEFAULT, ...);
Or (assuming you're giving values for all the columns) just:
INSERT INTO `table` VALUES(DEFAULT, DEFAULT, ...);
All the required information can be found in the documentation for INSERT
:
If you do not specify a list of column names for INSERT ... VALUES or INSERT ... SELECT, values for every column in the table must be provided by the VALUES list or the SELECT statement. If you do not know the order of the columns in the table, use DESCRIBE tbl_name to find out.
[..]
If you are not running in strict SQL mode, any column not explicitly given a value is set to its default (explicit or implicit) value. For example, if you specify a column list that does not name all the columns in the table, unnamed columns are set to their default values. Default value assignment is described in Section 10.1.4, “Data Type Default Values”. See also Section 1.8.6.2, “Constraints on Invalid Data”.
[..]
Use the keyword DEFAULT to set a column explicitly to its default value. This makes it easier to write INSERT statements that assign values to all but a few columns, because it enables you to avoid writing an incomplete VALUES list that does not include a value for each column in the table. Otherwise, you would have to write out the list of column names corresponding to each value in the VALUES list.
[..]
If both the column list and the VALUES list are empty, INSERT creates a row with each column set to its default value:
INSERT INTO tbl_name () VALUES();
[..]
In strict mode, an error occurs if any column doesn't have a default value. Otherwise, MySQL uses the implicit default value for any column that does not have an explicitly defined default.
Upvotes: 7
Reputation: 138960
In SQL Server it is like this.
insert into TableName default values
Upvotes: 14