

I'm a little short on time today, so please read the page I linked to for more information.Īlso, remember that if you're using a framework like Spring, it will probably have convenience functions that you can use to get the generated key from an auto_increment field. LAST_INSERT_ID() (with no argument) returns the first automatically generated value that was set for an AUTO_INCREMENT column by the most recently executed INSERT statement to affect such a column. MySQL automatically assigns the number automatically and explicitly, ranging from 0 to the number of generated column sequences. To get the generated key from a MySQL database table, just use the MySQL LAST_INSERT_ID function, calling it as shown below, immediately after performing your INSERT command (and very importantly, also using the same connection): In the above example, there exists no specified value for the AUTOINCREMENT column. (Some databases also refer to this as an identity field.) MySQL reset auto increment value examplesįirst, create a table named tmp and assign the AUTO_INCREMENT attribute to the id primary key column.Here's a quick look at how to get the generated key from a MySQL database table after performing a SQL INSERT statement on a table that has an auto_increment field. In MySQL, you can reset auto increment values in various ways. Sometimes, you may need to reset the value of the auto-increment column so that the first record’s identity that you insert into the table starts from a specific number e.g., 1. Whenever you insert a new row into a table, MySQL automatically assigns a sequence number to the AUTO_INCREMENT column.įor example, if the table has eight rows and you insert a new row without specifying the value for the auto-increment column, MySQL will automatically insert a new row with id value 9. Typically, you use the AUTO_INCREMENT attribute for the primary key column of the table. 1 row inserted, or 2 rows updated, or 0 rows affected).


int executeUpdate (String sql): executes an INSERT, UPDATE or DELETE statement and returns an update account indicating number of rows affected (e.g. The following SQL statement defines the 'Personid' column to be an auto-increment primary key field in the 'Persons' table: To let the. This method can be used with aStatement only. By default, the starting value for AUTOINCREMENT is 1, and it will increment by 1 for each new record. You can assign the AUTO_INCREMENT attribute to a column of a table to generate a unique identity for the new row. MySQL uses the AUTOINCREMENT keyword to perform an auto-increment feature. MySQL provides you with a useful feature called auto-increment. Summary: in this tutorial, we will show you various ways to reset auto-increment values of AUTO_INCREMENT columns in MySQL.
