If you do not want to use MySQL auto-increment, but want to implement the primary key serial number function, you can use the following method to maintain the serial number of multiple tables using a function, simple and practical
1. Create a Data Maintenance table that generates the serial numbers of multiple tables
Create Table seq (
Name varchar (20) not null,
Val int (10) unsigned not null,
Primary Key (name)
) Engine = MyISAM default charset = UTF-8
2. Insert several initialization data entries
Insert into seq values ('one', 100 );
Insert into seq values ('two', 1000 );
3. Create a function to generate a serial number
Create Function seq (seq_name char (20) returns int
Begin
Update seq set val = last_insert_id (Val + 1) Where name = seq_name;
Return last_insert_id ();
End
4. Test
-
- Mysql>SelectSeq('One'), Seq('Two'), Seq('One'), Seq('One');
- +------------ +
-
- | Seq('One')| Seq('Two')| Seq('One')| Seq('One')|
-
- +------------ +
-
- |102|1002|103|104|
-
- +------------ +
-
- 1RowIn Set (0.00Sec)