简体   繁体   中英

How to auto-increment a column in a MySQL table?

Ok so I am making a program in Java where it sends data to a MySQL server where it makes a new account. The table has 4 columns. ID, e-mail, username, and password. I know how to set the values of e-mail, username, and password but I need help setting ID. ID will auto-increment based on the last registered ID in the table.

Example: If the last registered user had an ID of 4 then it will auto-set the new user's ID to 5.

this is the way, you need to set your ID field to auto increment. If you have phpmyadmin you can do it easily on the UI otherwise you can do it on mysql command line.

ALTER TABLE table ADD ID int NOT NULL AUTO_INCREMENT

在MYSQL客户端中编辑Id字段并将其设置为AUTO INCREMENT

确保列ID在数据库中设置为自动递增,然后在插入时指定除ID字段之外的所有其他字段,该字段将自动填充下一个ID。

Like this:

ALTER TABLE Persons AUTO_INCREMENT=100 //use this if you want a certain number to start off from

To insert a new record into the "Persons" table, we will NOT have to specify a value for the "ID" column (a unique value will be added automatically):

INSERT INTO table (email, username, password)
VALUES ('email@domain','unixmiah', 'x007')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM