MySQL study notes: data type-field selection

Preface

When learning SQL statements and creating tables, it is indispensable to specify data types for fields.
So what are the data types in the mysql database?
What are the application scenarios of these data types?
Hope this blog is helpful to you!

type of data

Let’s first take a look at the data types from the official website documents. The data types are
Insert image description here
organized as follows:
Insert image description here
There are still many data types. Due to limited space, I will introduce the top three data types, numeric type, date and time type, and string type!

Numerical type

integer

Numeric types include the following:
Insert image description here

Applicable scene

  • tinyint 1 byte, 8 bits, often used for small numbers, unsigned 0-255, signed -128 - 127
  • smallint 2 bytes, 16 bits, unsigned 0-65535, signed -32768 - 32767
  • mediumint 3 bytes, 24 bits
  • int 4 bytes, 32 bits
  • bigint 8 bytes, 64 bits

TINYINT

Example: Create a t2 table, add an id field, the data type is tinyint, and signed signed numbers are used by default -128~127
root@chen 02:44  mysql>create table t2(id tinyint);
Query OK, 0 rows affected (0.02 sec)

root@chen 02:44  mysql>desc t2;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | tinyint(4) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)

root@chen 02:44  mysql>insert into t2(id) values(100);
Query OK, 1 row affected (0.00 sec)

root@chen 02:44  mysql>insert into t2(id) values(130);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
root@chen 02:45  mysql>insert into t2(id) values(127);
Query OK, 1 row affected (0.00 sec)

root@chen 02:45  mysql>insert into t2(id) values(128);
ERROR 1264 (22003): Out of range value for column 'id' at row 1

Example: Create a t3 table, using unsigned numbers 0~256
root@chen 02:45  mysql>create table t3(id tinyint unsigned);
Query OK, 0 rows affected (0.01 sec)

root@chen 02:47  mysql>desc t3;
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id    | tinyint(3) unsigned | YES  |     | NULL    |       |
+-------+---------------------+------+-----+---------+-------+
1 row in set (0.01 sec)

root@chen 02:47  mysql>insert into t3(id) values(130);
Query OK, 1 row affected (0.00 sec)

root@chen 02:48  mysql>insert into t3(id) values(300);
ERROR 1264 (22003): Out of range value for column 'id' at row 1

INT

Example: Create table t4, data type int, add phone number
root@chen 02:53  mysql>create table t4(id int);
Query OK, 0 rows affected (0.01 sec)

root@chen 02:55  mysql>insert into t4(id) values(18174458104);
ERROR 1264 (22003): Out of range value for column 'id' at row 1

The default is signed number, the phone number value is too large, int is not suitable for storing phone numbers.

Example: Create table t5, data type varchar, store phone number
root@chen 02:55  mysql>create table t5(id varchar(20));
Query OK, 0 rows affected (0.05 sec)

root@chen 02:58  mysql>insert into t5(id) values(18174458104);
Query OK, 1 row affected (0.00 sec)

root@chen 03:00  mysql>select * from t5;
+-------------+
| id          |
+-------------+
| 18174458104 |
+-------------+
1 row in set (0.00 sec)

fixed-point decimal

Features: Numerical accuracy

Applicable scene

Often used in auditing, accounting and other scenarios where money cannot make any mistakes

Example

root@chen 03:00  mysql>create table salary(id int(5),name varchar(20),salary decimal(10,2));
Query OK, 0 rows affected (0.02 sec)

root@chen 03:16  mysql>desc salary;
+--------+---------------+------+-----+---------+-------+
| Field  | Type          | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| id     | int(5)        | YES  |     | NULL    |       |
| name   | varchar(20)   | YES  |     | NULL    |       |
| salary | decimal(10,2) | YES  |     | NULL    |       |
+--------+---------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

decimal(10,2), 10 digits in total, 8 digits for integers and 2 digits for decimals, such as 19923124.12

Character type

Applicable scene

Suitable for storing character data

  • char fixed length string
  • varchar variable length string
  • text can store text data
  • blob stores binary text, such as images, videos, and audios
  • enum enumeration, specified options, fixed selection
  • set collection, specify options, the content can only consist of one or more

The difference between char and varchar

Insert image description here

char: character fixed length string

varchar: variable character variable length string, storage will add an extra space at the end.

Storage difference

  1. The character length stored in char (len) brackets is written, and the maximum value is 255. If the actual length of the string stored is less than the length in the brackets, it will be stored with spaces to complete the number of digits. ;
  2. varchar (len), the maximum length value is 65535, and will be stored without space completion;

Get data difference

When char is retrieved, the spaces behind the storage will be removed.

When reading data, varchar and char types will delete automatically filled spaces.

