简体   繁体   English

如何自动增加MySQL表中的列?

[英]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. 好的,所以我正在用Java编写一个程序,它将数据发送到MySQL服务器,并在其中创建一个新帐户。 The table has 4 columns. 该表有4列。 ID, e-mail, username, and password. ID,电子邮件,用户名和密码。 I know how to set the values of e-mail, username, and password but I need help setting ID. 我知道如何设置电子邮件,用户名和密码的值,但是我需要设置ID的帮助。 ID will auto-increment based on the last registered ID in the table. ID将根据表中最后注册的ID自动递增。

Example: If the last registered user had an ID of 4 then it will auto-set the new user's ID to 5. 示例:如果最后一个注册用户的ID为4,则它将自动将新用户的ID设置为5。

this is the way, you need to set your ID field to auto increment. 这样,您需要将ID字段设置为自动递增。 If you have phpmyadmin you can do it easily on the UI otherwise you can do it on mysql command line. 如果您有phpmyadmin,则可以在UI上轻松完成此操作,否则可以在mysql命令行上执行。

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): 要将新记录插入“人员”表中,我们不必为“ ID”列指定值(将自动添加一个唯一值):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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