Get the App
SLTechnology News&Howtos  ›  Database  › 

Mysql self-increasing column

Shulou Source: shulou.com Published: 2022-06-01 07:16:47 11月02日 Update

1. Mysql creates a table with self-increasing columns

Create table inc (id int (10) not null auto_increment,name varchar (20), primary key (id))

Note:

Auto-increment column must be key

There are no quotation marks in the front id, and there can be no quotation marks when the back is set to key

2. Use self-incrementing column

Mysql > insert into inc values (id,'leo')

Query OK, 1 row affected (0.00 sec)

Mysql > select * from inc

+-+ +

| | id | name |

+-+ +

| | 1 | leo |

+-+ +

1 row in set (0.00 sec)

Mysql > insert into inc values (id,'dem')

Query OK, 1 row affected (0.00 sec)

Mysql > select * from inc

+-+ +

| | id | name |

+-+ +

| | 1 | leo |

| | 2 | dem |

+-+ +

2 rows in set (0.00 sec)

You can see that the self-increment column starts from 1 by default.

3. Set self-increment column

If you do not want the increment column to start at 1, you can use the following statement to modify the starting value of the increment

Alter table inc auto_increment=100

Mysql > insert into inc values (id,'jack')

Query OK, 1 row affected (0.01sec)

Mysql > select * from inc

+-+ +

| | id | name |

+-+ +

| | 1 | leo |

| | 2 | dem |

| | 100 | jack |

+-+ +

3 rows in set (0.00 sec)

Tags: Quotation marks time statement face start Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Huawei Redmi Linux Shulou Tech Info