How 'Set Default value' to 'NOW()' with Sequel PRO? - mysql

I have a MySQL table with some dates,
I need that one of them have a default value equal to current time,
I'm using 'Sequel Pro' to build the database,
Then i wrote 'now()' (and 'GETDATE()') in default value, but doesn't work.
Can someone do help me, How 'Set Default value' to 'NOW()' with Sequel PRO?
ERROR:
An error occurred when trying to change the field 'DataDoPedido' via
ALTER TABLE Reserva CHANGE DataDoPedido DataDoPedido DATE
NOT NULL
DEFAULT 'now()'
MySQL said: Invalid default value for 'DataDoPedido'
thanks.

For MySQL, the DEFAULT specified for a column must be a constant; it cannot be the return from a function. The one exception to this is the TIMESTAMP datatype, which can have a DEFAULT CURRENT_TIMESTAMP.
If you need to initialize a DATE column, one workaround is to create a BEFORE INSERT ON trigger.

Related

MariaDB default float can't contain decimal places

I am learning about MariaDBs column types and noticed this on my MariaDB db (latest dockerized 10.3.13), connected via HeidiSQL 10.
I can't set the default value for my FLOAT column to a value that contains decimal places:
After hitting save the default value is just 42. This also happens when performing the ALTER / CREATE TABLE query manually. (In the screenshot the column type is FLOAT but I also tested with FLOAT(10,2).)
Edit: When creating the table with this SQL statement, new rows will have the default value 42 instead of 42.11:
CREATE TABLE test2 (
`float` FLOAT(10,2) NOT NULL DEFAULT '42.11'
)
Why?
I've just reported that to HeidiSQL, This is only a display bug on HeidiSQL: https://github.com/HeidiSQL/HeidiSQL/issues/593
You can execute "SHOW CREATE TABLE test2" or insert data into test2, this will show you that default value is not truncated.

"Field doesn't have a default value" without strict mode

I have a table with a column that is filled by a before insert trigger, this column is set to be NOT NULL and has no DEFAULT VALUE.
When I do a INSERT TABLE without passing this column, I receive the error: 1364 - Field 'column_name' doesn't have a default value. I'd search the web for a solution and have encountered this question: Field 'id' doesn't have a default value?. I then checked and changed the mysql_mode from:
"STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
to:
"NO_ENGINE_SUBSTITUTION"
but the insert query still returns the same error.
What should I do to bypass this "strict" mode? Or is it something else?
Please let me know if I need to include any config variable. I'm using mysql 5.6.39.
This is a known bug for the mysql 5.6.39 version. And has been fixed since version 5.7.

MySQL: Error #1067 when trying to add a boolean column with default 0

I'm developing a feature to show to the users how many unread messages there is on the site. So I'm trying to execute this query:
alter table messages add read boolean not null default 0
But the server returns this message:
#1067 - Invalid default value for 'Data'
I can't see what is wrong. I'm running MariaDB/MySQL 5.7.20-0ubuntu0.16.04.1.
Found the problem. The "Data" column is with a invalid default value. I got this database restored from a server that has a different MySQL version.

MySQL - Field <myfield> doesn't have a default value

I am aware that the certain field doesn't have a default value.
I've been using MySQL 5.5.28 and it does work whenever I insert without specifying a value on that field. The field is TINYINT and by default, without specifying any value AND without declaring a default value during creation of the table, a value of 0 will be inserted in that field during INSERT statement.
However, after updating to MySQL 5.5.30, the query doesn't work anymore and is returning Field doesn't have a default value.
I've been looking through the changelogs and didn't find any clue that something has changed with regards to the default values of Integer.
MySQL 5.5.29 : http://dev.mysql.com/doc/relnotes/mysql/5.5/en/news-5-5-29.html#mysqld-5-5-29-feature
MySQL 5.5.30 : http://dev.mysql.com/doc/relnotes/mysql/5.5/en/news-5-5-30.html
Test queries:
MyTable has the Fields MyField1 and MyField2
INSERT INTO MyTable(MyField2)VALUES('MICHAEL');
Result on MySQL 5.5.28:
MyField1 | MyField2
0 | MICHAEL
With warning: 1 row(s) affected, 1 warning(s): 1364 Field 'MyField1' doesn't have a default value
Result on MySQL 5.5.30:
No changes on data and throws an error
Error Code: 1364. Field 'MyField1' doesn't have a default value
INSERT INTO MyTable(MyField1, MyField2)VALUES(0, 'MICHAEL');
The above query will work though.
In the 1st server strict sql mode was not enabled, while in the 2nd one it was. Read more about strict mode in the mysql documentation.
Specifically:
If strict mode is not in effect, MySQL inserts adjusted values for invalid or missing values and produces warnings (see Section 13.7.5.40, “SHOW WARNINGS Syntax”). In strict mode, you can produce this behavior by using INSERT IGNORE or UPDATE IGNORE.

Mysql and inserting '' (empty not null) in datetime column

I have a system that insert some datetime field as an empty string '' and it works. However, i tried to install in a different machine and it fails.
It returns
Error Code: 1292. Incorrect datetime value: '' for column 'MyDate' at row 1 0.000 sec
btw, i can't change the system and both system (the working one and the new one) runs on windows.
Disable STRICT_MODE (which is enabled by default on Windows installations)