I've googled this for a bit and can't find anything on it.
Why does the MySQL column type "TIMESTAMP" require the "NULL" parameter to accept null values, when other column types dont?
Also, is this the only column type that requires the "NULL" parameter to accept null values?
Thank you.
This is related to a system variable that was added in 5.6.6 and later, explicit_defaults_for_timestamp. The default behavior is:
TIMESTAMP columns not explicitly declared with the NULL attribute are assigned the NOT NULL attribute. (Columns of other data types, if not explicitly declared as NOT NULL, permit NULL values.) Setting such a column to NULL sets it to the current timestamp.
The first TIMESTAMP column in a table, if not declared with the NULL attribute or an explicit DEFAULT or ON UPDATE clause, is automatically assigned the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP attributes.
TIMESTAMP columns following the first one, if not declared with the NULL attribute or an explicit DEFAULT clause, are automatically assigned DEFAULT '0000-00-00 00:00:00' (the “zero” timestamp). For inserted rows that specify no explicit value for such a column, the column is assigned '0000-00-00 00:00:00' and no warning occurs.
Setting this variable makes TIMESTAMP columns behave like other columns. The plan is that in some future release the non-standard behavior will be removed entirely, and the variable will then be deprecated.
Related
I found this syntax for a timestamped field in a database table:
TIMESTAMP NULL DEFAULT NULL
What is the meaning of the first NULL after Timestamp?
This would be in a create table syntax.
The first NULL simply means that the column allows NULL values. It is the opposite of NOT NULL.
This is an optional keyword, because when NOT NULL is not specified, the column can accept NULLs. However, for those who like to be specific, columns can be expressly declared that they allow NULL values.
I have a table with the below column
`update_ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
I have two databases set up using replication. On master while inserting null to the above column, Current time stamp is inserted.
But the same record on being replicated to the slave gives error. Error is that Null value is not permitted for the column
The only difference is that in slave DB, the specific column is indexed in the table.
Mysql reference has the below line
By default, TIMESTAMP columns are NOT NULL, cannot contain NULL values, and assigning NULL assigns the current timestamp.
So i expect the column to get the current time stamp
The data is put to master through spring.
Is this an expected behaviour?
tl;dr
The NOT NULL is permitting you to pass a null value which would normally become current_timestamp.
Original
Its inserted as "null". INSERT INTO table Values(null);
That's forbidden due to the NOT NULL requirement (the point with NOT NULL is to forbid null values) -- you're not suppose to mention the value. Skip the value and the default value will take it's correct place.
Let's take a look at the docs:
In addition, you can initialize or update any TIMESTAMP column to the current date and time by assigning it a NULL value, unless it has been defined with the NULL attribute to permit NULL values.
You are permitting null values because of your NOT NULL requirement. In order for your argument to be valid then you need to remove it.
Found the issue. The flag
explicit_defaults_for_timestamp
was not set in replication. Setting this solves the problem
SELECT * FROM table WHERE datefield IS NULL
Returns 20 records
SELECT * FROM table WHERE datefield IS NOT NULL
Returns the same 20 records and a further 20 records where the date is entered
Aren't IS NULL and IS NOT NULL mutually exclusive?
Field definition is:
datefield date NOT NULL
A NOT NULL column that doesn't have an explicit default value will use the implicit default value (for the type). For a date column, the default value is 0.
From MySQL docs:
If the column cannot take NULL as the value, MySQL defines the column
with an explicit DEFAULT clause, using the implicit default value for
the column data type. Implicit defaults are defined as follows:
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 11.3, “Date and Time Types”.
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.
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.
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
)
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;
I have a table which contains a couple timestamp fields and but I'm having some problems having some of the fields defaulting to NULL.
In my users table I have these timestamp tables:
(field),(NULL),(default val)
deleted_on YES NULL
last_change_attemp YES NULL
newpass_time YES NULL
last_login YES NULL
created NO 0000-00-00 00:00:00
updates YES NULL
When I go edit a row in phpMyAdmin, the row I'm editing already has the NULL checkbox checked for all NULL fields, except the first field: *deleted_on*. If I manually check the checkbox, it allows me to maintain that field as NULL.
When I update another field via a query, it automatically does a current_timestamp on that first field. Is this expected?
I've read through the docs again and noticed this piece of text
To specify automatic default or updating for a TIMESTAMP column other
than the first one, you must suppress the automatic initialization and
update behaviors for the first TIMESTAMP column by explicitly
assigning it a constant DEFAULT value (for example, DEFAULT 0 or
DEFAULT '2003-01-01 00:00:00'). Then, for the other TIMESTAMP column,
the rules are the same as for the first TIMESTAMP column, except that
if you omit both of the DEFAULT and ON UPDATE clauses, no automatic
initialization or updating occurs.
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
So for the time being I have the fields default to 0000-00-00 00:00:00 to prevent auto updates.
phpMyAdmin does some weird things with timestamps and null, especially if the column is marked as CURRENT_TIMESTAMP
I don't know why.