如何插入一个没有任何值的行,使所有列都具有默认值?

时间:2021-12-27 12:31:45

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?

我需要做一些简单的事情——向MySQL(使用PHP)插入一行,但不带值。MySQL表已经有它需要的默认值,所以我不需要插入任何值。如果没有要插入的值,insert语句看起来会怎么样?

4 个解决方案

#1


42  

INSERT INTO my_table VALUES ()

#2


9  

In SQL Server it is like this.

在SQL Server中是这样的。

insert into TableName default values

#3


5  

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.

如果您没有指定插入的列名列表…值或插入……选择,表中每一列的值必须由值列表或SELECT语句提供。如果不知道表中列的顺序,请使用DESCRIBE tbl_name查找。

[..]

[. .]

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”.

如果您没有在严格的SQL模式下运行,则未显式地给定值的任何列都被设置为其默认值(显式或隐式)。例如,如果您指定了一个列列表,该列列表不命名表中的所有列,那么未命名列将被设置为它们的默认值。默认值分配在第10.1.4节“数据类型默认值”中描述。参见1.8.6.2节“无效数据的约束”。

[..]

[. .]

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.

使用关键字DEFAULT将列显式设置为其默认值。这使编写INSERT语句更容易,因为它使您可以避免编写不包含表中每一列的值的不完整值列表。否则,您必须写出与值列表中的每个值对应的列名列表。

[..]

[. .]

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.

在严格模式下,如果任何列没有默认值,就会发生错误。否则,MySQL对没有显式定义默认值的任何列使用隐式默认值。

#4


1  

You can use default values:

可以使用默认值:

insert YourTable default values

#1


42  

INSERT INTO my_table VALUES ()

#2


9  

In SQL Server it is like this.

在SQL Server中是这样的。

insert into TableName default values

#3


5  

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.

如果您没有指定插入的列名列表…值或插入……选择,表中每一列的值必须由值列表或SELECT语句提供。如果不知道表中列的顺序,请使用DESCRIBE tbl_name查找。

[..]

[. .]

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”.

如果您没有在严格的SQL模式下运行,则未显式地给定值的任何列都被设置为其默认值(显式或隐式)。例如,如果您指定了一个列列表,该列列表不命名表中的所有列,那么未命名列将被设置为它们的默认值。默认值分配在第10.1.4节“数据类型默认值”中描述。参见1.8.6.2节“无效数据的约束”。

[..]

[. .]

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.

使用关键字DEFAULT将列显式设置为其默认值。这使编写INSERT语句更容易,因为它使您可以避免编写不包含表中每一列的值的不完整值列表。否则,您必须写出与值列表中的每个值对应的列名列表。

[..]

[. .]

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.

在严格模式下,如果任何列没有默认值,就会发生错误。否则,MySQL对没有显式定义默认值的任何列使用隐式默认值。

#4


1  

You can use default values:

可以使用默认值:

insert YourTable default values