Home > Database > Mysql Tutorial > body text

How to set the primary key and auto-increment of data table in mysql

coldplay.xixi
Release: 2020-10-19 10:58:03
Original
7335 people have browsed it

Mysql sets the primary key and auto-increment method of the data table: first start MySQL, open navicat to create a new table; then add fields and types; then execute relevant statements to insert data; finally select the field, and click below Put a check mark in front of Auto Increment to auto-increase.

How to set the primary key and auto-increment of data table in mysql

##More related free learning recommendations: mysql tutorial(Video)

Mysql sets the primary key and auto-increment method of the data table:

1. Start MySQL, open navicat, and create a new database aaa, right-click Tables, select new table, and create a new table.

How to set the primary key and auto-increment of data table in mysql

#2. Add field xsid, type is int, add field xsname, type is vachar. As shown in the figure, select the field xsid, and set this field as the primary key by clicking Primary Key, or at position 2 in the figure. Save the table as xstable.

How to set the primary key and auto-increment of data table in mysql

#3. Open the table xstable, create a new query, and enter the sql statement in the picture. After successful execution, a piece of data will be inserted into the table.

How to set the primary key and auto-increment of data table in mysql

#4. Set the xsid value in sql to empty. The first execution of the statement can be successful. The xsid is stored as 0 by default. When executing the second time, an error is reported. As shown in the picture. It can be seen that if the specific value of xsid cannot be set, the execution cannot be successful.

How to set the primary key and auto-increment of data table in mysql

5. Open the design interface of the table xstable, select the xsid field, and put a check in front of Auto Increment below to select automatic growth.

How to set the primary key and auto-increment of data table in mysql

#6. On the query page, execute the sql statement with an empty xsid again. The execution will be successful and can be executed multiple times. Open the xstable table, check the data, and find that multiple pieces of data have been successfully inserted, and the xsid value automatically increases by 1 each time. After setting the auto-increment, SQL can also ignore the xsid field and directly write it in the form of Figure 3.

How to set the primary key and auto-increment of data table in mysql

#7. Open the design interface of table xstable. The value set by Auto Increment in the figure is the initial value of automatic growth.

How to set the primary key and auto-increment of data table in mysql

The above is the detailed content of How to set the primary key and auto-increment of data table in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template