Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default value for a field if no default value is provided?

I hope this isn't a dumb question. You can set a default value for all variables or a function for when it is inserted. but if the field is not required to insert and you don't allow null values, what is the "blank" value that you see in phpMyAdmin? in a query is it returned as empty string, etc?

just trying to figure it out, I want to query for all records such that the value for a specific column in that record is not "empty" or blank or whatever.

thanks.

like image 378
lollercoaster Avatar asked Dec 19 '11 07:12

lollercoaster


People also ask

What is the default value of column for which no default value is define?

If a data type specification includes no explicit DEFAULT value, MySQL determines the default value as follows: If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause. If the column cannot take NULL as a value, MySQL defines the column with no explicit DEFAULT clause.

What is the default value of a field?

Default field values automatically insert the value of a custom field when a new record is created. You can use a default value on a formula for some types of fields or exact values, such as Checked or Unchecked for checkbox fields. After you have defined default values: The user chooses to create a new record.

What is the default value for NULL?

If no default value is declared explicitly, the default value is the null value. This usually makes sense because a null value can be considered to represent unknown data. In a table definition, default values are listed after the column data type.

What is the value of default value?

A default value is the value that is inserted into a column when an explicit value is not specified in an INSERT statement. A default value can be a literal character string that you define or one of the following SQL constant expressions: USER.


1 Answers

Referring to the manual,

For data entry for a NOT NULL column that has no explicit DEFAULT clause, if an INSERT or REPLACE statement includes no value for the column, or an UPDATE statement sets the column to NULL, MySQL handles the column according to the SQL mode in effect at the time:

  • If strict SQL mode is not enabled, MySQL sets the column to the implicit default value for the column data type.
  • If strict mode is enabled, an error occurs for transactional tables and the statement is rolled back. For nontransactional tables, an
    error occurs, but if this happens for the second or subsequent row of a multiple-row statement, the preceding rows will have been inserted.

So your question now may be, what are the implicit default values for the various column data types? Here you go:

Implicit defaults are defined as follows:

  • For numeric types, the default is 0, with the exception that for integer or floating-point types declared with the AUTO_INCREMENT
    attribute, the default is the next value in the sequence.
  • For date and time types other than TIMESTAMP, the default is the appropriate “zero” value for the type. For the first TIMESTAMP column in a table, the default value is the current date and time. See Section 10.3, “Date and Time Types”.
  • For string types other than ENUM, the default value is the empty string. For ENUM, the default is the first enumeration value.
like image 59
Question Overflow Avatar answered Oct 20 '22 04:10

Question Overflow