How to Set auto-increment fields in MySQL

Source: Internet
Author: User
Tags mysql index

Many people have asked how to set an auto-increment field in MySQL. The following describes how to set an auto-increment field in MySQL. I hope you can learn more about the MySQL database.

Related statements for setting auto-increment fields in MySQL:

Alter table album change ALBUM_ID bigint not null auto_increment;
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 int primary key auto_increment;

Note: auto_increment can be used only for int type and primary key.

If hibernate is used as the persistence layer, the identity type is used for the primary key ID.

For the primary key selection of a table, from the perspective of architecture design, it is better to design a business-independent field as the primary key for the system.
Of course, this will inevitably increase the size of the table data. Some people may think this side effect is not worthwhile and choose a field or multiple fields with business significance as the primary key.

But most of the time, the physical storage space added to this field is actually very small, and it is not as terrible as many people think. The logic simplicity is considerable.

In addition, whether to select the auto-increment type as the primary key has its own advantages. If auto-increment is used, the application can ignore the value setting of this primary key, but there is a risk that high concurrency will become a bottleneck. Of course, if the concurrency is not very high, it will not become a bottleneck. If you control this primary key value, you need to pay a little bit to generate this value. The concurrency problem can be solved by extending the application cluster.
 

The preceding section describes how to set auto-increment fields in MySQL.

Mysql inserts an instance of the Clob Field

Default length of common MySql Fields

MySQL index type

How to Set MySql access restrictions

Six Methods for changing the password of MySQL

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.