Mysql method for setting auto-increment to a field: First open the mysql port; then add the relevant MySQL auto-increment field statement, the code is [alter table album auto_increment=1;].
MySQL method for setting auto-increment fields:
MySQL related statements for setting auto-increment fields:
1、alter table album change ALBUM_ID ALBUM_ID bigint not null auto_increment; 2、alter table album auto_increment=1;
Create:
mysql>create table cc(id int auto_increment,name varchar(20),primary key(id));
Modify:
mysql> alter table cc change id id int primary key auto_increment;
Note: Only int type and primary key can be usedauto_increment
.
If Use hibernate as the persistence layer, then use the identity type for the primary key ID.
For the selection of the primary key of a table, from the perspective of architectural design, it will be better for the system to design a field that has nothing to do with the business as the primary key. Of course, this will inevitably increase the size of the table data. Some people may think that this side effect is not worth it and choose a field (or multiple fields) with business significance as the primary key
But for most of the time, The physical storage space added by this field is actually very small and not as terrible as many people imagine. And the logical simplicity it brings is considerable.
In addition, whether to choose the auto-increment type as the primary key can be said to have its own benefits. If auto-increment is used, the application does not need to care about the value of setting this primary key, but there is a risk of becoming a bottleneck under high concurrency. , of course, if the concurrency is not very, very high, it will generally not become a bottleneck. If you control this primary key value yourself, you need to pay a little more to generate this value. Concurrency problems can be solved by expanding the application cluster.
Related free learning recommendations:mysql database(Video)
The above is the detailed content of How to set auto-increment to field in mysql. For more information, please follow other related articles on the PHP Chinese website!