Straight to the interview site: How much do you know about MySQL data types?

foreword

Through the glass door, watching the interviewer walk slowly, with a few white hairs floating on his head, swaying with the wind while walking, it makes people have an urge to help him pluck it off.

The position of the interview this time is a database data type, the interviewer sat down and smiled at the interviewer Mu Fengxiaoyue, "Here we come"!

Although it was a simple word, it still made Xiaoyue feel a little shocked, as if a wave of true energy rushed to her face.

This person has a lot of skill, so Xiaoyue handed over her resume: "Hi interviewer, I'm here for an interview, here is my resume"

The interviewer didn't pick up the resume, but just nodded lightly, motioned Xiaoyue to put the resume on the table, and said slowly, "I basically understand your situation, and the interviewer on the other side has already told me, I don't know what you think Do you know the database, for example, how much do you know about MySQL data types.”

Xiaoyue breathed a sigh of relief, thanks to seeing it when she came to the interview, she actually had some secret joy in her heart, since she traveled to this planet, Xiaoyue felt so relaxed for the first time.

Then Xiaoyue got up, pulled the whiteboard, and began to explain his understanding of data types based on the memory of the database in the previous life

1. About MySQL data types

On the official website, MySQL supports nearly 39 data types, which can be roughly divided into three categories: numeric, date/time, and string (character) types.

1. Numeric type

Numeric types include integer types and floating point types:

Regarding numeric types, I drew a table here:

insert image description here
Signed and unsigned here are used in MySQL to define the range of numeric data types. There are the following differences between them:

  • Signed: Signed numeric types can represent positive numbers, negative numbers, and zero. This means that you can store positive and negative values, including zero, in signed numeric types. For example, the signed TINYINT type can store values ​​in the range -128 to 127, where negative numbers and zero are also valid.
  • Unsigned (Unsigned): An unsigned numeric type can only represent non-negative numbers (that is, positive numbers and zero), and cannot store negative values. Unsigned types extend the upper bounds of data types because they do not require a single bit to represent the sign. For example, the unsigned TINYINT type can store values ​​ranging from 0 to 255.

For example, the tinyint type occupies one byte, signed -128 to 127 and unsigned 0-255.
If a person's age is 0-100, theoretically you can use the tinyint unsigned range, 0-255 is enough, and it occupies The number of bytes obtained is relatively small.

Let's look at an example, Xiaoyue wrote on the whiteboard:

# 创建一个表tab1 ,设置三个不同类型的字段
mysql> create table tab1(t1 TINYINT, t2 SMALLINT,t3 BIGINT);
Query OK, 0 rows affected (0.04 sec)

mysql> DESC tab1;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| t1    | tinyint  | YES  |     | NULL    |       |
| t2    | smallint | YES  |     | NULL    |       |
| t3    | bigint   | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.01 sec)

# 当插入数值超过的数据类型的范围就会报错。

mysql> insert into tab1(t1) values(-128),(127),(12),(356);
ERROR 1264 (22003): Out of range value for column 't1' at row 4
mysql> 

It can be seen that the 356 here is already out of range, so the direct prompt is out of range.

2. Date and time types

insert image description here

DATE: used to store date values, the format is 'YYYY-MM-DD'.
Example: '2021-09-15'
TIME: used to store time values ​​in the format of 'HH:MM:SS'.
Example: '12:30:45'
DATETIME: Used to store date and time values ​​in the format 'YYYY-MM-DD HH:MM:SS'.
Example: '2021-09-15 12:30:45'
TIMESTAMP: Used to store date and time values ​​in the format of 'YYYY-MM-DD HH:MM:SS'. Similar to DATETIME, but it is automatically set to the current timestamp on insert or update.
Example: '2021-09-15 12:30:45'
YEAR: used to store the year value in the format of 'YYYY'.
Example: '2021'
INTERVAL: A data type used to represent time intervals, which can be used to perform date and time operations.
Example: INTERVAL '5' DAY means a time interval of 5 days.

