MySQL的基本类型

2018-09-06  本文已影响0人  孙子衡

数值类型

1.整数类型

示例代码


mysql> create table tb_num(
    -> n1 tinyint,
    -> n2 tinyint not null,
    -> n3 smallint,
    -> n4 int(4),
    -> n5 int(4) zerofill
    -> );
Query OK, 0 rows affected (0.02 sec)


mysql> desc tb_num;
+-------+--------------------------+------+-----+---------+-------+
| Field | Type                     | Null | Key | Default | Extra |
+-------+--------------------------+------+-----+---------+-------+
| n1    | tinyint(4)               | YES  |     | NULL    |       |
| n2    | tinyint(4)               | NO   |     | NULL    |       |
| n3    | smallint(6)              | YES  |     | NULL    |       |
| n4    | int(4)                   | YES  |     | NULL    |       |
| n5    | int(4) unsigned zerofill | YES  |     | NULL    |       |
+-------+--------------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> show create table tb_num\G;
*************************** 1. row ***************************
       Table: tb_num
Create Table: CREATE TABLE `tb_num` (
  `n1` tinyint(4) DEFAULT NULL,
  `n2` tinyint(4) NOT NULL,
  `n3` smallint(6) DEFAULT NULL,
  `n4` int(4) DEFAULT NULL,
  `n5` int(4) unsigned zerofill DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)


mysql> insert into tb_num values(10,20,30,400000,345);
Query OK, 1 row affected (0.00 sec)

mysql> select *from tb_num;
+------+----+------+--------+------+
| n1   | n2 | n3   | n4     | n5   |
+------+----+------+--------+------+
|   10 | 20 |   30 | 400000 | 0345 |
+------+----+------+--------+------+
1 row in set (0.00 sec)

mysql> insert into tb_num values(200,-1000,900,40000000000000000,345);
ERROR 1264 (22003): Out of range value for column 'n1' at row 1

mysql> insert into tb_num values(50,-1000,900,40000000000000000,345);
ERROR 1264 (22003): Out of range value for column 'n2' at row 1
mysql> insert into tb_num values(50,-50,900,40000000000000000,345);
ERROR 1264 (22003): Out of range value for column 'n4' at row 1

mysql> insert into tb_num values(50,-50,900,40000,346);
Query OK, 1 row affected (0.00 sec)

mysql> select *from tb_num;
+------+-----+------+--------+------+
| n1   | n2  | n3   | n4     | n5   |
+------+-----+------+--------+------+
|   10 |  20 |   30 | 400000 | 0345 |
|   50 | -50 |  900 |  40000 | 0346 |
+------+-----+------+--------+------+
2 rows in set (0.01 sec)
2.定点数(2)
DECIMAL和NUMERIC类型在MySQL中视为相同的类型。它们用于保存必须为确切精度的值。

使用方式如下:
  1. salary DECIMAL(5,2)

    下面的介绍将基于上面这个例子。

    我们看到其中有两个参数,即DECIMAL(M,D),其中M表示十进制数字总的个数,D表示小数点后面数字的位数,上例中的取值范围为-999.99~999.99。

    如果存储时,整数部分超出了范围(如上面的例子中,添加数值为1000.01),MySql就会报错,不允许存这样的值。

    如果存储时,小数点部分若超出范围,就分以下情况:

2.3、浮点数(3)

浮点数是用来表示实数的一种方法,它用 M(尾数) * B( 基数)的E(指数)次方来表示实数,相对于定点数来说,在长度一定的情况下,具有表示数据范围大的特点。但同时也存在误差问题。

如果希望保证值比较准确,推荐使用定点数数据类型。

MySql中的浮点类型有float,double和real。他们定义方式为:FLOAT(M,D) 、 REAL(M,D) 、 DOUBLE PRECISION(M,D)。

REAL就是DOUBLE ,如果SQL服务器模式包括REAL_AS_FLOAT选项,REAL是FLOAT的同义词而不是DOUBLE的同义词。

“(M,D)”表示该值一共显示M位整数,其中D位位于小数点后面。例如,定义为FLOAT(7,4)的一个列可以显示为-999.9999。MySQL保存值时进行四舍五入,因此如果在FLOAT(7,4)列内插入999.00009,近似结果是999.0001。

FLOAT和DOUBLE中的M和D的取值默认都为0,即除了最大最小值,不限制位数。允许的值理论上是-1.7976931348623157E+308~-2.2250738585072014E-308、0和2.2250738585072014E-308~1.7976931348623157E+308。M、D范围如下(MySql5.7实测,与IEEE标准计算的实际是不同的,下面介绍):

字符类型

示例代码

mysql> create table tb_str(
    -> str01 char(5) not null,
    -> str02 varchar(6),
    -> str03 text,
    -> str04 enum('1','0') default '1'
    -> );

mysql> desc tb_str;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| str01 | char(5)       | NO   |     | NULL    |       |
| str02 | varchar(6)    | YES  |     | NULL    |       |
| str03 | text          | YES  |     | NULL    |       |
| str04 | enum('1','0') | YES  |     | 1       |       |
+-------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> show create table tb_str\G;
*************************** 1. row ***************************
       Table: tb_str
Create Table: CREATE TABLE `tb_str` (
  `str01` char(5) NOT NULL,
  `str02` varchar(6) DEFAULT NULL,
  `str03` text,
  `str04` enum('1','0') DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> insert into tb_str values('szh','szh','yuioghjkvbnghjghjvbn','0');
Query OK, 1 row affected (0.00 sec)

mysql> select *from tb_str;
+-------+-------+----------------------+-------+
| str01 | str02 | str03                | str04 |
+-------+-------+----------------------+-------+
| szh   | szh   | yuioghjkvbnghjghjvbn | 0     |
+-------+-------+----------------------+-------+
1 row in set (0.01 sec)


时间和日期类型

示例代码

    -> da01 date,
    -> da02 datetime,
    -> da03 timestamp
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> desc tb_date;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type      | Null | Key | Default           | Extra                       |
+-------+-----------+------+-----+-------------------+-----------------------------+
| da01  | date      | YES  |     | NULL              |                             |
| da02  | datetime  | YES  |     | NULL              |                             |
| da03  | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)
mysql> insert into tb_date(da01,da02) values('2018-08-09',now());
Query OK, 1 row affected (0.00 sec)

mysql> select *from tb_date;
+------------+---------------------+---------------------+
| da01       | da02                | da03                |
+------------+---------------------+---------------------+
| 2018-08-09 | 2018-09-06 20:24:27 | 2018-09-06 20:24:27 |
+------------+---------------------+---------------------+
1 row in set (0.00 sec)

上一篇下一篇

猜你喜欢

热点阅读