【MySQL】建表(一):数据类型

建表:

建表过程就是一个画表头的过程(声明字段过程)
存储同样的数据,不同的lie类型所占的空间和效率是不一样的,这就是我们建表前要列类型的意义
所以,列类型的存储与占据的字节关系是学习的重点

一般而言,设某类型N字节
N字节,8N位
0 --> 2^8N-1
-2^(8N-1) ---> 2^(8N-1)-1
对于int类型:占的字节越多,存储的范围也越大

添加一个学分 列
alter table class add score tinyint unsigned not null default 0;

int系列的声明时的参数

(M)unsigned zerofill
zerofill: zero是零,fill是填充,代表0填充
M表示补0宽度,即位数不够,用0填充没有补齐的宽度,并不改变其大小
M必须和zerofill配合才有意义
用zerofill表示属性,则该属性也默认为unsigned类型

int的类型:
  1. tinyint
  2. smallint
  3. mediumint
  4. bigint
浮点类型
  1. float(D,M)
    M叫“精度”-->代表总位数
    D叫“标度”-->代表小数的总位数(即小数点右边的位数)
    float需要声明为unsigned,才能是unsigned
  2. decimal:定点
    float比decimal的精度低
char类型:(定长字符串)

char、varchar分别称为定长、变长类型
char(100)是存储0~100个长度
varchar(100)也是存储0~100个长度

  1. 对于char(N)不够N个长度,用空格在尾部补够N个长度,浪费了空间
  2. 对于varchar(N)不用空格补齐
    ,但列内容前,又1~2个字节来标志该列的长度(前缀码)
    利用率:
    char <= 100%
    varchar < 100%(不可能到达100%)
  3. 用concat连接字符,去看两者的区别:


    char和varchar.PNG
mysql> insert into test values
    -> ('hello','hello');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+-------+-------+
| ca    | vac   |
+-------+-------+
| hello | hello |
+-------+-------+
1 row in set (0.00 sec)

mysql> insert into test values
    -> ('aa ','aa ');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+-------+-------+
| ca    | vac   |
+-------+-------+
| hello | hello |
| aa    | aa    |
+-------+-------+
2 rows in set (0.00 sec)
mysql> select concat(ca,'!'),concat(vac,'!') from test;
+----------------+-----------------+
| concat(ca,'!') | concat(vac,'!') |
+----------------+-----------------+
| hello!         | hello!          |
| aa!            | aa !            |
+----------------+-----------------+
2 rows in set (0.09 sec)

char为定长,当输入长度不足设定的长度时,会用空格去补,而取出来时,会将空格去掉,所以,用concat连接时,char的空格被自动删除了

【注】char(M),varchar(M)限制的是字符,不是字节
即char(2) charset utf8, 储存2 个utf8字符,比如中国
utf8一个字符占3个字节

text:文本类型

一般用来存储文章内容、新闻内容等
声明text时,不必使用默认值

mysql> create table test2 (
    -> artice text not null default ''
    -> );
ERROR 1101 (42000): BLOB/TEXT column 'artice' can't have a default value

所以不必加上not null default '',而是直接使用artice text

blob:二进制类型

用来存储图像、音频等二进制信息
意义:blob防止因为字符集的问题,导致信息丢失
比如一张图中有0xFF字节,这个在ASCII字符集默认 非法,在入库的时候,被过滤了

日期/时间类型
  1. date型,存储 年 月 日
    date存储类型能储存1000-01-01到9999-12-31
mysql> create table class3 (
    -> star varchar(20) not null default '',
    -> birth date not null default '0000-00-00'
    -> )engine myisam charset utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> desc class3;
+-------+-------------+------+-----+------------+-------+
| Field | Type        | Null | Key | Default    | Extra |
+-------+-------------+------+-----+------------+-------+
| star  | varchar(20) | NO   |     |            |       |
| birth | date        | NO   |     | 0000-00-00 |       |
+-------+-------------+------+-----+------------+-------+
2 rows in set (0.12 sec)

mysql> insert into class3
    -> values
    -> ('张国荣','1961-03-12');
Query OK, 1 row affected (0.00 sec)

mysql> select * from class3;
+--------+------------+
| star   | birth      |
+--------+------------+
| 张国荣      | 1961-03-12 |
+--------+------------+
1 row in set (0.09 sec)
  1. time:时间类型,储存具体时间时:分:秒 xx:xx:xx
mysql> alter table class3 add sign time not null default '00:00:00';
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into class3
    -> (star,sign)        ;
    -> values             ;
    -> ('小罗','16:10:33');
Query OK, 1 row affected (0.00 sec)

mysql> select * from class3;
+--------+------------+----------+
| star   | birth      | sign     |
+--------+------------+----------+
| 张国荣      | 1961-03-12 | 00:00:00 |
| 小罗      | 0000-00-00 | 16:10:33 |
+--------+------------+----------+
2 rows in set (0.00 sec)
  1. datetime类型:日期时间类型
    YYY-MM-DD hh:mm:ss
mysql> create table test4 (
    -> sname varchar(20) not null default '',
    -> logintime datetime not null default '0000-00-00 00:00:00'
    -> )engine myisam charset utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> insert into test4
    -> values
    -> ('张三','2009-10-13 16:16:21');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test4;
+-------+---------------------+
| sname | logintime           |
+-------+---------------------+
| 张三      | 2009-10-13 16:16:21 |
+-------+---------------------+
1 row in set (0.00 sec)
  1. timestamp型:
mysql> create table test5 (
    -> ts timestamp default CURRENT_TIMESTAMP,
    -> \c
mysql> create table test5 (
    -> ts timestamp default CURRENT_TIMESTAMP,
    -> id int
    -> )engine myisam charset utf8;
Query OK, 0 rows affected (0.12 sec)

mysql> insert into test5
    -> (id)
    -> values
    -> ('1'),('2'),('3');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from test5;
+---------------------+------+
| ts                  | id   |
+---------------------+------+
| 2019-08-11 16:24:13 |    1 |
| 2019-08-11 16:24:13 |    2 |
| 2019-08-11 16:24:13 |    3 |
+---------------------+------+
3 rows in set (0.00 sec)
  1. year类型:
mysql> insert into test6
   -> values
   -> ('肥水之战',383);
ERROR 1264 (22003): Out of range value for column 'ya' at row 1

超出范围,year类型只占1字节,最多能存256种变化
范围:1901~2155
year能简写成两位,但不推荐这样写(但现在的版本不支持了)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容