Mysql数据类型

2019-08-04  本文已影响0人  温柔倾怀

整型

用于保存整数,常见的有tinyint,smallint,mediumint,int,bigint


MariaDB [test]> create table people(
    - > name varchar(32) not null default '' comment '用户名', 
    - > age tinyint unsigned not null default 0 comment '年龄'
    - > )charset=utf8 engine=myisam;
Query OK, 0 rows affected (0.013 sec)

关于zerofill的说明

zerofill叫做0填充
举例说明:

MariaDB [test]> create table test(
    -> num int,
    -> num2 int(4) zerofill,
    -> num3 int(4) unsigned zerofill);
Query OK, 0 rows affected (0.014 sec)

当int(4) zerofill 使用,如果添加的整数不够4位,则数值的左边使用0进行填充.

MariaDB [test]> insert into test values(1,23,345);
Query OK, 1 row affected (0.010 sec)

MariaDB [test]> select * from test;
+------+------+------+
| num  | num2 | num3 |
+------+------+------+
|    1 | 0023 | 0345 |
+------+------+------+
1 row in set (0.000 sec)

int(4) 不能理解成最大只能是4位的数,而应该理解成是0填充的宽度

MariaDB [test]> insert into test values(1,23456,34567);
Query OK, 1 row affected (0.004 sec)

MariaDB [test]> select * from test;
+------+-------+-------+
| num  | num2  | num3  |
+------+-------+-------+
|    1 |  0023 |  0345 |
|    1 | 23456 | 34567 |
+------+-------+-------+
2 rows in set (0.000 sec)

当一个字段被zerofill 修饰时,那么这个字段就自动成为unsigned

MariaDB [test]> insert into test values(1,-1,34567);
ERROR 1264 (22003): Out of range value for column 'num2' at row 1
MariaDB [test]> create table test2(id int zerofill,age int);
Query OK, 0 rows affected (0.040 sec)

MariaDB [test]> insert into test2 values(1,18);
Query OK, 1 row affected (0.005 sec)

MariaDB [test]> select * from test2;
+------------+------+
| id         | age  |
+------------+------+
| 0000000001 |   18 |
+------------+------+
1 row in set (0.000 sec)

数值类型-bit

bit类型就是位类型

MariaDB [test]> create table test10(id int,a bit(7));
Query OK, 0 rows affected (0.005 sec)
MariaDB [test]> insert into test10 values(1,64);
Query OK, 1 row affected (0.010 sec)
MariaDB [test]> select * from test10;
+------+------+
| id   | a    |
+------+------+
|    1 | @    |
+------+------+
1 row in set (0.000 sec)
这里有一个困惑:a这个字段,bit位数为7的时候刚好,小了不行
解惑:bit(1) 这里1呢 就是一个bit位,意思是只能有1位来表示你的数据,就只能表示0和1

可见,bit字段在显示时,按照对应的ascii码对应的字符显示

MariaDB [test]> select * from test10 where a=64;
+------+------+
| id   | a    |
+------+------+
|    1 | @    |
+------+------+
1 row in set (0.000 sec)

MariaDB [test]> select * from test10 where a='@';
Empty set, 1 warning (0.001 sec)

数值类型--小数

float(4,2)表示的范围是 -99.99~99.99
float(4,2) unsigned 表示的范围是 0-99.99
decimal(5,2)表示的范围是 -999.99~999.99
decimal(5,2) unsigned 表示的范围是 0-999.99
指定长度,指定小数点位数

数据类型--字符串

MariaDB [test]> create table `user300`( 
    -> id int unsigned not null default 0, 
    -> name varchar(64) not null default '', 
    -> post_code char(6) not null default ''
    -> )charset=utf8 engine=myisam;
Query OK, 0 rows affected (0.013 sec)

MariaDB [test]> insert into user300 values(100,'你好','hello');
Query OK, 1 row affected (0.010 sec)
MariaDB [test]> select * from user300;
+-----+--------+-----------+
| id  | name   | post_code |
+-----+--------+-----------+
| 100 | 你好   | hello     |
+-----+--------+-----------+
1 row in set (0.002 sec)

注意:

char(4)是定长 这里既是插入的是'aa',也是占用分配的4个字节
varchar(4)是变长,如果插入了'aa',实际占用空间大小是L+1
L:实际数据的长度
加的一个字节用来记录长度 

数据类型--时间和日期

date,datetime,timestamp

