Mysql Date Default insert in 1970-01-01 - mysql

I am inserting an empty date-field, but a default value 1970-01-01 gets inserted automatically. Why?
I even changed the Date structure to allow null, set Default as Null, still it's inserting 1970-01-01.

It's the default behavior of MySQL, check in the Database that the field can be NULL.
Check this: old post

You are most likely inserting an empty string. Inserting an entry string will result in 1970-01-01 being written instead. You have to really insert NULL if you don't want this to happen.
What you want to do is:
INSERT INTO table
SET datefield = NULL
Not "NULL" but NULL Otherwise it will read it as a string and not as NULL
You can also just don't set the column at all in your insert and than it will use NULL as well

Related

mysql column default value 0 saves as NULL

I have a mysql database with a TINYINT(4) column that previously had no value set to default, and I assume null was being set as the default value. I had to manually push '0' into the database saving logic every time in my code, so I have now updated the column to instead have an explicit default value of '0'. Now when I save my data, instead null is being set, and throwing off my code. When I set not null for the column, then it throws an error due to my not passing in a value in my database saving code. I've checked all of my other entries in the table, and do not see any other 'null' values that may be throwing this off - all are either '0' or '1'.
There must be some quirk here with my database. I have the same exact column with the same logic and that is saving as '0' perfectly fine. Looking for any insight into what could be causing this.
Are you using a framework where objects are automatically converted into SQL statements for saving? If the value of the property in your PHP class is not set and the column is nullable then it will insert null instead of 0.
Consider:
class Foo{
protected $propertyName;
}
That is equivalent in PHP to
class Foo{
protected $propertyName = null;
}
If the TINYINT(4) column for propertyName is nullable when it builds the query to save the data it will save as null in the database. If you are using a design pattern like this you need set the default value in the PHP class itself. Something like
class Foo{
protected $propertyName = 0;
}
Note, if the column is not null-able then saving the object would throw an error in this scenario. If you wanted to get fancy, you can fetch the default values for a column using
DESC tableName;
That will return information about the table, there will be a column NULL which will be YES or NO (describing if the column is null-able). And a column Default which will be the default value (or NULL if there is none). You could then populate null fields in your class based on the default values from the database. You would want to be careful here as there are likely some columns that should be NULL. Ideally, you would make any fields that can't accept null non-nullable and then key your logic for handling defaults only fire when the Null column is NO.
When inserting a new row, the default value for a column with an expression default can be inserted either by omitting the column name or by specifying the column as DEFAULT (just as for columns with literal defaults).
source: https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html
In case you specify NULL for the column on INSERT MySQL try to set the NULL for the column (and fails in case of NOT NULL). You have to ommit the column on INSERT or using DEFAULT as value on the INSERT statement.
See the following example:
CREATE TABLE test (
col1 INT DEFAULT 0 NOT NULL,
col2 INT DEFAULT 0 NULL,
col3 INT NULL
);
-- doesn't work since col1 can't be NULL
INSERT INTO test VALUES (NULL, NULL, NULL)
-- is working: col1 is 0 after INSERT, col2 IS NULL
INSERT INTO test (col2, col3) VALUES (NULL, NULL)
-- is working: col1 is 0 after INSERT, col2 IS 0 - because using DEFAULT instead of NULL.
INSERT INTO test (col2, col3) VALUES (DEFAULT, NULL)

TimeStamp column always gets zero value

I have the following query to set a default value to a column:
ALTER TABLE tableName ADD COLUMN testDate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
But I see 0000-00-00 00:00:00 for testDate column in newly inserted rows.
I would like to see current date and time in it.
Can anybody explain why this might be happening?
Data, you specified in your INSERT query, override default values of column, i.e. default values works if no data set for column.
See my fiddle.
Default values only work if the value is not defined in the query.
This will use the default value for test:
INSERT INTO mytable(userid, email) VALUES(42, 'info#example.org');
While this will override the default value of test:
INSERT INTO mytable(userid,email,test/*here is when it goes wrong, you shouldn't mention the column in your insert statement at all*/) VALUES(42,'bush#example.did','2001-09-11 00:00:00');

MYSQL Timestamp field inserting 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

MySQL can distinguish NULL and ""(null character string)

I set default value in a field NOT NULL.
The sql is as follows.
insert into table_name () values ()
I expected this sql should be rejected by MySQL as a field restricts NULL.
But, the field had a value ""(null character string).
Then, I tried another sql.
insert into table_name (name) values (NULL)
As this sql was rejected, no value was inserted.
MySQL seems to distinguish NULL and "". Do I have to avoid first sql such as "values ()"?
For a good practice always make the column as not null and set a default value whenever you create a table.
At insert time if you do not provide a value, the column will then be assigned the default value.
Do I have to set default value in a field that is set as NOT NULL?
YES if you are doing an INSERT where in you will not set any values to the NOT NULL columns
and
NO not needed anymore, if you are providing a value on the columns that are NOT NULL on INSERT.
You can refer to this
http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html

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