mysql --- ¿qué tipo debo usar? [cerrado]

when created the field for a table, i don't know which type (int, char,varchar....)and size i should use on the field. eg: email, time, address, phone....and so on.

preguntado el 09 de enero de 11 a las 06:01

Obviously you have to use the más apropiado tipo. -

3 Respuestas

Here's a general set of rules of thumb. Apply them in the order listed:

  1. If it's a time or date, use a TIME or DATE type
  2. If you're going to do math on it, use a numeric type such as INTEGER, FLOAT, REAL, or DECIMAL. Numbers such as telephone numbers or student IDs don't need to be numeric types, but some of them can be if you want. If you're going to add, subtract, multiply, or compare ranges of numbers, definitely make them numeric types. If you aren't going to then it probably doesn't need to be numeric. A good related rule of thumb is that if the number "00" is the same as the number "0" for your situation, then it should be numeric. If those are different numbers, (such as room numbers or student IDs in some cases) then it should not be a number. (I'll put rules of thumb for choosing which numeric type below.)
  3. For everything else, use string types. Specifically, if you know the length either exactly or within a couple of characters, use a CHAR of that length.
  4. If you don't know the length, use a VARCHAR and choose a reasonable maximum length. That is, something that you're sure would be long enough without getting silly with it. For instance, a VARCHAR(15) for last name just because none of your friends has a last name over 15 characters sounds find until you need to insert Mr. Schwietzer-McCullough. But a VARCHAR(100000) is probably overkill. For last names, something on the order of a VARCHAR(50) would probably be appropriate, but VARCHAR(100) would be reasonable.

Choosing numeric types:

  1. If it's a whole number and will always be a whole number, chose an INTEGER type. Again, you'll have to choose size based on the largest value it could have. There's a page here: http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html which includes a chart of largest values for the different sizes of INT types in MySQL.
  2. If it's a decimal number such as money values, percents, temperatures, or most any ordinary decimal values, use DECIMAL. (NUMERIC means the same as DECIMAL in MySQL.) You'll choose two numbers to go along with this (such as DECIMAL(6,2)). The first number is the total number of digits to be stored. The second number is the number of digits to the right of the decimal place. That is, if you need to store prices of toothpaste which range from about $1 to $10, you could do DECIMAL(4,2) which would store numbers from 0.00 to 99.99. If you have to store stock prices which can include fractional cents and be quite high, you'd probably want DECIMAL(10,3) which would range from 0.000 to 9999999.999.
  3. If you have numbers which need to be expressed in scientific notation and on which you'll be doing a lot of math and you need it to be efficient, use REAL or DOUBLE. Don't use these for storing prices or percents or stuff like that (see 2) because they'll introduce subtle inaccuracies. But if your answer could vary from 8x10^-5 to 2.58x10^34, then yeah, for stuff like that, use DOUBLEs. Be warned that these aren't actually stored in the form of X*10^Y, but rather X*2^Y because computers work in binary, so some numbers may be slightly different from their decimal representations. This is why we don't use them for currency.
  4. If you've got a number like from 3, but you are really pressed for storage space, then use FLOAT. Really, in practice, you'll probably never use FLOAT. They're like DOUBLEs, but half as big.

Respondido el 09 de enero de 11 a las 10:01

many thanks,you made me know a lot. - corre todos los días

I should actually add a small note that time and date types can sometimes be a pain especially when working across systems, so some people use alternate means of storing times and dates which is not necessarily a bad idea, but I'd use the TIME and DATE types unless you have a specific reason not to. - Keith Irwin

when i using an ID as the primary key and making it autoincrement, which type should i use? serial or int? and to the room number and student id, i shoule use the varchar(10) and varchar(15).am i right. thank you - corre todos los días

and if the field is an article's body, which type should i use? - corre todos los días

@runeveryday serial is just the same as BIGINT. I would say that which you use is determined by how big your table will get. As for the room number and student ID, are you unsure how long they are? If you know their length, you should use a CHAR. If a field represents an articles body, do you know how long it will be? - Keith Irwin

Don't use integer for numbers, don't use varchar for variable width character lines. If you use a size smaller than the actual input you will save disk space by losing data.

Respondido 18 Oct 11, 21:10

Well, you can use varchar field for all the fields except for time. for time field you can use a datatype called time since this will help you in easy manipulation of the data.

Respondido el 09 de enero de 11 a las 09:01

The worst advice that can be ever given to such question. - zerkms

I donot understand why this is a bad advice. The question was asked how to specify the datatypes for address,email.You can use varchar for the same.same applies to time field as well.Its much better answer as compared to saying "don't use integer for numbers etc.." - programador

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.