数据类型(Data Type)

数据类型
MySQL里有哪些数据类型?
MySQL支持几种类别的SQL数据类型:数值类型,日期和时间类型,字符串(字符和字节)类型,空间类型和JSON数据类型。

创建一个表格

root@nongda 11:30 mysql>create table student_inform(
    -> id int(6) primary key,
    -> name varchar(20),
    -> sex char(1),
    -> birth_date date,
    -> grade decimal(5,2),
    -> address varchar(20),
    -> class varchar(20),
    -> major varchar(20),
    -> college varchar(20),
    -> marry char(1)
    -> );
	Query OK, 0 rows affected (0.01 sec)

数值类型

int,tinyint,smallint,mediumint,bigint,bit,float,double,decimal在这里插入图片描述在这里插入图片描述

日期和时间类型

datetime,date,timestamp,time,year
在这里插入图片描述

root@sanchuang 15:31 mysql>create table t1(name int,brithday date,check_sc timestamp);
Query OK, 0 rows affected (0.01 sec)
root@sanchuang 15:36 mysql>insert into t1(name,brithday,check_sc) values(5,'1984-10-01',now());
Query OK, 1 row affected (0.00 sec)        -------》now()   获得当前时间的函数
root@sanchuang 15:36 mysql>select * from t1;
+------+------------+---------------------+
| name | brithday   | check_sc            |
+------+------------+---------------------+
|    5 | 1984-10-01 | 2020-12-03 15:36:28 |
+------+------------+---------------------+
1 row in set (0.00 sec)

字符串类型

char,varchar,blob,text,enum,set,binary,varbinary
在这里插入图片描述

char和varchar的差别?

char  固定长度的字符串类型    character 字符   --》在存储的时候,不够固定长度,就在前面填充空格,达到固定长度
varchar  可变长的字符串类型  variable character
root@sanchuang 15:50 mysql>create table t1(name char(30));
Query OK, 0 rows affected (0.01 sec)
root@sanchuang 15:50 mysql>insert into t1(name) values('xdd');
Query OK, 1 row affected (0.00 sec)
root@sanchuang 15:51 mysql>insert into t1(name) values('左爷');
Query OK, 1 row affected (0.00 sec)
root@sanchuang 15:53 mysql>select name,length(name),char_length(name) f
rom t1;
+--------+--------------+-------------------+              
| name   | length(name) | char_length(name) |         
+--------+--------------+-------------------+
| xdd    |            3 |                 3 |
| 左爷   |            6 |                 2 |
+--------+--------------+-------------------+
2 rows in set (0.00 sec)

length(name) 统计字符串的存储的字节数
char_length(name) 统计的是字符的个数

枚举类型

ENUM

root@nongda 17:47 mysql>create table t1(
    -> id tinyint,
    -> sex enum('man','woman'),
    -> name varchar(10)
    -> );
Query OK, 0 rows affected (0.01 sec)

root@nongda 17:49 mysql>insert into t1(id,name,sex) values(1,'lhc','man'),(2,'tyl','man'),(3,'pzy','woman');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

root@nongda 17:50 mysql>desc t1;
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id    | tinyint(4)          | YES  |     | NULL    |       |
| sex   | enum('man','woman') | YES  |     | NULL    |       |
| name  | varchar(10)         | YES  |     | NULL    |       |
+-------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

root@nongda 17:50 mysql>select *,length(name) from t1;
+------+-------+------+--------------+
| id   | sex   | name | length(name) |
+------+-------+------+--------------+
|    1 | man   | lhc  |            3 |
|    2 | man   | tyl  |            3 |
|    3 | woman | pzy  |            3 |
+------+-------+------+--------------+
3 rows in set (0.00 sec)

set类型

root@nongda 17:51 mysql>create table t2(mamu 
    -> set('a','b','c','d')
    -> );
Query OK, 0 rows affected (0.01 sec)

root@nongda 18:00 mysql>insert into t2(mamu) values(
    -> 'a,b')
    -> ,('a,c'),
    -> ('a,d'),
    -> ('b,c'),
    -> ('b,d'),
    -> ('c,d')
    -> ;
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

root@nongda 18:02 mysql>select * from t2;
+------+
| mamu |
+------+
| a,b  |
| a,c  |
| a,d  |
| b,c  |
| b,d  |
| c,d  |
+------+
6 rows in set (0.00 sec)
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值