@nalan90
2016-09-07T19:30:05.000000Z
字数 1572
阅读 489
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: t
Create 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=latin1
1 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,添加zerofill
mysql> alter table t modify num int(100) zerofill;
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> show create table t \G
*************************** 1. row ***************************
Table: t
Create 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=latin1
1 row in set (0.01 sec)
mysql> select * from t \G
*************************** 1. row ***************************
id: 1
num: 0000000000000000000000000000000000000000000000000000000000000000000000000000100
1 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 |