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);
Related
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.
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.
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.
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)
INSERT INTO `configuration` VALUES ('', 'News Box Character Count', 'NEWS_BOX_CHAR_COUNT', '200', 'Set the number of characters (bytes) that you want to display in the news preview box.', 19, 99, NULL, '2004-09-07 12:00:00', NULL, NULL);
I run this command in phpMyAdmin, it shows
#1366 - Incorrect integer value: '' for column 'configuration_id' at row 1
configuration_id is an auto increment field beginnning of 1
Instead of this INSERT INTO configuration VALUES ('', 'News Box Character Count',
Pass the value as NULL for auto_increment or integer column or you can simply not to include that column in sql query.
INSERT INTO `configuration` VALUES (NULL, 'News Box Character Count', ...
This is because, mysql is running in the strict mode.
You can either use NULL for all the integer columns when there is nothing to enter them or turn off the MySql Strict mode.
For an autoincrement in MySQL, either insert NULL or insert nothing at all:
Easiest & Cleanest: using NULL
INSERT INTO `configuration` VALUES (NULL, 'News Box Character Count', 'NEWS_BOX_CHAR_COUNT', '200', 'Set the number of characters (bytes) that you want to display in the news preview box.', 19, 99, NULL, '2004-09-07 12:00:00', NULL, NULL);
More work: name every column except the autoincrement one
INSERT INTO `configuration` (every,column,except,the,first) VALUES ('News Box Charac`ter Count', 'NEWS_BOX_CHAR_COUNT', '200', 'Set the number of characters (bytes) that you want to display in the news preview box.', 19, 99, NULL, '2004-09-07 12:00:00', NULL, NULL);`
It seems pretty obvious to me? You gave an integer column and you are explicitly inserting a string (although it's empty). If the column is set to auto increment, remove the first value ('') from your values array and you should be fine. Also, maybe you wanto to specify the columns you are inserting values for, like:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
For the AUTO_INCREMENT field you can set NULL; it will generate new value automatically.
For example -
INSERT INTO table_name VALUES(NULL, 'value', ...)...
If the column is auto-increment then you can simple omit it from the INSERT statement.
Replace the '' value with NULL ('' isn't a valid integer). See here.
You should always use column names when inserting. So just don't have to insert into the column configuration_id and your sql statement doesn't break when changing the columns.
Use something like this: INSERT INTO table (column1, column2) VALUES (value1, value2);