search
HomeSystem TutorialLINUXMariadb learning summary (3): data types

Mariadb learning summary (3): data types

Jul 18, 2024 am 08:15 AM
linuxlinux tutorialRed Hatlinux systemlinux commandlinux certificationred hat linuxlinux video

Data type

Data type-> is an abstraction of a data classification with the same attributes and properties.

For example:
A string is a string composed of characters... In computers, substrings can be divided and new characters can be added at the end of the string. However, such operations can only operate on string data, not on string data. Operate on integers.
Numeric type, the Arabic numerals we are most exposed to, can be used for arithmetic operations, logical operations and other operations

Data types in Mysql

MySQL supports multiple types, which can be roughly divided into three categories: numerical, date/time and string (character) types.

Numeric type

Mariadb learning summary (3): data types

1. For integer types, you can limit their length, the format is as follows:

整数类型[(M)] [SIGNED | UNSIGNED | ZEROFILL]

M is the number of digits in the number. For example, TINYINT(3) can only store three digits, and the number of digits should not exceed the range it can represent
SIGNED: The default is signed number
UNSIGNED: specified as an unsigned number
ZEROFILL: When M bits are not satisfied, the front is filled with 0 and becomes an unsigned number

2. For floating point numbers, the overall number of digits and the number of decimal places can be limited

(FLOAT|DOUBLE)[(M,D)] [SIGNED | UNSIGNED | ZEROFILL]

M is the total number of digits, D is the number of digits after the decimal point
ZEROFILL has the same effect as UNSIGNED for floating point types

MariaDB [mydb]> DESC t1;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| c1    | float(5,2)   | YES  |     | NULL    |       |
| c2    | double(10,3) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

MariaDB [mydb]> INSERT INTO t1 VALUES(23.5,12.34566777);
Query OK, 1 row affected (0.01 sec)

MariaDB [mydb]> SELECT * FROM t1;   //这里可以看到,在其后补了0
+-------+--------+
| c1    | c2     |
+-------+--------+
| 23.50 | 12.346 |
+-------+--------+
1 row in set (0.00 sec)

Date and Time Type

Mariadb learning summary (3): data types

Format abbreviation: Date and time format, in addition to the standard format, it also supports loose formats.

Create a test table with the following structure:

MariaDB [mydb]> desc datetable;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type      | Null | Key | Default           | Extra                       |
+-------+-----------+------+-----+-------------------+-----------------------------+
| c1    | date      | YES  |     | NULL              |                             |
| c2    | time      | YES  |     | NULL              |                             |
| c3    | datetime  | YES  |     | NULL              |                             |
| c4    | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+

For DATE type: YY-MM-DD, YYMMDD, YYYY/MM/DD

MariaDB [mydb]> INSERT INTO datetable(c1) VALUES('2018-01-01'),('18-01-01'),
    -> ('180101'),('2018/01/01'),(180101);
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

MariaDB [mydb]> SELECT c1 FROM datetable;
+------------+
| c1         |
+------------+
| 2018-01-01 |
| 2018-01-01 |
| 2018-01-01 |
| 2018-01-01 |
| 2018-01-01 |
+------------+
5 rows in set (0.00 sec)

For TIME type: 'D HH:MM:SS', 'HH:MM:SS', 'HH:MM', 'D HH:MM', 'D HH', 'SS', 'HHMMSS'
D stands for day, which is TIME+D*24

MariaDB [mydb]> INSERT INTO datetable(c2) VALUES
    -> ('12:20:20'),('1 12:20:20'),
    -> ('12:20'),('1 12'),('20'),('122020'), 
    -> (122020);
Query OK, 7 rows affected (0.03 sec)
Records: 7  Duplicates: 0  Warnings: 0

MariaDB [mydb]> SELECT c2 FROM datetable;
+----------+
| c2       |
+----------+
| 12:20:20 |
| 36:20:20 |
| 12:20:00 |
| 36:00:00 |
| 00:00:20 |
| 12:20:20 |
| 12:20:20 |
+----------+
7 rows in set (0.00 sec)

