Suppose i have a database that store report from employees which first column is reportID(auto increment), staffID,name,department and so on. Everytime i submit i report from my php page it will display increment number (reportID) in my php page. So lets say i got 3 report ID stored in the table, i want to delete three record, and then add 1 new record to the table, but now the reportID display as 4 because the past three reportID are 1,2,3, so how do i reset the new record to 1 instead of 4? is there a code to reset it? This is my deletereport.php In this example i am going to explain How to reset AUTO INCREMENT to one in MySQL PHP. There is many way to reset AUTO_INCREMENT to 1in MySQL Using SQL QueryRun the query in your database. ALTER TABLE tablename AUTO_INCREMENT = 1; Example:ALTER TABLE student_data AUTO_INCREMENT = 1; Using PHP codeindex.php<?php $host='localhost'; $username='root'; $password=''; $conn=mysqli_connect($host,$username,$password,"student"); mysqli_query($conn,"ALTER TABLE category AUTO_INCREMENT = 1"); echo "Auto_increment reset to 1 Successfully ! "; ?> This MySQL tutorial explains how to reset sequences using the AUTO_INCREMENT attribute in MySQL with syntax and examples. DescriptionYou can reset the next value assigned by the AUTO_INCREMENT at any time using the ALTER TABLE statement in MySQL. SyntaxIn MySQL, the syntax to reset the AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = value;table_nameThe name of the table whose AUTO_INCREMENT column you wish to reset.valueThe next value that will be used in the AUTO_INCREMENT column. ExampleLet's look at an example of how to reset the next value assigned to an AUTO_INCREMENT column in a table in MySQL. For example, if we had a suppliers table that was defined as follows: CREATE TABLE suppliers ( supplier_id INT(11) NOT NULL AUTO_INCREMENT, supplier_name VARCHAR(50) NOT NULL, account_rep VARCHAR(30) NOT NULL DEFAULT 'TBD', CONSTRAINT suppliers_pk PRIMARY KEY (supplier_id) ); We could reset the next value in the sequence for the supplier_id field (which is the AUTO_INCREMENT field in the suppliers table) with the following ALTER TABLE statement: ALTER TABLE suppliers AUTO_INCREMENT = 1; This example would change the next value in the AUTO_INCREMENT field (ie: next value in the sequence) to 1 for the supplier_id column in the suppliers table. Now when a new record is inserted into the suppliers table, the supplier_id column will be assigned the value of 1 in the newly created record. TIP: Because there can only be one AUTO_INCREMENT field in a table, you only need to specify the table name (and not the field name) in the ALTER TABLE statement when resetting the next value in the sequence. phpmyadminPerhaps you could just select the phpMyAdmin Operations tab:
Since this one of the most frequently asked questions for phpmyadmin, you can learn more about this in this blog : http://trebleclick.blogspot.com/2009/01/mysql-set-auto-increment-in-phpmyadmin.html Supplemental InfoFor an empty table, another way to reset the auto_increment attribute is to run
Don't run this if you have data in it. If you want to hose the data, then be my guest. In phpmyadmin,
just click the For a nonempty table, you may want to adjust the auto_increment attribute to the highest existing id in use in case higher entries were deleted. First, optimize the table
Next, locate the highest value for the auto_increment column (say it is
Suppose the answer returns 27. Goto the Operations tab and enter 28. |