Create a mysql database field type selection _ MySQL-mysql tutorial

Source: Internet
Author: User
Select the mysql database field type (go to bitsCN.com)

Currently, many developers use PHP + mysql to develop websites. when creating a database, we should not only consider the comprehensiveness of database fields, but also consider the rationality of field type settings in tables, because a database has no requirements for table design, it will be a waste of databases. of course, if you say your database is large enough, you also need to know some information about field settings, it allows you to write programs more flexibly.
In fact, I have never paid much attention to the setting of database fields. I followed an old method to use the int type for numeric type and varchar type for string type, if it is text, the text type is used. when I see a problem on CSDN today, I have redefined the field type selection.
The problem with CSDN is as follows:
Is it better to use datetime or timestamp to record the time in the database?
My first response is what type of datetime is? I know that the timestamp is a string of numbers. you can use the php function to process these numbers to convert them into various time types, such as OR. It is really unfamiliar with datetime. I only need to ask google.
The network is interpreted as follows:
A combination of dates and times. The supported range is '2017-01-01 00:00:00 'to '2017-12-31 23:59:59 ′. MySQL displays DATETIME values in 'yyyy-MM-DD HH: MM: SS' format, but allows you to assign values to DATETIME columns using strings or numbers.
For the above questions, I feel pretty good about the answers from another netizen on the CSDN Forum:
"If you are not planning to use your program for other databases, use datetime.
This makes it easy to use the rich date and time functions provided by the database.
Otherwise, it is better to use int type to save the unix timestamp.
Because the date and time functions provided by various databases are different, it is difficult to coordinate in SQL commands.
You can only use php to calculate the date and time"
SQL functions can be used to process datetime-type data, and I have not explored it. I know few SQL functions. if you want to learn more, try creating a database.

Here we are going to discuss how to set the field type. once we talk about the time type, we will talk about other types, such as creating a database table for students in a class. First, this table should include the following fields:
Id: student id. this is set as the primary key.
Name of the student
Sex student gender
OK. Now let's look at the id type, because it is a numeric type, we will think of int, but is there any better? Maybe you are not familiar with other types such as smallint tinyint. let's take a look at the explanations on the Internet.
TINYINT [(M)] [UNSIGNED] [ZEROFILL]

A small integer. The signed range is-128 to 127, and the unsigned range is 0 to 255.

SMALLINT [(M)] [UNSIGNED] [ZEROFILL]

A small integer. The signed range is-32768 to 32767, and the unsigned range is 0 to 65535.

MEDIUMINT [(M)] [UNSIGNED] [ZEROFILL]

An integer of medium size. The signed range is-8388608 to 8388607, and the unsigned range is 0 to 16777215.

INT [(M)] [UNSIGNED] [ZEROFILL]

A normal integer. The signed range is-2147483648 to 2147483647, and the unsigned range is 0 to 4294967295.

If we see the above content, will we consider using tinyint? because the number of students in a class is no more than 100. if we use int, it's not a big deal!

Therefore, when we are familiar with the scope of these types, we may design more rational types. You can also use int (3) and MEDIUMINT (3.

In addition, the name type is generally varchar. the varchar type will automatically expand and contract according to the size of your string. it looks good, but when will the char type be used? We recommend an article "varchar and char contention" here. I believe that after reading this article, you may be more specific about the use of varchar and char.
For more types of use, you still need to summarize them in the project, and do not forget to check the mysql manual.

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.