3
How to put a field in mysql that only allows 4 characters. No more and no less?
I’m doing like this:
alter table cartoes add os_4_ultimos_digitos int (4); <
But it takes a Constraint and I can’t do it.
3
How to put a field in mysql that only allows 4 characters. No more and no less?
I’m doing like this:
alter table cartoes add os_4_ultimos_digitos int (4); <
But it takes a Constraint and I can’t do it.
3
To fill an INT value with zeros on the left:
ALTER
TABLE cartoes
CHANGE
COLUMN `os_4_ultimos_digitos`
`os_4_ultimos_digitos` INT(4) ZEROFILL
To add a column already with this feature:
ALTER
TABLE cartoes
ADD
os_4_ultimos_digitos INT(4) ZEROFILL;
If you prefer, you can use a VARCHAR field, and force DB not to accept anything other than 4 characters when inserting with a Trigger:
DELIMITER //
CREATE TRIGGER trg_len_4_insert BEFORE INSERT ON cartao
FOR EACH ROW
BEGIN
DECLARE msg VARCHAR(255);
IF LENGTH( new.os_4_ultimos_digitos ) != 4 THEN
SET msg = concat( 'Erro: tamanho errado do campo: ', CAST(new.os_4_ultimos_digitos AS CHAR) );
SIGNAL SQLSTATE '45000' SET message_text = msg;
END IF;
END
DELIMITER ;
See this code working here: SQL Fiddle.
Create one for BEFORE UPDATE as well, if applicable
Notes:
I posted the above alternatives to answer the question with the Mysql tag, but if you are using some other language for the application, the most appropriate place to treat this is in the application, and not in the structure of DB.
You can keep the INT as is, and handle the data at the time the user fills the data, and while recovering the same from DB, do the padding adequately (which, by the way, is the simplest alternative).
I swore Mysql didn’t have the check constraints feature!
@renatoargh Mysql "has but does not work". I adapted the response, grateful for the alert. Do without testing is complicated, this time I made a fiddle to see if it is not misleading advertising :)
1
If the field is new:
ALTER TABLE cartoes
ADD COLUMN os_4_ultimos_digitos CHAR(4);
existing field:
ALTER TABLE cartoes
CHANGE COLUMN os_4_ultimos_digitos CHAR(4);
tries to reduce the name of this field, it is strange
I thought the name was perfect. What would you suggest, @Marcelo?
If it is perfect for you it is what matters :) I usually for fields leaner and that refer the quality of the value that will allocate... Ex: digit_checker already on the screen you put the most familiar name to the user.
digito_verifier is also great. I thought you’d say you’d do something like 4_ult_dig . Then I’d have to respectfully disagree, rs.
@Marcelo Bonus maybe the name of the field would be: nr_4_ultimos or nr_ultimos who is developing knows the name, but sometimes, because I did not follow a logic to understand, some programmers put short names and I do not like it. For example: name "nm", things like that. I’d rather put two whole names than several abbreviations.
@Marcelo Bonus your dml commands allow data smaller than 4 digits. int (4) allows from 1 to 4 digits and is not what I want. I only want four digits, no more, no less. For example, end card 3579, if I take the previous number to form 5 digits, not accepted in the system or if I only enter 3 of the 4 digits, also not validate.
I see, puts char in the place of int
But Voce should check the size of the field in your form to avoid a call to the server
Browser other questions tagged mysql
You are not signed in. Login or sign up in order to post.
Is to sweep the field?
– rray
@rray As I asked the question: alter table cards add os_4_ultimos_digitos int (4); <
– André Nascimento
You want 4 characters with zero left or no?
ALTER TABLE cartoes ADD CHECK ( os_4_ultimos_digitos > 999 )
forces from 1000 up. If you want 4 digits with zero on the left, it is a case of enabling the zerofill country.– Bacco
@Bacco can be with 0 left by which the 4 last card number can start with 0.
– André Nascimento
@Then just turn on the Mysql zerofill that is already filled. But I think the right place to take care of it is in the application, not in DB.
– Bacco