Auto Increment Constraint [MySQL] [Database]

auto increment

The role of auto-increment:

is the value of a field that increments

Features of auto-incrementing columns:

  1. There can only be at most one auto-incrementing column in a table
  2. When we need to generate a unique identifier or sequential value, we can set the auto-increment column
  3. The column of the self-growing column constraint must be a key column (primary key or unique key)
  4. The data of the column with the auto-increment constraint must be of integer type
  5. 0 and null cannot be added to the auto-increment column. If you try to add 0 or null to the auto-increment column, then it will auto-increment based on the maximum value of the current auto-increment column, that is, if you try to add 0 or null, Then it will be automatically incremented, just like no addition, but if a specific value other than 0 is manually specified for the auto-increment column, it will be directly assigned to this specific value at this time.

So if you set up an auto-increment column?

Here we also have two ways to set up self-growth columns:

Method 1: Set during CREATE TABLE

CREATE TABLE test7(
id INT PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(15)
);
  • Here we add a primary key constraint to the id field and set id as an auto-incrementing column

Here we illustrate some issues with examples:

INSERT INTO test7(id,last_name)
VALUES(0,'tom');
  • At this time, we try to add 0 to the id field set as an auto-increment column. As we said earlier, 0 and null cannot be added to the auto-increment column. If 0 or null is added, it is considered not to be added at this time, and the default method is used to add it. , that is, the default assignment is performed, and the auto-increment column starts to auto-increment by default from 1. At this time, after we open the table, we can find that the id corresponding to tom in the table is not 0, but 1
INSERT INTO test7(id,last_name)
VALUES(NULL,'tom1');
  • At this time, after we execute it, we can find that at this time, our id field was previously set as the primary key constraint (our auto-increment column can only be set on the key column). At this time, we add null to the primary key constraint field, it stands to reason It cannot be added, and an error will be reported, but at this time we can find that no error will be reported, so why? — This is actually because if we try to add null or 0 in the auto-increment column, it will be added by default. , that is, normal auto-increment, then at this time there is a record with an id of 1, this time it will auto-increment, and then add a null, and will not really add null, so for us, we did not add null, This time it won't go wrong
INSERT INTO test7(id,last_name)
VALUES(-10,'tom3');
  • At this time, there are two records in front of the table, with ids 1 and 2 respectively. If it is a normal self-increment, the next value of id should be 3, but at this time, when we add a new record, we assign the value of id to id. -10, then at this time our auto-increment column as we said before, as long as it is not adding 0 or adding null, it can be added, then at this time we can find that the id corresponding to the tom3 we added is -10
    • And we can also find that the record we added with an id of -10 will go to the position of the first record in the table, because our auto-increment columns are arranged in the order of natural numbers from small to large, at this time -10 Less than 1 and 2, then 10 will become the first record in the table at this time

Note: In actual development, we generally set the field of the primary key constraint as an auto-increment column, and we should pay attention that after adding auto_increment to the primary key constraint, that is, after setting the auto-increment, we do not explicitly set this field. Assignment, let this field grow by default

Method 2: Set during ALTER TABLE

First, let's create a data table

CREATE TABLE test8(
id INT PRIMARY KEY,
last_name VARCHAR(15)
);
  • At this time, we created a table test8, and at this time we added a primary key constraint to the id field in this table

Next, we use an example to illustrate how to set auto-increment during ALTER TABLE

ALTER TABLE test8()
MODIFY id INT AUTO_INCREMENT;
  • Here we set the id field to an auto-increment column by modifying the field

How to delete auto-increment (that is, set to not auto-increment)?

We also delete auto-increment during ALTER TABLE

ALTER TABLE test8
MODIFY id INT;
  • Here we successfully set the id field in the test8 table to not increase automatically (we can find that: we delete the auto increase is actually set to not increase, that is, the field is modified)

Guess you like

Origin blog.csdn.net/m0_57001006/article/details/123587309