全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 数据分析与数据挖掘
978 0
2020-12-10
面向数据科学初学者的MySQL数据类型指南
数据库无处不在–从社交媒体和体育到电子商务和在线观看的电影,数据库是一切的核心。为了管理,检索,存储和操作这些数据库,我们有数据库管理系统(DBMS)。
市场上有很多DBMS可以管理如此大量的数据,例如MySQL,PostgreSQL,SQLite,Oracle数据库,Apache CouchDB等。每个DBMS都提供了自己的数据类型,并进行了一些修改,但基本思想是相同的。今天,我将介绍MySQL DBMS提供的数据类型。
数据类型MySql
MySQL中的数据类型分为三大类。我将详细介绍每一个,以使您清楚地了解它们,以便您知道在哪里以及如何使用它们。不用再拖延了,让我们开始吧。
目录
什么是数据类型?
MySQL中的数据类型
字符串/字符数据类型
数值数据类型
日期和时间数据类型
什么是数据类型?
顾名思义,数据类型是数据所属的类型或类别。它是数据的属性,它定义对象可以保存的数据类型。在SQL中,数据类型定义要存储在表的每一列中的数据的类型。每列都有其自己的数据类型。重要的是指定所有列的数据类型,以便可以向其中添加相似的值。这意味着一列只能保存一种类型的数据。
在创建表的结构时指定数据类型。例如-
创建 表 table_name(
     column1数据类型,
     column2数据类型,
     column3数据类型,
);
例如,您要创建一个学生表,那么这就是创建它的方式-
创建表格学生(
    Roll_no int,
    名称char(50),
    年龄,
);
在这里,我们指定了Roll_no和Age列将仅保留整数值,而Name列将保留字符值。
MySQL中的数据类型
就像int和varchar一样,我们在示例中看到,MySQL提供了许多类型的数据类型,这些数据类型已分为3大类。让我们研究其中的每一个。
1 – MySQL中的字符串/字符数据类型
Data typeDescription
CHAR(size)Stands for CHARACTER. It can hold a fixed-length string(alphabets
Your string should not exceed the length defined using the size parameter. Although it can be less than the size. If you do not provide any value then the default size is 1.  It will occupy the space in the memory according to the size parameter. For example
VARCHAR(size)Stands for VARIABLE CHARACTER. It can hold a variable-length string. The range of characters can be between 0 and 65
But the memory occupied by the varchar is dependent on the actual size of the string and not on what you have set. For example
BINARY(size)Similar to CHAR()
VARBINARY(size)Similar to VARCHAR()
BLOB(size)BLOB is a Binary Large OBject that can hold a variable amount of data. It can store binary data such as images
It takes 2-byte overhead. It means that BLOB will occupy [the number of Binary values used+2]Bytes in the memory. Each BLOB value is stored using a two-byte length prefix that indicates the number of bytes in the value.
For instance
TINYBLOBIt is a type of BLOB that can hold up to 255 bytes of data. It takes 1-Byte overhead.
MEDIUMBLOBIt is a type of BLOB that can hold up to 16
LONGBLOBIt is a type of BLOB that can hold up to 4
TEXT(size)The TEXT is useful for storing long-form text strings such as articles
It takes 2-byte overhead. It means that TEXT will occupy [the number of character used+2]Bytes in the memory. For instance
TINYTEXTTINYTEXT can store up to 255 characters i.e 255 bytes. It is suitable for storing summaries of articles
It takes 1-Byte overhead.
MEDIUMTEXTMEDIUMTEXT can store up to 16
It takes 3-Bytes overhead.
LONGTEXTLONGTEXT can store the maximum characters among all four
LONGTEXT takes 4-Bytes overhead.
ENUM(val1
This is how you can define ENUM-
CREATE TABLE table_name(
    column_name ENUM(Val1
);
2 – MySQL中的数值数据类型
数据类型描述
INT(尺寸)INT用于存储精确数字。有五种INT类型:TINYINT,INT,SMALLINT,MEDIUMINT和BIGINT(TINYINT的范围最小,而BIGINT的范围最大)。
INT的有符号范围为-2147483648至2147483647,无符号范围为0至4294967295。您可以在列定义中指定有符号或无符号int。所述尺寸参数指定为255的数量的最大长度。
示例:对于unsigned int为4294967294,对于signed int为-1??7826537。
忍者(尺码)TINYINT的有符号范围是-128到127,无符号范围是0到255。这意味着您可以在TINYINT中输入从0到255的数字。您甚至不能在其中存储256个。对于该下一个数据类型,可以使用。
TINYINT的最大显示长度也是255。
示例:254表示无符号int,-116表示有符号int。
SMALLINT(大小)SMALLINT的有符号范围是-32768到32767,无符号范围是0到65535。SMALLINT的最大显示长度也是255。
示例:对于无符号int为65534,对于有符号int为-32423。
MEDIUMINT(size)MEDIUMINT的有符号范围是-8388608至8388607,无符号范围是0至16777215。MEDIUMINT的最大显示长度也是255。
例如:16777214表示无符号int,-8251625表示有符号int。
BIGINT(大小)BIGINT的有符号范围为-9223372036854775808至9223372036854775807,无符号范围为0至18446744073709551615。BIGINT的最大显示长度也为255。
示例:对于unsigned int为18446744073709551614,对于signed int为-90837625537882。
浮点数(p)FLOAT用于存储近似值。MySQL使用p的值来确定对结果数据类型使用FLOAT还是DOUBLE。如果p的值从0到24,则数据类型为FLOAT()。如果p为25到53,则数据类型为DOUBLE()
示例:500.25
浮点数(size,d)数字的长度是使用size参数指定的。小数点后的位数在d参数中指定。浮点数具有8位精度的32位(4字节)。所述d应小于或等于大小(d <=大小)。
例如FLOAT(3
DOUBLE(size,d)float和double的区别在于,当您想获得较高的精度时会使用DOUBLE,因为double具有64位(8字节)的16位精度。使用size参数指定位数的总数。小数点后的位数在d参数中指定。所述d应小于或等于大小(d <=大小)。
示例:502.00232
十进制(size,d)DECIMAL用于在数据库中存储精确的数值。例如,DECIMAL数据类型用于保留精确度的列,例如会计系统中的货币数据。
使用size参数指定位数的总数。小数点后的位数在d参数中指定。但是渔获是size的最大数量为65,d的最大数量为30。您不能超过这些值,并且d应当小于或等于size (d <= size)。为默认值的大小为10,作为默认值d是0。
示例:250.50
BIT(大小)BIT用于存储位值,即仅包含0和1的数字。每个值的位数是使用size参数指定的。的尺寸参数可容纳从1到64的值的默认值大小是1。
示例:1101
布尔/布尔假值存储为零,真值存储为一。该数据类型只有两个值。MySQL不提供内置的布尔数据类型。它改用TINYINT(1)来工作。为了方便起见,MySQL提供TINYINT(1)的同义词为BOOLEAN或BOOL,以便您可以使用它们进行简化。
您可以像这样使用BOOLEAN-
创建表table_name(
   column1_name BOOLEAN,
   column2_name BOOL,
   column3_name TINYINT(1)
);
所有三行代码都相同。
示例:对,错
3 – MySQL中的日期和时间数据类型
Data typeDescription
DATEDATE is one of the five temporal data types provided by MySQL for managing dates. It can store the date in the YYYY-MM-DD format. Dates within the range from ‘1000-01-01’ to ‘9999-12-31’ can be stored. But if you want to store dates apart from this range you can use other non-temporal formats such as INT.
EXAMPLE: 2020-10-24
TIME
TIME is used for storing the time of a day. The time can be stored in the hh:mm:ss format only. Also
EXAMPLE: 09:00:00
DATETIMEIt contains a combination of date and time. The format for storing data and time is YYYY-MM-DD hh:mm: ss. Dates within the range from ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’ can be stored. You can add DEFAULT and ON UPDATE in the column definition to get automatic initialization and updating to the current date and time
EXAMPLE: 2020-10-24 11:55:40
TIMESTAMPTIMESTAMP values are stored as the number of seconds since the Unix epoch (‘1970-01-01 00:00:00’ UTC). The format is similar to the DATETIME as YYYY-MM-DD hh:mm:ss. The supported range for TIMESTAMP is from ‘1970-01-01 00:00:01’ UTC to ‘2038-01-09 03:14:07’ UTC. The difference you can see is that MySQL stores TIMESTAMP in UTC(Universal Time coordinated) value. When you insert a TIMESTAMP value into a table
Automatic initialization and updating to the current date and time can be specified using DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP in the column definition
EXAMPLE: 2020-10-24 11:55:40
YEARYEAR can be used for storing a year in a four-digit format. Values are allowed in the four-digit format: 1901 to 2155
EXAMPLE: 2020
样品表
该表具有所有三种数据类型的组合:
创建表学生(Admission_Id Varchar(10),
                      Tinyint(2)类,
                      Class_Roll_No Int,
                      名字Varchar(50),
                      姓氏Varchar(50),
                      年龄Tinyint,
                      DOB日期,
                      Year_of_Admission Year);
插入学生值('AB121',10,1,'Abhay','Singh',16,'2004-05-11','04');
插入学生值('AB152',10,2,'Aryan','Verma',15,'2005-11-13','2005');
插入学生的价值观('AB233',10,3,'Ayat','Khan',16,'2004-03-10','2006');
SELECT * from Student;
题库
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群