MySQL字段自增长的实现

MySQL字段自增长的实现

MySQL字段自增长是我们经常要用到的,下面就为您详细介绍MySQL字段自增长的实现过程,如果您对此方面跟兴趣的话,不妨一看。

MySQL字段自增长:

 
 
  
  
  
  
  1. create table tableName (id unsigned int primary key auto_increment not null,  
  2.  title varchar(32),  
  3.  content text);  

auto_increment就是自增长的属性

mysql如何指定id,然后自增长
auto_increment=100;

让MySQL自增长字段号从不连续变成连续的

 
 
  
  
  
  
  1. ALTER TABLE tablename DROP id;  
  2. ALTER TABLE tablename ADD id INT NOT NULL PRIMARY KEY AUTO_INCREMENT FIRST  

mysql的自增长的ID(int)不够用了,则改用bigInt
Mysql中:
INT[(M)] [UNSIGNED] [ZEROFILL]
一个正常大小整数。有符号的范围是-2147483648到2147483647,无符号的范围是0到4294967295
BIGINT[(M)] [UNSIGNED] [ZEROFILL]
一个大整数。有符号的范围是-9223372036854775808到9223372036854775807,无符号的范围是0到

18446744073709551615

从MySQL中获取最大ID,select max(id) from tableName;

实现ID的自增

 
 
  
  
  
  
  1. public int maxid() throws SQLException  
  2. {  
  3.  stmt = conn.createStatement();  
  4.  rs = stmt.executeQuery("select max(id) from tableName");  
  5.  int maxid = 1;  
  6.  while(rs.next())  
  7.  {  
  8.   maxid = rs.getInt(1) + 1;  
  9.  }  
  10.    
  11.  return maxid;  
  12. }   
  13.  

你可能感兴趣的:(mysql,自增长)