This question already has answers here:
How to reset AUTO_INCREMENT in MySQL
(25 answers)
Closed 8 years ago.
I was testing some data in my tables of my database, to see if there was any error, now I cleaned all the testing data, but my id (auto increment) does not start from 1 anymore, can (how do) I reset it ?
ALTER TABLE `table_name` AUTO_INCREMENT=1
You can also do this in phpMyAdmin without writing SQL.
Click on a database name in the left column.
Click on a table name in the left column.
Click the "Operations" tab at the top.
Under "Table options" there should be a field for AUTO_INCREMENT (only on tables that have an auto-increment field).
Input desired value and click the "Go" button below.
Note: You'll see that phpMyAdmin is issuing the same SQL that is mentioned in the other answers.
ALTER TABLE xxx AUTO_INCREMENT =1;
or
clear your table by TRUNCATE
I agree with rpd, this is the answer and can be done on a regular basis to clean up your id column that is getting bigger with only a few hundred rows of data, but maybe an id of 34444543!, as the data is deleted out regularly but id is incremented automatically.
ALTER TABLE users DROP id
The above sql can be run via sql query or as php. This will delete the id column.
Then re add it again, via the code below:
ALTER TABLE `users` ADD `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST
Place this in a piece of code that may get run maybe in an admin panel, so when anyone enters that page it will run this script that auto cleans your database, and tidys it.
I have just experienced this issue in one of my MySQL db's and I looked at the phpMyAdmin answer here. However the best way I fixed it in phpMyAdmin was in the affected table, drop the id column and make a fresh/new id column (adding A-I -autoincrement-). This restored my table id correctly-simples! Hope that helps (no MySQL code needed-I hope to learn to use that but later!) anyone else with this problem.
Related
I have some tables in my phpmyadmin with one column that is auto incremented.
The problem is that when I delete some rows from the table (For example the element Car, with index auto incremented 1) and I create another row into the table, the new row have the index 2, but in the table there is only one row.
I want that this second element created to have the index equal to the position of the row, for example, if I have 3 rows that the third element will have the index equals to 3.
I was looking for a method that let me to use my phpMyAdmin like this but I couldn't find anything.
Is it possible? If it is true, what should I have to do? Do I have to create the table again?
This is generally a bad idea. Auto increment is used for creating unique ID of the row. Imagine you have a record "1 - John". Then you delete it and add another "1 - Jack". From the point of common database logic, it will seem that John was renamed to Jack (it has the same ID = it is the same entity) rather than it is another record. You should let DB assign new ID to each new record, even with leaving gaps after deleted records.
If you really want to do so, you can modify auto increment value using this query:
ALTER TABLE users AUTO_INCREMENT=123
but it is still not the way auto increment is designed for.
It is possible, but you shouldn't do that on production.
The SQL query is:
ALTER TABLE tablename AUTO_INCREMENT = 1
The most important part of this is to prevent overriding. For example you have an user and this user has id of 81 and if you delete this user and the database doesn't remember that this id 81 has ever been taken by an user (and for example, you have some relations - like friend lists) the user that is going to have the same ID will probably have the same data.
So basically, you don't want to reset auto increment values.
Execute this SQL sentence:
ALTER TABLE tablename AUTO_INCREMENT = 1
I struggled with this a bit too, then found in the "Operations" tab, there is a setting under "Auto Increment" where you can set the number it will start from, so you can roll it back if you want to do that.
It is possible. It is not a feature of phpmyadmin, but of mysql. Execute this
ALTER TABLE tablename AUTO_INCREMENT = 1
More info on this on stackoverflow
And in the mysql reference
This question already has answers here:
LAST_INSERT_ID() MySQL
(14 answers)
Closed 8 years ago.
I didn't find an exact answer so I try to ask here.
I running an SQL INSERT Query and after that I want to use the ID of the
this INSERT Query, I found this: LAST_INSERT_ID() and it's work, but I thinking what would happen if I not the only one who run the query on the server I can get a wrong ID from the DB, so there is other to take the last ID ?
This is both an answer and a clarification of the misconceptions presented elsewhere in this Q&A.
After you do an INSERT into a table with an AUTO_INCREMENT, the value of that AI is waiting for you in that connection; you can fetch it via LAST_INSERT_ID (or whatever synonym your client's API has).
It is tied to the connection, not a transaction.
Being tied to the connection, no other connection can accidentally get the id that you just created.
MAX(id) does run the risk of another connection INSERTing another row, thereby bumping id beyond the value you just inserted. Hence, MAX(id) could give you the wrong value.
If you need to do INSERT..ON DUPLICATE UPDATE, the online manual has a clear example of how to get the id of the row, whether it was INSERTed or UPDATEd. It requires that you the kludgy looking id = LAST_INSERT_ID(id) in the UPDATE part.
The only requirement for an AUTO_INCREMENT be that it is the first column in some index, not necessarily the PRIMARY KEY. (MyISAM has an exception, for which InnoDB has no counterpart.) What you could lose by not saying PRIMARY KEY(id) is the prevention of duplicate ids when you explicitly INSERT the same id again.
It should return the last inserted row id from the same connection. So if other user has inserted another row in between, LAST_INSERT_ID function will still return the relevant idfor the first user.
Quoting from mysql documentation:
The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function(LAST_INSERT_ID) to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client
Here is the link http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id .
you can get last id from your table like this it would be better if id is auto increment
Select max(id) from yourtable
On my website when a new user registers a ID is assigned to them and this ID is the primary key in my PHPMYADMIN database.
It auto_increments by one for each new user added.
Now what I want to know is how can I reset it too one without having to kill the table and bring it back.
Thanks for your support.
EDIT:
at the moment im developing a script so im testing my registration page out.
if you really must:
ALTER TABLE `TABLE_NAME` AUTO_INCREMENT =VALUE
in phpmyadmin you have table option 'operate' or 'operations'.
That's where you can reset the auto_increment value to 0
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get Insert Statement for existing row in MySQL
Lets say we have a table called users:
CREATE TABLE IF NOT EXISTS users(
UID int(11) PRIMARY KEY NOT NULL auto_increment,
fname varchar(100) default NULL,
lname varchar(100) default NULL,
username varchar(20) default NULL UNIQUE,
password blob
)ENGINE=InnoDB DEFAULT CHARSET=utf8
Lets assume there's a few rows filled up in the table.
I know there is a query that returns the creation of the table -> SHOW CREATE TABLE users
But I'm looking to recreate individual insert statements to save them in a log...I know this sounds weird, but I am making a custom CMS where everything is logged, and hopefully where updated/deleted rows could rollback at point in time...therefore I would need to recreate exact insertion query from table data (I have searched all over the web for an answer and can't find it...)
Is there a query that would return "automatically" the query that inserted that specific row by primary key?
I am looking for a query that would do this:
SHOW INSERT FROM users WHERE PRIMARY_KEY=2
Returns:
INSERT INTO users (UID,fname,lname,username,password) VALUES (2,'somename','somelastname','someusername','someAESencryptedPassword')
The reason I'm thinking such a query would exist/be possible is because when you backup a database with php myadmin (cpanel) and you open the file, you can actually view each insert to recreate the table with all rows a that point in time...
There is no such "command" (the data is stored, but not the actual SQL that inserted it), and it wouldn't make sense to do what you're asking. You do realize your "log" would be about 20 times larger (at least) than the actual table and data itself? And it's not going to able to retrieve the INSERT statement without a lot of work to track it down (see the comments below) anyway.
Study transactional SQL, make use of server logging and transactions, and back up the data regularly like you're supposed to and quit trying to reinvent the wheel. :-)
There is no such command to retrieve the original insert statement. However, you can always remake the insert statement based on the existent table structure and data.
The following link may help you, where this has already been asked:
Get Insert Statement for existing row in MySQL
Another possible option is using mysqldump with php to export the data as SQL statements.
I'm working on a script that sadly I inherited - with no commenting or anything. Argh!
For testing purposes I duplicated one of the tables in the database which had an auto-incrementing ID. When the data is saved to the database, though, the ID number just reads "0" -- which is the default for that column. I'm not sure why it's not auto increasing anymore... any thoughts? Thank you!
Are you sure you set the field in the duplicate table to auto-increment? Try running:
ALTER TABLE `duplicate_table` CHANGE `ai_key` `ai_key` INT( key_length ) NOT NULL AUTO_INCREMENT
And see if it is set or not.
Did you create the new table from scratch or as a real copy? If the column is supposed to auto increment it should be a primary (or at least a unique) key, with no default value.
Just to double check use the sql statement to show the show create table syntax for both tables and compare.
show create table <table>
It sounds like your column isn't actually auto_increment any more. This has happened to me a couple of times because there was a bug (?) in phpMyAdmin which I used to create backups: it wouldn't add the auto_increment keyword into the CREATE TABLE statements. That was a massive pain in the butt...