What is the default value for a field if no defaul

2019-02-11 23:36发布

问题:

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.

回答1:

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.


回答2:

There IS no default value unless you specify one (i.e. unless you define a "default constraint" for the column in question).

Here's an example for adding a default on an existing column:

ALTER TABLE dbo.customer ALTER COLUMN contactname SET DEFAULT 'Unknown'

Here's an example creating the table with a default:

CREATE TABLE Books (
  ID SMALLINT NOT NULL,
  Name VARCHAR(40) NOT NULL,
  PubID SMALLINT NOT NULL DEFAULT 0
)

It's good practice to declare ALL columns "not null", and provide default constraints as appropriate.

In the "books" example above, if you "insert" without specifying PubID, the PubID will be zero.

In the same example, if you "insert" without specifying ID or Name ... you'll get an error.

If you want MySQL to auto-assign an ID, use this syntax instead:

CREATE TABLE Books (
  ID SMALLINT NOT NULL AUTO_INCREMENT,
  Name VARCHAR(40) NOT NULL,
  PubID SMALLINT NOT NULL DEFAULT 0
)


回答3:

If you want to disallow null :-

alter table YOUR_TABLE modify column COLUMN varchar(255) not null default '';

The above query will disallow null and assign an empty string when the value is not supplied.
In phpmysqladmin, blank = empty.
Via PHP mysqli function or mysql function, null value is returned as null still.

Once you have apply the query, you can easily filter that by using

select ... from YOUR_TABLE 
where COLUMN != "";        <-- no need to check is null
                           <-- because the first query already enforce not null

However, is best for you do this before perform the alter :-

update YOUR_TABLE set COLUMN = ""
where COLUMN is null;