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.
Related
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.
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.
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.
I tried creating a field as a TINYINT(1), NOT NULL and a DEFAULT value of -1 to indicate 'unknown', but I got this error from my client:
Error altering MyTable: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
I also tried making the length 2 and got the same message
Guessing TINYINT isn't the right data type for this kind of designation, but what is?
EDIT: I got it working after reading Ed Cottrell's comment. I think there was an extra character somewhere, here was the statement that my client (using Querious for Mac) generated:
ALTER TABLE `DBName`.`MyTable`
CHANGE COLUMN `MyColumn` `MyColumn` TINYINT(1) NOT NULL DEFAULT -1 COMMENT ''
AFTER `MyOtherColumn`;
Noticed that COMMENT there and made sure everything was clean.
Other comments and answers were appreciated; I have decided to let NULL mean unknown in this case
I think you should store this as a bit (if you care about storage size), and let NULL mean "unknown".
field bit(1) default NULL,
It seems strange to declare a field to be not null and then to have a special value that, essentially, means NULL.
EDIT:
The following syntax "works" on SQL Fiddle:
create table t (
val int,
flag tinyint(1) default -1
);
"works" is in quotes because the default value prints as "1" rather than "-1" -- after all, the (1) is saying just print one digit.
Perhaps in some earlier versions of MySQL it generates an error when it sees that -1 won't display correctly. (To be honest, that would surprise me.)
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)