Detailed explanation of MySql data type tutorial examples

Detailed explanation of MySql data type tutorial examples

1. Brief Overview

Why should we open the MySQL learning section? Because this is a necessary skill for a data analyst. What is the most important thing in analyzing data? Of course it’s the data, in that case! In an age of explosive data growth, how can we not learn about databases? In fact, this is what many readers want to see, and it is they who suggested that I write it.

It is difficult for those who don’t know how to do it, and it is not difficult for those who know how to do it! In fact, there are many MySQL summary articles on the Internet. It is undeniable that the knowledge points are very comprehensive, but! This is just a query manual for friends who know how to use the MySQL database. Those who don’t know how to use it still won’t know how to use it (there is no detailed introduction). So I started writing this section, hoping to help those friends who want to change careers and learn MySQL. Of course, I would like to add that these sections are not aimed at those who are developers or want to be DBAs, but at those who want to switch to data analysis and need to learn MySQL database.

We have already shown you how to install the MySQL database. I also believe that this installation tutorial I wrote should be the most detailed tutorial on the entire Internet. If you don’t believe me, take a look.

MySQL Installation Tutorial

Of course, if you are worried about the second installation of MySQL (the first installation failed) at this time, then this MySQL uninstallation article should be of great help to you.

MySQL Uninstallation Tutorial

Although we are not DBAs, it is necessary for us to understand some knowledge other than MySQL addition, deletion, modification and query. For example, why is charset=utf8 added to the table creation statement? What kind of process do we go through from entering an SQL statement to the server returning data to us? You can actually read the following article.

Do you really understand the charset=utf8 encoding problem? 》

2. Detailed explanation of MySQL data types

Anyone who has learned a programming language knows how to learn a programming language? First we need to start learning about data types. Although the database was created by others and the table was designed by others, these seem to have nothing to do with you, but mastering such a knowledge point will definitely help you better understand and learn MySQL.

1) String type

① char(m): fixed-length string.

insert image description here

② varchar(m): a character string of variable length.

insert image description here

The above knowledge can be understood by referring to the following figure.

insert image description here

③ Comparison of space utilization of char and varchar storage.

insert image description here

From the table above we can see that:

insert image description here

④ tinytext, text, longtext

insert image description here

Note: As long as the string type is stored, you must pay attention to the encoding problem. Generally, utf8 encoding is used.

2) Integer Type

insert image description here

Detailed explanation of signed and unsigned bits: https://www.jb51.net/article/178768.htm

insert image description here

① Parameter issues when declaring integer data types

insert image description here

② unsigned parameter.

-- Create table create table person(
    pname varchar(20),
    page tinyint unsigned,
    psex bit(1)
) charset=utf8;

-- Insert two records insert into person
(pname,page,psex)
values
("Zhang San",18,0),
("Li Si",22,0);

Observe the following figure:

insert image description here

As can be seen from the above figure:

insert image description here

③ The zerofill parameter must be used in conjunction with the M parameter to be meaningful.

-- An explanation of the student number field:
-- 1: The student number cannot be negative;
-- 2: Student ID numbers usually have the same number of digits. Even if they are different, they will be padded with 0.
-- eg: 00001, 00013, 00128, 01280.
-- Create table create table student(
    sid smallint(5) zerofill not null default 0,
    sname varchar(20),
    sage tinyint unsigned,
   ssex bit(1) default 0
) charset=utf8;

-- Insert two records insert into student(sname,sid)
values ​​("Zhang Fei",5),("Lü Bu",1);

Observe the following figure:

insert image description here

As can be seen from the above figure:

insert image description here

3) Floating point type

insert image description here

For example:

-- swage represents salary; sbonus represents allowance, and allowance cannot be a negative number.
-- Create table create table salary(
    sname varchar(20),
    swage float(6,2),
    sbonus float(5,2) unsigned not null default 0
) charset=utf8;

-- Insert two records insert into salary
(sname,swage,sbonus)
values
("Ji Xiaolan",9999.99,111.11),
("He Shen",-9999.99,444.44);

Observe the following figure:

insert image description here

From the above figure, we can see that:

insert image description here

① Comparison of float/double and decimal precision

-- Create table create table bank(
    id varchar(20),
    acc1 float(9,2),
    acc2 decimal(9,2)
) charset=utf8;

-- Insert two records insert into bank(id,acc1,acc2)
values
(1,1234567.45,1234567.45),
(2,1234567.678,1234567.678);

Observe the following table:

insert image description here

From the table above we can see that:

insert image description here

4) Date/Time Type

① What are date types and time types?

1) Date type: refers to year, month, and day, similar to 2019-11-16 (November 16, 2019)

2) Time type: refers to hours, minutes, and seconds, similar to 10:45:30 (10:45:30)

② Date/time type

insert image description here

The above is the detailed content of the MySql data type tutorial example. For more information about MySql data types, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Basic knowledge of MySQL database
  • Detailed explanation of the integer data type tinyint in MySQL
  • MySQL data type details
  • MySQL data type selection principles
  • MySQL database operations and data types

<<:  JS generates unique ID methods: UUID and NanoID

>>:  htm beginner notes (must read for beginners)

Recommend

Solution to installing vim in docker container

Table of contents The beginning of the story Inst...

js implementation of verification code case

This article example shares the specific code of ...

Vue realizes click flip effect

Use vue to simply implement a click flip effect f...

Summary of flex layout compatibility issues

1. W3C versions of flex 2009 version Flag: displa...

Analysis of the principle of Mybatis mapper dynamic proxy

Preface Before we start explaining the principle ...

Use PHP's mail() function to send emails

Sending emails using PHP's mail function The ...

Limit input type (multiple methods)

1. Only Chinese characters can be input and pasted...

HTML table tag tutorial (35): cross-column attribute COLSPAN

In a complex table structure, some cells span mul...

Introduction to useRef and useState in JavaScript

Table of contents 1. useState hook 2. useRef hook...

Methods and steps for deploying go projects based on Docker images

Dependence on knowledge Go cross-compilation basi...

zabbix custom monitoring nginx status implementation process

Table of contents Zabbix custom monitoring nginx ...

How to use negative margin technology to achieve average layout in CSS

We usually use float layout to solve the compatib...