Storage size difference

The char type can store up to 255 bytes

The varchar type can store 65535 bytes

text type

Insert image description here

blob type

Insert image description here

Pictures, videos and audio are generally not stored in blob type. Generally, only a URL or path is stored.

enum enumeration type

When inserting data, fields of enum data type must be selected from the enumeration options.

An is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time. ENUM

root@lianxi 11:10  mysql>CREATE TABLE shirts (
    ->     name VARCHAR(40),
    ->     size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
    -> );
Query OK, 0 rows affected (0.01 sec)

root@lianxi 11:11  mysql>desc shirts;
+-------+----------------------------------------------------+------+-----+---------+-------+
| Field | Type                                               | Null | Key | Default | Extra |
+-------+----------------------------------------------------+------+-----+---------+-------+
| name  | varchar(40)                                        | YES  |     | NULL    |       |
| size  | enum('x-small','small','medium','large','x-large') | YES  |     | NULL    |       |
+-------+----------------------------------------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

root@lianxi 11:11  mysql>INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),
    ->   ('polo shirt','small');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

root@lianxi 11:12  mysql>select * from shirts;
+-------------+--------+
| name        | size   |
+-------------+--------+
| dress shirt | large  |
| t-shirt     | medium |
| polo shirt  | small  |
+-------------+--------+
3 rows in set (0.00 sec)

set set type

There can be zero string objects or more values, each of which must be chosen from the allowed values ​​specified when the table was created. A collection of column values ​​consisting of multiple members specified by members separated by commas ().

root@lianxi 11:21  mysql>CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));
Query OK, 0 rows affected (0.04 sec)

root@lianxi 11:21  mysql>desc myset;
+-------+----------------------+------+-----+---------+-------+
| Field | Type                 | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+-------+
| col   | set('a','b','c','d') | YES  |     | NULL    |       |
+-------+----------------------+------+-----+---------+-------+
1 row in set (0.00 sec)

root@lianxi 11:22  mysql>INSERT INTO myset (col) VALUES   ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

root@lianxi 11:22  mysql>select * from myset;
+------+
| col  |
+------+
| a,d  |
| a,d  |
| a,d  |
| a,d  |
| a,d  |
+------+
5 rows in set (0.00 sec)

date and time type

Applicable scene

  • year year
  • time hours minutes seconds
  • datetime year month day hour minute second
  • timestamp year month day hour minute second
  • date year month day

writing format

Insert image description here

storage

Insert image description here

Common date and time types – timestamp type

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of ‘1970-01-01 00:00:01’ UTC to ‘2038-01-19 03:14:07’ UTC.

timestamp consumes 4 bytes, 2^32, converted into seconds, starting from 1970-01-01 00:00:01, which is exactly until 2038.

scenes to be used

  1. staff attendance
  2. Birthday and horoscope

Example

root@lianxi 11:42  mysql>create table t1(id int,name varchar(20),birth timestamp);
Query OK, 0 rows affected (0.01 sec)

root@lianxi 11:43  mysql>desc t1;
+-------+-------------+------+-----+-------------------+-----------------------------+
| Field | Type        | Null | Key | Default           | Extra                       |
+-------+-------------+------+-----+-------------------+-----------------------------+
| id    | int(11)     | YES  |     | NULL              |                             |
| name  | varchar(20) | YES  |     | NULL              |                             |
| birth | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-------------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)

root@lianxi 11:46  mysql>insert into t1(id,name,birth) values(1,'chenlibiao','2001-12-29 12:34:02');
Query OK, 1 row affected (0.00 sec)

root@lianxi 11:47  mysql>select * from t1;
+------+------------+---------------------+
| id   | name       | birth               |
+------+------------+---------------------+
|    1 | chenlibiao | 2001-12-29 12:34:02 |
+------+------------+---------------------+
1 row in set (0.00 sec)

root@lianxi 11:47  mysql>insert into t1(id,name,birth) values(1,'felix',now());
Query OK, 1 row affected (0.01 sec)

root@lianxi 11:47  mysql>select * from t1;
+------+------------+---------------------+
| id   | name       | birth               |
+------+------------+---------------------+
|    1 | chenlibiao | 2001-12-29 12:34:02 |
|    1 | felix      | 2023-04-19 11:47:37 |
+------+------------+---------------------+
2 rows in set (0.00 sec)

now() function, obtains the current date and time.

Conclusion

Due to limited space, many data types will not be introduced in detail
. Readers in need can check the official website documentation.
Official documentation
https://dev.mysql.com/doc/refman/8.0/en/

Guess you like

Origin blog.csdn.net/qq_57629230/article/details/130432176