From: http://blog.chinaunix.net/space.php? Uid = 13626447 & Do = Blog & cuid = 392987
Usage:
Create Table Test
(
Id int unsigned not null primary key auto_increment,
Username varchar (15) not null
)
Auto_increment = 100;
In database applications, we often use unique numbers to identify records. In MySQL, The auto_increment attribute of the data column can be automatically generated. MySQL supports multiple data tables, and the auto-increment attributes of each data table are different. Here we will introduce the auto-increment attributes of Data columns in various data tables.
When using auto_increment, pay attention to the following points:
Auto_increment is an attribute of a data column. It is only applicable to integer data columns.
-
The data column that sets the auto_increment attribute should be a positive number sequence, so the data column should be declared as unsigned, so that the number of the sequence can be doubled.
-
The auto_increment data column must have a unique index to avoid repeated sequence numbers.
-
The auto_increment data column must have the not null attribute.
-
The maximum number of the auto_increment data column is subject to the data type constraints of this column. For example, the maximum number of the tinyint data column is 127. If unsigned is added, the maximum value is 255. Once the upper limit is reached, auto_increment becomes invalid.
-
When deleting a full table, auto_increment starts numbering from 1. When a full table is deleted, the following two statements are issued:
Delete from table_name; ortruncate table table_name
This is because MySQL optimized the entire table: delete all data and indexes in the data table first, then, recreate the data table. If you want to delete all data rows and retain sequence numbers, you can use a DELETE command with where to suppress MySQL optimization:
Delete from table_name where 1;
This forces MySQL to evaluate a conditional expression for each Deleted Data row.
The method that forces MySQL not to reuse the used sequence value is: create a data table dedicated to generating the auto_increment sequence and never delete the records of the table. When you need to insert a record in the primary data table, First insert a null value in the table that specifically generates the sequence number to generate a number. Then, when inserting data into the primary data table, use the last_insert_id () function to obtain this number and assign it to the data column of the storage sequence of the master table. For example:
Insert into Id set id = NULL; insert into main set main_id = last_insert_id ();
-
You can use the alter command to add a data column with the auto_increment attribute to a data table. MySQL automatically generates all numbers.
-
To rearrange an existing serial number, the simplest way is to delete the column and recreate it. MySQL will regenerate the continuous serial number sequence.
-
Generate a sequence without using auto_increment, you can use the last_insert_id () function with parameters. If you use a last_insert_id (expr) with parameters to insert or modify a data column, and then call the last_insert_id () function without parameters, the second function call returns the value of expr. The following shows the specific operation of this method:
Create a data table with only one data row: Create Table seq_table (ID int unsigned not null); insert into seq_table values (0 ); then, use the following operations to retrieve the serial number:
Update seq_table set seq = last_insert_id (SEQ + 1); select last_insert_id ();
By modifying the constant value in seq + 1, you can generate a sequence with different step sizes. For example, seq + 10 can generate a sequence with a step size of 10.
This method can be used for counters, insert multiple rows in the data table to record different count values. Then, use the return value of the last_insert_id () function to generate the Count values of different content. The advantage of this method is that the unlock table can generate unique sequence numbers without transactions or locks. Normal table operations of other Programs are not affected.