Today to introduce the database design process of the selection of the principle of the field, that is, when the use of int, when with varchar, when using char and so on __ database

Source: Internet
Author: User

I'm here to introduce you to some of the most commonly used fields, such as int, float, double, decimal, varchar, char, date, DateTime, and eight commonly used types.

In the database design process we should be in line with the principle of sufficient, if blindly set the data field to the maximum or default value, will lead to a large amount of storage space waste. In the case of a particularly large amount of data, such a design principle will cause a serious waste of database space, but also the efficiency of the implementation of the database has a great impact. So we should be careful and careful when we do the database design.

The following is the principle of using the fields:

1. When the data is stored in characters, and the length is a fixed interval value, you can consider using char for storage, if the length of the characters is unknown and the length of the change is particularly obvious, this time it is best to use varchar to store. However, regardless of the type of field used to store, do not set the initial length of the field to maximize, should be based on business requirements to store the most appropriate length of the field.

2. Use int as a field type in the database design process, because int is best in all data types, whether it is storage space or execution speed. For example, if you have data stored in your business that is not very long, consider using int for storage, or you can use an int to store the data in order to sort it. But do not because the int efficient, and consciously put all the fields are designed as an int to deal with, ultimately, according to the specific needs of the business to design the corresponding fields.

3. When it comes to the amount of money, if the demand for precision is not high, you can use float first, followed by using double to store. It is best to use decimal to store if the precision requirements are high, but the corresponding efficiency is not as efficient as float and double. The specific use of which or according to the specific needs of the business to choose.

4. The use of date and datetime depends on the exact value of the time you want to store, if you just want to be accurate to the day, using date is OK, if you want to be accurate to the second level will use DateTime. Sometimes the use of datetime is only stored in the day of the date unit, this time will automatically add 00:00:00 behind the data, as a second level of data.

What is the difference between 5.int (5) and int (10). Presumably everyone is also very curious about it, in fact, 5 and 10 is only to show the length of the but not just, that is, regardless of the value of int (x) x, the value of the storage number or int itself data type range, X is only the length of the data display.

What is the difference between 6.varchar (10) and char (10). What happens when the store is over? This problem is also relatively classic, and I have been a long time to find out. After mysql5,5, varchar (x) x is actually the length of the storage field, which is a character representing a length, whether the character is a man or a character is a unit of length. varchar Although set the length value, but because varchar is a variable length type, that is, when the storage length is less than x time, actually the actual storage space is not x but actually stored character length + some marked space. Of course, if you exceed the length of x, there will be an error. The value of y in char (y) is the actual storage length of the storage space, which will be an error if it exceeds this length.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.