@nalan90
2016-09-07T11:30:05.000000Z
字数 1572
阅读 631
MySQL
MySQL常用基本数据类型
| 数据类型 | 长度 | 范围(signed) | 范围(unsigned) |
|---|---|---|---|
| tinyint | 1 byte | -2^7 ~ 2^7-1 | 0 ~ 2^8-1 |
| smallint | 2 byte | -2^15 ~ 2^15-1 | 0 ~ 2^16-1 |
| mediumint | 3 byte | -2^23 ~ 2^23-1 | 0 ~ 2^24-1 |
| int | 4 byte | -2^31 ~ 2^31-1 | 0 ~ 2^32-1 |
| bigint | 8 byte | -2^63 ~ 2^63-1 | 0 ~ 2^64-1 |
mysql> create table t(id int primary key auto_increment,num int(20) not null default 0);Query OK, 0 rows affected (0.01 sec)mysql> show create table t \G*************************** 1. row ***************************Table: tCreate Table: CREATE TABLE `t` (`id` int(11) NOT NULL AUTO_INCREMENT,`num` int(20) NOT NULL DEFAULT '0',PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin11 row in set (0.00 sec)mysql> insert into t(num) values(100);Query OK, 1 row affected (0.00 sec)mysql> select * from t;+----+-----+| id | num |+----+-----+| 1 | 100 |+----+-----+1 row in set (0.00 sec)修改字段num,添加zerofillmysql> alter table t modify num int(100) zerofill;Query OK, 1 row affected (0.02 sec)Records: 1 Duplicates: 0 Warnings: 0mysql> show create table t \G*************************** 1. row ***************************Table: tCreate Table: CREATE TABLE `t` (`id` int(11) NOT NULL AUTO_INCREMENT,`num` int(100) unsigned zerofill DEFAULT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin11 row in set (0.01 sec)mysql> select * from t \G*************************** 1. row ***************************id: 1num: 00000000000000000000000000000000000000000000000000000000000000000000000000001001 row in set (0.00 sec)
注意事项
| 数据类型 | 长度 | 范围(signed) | 范围(unsigned) |
|---|---|---|---|
| float | 1 byte | -2^7 ~ 2^7-1 | 0 ~ 2^8-1 |
| double | 2 byte | -2^15 ~ 2^15-1 | 0 ~ 2^16-1 |
| decmial | 3 byte | -2^23 ~ 2^23-1 | 0 ~ 2^24-1 |