您的位置:首页 > 数据库 > MySQL

mysql的数据类型int、bigint、smallint 和 tinyint及id 类型变换

2017-07-09 14:55 639 查看
bigint
从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字)。存储大小为 8 个字节。

int
从 -2^31 (-2,147,483,648) 到 2^31 - 1 (2,147,483,647) 的整型数据(所有数字)。存储大小为 4 个字节。int 的 SQL-92 同义字为 integer。

smallint
从 -2^15 (-32,768) 到 2^15 - 1 (32,767) 的整型数据。存储大小为 2 个字节。

tinyint
从 0 到 255 的整型数据。存储大小为 1 字节。

有时MySQL创建表时存储设计过小,可以通过以下等方法查看修改:

1。 创建表的时候定义:
create table test ( id int primary key auto_increment)

2. 创建表的时候指定auto_increment的起始值
create table test(id int primary key auto_incrment) auto_increment = 100;
start with 100.... default is 1;

3.去掉自增属性后,其默认值将变为0
alter table test modify column id int;

4.为字段添加auto_increment属性
alter table test modify column id int auto_increment;

5. 修改字段的初始值
alert table test auto_increment = 200;

6. 怎样查看一个表的auto_increment的下一个自增ID值
我们知道getLastInsertID()属性只是获取插入记录之后的最大ID,并不是我们想要的,所以我们采用
show table status like 'tablename', 里面包含Auto_increment的字段

第二:

use information_schema;
select Auto_increment from tables where table_name = 'table_name';
其中information_schema一般用户无法访问.

7. 修改全局自增参数:
利用 show variables like 'AUTO_INCREMENT% ';
我们将看到
auto_increment_increment, auto_increment_offset, 它们代表全局的起始值和步进,通过如下方式修改:
set auto_increment_increment = 100;
set auto_increment_offset = 10 ;
这将对全局的auto_increment列产生影响,建议慎用!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