how to count values in a row that default instance have NONE? - mysql

Explain :
I have a table for Users and there is a column android_token, that can be null, but DEFAULT has NONE, means by default it doesn't sent null value for each row.
Now..
When I want to select all users that have their android_token not null, I can't get the result..
for example
SELECT * from tbl_users where android_token IS NOT NULL;
it gives me no sorting result, because rows that have it's android_token value are ok, but other users that have their android_token empty don't have even NULL value on it, in the structure of the table android_token DEFAULT IS NONE.
Can someone explain me, how can I update the table and send NULL value for each row that IS EMPTY only , or how can I sort users that have android_token with current configuration?
Thanks in Advance.

Explanation
Implicit defaults are defined as follows:
For numeric types, the default is 0, with the exception that for
integer or floating-point types declared with the AUTO_INCREMENT
attribute, the default is the next value in the sequence.
For date and time types other than TIMESTAMP, the default is the
appropriate “zero” value for the type. For the first TIMESTAMP column
in a table, the default value is the current date and time. See
Section 10.3, “Date and Time Types”.
For string types other than ENUM, the default value is the empty
string. For ENUM, the default is the first enumeration value.
Code
If android_token is a string:
UPDATE tbl_users SET android_token=NULL WHERE android_token='';
This will change all empty cells to NULL

Related

MySQL set default value for DATE column using DATE_ADD?

I'm trying to add a DATE column to my table with DEFAULT DATE value using an expression:
ALTER TABLE `wp_ezts_project_params` ADD `est_completion` DATE NOT NULL
DEFAULT DATE_ADD( CURRENT_DATE(), INTERVAL 1 MONTH ) AFTER `client_id`
I have tried different variations of several SQL functions, but every time get an syntax error near DATE_ADD.
Are we not allowed to use expressions as default values in phpMyAdmin?
Are we not allowed to use expressions as default values in phpMyAdmin?
Yes, it's not allowed. It's explictly stated in the manual
The DEFAULT value clause in a data type specification indicates a
default value for a column. With one exception, the default value must
be a constant; it cannot be a function or an expression. This means,
for example, that you cannot set the default for a date column to be
the value of a function such as NOW() or CURRENT_DATE. The exception
is that you can specify CURRENT_TIMESTAMP as the default for TIMESTAMP
and DATETIME columns
So if you really want to assign a value for this column you will a) need to pass that in or b) use a trigger
But really you don't need this column at all. It sounds like you are storing a value that's the result of a simple date add - storing it means you are introducing redundancy. Simply calculate it on the fly.

how to fetch the smallest value in mysql database by comparing it with default value if value does not given in php form

CREATE TABLE Orders
(
O_Id int(primary key),
OrderNo varchar default 'no',
P_Id varchar default 'no',
rate varchar DEFAULT 'no'
)
in this case i have set default value as 'no'.even if i did not give a value in my php form it should take the value as no.
now i want to compare the smallest value in the row i have 3 attributes to which i have to compare and which value is the smallest then i have to display it.
but the problem is when i am not giving the value in php form it is not taking the default value and not comparing with other values which actually exist.
finally,i want to compare the values with other values and should fetch the smallest value in the row even i am giving value in my php form how to make it happen?? i need the sql query to execute the above context.

mysql company update date value is blank

I have a table company_update and have the following field:
ID auto_increment
company_status varchar(20)
company_update_date date
For company_update_date field, if the value is blank, should I use NULL or '0000-00-00'? Which one is better?
NULL means "The data does not exist, or is unknown." Date('0000-00-00') essentially means, "This happened before time began." It's up to you, but I'd probably go with NULL.
NULL is is better because we can easily compare null value to other value
But if column has been defined with NOT NULL or with a default
'0000-00-00'. You need to change the column definition to allow NULLs and have no default.
The Null is better to know as start point. Example : You create company but it still not active (Now you set Null) you decided will be active after one month will you insert the date. So, you can check the Active, Pending, till not active. As other colleague say depending on requirement.

MySqlHelper.ExecuteDataset returning wrong values on field Tinyint(1)

I have a Database with a table who has a field Tinyint(1) but isn't a boolean it's simply a number from 0 to 9.
When I extract data from this table using:
Dim ds As DataSet = MySqlHelper.ExecuteDataset(CnStr, SqlStr)
the resulted "dataset.table(0)" doesn't contain the field value.
The returned value is a boolean. So I get only "TRUE" for every value of the field and when I insert data into another table, the inserted value is 0.
I solved changing field type from Tinyint(1) to Tinyint(2) but I'd like to know if someone else faced the same problem and if is there a better way to solve.
Add this to the connection string
TreatTinyAsBoolean=false
http://dev.mysql.com/doc/connector-net/en/connector-net-connection-options.html

Mysql test against value excludes NULL entries - can someone explain?

I've got a table shop_categories with a field called category_is_hidden which is defined as:
category_is_hidden tinyint(4) DEFAULT NULL
In the database, the values for that field are either 1 or NULL.
SELECT * FROM shop_categories where category_is_hidden IS NULL
returns all the null entries.
SELECT * FROM shop_categories where category_is_hidden <> 1
returns an empty sets (that is, it excludes the null values).
Why does that last statement not include null entries? isn't null <> 1?
Edit: Tested on MySQL 5.1 & 5.5
Since your category_is_hidden column appears to be a flag, I'd change it to tinyint(1) and make it be either 1 or 0 instead of 1 or NULL. Allowing a column to be null will add a byte to the storage requirements of the column, leading to an increased index size.
Next, the question you actually asked. NULL by definition is UNKNOWN. Your query says "give me everything where category_is_hidden is not 1". But the NULL column values are all unknown. So MySQL doesn't know if they are not 1. You need to rewrite the WHERE as IS NOT NULL. If your column is going to be tri-state (1, NULL, other value), you need to make your WHERE have an OR in it to allow for that.
If a field is null, then it means that it does not have a value. It is not zero, or an empty string. If you check if NULL <> 1, then it is not, because it is not a number; it is not anything, and therefore cannot be compared.