Can you set a CHAR to NULL or an empty string? - mysql

I haven't used CHAR much in the past as I seemed to use VARCHAR too much; I'm trying to use CHAR when appropriate lately & from what I understand you use it when the data is all the same length in that certain column (else it is padded with spaces).
Because all the data is supposed to be the same length I was wondering can a CHAR field be NULL or an empty string? Such for cases when that specific field doesn't have a value whilst others do.

The answer to your question(s) is Yes.
Yes, the CHAR type can be NULLable. I believe every column type can allow for NULL.
Yes, the CHAR type can be an empty string. The DB will not see an empty string as any different from any other string. I wouldn't suggest ever using empty string though because, in almost every case, an empty string (lack of ANY characters) is trying to represent a lack of data, which is what NULL is for.
If you want to know more specific details around the difference of CHAR vs. VARCHAR in MySQL specifically...
MySQL 5.0 Reference - The CHAR and VARCHAR Types

Yes, both NULL and empty string can be used. The data doesn't have to be the same length in the column, as you noted, anything smaller is just padded with spaces to fill the column.

Related

mysql only compares first numerical part of string to int column in where

This is more a "why" or "any pointers to documentations" question.
Today I realised that a comparison in WHERE clause takes only the first numerical part of the string if compared to an INT column. (My own conclusion)
For example:
SELECT * FROM companies c WHERE id = '817m5'
will still return companies with id = 817.
It does make sense but I'm failing to find this documented anywhere to see if my conclusion is correct and if there are additional things to know about this exact behaviour. WHERE, INT type, comparison documentation? Where? How is it called?
Thanks!
This is the comparison:
WHERE id = '817m5'
and id is an integer.
So, MySQL has to compare an integer to a string. Well, it can't do that directly. It either has to convert the string to a number or the number to the string. MySQL converts the string to a number, which it does by converting the leading numeric characters. So, the '817m5' becomes 817.
Here is the exact quote:
To cast a string to a numeric value in numeric context, you normally
do not have to do anything other than to use the string value as
though it were a number:
mysql> SELECT 1+'1'; -> 2

What data type does a URL correspond to in MySQL?

I am going to store URLs inside of my database and I just had a simple question. Do URLs have to be a certain data type?
Simply put the data type should be VARCHAR
URLs can contain any number of characters, and can be any length (within reason on the smaller end). A CHAR field can only contain the number of characters that is set in the table definition. A VariableCharacter (VARCHAR) field can contain a variable number of characters. So since not all URL's are of equal length you need the variability. You could make an argument to use a TEXT field if you needed to store really long URLs; however, for most use cases VARCHAR will suffice.
An url has special, numerically and normal chars.
Therefor you should use the type "VARCHAR" in MySql.

MySQL truncates my first zeros data

I have a MySQL column "phone" , and when data comes from my php form, if there's a zero at the beginning, it will disappear in the table, for example :
"06719823" becomes "6719823"
I first though it was a problem from php, cause I'm using mysql_real_escape_string(), but then I tried to modify the SQL field directly on phpmyadmin, and I can't add a first 0, it always delete it.
Colonne Type Interclassement Attributs Null Défaut Extra
phone int(10) Oui NULL
What's wrong ? Should I assign an "interclassement" utf8_general_ci ?
Change your column type to char(10) for 10 digit phone numbers.
If the column type is int (integer), the number will be internally represented as an integer, meaning "first 0s" won't be stored, as they hold no meaning for integers.
Since what you are actually trying to store has meaning as a sequence of characters, and not as a quantity, it would make more sense to store it as a char(n), for n-digit sequences, or as a varchar for sequences whose size varies a lot.
Make your phone attribute as Varchar or Text to avoid this.
Phone numbers can at time also contain brackets and hyphens plus you can avoid your problem as well.
Change your data type. Int Data type will not store the starting 0's.
You can try as suggested above char or varchar
Integers : 06719823 = 6719823 = 0006719823
Save the phone as varchar if you would like to retain zeros in the begining

String equivalent in mysql datatypes

I want a create a column that needs to store unknown length strings.
I am making a parsing of some file and pushing it's content to the DB and I have no idea about the length , can be pretty big.
So is there any equivalent to String type ?
Thanks
Depends on what your storing...
CHAR is a fixed length string
VARCHAR is a variable length string
TINYTEXT/TEXT/MEDIUMTEXT/LONGTEXT is a text field (long string)
Choose the format that is most appropriate for what your storing.
See http://dev.mysql.com/doc/refman/5.0/en/blob.html for more information on the TEXT field and the differences.

MySQL Tri-state field

I need to create a good/neutral/bad field. which one would be the more understandable/correct way.
A binary field with null (1=good, null=neutral, 0=bad)
An int (1=good, 2=neutral, 3=bad)
An enum (good, neutral, bad)
Any other
It's only and informative field and I will not need to search by this.
NULL values should be reserved for either:
unknown values; or
not-applicable values;
neither of which is the case here.
I would simply store a CHAR value myself, one of the set {'G','N','B'}. That's probably the easiest solution and takes up little space while still providing mnemonic value (easily converting 'G' to 'Good' for example).
If you're less concerned about space, then you could even store them as varchar(7) or equivalent and store the actual values {'Good','Neutral','Bad'} so that no translation at all would be needed in your select statements (assuming those are the actual values you will be printing).
In Mysql you ought to be using an enum type. You can pick any names you like without worrying about space, because Mysql stores the data as a short integer. See 10.4.4. The ENUM Type in the documentation.