【MySQL】MySQL data type

Table of contents

1. tinyint type

2. bit type

3. Decimal type

1. float type

2. Decimal type

3. String type

1. char type 

2. varchar type

4. Date type

5. enum and set

1. Enumeration and collection type syntax

2. Search for enumeration and collection types

6. find_in_set function

Write at the end:


1. tinyint type

We take the tinyint type as an example to introduce the types of the int series.

1) Let's build a simple table first:

create table if not exists t1(
    num tinyint
);

2) Then we directly observe Sanlian to check this table:

Actions include:

desc t1;
show tables;
show create table t1;

3) Next is the insert operation:

The value range of tinyint is -128 ~ 127, let's try inserting some values ​​now:

insert into t1 values (-128);
insert into t1 values (127);
insert into t1 values (0);
insert into t1 values (1);
insert into t1 values (-1);

Then let's look at the data we just inserted:

select * from t1;

What if we insert an incorrect value:

MySQL will directly report an error and prevent us from inserting.

Next we create another table and create an unsigned type:

create table if not exists t1(
    num tinyint unsigned
);

His data range is 0 ~ 255.

So if we insert a negative number, it will definitely report an error:

2. bit type

1) The old rules, or start from the table:

create table if not exists t3(
    id int,
    online bit(1)
);

2) View table:

3) Insert data:

insert into t3 (id, online) values (123, 1);
insert into t3 (id, online) values (123, 2);

 An error is reported when 2 is inserted, because it only supports one bit:

We can modify his value:

alter table t3 modify online bit(10);

We can see that the modification was successful here:

If we don't include the bit size when we build the table, it defaults to 1.

3. Decimal type

1. float type

 1) As usual, start learning from table creation:

create table if not exists t5(
    id int,
    salary float(4, 2)
);

2) Look at the table:

3) Try to insert data:

insert into t5 (id, salary) values (1, 99.99);
insert into t5 (id, salary) values (1, 199.99);

In the parentheses of the float type, the first one is its number of digits, we chose 4 so the maximum is 4 digits,

The second is his precision, we chose 2 so his decimal place is at most 2. 

It should be noted here that because the precision requires two digits, the integer part can only be two digits.

There is another magical phenomenon here, that is, the accuracy part is calculated according to rounding.

So 99.994 is allowed to insert such numbers.

When we build a table, we can also add unsigned after the float type to make it unsigned.          

If it is the float used by default, it is set by yourself, then it will have a certain loss of precision. (about 7 digits)

2. Decimal type

In use, it is exactly the same as the float type.

But he has a unique advantage over float, so I'll just say it here:

When float stores some relatively large numbers, there will be a problem of loss of precision.

But what decimal stores is what it is.

3. String type

1. char type 

1) Create a table

create table if not exists t8(
    id int,
    name char(2)
);

2) Insert data:

We can know that char (2) means that the longest character length is 2.

Note: The maximum char can only be 255.

2. varchar type

1) Create a table

create table if not exists t7(
    id int,
    name varchar(6)
);

2) insert

It can be seen that the maximum length he can insert is 6. Note that varchar supports up to 21845.

That char and varchar look the same, what is the difference between them?

char is fixed length (opened up at the beginning), varchar is variable length (how much is used, how much is opened)

4. Date type

There are three common date types: date, datetime, timestamp

1) Create a table

create table if not exists t9(
    t1 date,
    t2 datetime,
    t3 timestamp
);

2) View table

Timestamp has a default value, which should be the current timestamp.

3) Insert value

We can see that this is inserted successfully

4) View the insertion result

 t3 automatically displays the current time.

5. enum and set

1. Enumeration and collection type syntax

enum enumeration value is a radio type

The set collection type is a multi-selection type

1) Create a table

create table if not exists t10(
    username varchar(20),
    sex enum('男', '女'),
    hobby set('写代码', '睡觉', '打游戏')
);

2) View table

3) Insert value 

Then:

We can see the value we inserted.

It should be noted here that when you insert at the sex position in the future, you can only insert the enumeration value he gave, and the enumeration value starts from 1, so you can also insert the enumeration value to represent the value we inserted, such as inserting 1 It is to insert a man. 

Let's see if we can insert multiple values ​​into the collection:

We can see that multiple values ​​can be inserted. It should be noted here that if nothing is inserted, it will be NULL, and if 0 is inserted, it will be an empty string, but the numbers in the collection are different from those in the enumeration. We can imagine the 3 values ​​in this collection as A binary 000, the lowest bit represents the first value, so inserting 1 is 001, inserting and writing code:

If the insert is 3, the insert should be writing code and sleeping:

It can be seen that this is indeed the case. 

Summary: Enumerations are subscripts, collections are bitmaps.

2. Search for enumeration and collection types

 Let's first look up according to the enumeration value:

select * from t10 where sex=1;
select * from t10 where sex=2;

The set is actually found like this:

select * from t10 where hobby=1;
select * from t10 where hobby=3;
select * from t10 where hobby=7;

So if we want to filter out, for example, find out all hobbies and sleeping, how should we find it?

Because our search above is a strict match, is there a more flexible way?

6. find_in_set function

In fact, functions can be executed in MySQL:

find_in_set can only find whether an element is in the corresponding set.

This way we can:

select * from t10 where find_in_set('睡觉', hobby);

 Found it successfully.

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/132135402