A
A
Alexander Lashchevsky2015-11-27 06:08:07
PHP
Alexander Lashchevsky, 2015-11-27 06:08:07

What is the fundamental difference between INT(3) and VARCHAR(3)?

Suppose I need to store some set of numbers from 0 to 100 in MySQL database cells .
What is the fundamental difference between INT(3) and VARCHAR(3)?
I understand that the first contains only numbers and signs, and the second also contains letters, but in any case, I do a field check before sending it to the database.
The only difference I see is that if there is no information, INT will contain 0 , and VARCHAR will contain nothing, which, by and large, is the same, since both of them will return FALSE in the absence of information ...
UPD A :
I just have the following situation.
There are two tables: _users and _users_log with identical fields. The first contains information about users, the second logs its changes (a new line is added, in _users the information just changes). When information is changed, data is sent to the log ONLY if it has been changed.
The problem is with the following. There is some column that can take the values ​​0, 1 or 2. Let it be INT. But since the information in the column with the log table is entered only if it has been changed, then if the user has not changed this column, it will be 0. And it is not clear: did he change the information to 0 or did he not change it and therefore 0? If you set the varchar, then the situation is clearer visually: if the field is empty, then it has not changed, if 0, then it has changed.
That's why I'm interested, because there is some reason to think about it.
If you can advise me to use another way to track information changes (the fact of the change + what I changed to), I will be glad

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Yanin, 2015-11-27
@Alexanevsky

The difference will be visible only in large projects. Because VARCHAR takes up more space. But this will not affect the scale of a small project in any way. But according to the rules of good form, of course, it is more correct to use INT for numbers.
And about the comparison because of which you are soaring. Regarding the introduction of 0 into INT, I see the solution as such. When sending information entered by the user, we add an arbitrary character to the beginning (for example, "!") The user did not change the information = 0. The user entered information in the form of 0, we get !0 at the output. We do a check made did not make. If introduced, then we cut off the character "!" and store the value in a variable.

A
AVKor, 2015-11-27
@AVKor

The fact that in one case we are dealing with a number, and in the other with a text, does this not lead to an independent answer to the question?
Everything else can be considered insignificant in comparison with this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question