default values in mysql table - mysql

I have wrriten a vbs macro to automatically insert values from a DDE Excel sheet to a DB table.
The problem, is that some of the cells are empty
so the query turns out :
INSERT INTO `stock_realtime` (`fkstock`, `benefit_month`)
VALUES (77, '')
ON DUPLICATE KEY UPDATE `benefit_month` = '', `created` = NOW();
But my sql table contains default values of '0' just for cases like this when the value is empty.
Still it errors Incorrect integer value:'' for column 'benefit_month' at row 1'
The other (full) queries work fine.

'' is NOT empty (numeric) value, NULL is.

You are explicitly setting a value for benefit_month. If you don't set a value for a field then the default value will be set.
That would set the default value for benefit_month:
INSERT INTO `stock_realtime` (`fkstock`)
VALUES (77)

Related

MySql INSERT...ON DUPLICATE UPDATE with partial VALUES

I have a list of possibly-incomplete set of values that will be used to append to or update a MySql table using the INSERT...ON DUPLICATE KEY UPDATE construct. The requirements are as follows:
If an operation resolves to an INSERT and the field value IS supplied, use the value supplied;
If an operation resolves to an INSERT and the field value IS NOT supplied, use the field's table DEFAULT value;
If an operation resolves to an UPDATE and the field value IS supplied, use the value supplied;
If an operation resolves to an UPDATE and the field value IS NOT supplied, retain the current (table) field value.
I've come up with the following statement, but the clauses wrapped in ** are erroneous and I'm having difficulty expressing them:
INSERT INTO `test`
(`id`, `num`, `text`)
VALUES
('1', 100, 'aaa'),
('2', 200, DEFAULT),
('3', DEFAULT, 'ccc')
ON DUPLICATE KEY UPDATE
`num` = IF (**VALUES(`num`) = DEFAULT**, `num`, VALUES(`num`)),
`text` = IF (**VALUES(`text`) = DEFAULT**, `text`, VALUES(`text`));
Notes: id is the unique key. Both num and text have default (NOT NULL) values set.
Things I've tried, but aren't satisfactory:
Replacing DEFAULT in VALUES with NULL, and then test for, e.g., IF (VALUES (num) = NULL .... This works, but will insert NULL on INSERT (and generate a warning - e.g., "Column 'text' cannot be null"), which is not acceptable - I need to have the default value applied to the missing fields;
Using something like 'xxx' instead of DEFAULT for missing values, and testing for 'xxx' (STRCMP), but this will insert 'xxx' in case of INSERT;
I've not tried this as I can't find the command/proper syntax, but the idea is to test (in the IF clause) whether num and text in VALUES are literals (num or string) or a MySql keyword (i.e., DEFAULT) - possibly using regex? - and then act accordingly.
Of course, an alternative to the above might entail obtaining existing values from the database and/or hardcoding into the query the default values for the missing fields, but I trust the same result can be achieved more elegantly using a single MySql statement.
Thanks in advance for your feedback.

Cannot insert zero in to MySQL INT NULL

I have a query that save zero in one of the MySQL column.
Column was set to be NULL = no and Default = none. But my query wasnt working when i had that structurer when I save 0. So i change the value to null. But when saving 0 (zero) in database its still getting saved NULL.
When i had the column set to be NULL = no and Default it was working fine on my localhost but in my hosting query wasn't getting inserted. that is why i change it to null.
MySQL version 5.6.30
Query
$mysqli->query("INSERT INTO categories(cname, cat_description, cat_ parent) VALUES ('$cname', '$description', '0')");
You are actually trying to insert a string in an integer column. MySQL does not accept this. And it shouldn't.
Insert an integer:
$mysqli->query("INSERT INTO categories(cname, cat_description, cat_ parent) VALUES ('$cname', '$description', 0)");
Just remove the quotes around 0.

Default values not working in phpmyadmin/mysql database

I can't get a table to accept "" or '' and use the default value. It is inserting NULL instead.
I am trying these commands in the direct input sql window.
INSERT INTO test01 VALUES ("", now(), "");
INSERT INTO test01 VALUES ('', now(), '');
But both just give NULL in the 3rd column. The structure is set to non-null with a default value of "yes". (Without quotation marks).
Here is a screenshot of the structure. You can see NULL is not checked.
http://garryjones.se/extras/so3.png
Default values only work if no value is inserted/updated. If you explicitly set it to an empty string (which is NOT the same as a NULL value) then it will end up with an empty string in the column. Instead of the code above you should eliminate the column from the INSERT statement at all:
INSERT INTO test01 (t1, t2) VALUES ('', now())
Other is already explain the reason here I am adding one more point you are also using current time stamp on update so do not need to use this column as well.
INSERT INTO test01 (t1) VALUES ('')
You could use the DEFAULT keyword: INSERT INTO test01 VALUES ("", now(), DEFAULT);

issue with NOT NULL in mysql

I am using wamp in win 7.
In my database, I have one table be_users, two fields: username and email, both of them are set NOT NULL.
But why I still can insert empty value and null value into field: email, see below image:
Actually, you are inserting text and they are not NULL. The text NULL is very different from NULL. You can never violate the rule if the field is set to NOT NULL.
Try executing this statement,
INSERT INTO tableName VALUES (null, null) -- will fail
it will surely fail because those are NULL. But this statement below will surely work because you are inserting string.
INSERT INTO tableName VALUES ('', 'null') -- will work
An empty value does not equal NULL, and it looks like you are inserting 'null' not NULL into the database, so you are ending up with a string 'null' not a NULL value.
Dont send NULL enclosed with quotes 'NULL' or "NULL" its getting treated as text value.
the issue is due to the data type used for thease two field. "text" you can change this to varchar(255) to use NULL. There are some issue with NULL and data type text.
Null is a keyword. If you will enter null directly then it will show you error.
Example-
insert into tablename(fieldname) values(null);
This above line will generate error(if you have mentioned not null).
I think you have enter something else. Please check again the table structure and enter new data.

Mysql DB Exception while saving data

I am using mysql query browser in that i set not null and default value for one column but while saving it is not considering default value it is showing error as column should not be null. how to solve this please help me
If you explicitly specify the column in an INSERT statement with a value of NULL, defaults are not considered.
For example, in the following query, even if there were a default for column foo, the engine will ignore it and try to insert a NULL:
INSERT INTO myTable(foo, bar) VALUES(NULL, 2);
Either omit the column from your INSERT statement entirely (recommended):
INSERT INTO myTable(bar) VALUES(2);
Or you can use a BEFORE INSERT trigger to catch the NULL value and replace it with what you want.