• MySQL中的数据类型


    MySQL官方文档:https://dev.mysql.com/doc/refman/8.0/en/data-types.html

    MySQL中定义数据字段的类型对数据库的优化非常重要。

    MySQL中的数据类型可分:

    字符串类型:分为定长字符串和变长字符串。字符串必须在引号内,通常单引号更好。

    数值类型:分为整数类型和浮点数类型。所有数值类型(除了BIT和BOOLEAN外)都可以有符号或无符号,默认是有符号。

    日期和时间类型:专门用于存储日期和时间值

    二进制数据类型:可用于存储任何数据,如图像、多媒体、字处理文档等


    字符串数据类型

    在MySQL中字符串数据类型又可分为定长字符串和变长字符串。MySQL处理定长列比处理变长列要快的多,且MySQL不允许对变长列(或者一个列的变长部分)进行索引。

    定长字符串接受固定长度的字符串,其长度是在创建表时指定的。

    变长字符串存储可变长度的文本

    字符串类型 说明
    [NATIONAL] CHAR[(M)] [CHARACTER SET charset_name] [COLLATE collation_name] A fixed-length string that is always right-padded with spaces to the specified length when stored. M represents the column length in characters. The range of M is 0 to 255. If M is omitted, the length is 1.
    [NATIONAL] VARCHAR[(M)] [CHARACTER SET charset_name] [COLLATE collation_name]  A variable-length string. M represents the maximum column length in characters. The range of M is 0 to 65,535.
    BINARY[(M)]  The BINARY type is similar to the CHAR type, but stores binary byte strings rather than nonbinary character strings. An optional length M represents the column length in bytes. If omitted, M defaults to 1.
    VARBINARY(M) The VARBINARY type is similar to the VARCHAR type, but stores binary byte strings rather than nonbinary character strings. M represents the maximum column length in bytes.
    TINYTEXT [CHARACTER SET charset name] [COLLATE collation name] A TEXT column with a maximum length of 255 (28 − 1) characters.
    TEXT[(M)] [CHARACTER SET charset name] [COLLATE collation name]  A TEXT column with a maximum length of 65,535 (216 − 1) characters.
    MEDIUMTEXT [CHARACTER SET charset name] [COLLATE collation name]  A TEXT column with a maximum length of 16,777,215 (224 − 1) characters.
    LONGTEXT [CHARACTER SET charset name] [COLLATE collation name]  A TEXT column with a maximum length of 4,294,967,295 or 4GB (232 − 1) characters.
    ENUM('val1','val2',...) [CHARACTER SET charset name] [COLLATE collation name]  An enumeration. A string object that can have only one value, chosen from the list of values 'value1', 'value2', ..., NULL or the special '' error value.
    SET('val1','val2',...) [CHARACTER SET charset name] [COLLATE collation name] A set. A string object that can have zero or more values, each of which must be chosen from the list of values 'value1', 'value2', ...
    [NATIONAL] CHAR[(M)] [CHARACTER SET charset_name] [COLLATE collation_name]  --定长字符串,M指定字符串的长度,范围从0-255,默认是1。
    [NATIONAL] VARCHAR[(M)] [CHARACTER SET charset_name] [COLLATE collation_name]  --变长字符串,M指定字符串的最大长度,范围0-65535,M不能省略。
    BINARY[(M)]  --
    VARBINARY[(M)]  --
    TINYTEXT [CHARACTER SET charset name] [COLLATE collation name]  --
    TEXT[(M)] [CHARACTER SET charset name] [COLLATE collation name]  --
    MEDIUMTEXT  [CHARACTER SET charset name] [COLLATE collation name]   --
    LONGTEXT [CHARACTER SET charset name] [COLLATE collation name]  --
    ENUM('val1', 'val2',...) [CHARACTER SET charset name] [COLLATE collation name]  --
    SET('val1', 'val2', ...) [CHARACTER SET charset name] [COLLATE collation name]  --
    -- 标准SQL通过使用 NCHAR 或者 NATIONAL CHAR 来指明字符串列使用某个预定义好的字符集。MySQL使用utf8作为预定义的字符集。所以以下三种声明方式相同:
    CHAR(10) CHARACTER SET utf8
    NATIONAL CHARACTER(10)
    NCHAR(10)
    -- CHARACTER SET 指定字符集,默认是utf8
    -- COLLATE 即比对方法,用于指定数据集如何排序以及字符串的比对规则。每个CHARACTER SET会对应一定数量的COLLATION。

    数值类型 (整数型、浮点型)

    --整型数值类型
    BIT[(M)]  --M指定数值的位数,即列的宽度,默认是1. 取值范围是1-64,每一位只能存储0或者1。
    BOOL  --等同于TINYINT(1)。0是false,非0是true;但反过来true只能转为1,false只能转为0
    BOOLEAN  --同上
    TINYINT[(M)] [UNSIGNED] [ZEROFILL]  -- M指定数值的位数,即列的宽度,默认是1.当数值实际位数比M多时,默认不会补0,但当指定ZEROFILL时,会补0
    SMALLINT[(M)] [UNSIGNED] [ZEROFILL]  --默认是有符号型,除非指明UNSIGNED
    MEDIUMINT[(M)] [UNSIGNED] [ZEROFILL]
    INT[(M)] [UNSIGNED] [ZEROFILL]
    INTEGER[(M)] [UNSIGNED] [ZEROFILL]  --和INT相同
    BIGINT[(M)] [UNSIGNED] [ZEROFILL]
    --浮点型数据类型
    DEC[(M[,D])] [UNSIGNED] [ZEROFILL]  --M指总位数,不包括小数点,默认是10;D指小数位数,默认是0。
    DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL]  --同上
    NUMERIC[(M[,D])] [UNSIGNED] [ZEROFILL]
    FIXED[(M[,D])] [UNSIGNED] [ZEROFILL]
    FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]
    DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]

     

     日期和时间类型

    DATE  -- 格式“YYYY-MM-DD” 范围从"1000-01-01"到"9999-12-31"
    TIME[(fsp)]  -- 格式"HH:MM;SS"。fsp:fractional seconds percision指定秒的小数位数,默认是0。
    YEAR[(4)]  -- 格式"YYYY",范围从"1901"到"2155"或者"0000"。MySQL8.0不再支持YEAR[(2)]
    DATETIME[(fsp)]  -- 格式"YYYY-MM-DD HH:MM:SS",范围从"1000-01-01 00:00:00"到"9999-12-31 23:59:59"
    DATESTAMP[(fsp)]  -- 格式与DATATIME相同,范围小,从"1970-01-01 00:00:01"UTC到"2038-01-19 03:14:07"UTC

    二进制数据类型

    BLOB: binary large object 二进制大数据,是一个可以存储二进制文件的容器

    二进制类型 说明
    TINYBLOB  A BLOB column with a maximum length of 255 (28 − 1) bytes.
    BLOB  A BLOB volumn with a maximum length of 65,535(216-1) bytes
    MEDIUMBLOB  A BLOB column with a maximum length of 16,777,215 (224 − 1) bytes.
    LONGBLOB  A BLOB column with a maximum length of 4,294,967,295 or 4GB (232 − 1) bytes.

    这四个不同的二进制类型的唯一区别是在存储文件的最大大小上不同。

    数据类型的属性

    MySQL关键字 说明
    NULL 该列可以包含NULL值
    NOT NULL 该列不能包含NULL值
    DEFAULT 为该列设置默认值
    PRIMARY KEY 设置主键列
    AUTO_INCREMENT 自动递增,适用于整数类型
    UNSIGNED 将数值类型设为无符号
    CHARACTER SET name 指定一个字符集

    https://dev.mysql.com/doc/refman/8.0/en/date-and-time-type-overview.html

    https://www.cnblogs.com/-xlp/p/8617760.html

    http://www.runoob.com/mysql/mysql-data-types.html

    http://www.runoob.com/mysql/mysql-delete-query.html

                  

  • 相关阅读:
    关于win10输入法问题(打不出中文)解决方法
    Docker 修改默认存储位置
    Enabling and Mounting NFS on CoreOS
    docker run mysql
    Specified key was too long; max key length is 767 bytes mysql
    C# 实现 Snowflake算法 ID生成
    无忧之道:Docker中容器的备份、恢复和迁移
    IIS Express 虚拟目录
    从零開始学android<AnalogClock与DigitalClock时钟组件.三十一.>
    jquery版本号升级不兼容的问题:$("input").attr("value")功能发生改变
  • 原文地址:https://www.cnblogs.com/0820LL/p/9727153.html
Copyright © 2020-2023  润新知