In a Lightning Deal event, you can use the time type to record and process the event's start time, end time, and participant's order time. The following is an example of using the time type:
The seckill_activity table is used to store seckill activity information

MySQL [school]> create table seckill_activity(
    -> id int ,
    -> name varchar(50),
    -> start_time DATETIME,
    -> end_tim DATETIME
    -> );
Query OK, 0 rows affected (0.01 sec)

3. String type

insert image description here

MySQL [mufenggrow]> create table users(
    -> id int,
    -> name varchar(50),
    -> email varchar(100)
    -> );
Query OK, 0 rows affected (0.00 sec)

MySQL [mufenggrow]> INSERT INTO users (id, name, email)
    -> VALUES (1, 'John Doe', '[email protected]'),
    ->        (2, 'Jane Smith', '[email protected]');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

MySQL [mufenggrow]> 

Of course, if you want to summarize, you can make a mind map:

insert image description here

2. The interviewer's questioning session

After Mu Feng Xiaoyue finished explaining, the interviewer smiled slightly, then nodded slightly, motioned Xiaoyue to sit down, and said slowly: "Next, I will ask you a few questions."

1. What is the difference between char and varchar?

Answer: From three aspects:

  • Storage method: The CHAR type is of fixed length, and it will allocate a fixed-size storage space, regardless of the length of the actual stored string, it will occupy a fixed space. The VARCHAR type is of variable length, it will only allocate the space required for the actual stored string length, and will not waste additional space.

  • Applicable scenarios: Since the CHAR type is of fixed length, it is suitable for storing fixed-length character strings, such as storing zip codes or phone numbers. The VARCHAR type is suitable for storing strings of variable length, such as storing the user's name or address.

  • Storage efficiency: Since the CHAR type occupies a fixed space when stored, it is usually slightly more efficient for reading and storing fixed-length strings. For strings of variable length, VARCHAR is more efficient in storage, because it only allocates the space actually needed and does not waste additional storage space.

in addition:

1.char(n) If the number of stored characters is less than n, fill it with a space, and remove the space when querying. Therefore, the string stored in the char type cannot have spaces at the end, and varchar is not limited to this.

2. char(n) has a fixed length, char(4) will occupy 4 bytes no matter how many characters are stored, varchar is the actual number of characters stored + 1 byte (n<=255) or 2 bytes (n>255),

So varchar(4), storing 3 characters will occupy 4 bytes.

The difference is shown in the figure:
insert image description here

2. What are the floating point types?

Answer: Floating point types mainly include float and double. Floating point types are stored in the database as approximate values, such as float(6,3). If a number 123.45678 is inserted, the actual value stored in the database is 123.457, but the total number It is also based on the actual situation, that is, 6 digits, and the maximum integer part is 3 digits.

Of course, floating-point float and double are not used too much.

insert image description here

3. Do you have any suggestions on the choice of MySQL data type

Answer: When multiple data types can be selected for a column, the number type should be given priority, followed by the date or binary type, and finally the character type. For data types of the same level, the data type with a small footprint should be selected first

numeric type > date or binary type > character type

The interviewer looked at Xiaoyue who was eloquent, pressed the pause button, with a smile on his brow, as if he wanted to accept Xiaoyue as his disciple, "The answer is very good, let's stop here today, and I will take this book The unique knowledge of martial arts in the Yunyuan Growth Manual is given to you, you must study it carefully after you go back."

"Okay, thank you interviewer." Xiaoyue took the book, thanked her, and summoned colorful auspicious clouds, which floated away from the window.

Summarize

Xiaoyue came home with mixed joys and sorrows. She was happy that she finally got an interview right. What she was worried about was that there were so many technology stacks in the cloud-native field. A few questions asked by the interviewer are posted on the blog, which can be regarded as a summary of knowledge

Guess you like

Origin blog.csdn.net/wisdom_futrue/article/details/131302755