Mysql - Types for CPF, RG and CNPJ

Asked

Viewed 5,099 times

1

Fala galera.
I am at the beginning of Mysql and I have a question about field types in RG, CPF, CNPJ.
I did some research but I can only find answers from years ago, so I’d need something more up-to-date.
If I’m not mistaken, RG does not have as many fixed characters as CNPJ and CPF, then which one should use?
Also CPF and CNPJ if I’m not mistaken, can own zero in front, then use char?
I thought I’d use the guys below:

cpf char(14)
cnpj char(18)

I put in char so that the zero in front appears, and I will put points and strokes by mask in JS.
What I was in doubt is the ID, why I do not know if there are Rgs with zero in front, but I could not use char because as far as I know, it has no fixed number of characters.
Thank you.

  • In the case of RG you can use varchar of 15 for example. I recommend you do not store Cpf and cnpj without the scores, then your fields would be char(11) for Cpf and char(14) for cnpj.

  • Hello Roberto. Why would not recommend storing with the scores? Thank you!

  • Because every time you have to do some searching for Cpf you will have to put the scores out, which takes up more space in your database.

  • The question of comparison I think would not be a problem because as I will use mask to insert the points and strokes, will automatically stay in the registered pattern, but on the occupy more space makes sense hahaha Thank you!

  • "I recommend you not store without the scores"I think I meant otherwise, but I recommend to store with the scores, also phone, space of 5 characters, it does not matter nowadays, although it is necessary to perform calculation to validate CPF/ CNPJ , it is only necessary to validate once, already the mask, will have to apply in each select. I have done both ways and the best thing for me was to store with the mask. In the query, just clean the mask in the query

  • The comments here don’t make much sense, an hour speaks in by scores and then speaks that taking the scores takes more space. You should not use punctuation because this is formatting, the code has no punctuation, punctuation is not part of the data. When misconception in the future something goes wrong and creates problem.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.