MariaDB [test]> create table `user901`(
    -> id int,
    -> birthday date,
    -> cardtime datetime,
    -> login_time timestamp
    -> )charset=utf8 engine=myisam;
Query OK, 0 rows affected (0.010 sec)

MariaDB [test]> insert into user901 values(100,'2019-8-3','2019-8-8 8:8:4','2011-1-11 11:22:33');
Query OK, 1 row affected (0.009 sec)

MariaDB [test]> select * from user901;
+------+------------+---------------------+---------------------+
| id   | birthday   | cardtime            | login_time          |
+------+------------+---------------------+---------------------+
|  100 | 2019-08-03 | 2019-08-08 08:08:04 | 2011-01-11 11:22:33 |
+------+------------+---------------------+---------------------+
1 row in set (0.001 sec)

数据类型--枚举enum,集合set

MariaDB [test]> create table `user902`(
    -> id int unsigned not null default 1,
    -> hobby set('A','B','C','D') not null default 'A' comment '选项',
    -> sex enum('男','女') not null default '男' comment '性别'
    -> )charset=utf8 engine=myisam;
Query OK, 0 rows affected (0.009 sec)

MariaDB [test]> insert into user902 values(100,'A','男');
Query OK, 1 row affected (0.000 sec)

MariaDB [test]> insert into user902 values(200,'A,B','男');
Query OK, 1 row affected (0.000 sec)

MariaDB [test]> select * from user902;
+-----+-------+-----+
| id  | hobby | sex |
+-----+-------+-----+
| 100 | A     | 男  |
| 200 | A,B   | 男  |
+-----+-------+-----+
2 rows in set (0.002 sec)
MariaDB [test]> insert into user902 values(300,'C',1);
Query OK, 1 row affected (0.000 sec)

MariaDB [test]> insert into user902 values(400,'D',2);
Query OK, 1 row affected (0.000 sec)

MariaDB [test]> select * from user902;
+-----+-------+-----+
| id  | hobby | sex |
+-----+-------+-----+
| 100 | A     | 男  |
| 200 | A,B   | 男  |
| 300 | C     | 男  |
| 400 | D     | 女  |
+-----+-------+-----+
4 rows in set (0.000 sec)
A    B    C    D
1    2    4    6

7 => 1+2+4

要借助函数
select * from user where find_in_set('A',hobby);
find_in_set('A',hobby) 返回'A'在hobby集合中处于第几位

图片电影音频数据应该如何存放?

通常不会直接存储在数据库汇中,实际开发中存放的是路径地址,然后通过地址去读取。

head_img varchar(64) //存储路径

创建表练习

MariaDB [test]> create table employee(
    -> id int unsigned not null default 0 comment '雇员id',
    -> name varchar(64) not null default '' comment '姓名',
    -> sex enum('男','女','保密') not null default '保密' comment '性别',
    -> birthday date not null comment '生日',
    -> entry_date date not null comment '入职时间',
    -> job varchar(32) not null default '' comment '职位',
    -> salary decimal(10,2) not null default 0.0 comment '薪水',
    -> resume text not null comment '个人介绍'
    -> )charset=utf8 engine=myisam;
Query OK, 0 rows affected (0.004 sec)
MariaDB [test]> insert into employee values(100,'小妖怪','男','2001-10-11','2012-11-11','巡山',2300.00,'大王.我来巡山');
Query OK, 1 row affected (0.001 sec)
MariaDB [test]> insert into employee values(200,'老妖怪','女','1999-10-11','2010-11-11','捶背',4300.00,'我给大王捶背');
Query OK, 1 row affected (0.000 sec)
MariaDB [test]> select * from employee;
+-----+-----------+-----+------------+------------+--------+---------+-----------------------+
| id  | name      | sex | birthday   | entry_date | job    | salary  | resume                |
+-----+-----------+-----+------------+------------+--------+---------+-----------------------+
| 100 | 小妖怪    | 男  | 2001-10-11 | 2012-11-11 | 巡山   | 2300.00 | 大王叫我来巡山        |
| 200 | 老妖怪    | 女  | 1999-10-11 | 2010-11-11 | 捶背   | 4300.00 | 我给大王捶背          |
+-----+-----------+-----+------------+------------+--------+---------+-----------------------+
2 rows in set (0.002 sec)
    登录mysql的时候
    mysql -u root -p --dafault-character-set=latin1
    进入mysql之后 set names gbk;
上一篇下一篇

猜你喜欢

热点阅读