DataBases
简介
MariaDB
MySQL
数据类型
创建表
索引 index
主键 primary key
外键 foreign key
数据导入与导出
where条件判断
用户授权与密码
数据备份与恢复
数据热备
主从同步
读写分离
多实例
分库分表
集群-MHA
集群-PXC
存储引擎
本文档使用 MrDoc 发布
-
+
首页
数据类型
#### 常用的SQL命令分类 ```asp DDL:数据定义语言 DML:数据操作语言 DCL:数据控制语言 DTL:数据事物语言 ``` #### 信息种类 ```asp 数值型:体重、身高、成绩、工资 字符型:姓名、工作单位、通信地址 枚举型:兴趣爱好、性别、专业 日期时间型:出生日期、注册时间 ``` #### 字符类型 ```asp char:定长,最大字符个数255,不够指定字符个数时在右边自动用空格补全,字符个数超出时无法写入。 varchar:变长,最大字符个数65532,按照数据实际大小分配空间字符不够不会用空格补,字符个数超出时无法写入 text/blob:大文本类型,字符数大于65535时使用 ``` ```asp create table t2(name char(5),email varchar(30)); #创建字符类型字段,指定字符长度 desc t2; #查看 +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | char(5) | YES | | NULL | | #char字段不指定长度默认为1 | email | varchar(30) | YES | | NULL | | #varchar定长字段必须指定长度否则无法创建 +-------+-------------+------+-----+---------+-------+ insert into t2 values("done","done@gmail.com"); #写入内容 mysql> select * from t2; #查看 +------+----------------+ | name | email | +------+----------------+ | done | done@gmail.com | +------+----------------+ ``` #### 整数类型 有符号-负数,无符号-正数 ![](/media/202405/2024-05-10_114152_0647910.8019810461452354.png) ```asp create table t3 (name char(5), age tinyint); #创建一个tinyint类型不指定范围,使用默认范围 desc t3; #查看 +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | name | char(5) | YES | | NULL | | | age | tinyint(4) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ insert into t3 values("done",-129); #-129超出最大范围无法写入 insert into t3 values("done",-128); #-128可以写入 insert into t3 values("done", 127); #127可以写入 select * from t3; #查看 +------+------+ | name | age | +------+------+ | done | -128 | | done | 127 | +------+------+ create table t4(name char(5),age tinyint unsigned); #tinyint有符号负数,unsigned无符号正数 desc t4; #查看 +-------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+-------+ | name | char(5) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | +-------+---------------------+------+-----+---------+-------+ insert into t4 values("tom",-1); #-1是负数范围无法写入 insert into t4 values("tome",256); #256超出最大范围无法写入 insert into t4 values("tome",0); #0在正常范围可以写入 insert into t4 values("tome",255); #255在正常范围可以写入 insert into t4 values("done",99.99); #写入小数会四舍五入只保留整数 mysql> select * from t4; #查看 +------+------+ | name | age | +------+------+ | tome | 0 | | tome | 255 | | done | 100 | +------+------+ ``` #### 浮点类型 ![](/media/202405/2024-05-10_114817_9815540.8264683619037416.png) ```asp create table t5( name char(5),salary float(7,2)); #创建一个float类型位数为7位,小数点后2位(整数位为7-2) desc t5; #查看结构 +--------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+------------+------+-----+---------+-------+ | name | char(5) | YES | | NULL | | | salary | float(7,2) | YES | | NULL | | #salary最大范围99999.99,最小范围-99999.99 +--------+------------+------+-----+---------+-------+ insert into t5 values("done",99999.99); #99999.99在范围内可以写入 insert into t5 values("done",100000.88); #100000.88不在范围内无法写入 ERROR 1264 (22003): Out of range value for column 'salary' at row 1 insert into t5 values("done",-99999.99); #-99999.99在范围内可以写入 inset into t5 values("tom",12.886); #写入小数后第三位会四舍五入 insert into t5 values("tom",99); #不写入小数位默认会用0补全 select * from t5; #查看 +------+-----------+ | name | salary | +------+-----------+ | done | 99999.99 | | done | -99999.99 | | tom | 12.89 | | tom | 99.00 | +------+-----------+ ``` #### 时间类型 ```asp datetime #存储范围10000-01-01 00:00:00~9999-12-31 23:59:59 格式:yyyymmddhhmmss timestamp #存储范围1970-01-01 00:00:00~2038-01-19 00:00:00 格式:yyyymmddhhmmss date #范围0001-01-01~9999-12-31 格式:yyyymmdd year #范围1901~2155 格式:yyyy time #格式HH:MM:SS ———————————————————————————————————————————————————————————————————————————————————————————————————————— create table t7 (name char(10),your_start year,up_time time,birthday date,party datetime); #创建时间表 desc t7; #查看表结构 +------------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+----------+------+-----+---------+-------+ | name | char(10) | YES | | NULL | | | your_start | year(4) | YES | | NULL | | | up_time | time | YES | | NULL | | | birthday | date | YES | | NULL | | | party | datetime | YES | | NULL | | +------------+----------+------+-----+---------+-------+ insert into t7 values("done",1990,083000,20080808,20090909213045); #写入内容 select * from t7; #查看 +------+------------+----------+------------+---------------------+ | name | your_start | up_time | birthday | party | +------+------------+----------+------------+---------------------+ | done | 1990 | 08:30:00 | 2008-08-08 | 2009-09-09 21:30:45 | +------+------------+----------+------------+---------------------+ create table t8(meeting datetime,party timestamp); #创建表 desc t8; #查看表结构 +---------+-----------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +---------+-----------+------+-----+-------------------+-----------------------------+ | meeting | datetime | YES | | NULL | | | party | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +---------+-----------+------+-----+-------------------+-----------------------------+ insert into t8 values( now(),now()); #获取当前时间写入 insert into t8(meeting) values(20180808080808) #单独写入datetime的值timestamp默认获取当前系统时间 insert into t8(party) values(20190909090909); #单独写入timestamp的值datetime不会写入为空 select * from t8; #查看 +---------------------+---------------------+ | meeting | party | +---------------------+---------------------+ | 2023-02-21 11:09:13 | 2023-02-21 11:09:13 | | 2018-08-08 08:08:08 | 2023-02-21 11:10:20 | | NULL | 2019-09-09 09:09:09 | +---------------------+---------------------+ ``` #### 时间函数 ```asp select curtime(); #当前系统时间 select curdate(); #当前系统日期 select now(); #当前系统时间+日期 select year(now()); #当前年 select month(now()); #当前月 select day(now()); #当前日 select date(now()); #当前年月日 select time(now()); #当前分钟 insert into t7 values("tom",year(now()),curtime(),curdate(),now()); #用时间函数插入数值 select * from t7; #查看 +------+------------+----------+------------+---------------------+ | name | your_start | up_time | birthday | party | +------+------------+----------+------------+---------------------+ | tom | 2023 | 14:50:33 | 2023-02-20 | 2023-02-20 14:50:33 | +------+------------+----------+------------+---------------------+ ``` #### 枚举类型 字段值只能在列举的范围内选择 ```asp enum #单选 set #多选 create table t9(name char(10),sex enum("boy","girl","no"),likes set("eat","drink","game","happy")); #创建字段 desc t9; #查看 +-------+-----------------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------------------+------+-----+---------+-------+ | name | char(10) | YES | | NULL | | | sex | enum('boy','girl','no') | YES | | NULL | | | likes | set('eat','drink','game','happy') | YES | | NULL | | +-------+-----------------------------------+------+-----+---------+-------+ insert into t9 values("bob","man","book,it"); #规定范围外的无法写入 insert into t9 values("bob","boy","game"); #规定范围内的写入 select * from t9; #查看 +------+------+-------+ | name | sex | likes | +------+------+-------+ | bob | boy | game | +------+------+-------+ ```
done
2024年5月11日 11:05
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码