Implementation of MySQL Field Auto-Increment

Source: Internet
Author: User

We often use MySQL Field Auto-growth. The following describes the implementation process of MySQL Field Auto-growth in detail. If you are interested in this aspect, take a look.

MySQL field self-growth:

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

Auto_increment is an auto-increment attribute.

How does mysql specify the id and then auto-Increment
Auto_increment = 100;

Enables MySQL to automatically increase field numbers from continuous to continuous

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

If the auto-increment ID (int) of mysql is insufficient, use bigInt instead.
Mysql:
INT [(M)] [UNSIGNED] [ZEROFILL]
A normal integer. The signed range is-2147483648 to 2147483647, And the unsigned range is 0 to 4294967295.
BIGINT [(M)] [UNSIGNED] [ZEROFILL]
A large integer. The signed range is-9223372036854775808 to 9223372036854775807, And the unsigned range is 0

18446744073709551615

Obtain the largest ID from MySQL, select max (id) from tableName;

Implement ID auto-Increment

 
 
  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.  

How MySQL defines foreign keys

New Pricing strategies for MySQL database products

Provides you with an in-depth understanding of MYSQL string connection

View MySQL database table commands

Internal lock of MySQL Server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.