For the two formats DATETIME and TIMESTAMP, they are the abbreviations of the above time abbreviation format, for example:

20180101122020 -> '2018-01-01 12:20:20'

For the format TIMESTAMP, which is more commonly used, it should be said that it stores the number of milliseconds from '1970-01-01 00:00:00' to the storage time. Its default value can be CURRENT_TIMESTAMP or its synonym: CURRENT_TIMESTAMP() , NOW(), LOCALTIME, LOCALTIME(), LOCALTIMESTAMP,LOCALTIMESTAMP()

String type

Mariadb learning summary (3): data types

CHAR and VARCHAR types are similar, but they are saved and retrieved differently. They also differ in terms of their maximum length and whether trailing spaces are preserved. No case conversion is performed during storage or retrieval.

BINARY and VARBINARY classes are similar to CHAR and VARCHAR, except that they contain binary strings instead of non-binary strings. That is, they contain byte strings rather than character strings. This means that they do not have a character set, and sorting and comparison are based on the numeric value of the column value bytes.

A BLOB is a binary large object that can hold a variable amount of data:
There are 4 BLOB types: TINYBLOB, BLOB, MEDIUMBLOB and LONGBLOB. They only differ in the maximum length they can hold a value.
There are 4 TEXT types: TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT. These correspond to 4 BLOB types, with the same maximum length and storage requirements.

Other types

Enumeration type: ENUM('value1','value2',...)
Storage bytes: Because it stores element numbers, elements 0-255 only occupy one byte, while 255-65535 occupy two elements

The above is the detailed content of Mariadb learning summary (3): data types. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Why is Windows commonly used for desktop computing and gaming?Why is Windows commonly used for desktop computing and gaming?Apr 27, 2025 am 12:01 AM

Windowsispreferredfordesktopcomputingandgamingdueto:1)itsvastsoftwareandgamelibrary,2)user-friendlyandcustomizableinterface,3)extensivehardwarecompatibility,and4)performanceoptimizationcapabilities,despitesomeresource-heavyandupdate-relatedissues.

The Future of Linux Software: Will Flatpak and Snap Replace Native Desktop Apps?The Future of Linux Software: Will Flatpak and Snap Replace Native Desktop Apps?Apr 25, 2025 am 09:10 AM

For years, Linux software distribution relied on native formats like DEB and RPM, deeply ingrained in each distribution's ecosystem. However, Flatpak and Snap have emerged, promising a universal approach to application packaging. This article exami

What are the differences in how Linux and Windows handle device drivers?What are the differences in how Linux and Windows handle device drivers?Apr 25, 2025 am 12:13 AM

The differences between Linux and Windows in handling device drivers are mainly reflected in the flexibility of driver management and the development environment. 1. Linux adopts a modular design, and the driver can be loaded and uninstalled dynamically. Developers need to have an in-depth understanding of the kernel mechanism. 2. Windows relies on the Microsoft ecosystem, and the driver needs to be developed through WDK and signed and certified. The development is relatively complex but ensures the stability and security of the system.

Compare and contrast the security models of Linux and Windows.Compare and contrast the security models of Linux and Windows.Apr 24, 2025 am 12:03 AM

The security models of Linux and Windows each have their own advantages. Linux provides flexibility and customizability, enabling security through user permissions, file system permissions, and SELinux/AppArmor. Windows focuses on user-friendliness and relies on WindowsDefender, UAC, firewall and BitLocker to ensure security.

How does hardware compatibility differ between Linux and Windows?How does hardware compatibility differ between Linux and Windows?Apr 23, 2025 am 12:15 AM

Linux and Windows differ in hardware compatibility: Windows has extensive driver support, and Linux depends on the community and vendors. To solve Linux compatibility problems, you can manually compile drivers, such as cloning RTL8188EU driver repository, compiling and installing; Windows users need to manage drivers to optimize performance.

What are the differences in virtualization support between Linux and Windows?What are the differences in virtualization support between Linux and Windows?Apr 22, 2025 pm 06:09 PM

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

What are the main tasks of a Linux system administrator?What are the main tasks of a Linux system administrator?Apr 19, 2025 am 12:23 AM

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use