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.
Related
I have the following query which I am trying to run in MySQL. Basically I am trying to work out how to update the corresponding field which isn't null. I know its going to be one of two possible fields but I want to update the one which isn't empty. Maybe I am missing something blindingly obvious, but this is what I've tried thus far:
UPDATE `table`
#SET IF(FieldA IS NULL,FieldB,FieldA) = 1234
#SET IFNULL(FieldA,FieldB) = 1234
WHERE `FieldC` = '5678'
AND (
`FieldA` = '1234'
OR `FieldB` = '1234'
)
I suspect there may be a CASE solution but I'd prefer a shorthand/simple version option if it exists.
(My)SQL has no syntax that allows you to dynamically change the column you want to update, e.g. something like update ... set {PickFieldBaseOnCondition:FieldA|FieldB} = 1234. You have to specify a column there. The moment you start your query, the basic structure of the query (and all fields involved) have to be clear and fixed, only the values can change.
So you need to update both fields in your query if it shall be able to modify two different columns. But you can of course decide to just not modify the content of a field based on its content, e.g. keep it if it is null already:
update `table`
set FieldA = IF(FieldA IS NOT null, 1234, FieldA),
FieldB = IF(FieldB IS NOT null, 1234, FieldB)
where ...
Note that the requirement "update the other field if a field is null" only works if your initial condition that one field is null and one is not null is fulfilled, which you said is given. Otherwise, you should include a test if both fields are null or both fields are not null (in the comparison for the first field), which you could e.g. do with
update `table`
set FieldA = IF(FieldB IS null, 1234, null),
FieldB = IF(FieldB IS NOT null, 1234, FieldB)
where ...
FieldA can now be changed from content to null if both fields are not null (and from null to content if both fields are null), to enforce the condition that exactly one field is not null.
Please also note that IF() is a MySQL-only shorthand for CASE and doesn't work in all databases. You prefered a non-case solution, but it can trivially be rewritten using the sql-standard CASE.
I have a sample table with one of the column called Expression with the TEXT data type. This is the data I am going to insert and the number of characters in that string is 504 characters.
Expression="CASE WHEN (txt_Second_23.TYPE_OF_AUTH='OP' or txt_Second_23.TYPE_OF_AUTH='OPS') then '1' else (CASE WHEN ((txt_Second_23.TYPE_OF_AUTH='IP' or txt_Second_23.TYPE_OF_AUTH='IPN') and ((txt_Second_23.Requested_Provider_NPI is not null or txt_Second_23.FACILITY_NPI is not null) OR (txt_Second_23.Requested_Provider_Last_Name is not null or txt_Second_23.Facility_Name is not null) OR (txt_Second_23.REQUESTED_PROVIDER_CITY is not null or txt_Second_23.FACILITY_CITY is not null) OR (txt_Second_23.abcdef) END"
Whenever I tried to add one more character to the above string, it's not allowing to insert. Can anyone please let me what is causing the issue or am I missing something here.
Note: I have changed the data type from TEXT to VARCHAR(1000), still no luck.
Update: This issue is happening while inserting it through programatically with prepared statement. I am able to insert when I do it manually by creating sample table with expression column.
This is the actual string I am trying to insert.
CASE WHEN (txt_Second_23.TYPE_OF_AUTH='OP' or txt_Second_23.TYPE_OF_AUTH='OPS') then '1' else (case when ((txt_Second_23.TYPE_OF_AUTH='IP' or txt_Second_23.TYPE_OF_AUTH='IPN') and ((txt_Second_23.Requested_Provider_NPI is not null or txt_Second_23.FACILITY_NPI is not null) OR (txt_Second_23.Requested_Provider_Last_Name is not null or txt_Second_23.Facility_Name is not null) OR (txt_Second_23.REQUESTED_PROVIDER_CITY is not null or txt_Second_23.FACILITY_CITY is not null) OR (txt_Second_23.REQUESTED_PROVIDER_STATE is not null or txt_Second_23.FACILITY_STATE is not null) OR (txt_Second_23.requested_provider_zip is not null or txt_Second_23.facility_zip is not null))) then '1' else null end) END
Thanks In Advance.
In my code some where expression column was appending to one more column called description and it's length is only 512 characters. That's causing the issue.
Taken care it by changing the data type from varchar(512) to text.
If I use the MIN function on a text field, I get an unexpected result.
I have a table with one text field with following values 'a', '', 'c'.
My expectation is that the empty value is returned (when you would sort the table the empty string comes before the 'a'), but instead I get the 'c' as a result !!!
One should easily be able to see this problem by performing the following queries in an access database:
create table testbug (Field1 varchar (255) NULL)
insert into testbug (Field1) values ('a')
insert into testbug (Field1) values ('')
insert into testbug (Field1) values ('c')
insert into testbug (Field1) values ('d')
select min(field1) from testbug
If the blank value contains something else (even a space for example) then it works correct!
Can anyone reproduce this and/or explain and/or indicate whether this is a known bug (I could not find anything about this).
I work with the following version:
Microsoft Office Professional 2010
Microsoft Access Version: 14.0.7166.5000 (32-bit)
MIN simply ignores NULL values.
If you still want to use MIN with NULL values you can try :
SELECT MIN(Nz(Field1, " ")) FROM testbug
the MIN function indeed seems to handle empty values (zero length string values) as NULL.
I added a Field2 to your testbug table and added two rows manually:
no value for Field1, value 'k' for Field2
value for Field1, value 'l' for Field2, remove value for Field1
I also entered a value 'm' in Field2 for the second row you inserted in your table.
Next, I ran this query:
SELECT Field1, Field2,
IIf(min(Field1) Is Null,'null','not null') AS result, Len(Field1)
FROM testbug
GROUP BY Field1, Field2
The IIF function returns null for all values being null or empty.
For the second row you inserted, the function Len returns 0, meaning this is a zero length value. However, the IIF function returns null.
Meaning, MIN seems to handle empty values as NULL values.
Apparently, this is standard behavior within MS Access.
Hope this helps.
Also hope help here was better than the help you got on Helpmij ;)
I have a table called User_info. In which some columns contains values and some columns contains null values. How can I select value fields alone?
From your comment:
In my case I want to select all the values from the table and need to
bind the values to the corresponding fields again in the form. while
binding, if a column contain null value then it raising an error. The
error is
Conversion from type 'DBNull' to type 'String' is not valid
Then you either have to show the code that causes the invalid cast or replace nulls with an empty string:
SELECT Col1 = COALESCE(Col1,'')
FROM User_info u
Instead of COALESCE you could just as well use ISNULL.
I've looked all over the internet for my answer, and perhaps I'm just doing things wrong. I have a column in my MySQL table that I need to replace all the NULL values with a text string in my SQL Query using phpMyAdmin. I don't want the output to come out that way, I want to actually replace the null values with the text string.
I've tried
UPDATE `tablename` SET fieldname = replace (fieldname, "", "textstring")
I've read up on
SELECT ISNULL(field,"replacetext)
But this only shows the output, but doesn't actually replace it in the table.
I can't figure this out, and I've wasted so much time trying to find an answer.
update tablename set fieldname = "textstring" where fieldname is null;
Have you tried
UPDATE `tablename` SET fieldname = '' where fieldname is null