How to configure the structure of a mysql database - mysql

Is it possible to restore the previous state of a mysql database through phpmyadmin?
I have one column in 4 of the tables that have the auto increment.
And my problem is how to begin again in the count of number 1 when I try to add a data.
I tried deleting all the records then add a record but it doesn't start with 1.
How do I do this without building the database over again by typing and selecting the data types.
What I want to do is to start the counting from 1 again. Is it possible?

Open phpMyAdmin and then select the DB you wish to change from the top left panel.
Now click the "query" button also top left to open a query window where you can then run the sql code below to reset the auto increment count for your table.
ALTER TABLE your_table_name AUTO_INCREMENT=1

If you dont care about the data in the table you can just hit the empty button (make sure you are on the right table). That will tuncate your table and anything you add after that will start at 1. If you set a field to auto increment then each item after that will increase by 1.

Have you tried ...
ALTER TABLE t2 AUTO_INCREMENT = 1;
It will reset the value to the smallest value allowed. Other than that you are out of luck.

Related

create a new table with auto_increment of 10 each time

how do you create a table with an auto_increment_increment of 10 instead of the default of 1
I am using mySQL and mySQL workbench as well.
After creating the table either with the workbench gui or by statements, I have tried this in workbench and it works only when I add a new record from the workbench but not thru my web app. If I use the web app it starts to auto increment by one again...I just want to create the table and set its increment to 10 every time so first item is 10 then second is 20 then 30 and so on.
SELECT Auto_increment FROM information_schema.tables WHERE table_name='items';
SET ##auto_increment_increment=10;
Thanks.
You would need to set the auto_increment_increment=10 globally. The SET command you show only sets it for the current session.
Setting it globally makes it affect every table, not just your items table.
There's no support in MySQL for changing the increment size to a different value for each table.
You can change a global option in MySQL with SET GLOBAL. To make the change persist when the MySQL server is restarted, you must edit the options file. Read:
https://dev.mysql.com/doc/refman/5.6/en/using-system-variables.html
https://dev.mysql.com/doc/refman/5.6/en/option-files.html

Resetting data in MySql database

I am faced with a scenario where I would like to remove all existing data from one of my tables and then reuse the table again and fill it with new data.
My Question: Should I destroy/recreate the exact same table to do this? Or is there an easier way? -Side Note: I am storing user id's and would like for the id's to be set back again as opposed to continuing at the number in which last data was stored.
I'm using PhpMyAdmin.
If you use TRUNCATE TABLE.
TRUNCATE TABLE empties a table completely. Logically, this is equivalent to a DELETE statement that deletes all rows, but there are practical differences under some circumstances.
...
it resets any AUTO_INCREMENT counter to zero
So if you want to keep the counter use DELETE FROM
If you delete all rows in the table with DELETE FROM tbl_name (without a WHERE clause) in autocommit mode, the sequence starts over for all storage engines except InnoDB and MyISAM.
You can also look here: How to prevent mySQL from resetting auto increment value?
You have to truncate your table, see documentation here: https://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
In phpmyadmin there's a button in the table list called: Empty

Setting an auto-incrementing field back to its original

I have a SQL database with a table inside called members and inside that there are some columns, one being an ID which auto-increments.
However I have done a few tests, and the auto-increment does work. But even after deleting the tests the auto-increment will not start from 0 again.
How do I make it will start back from 0 rather than carry on from about 17 or something...
EDIT:
I have worked out the answer:
In the "Operations" tab in phpMyAdmin there is a section called Table Options.
In there you can edit where the auto-increment continues from.
Assuming You're using MySQL:
To reset the next value of *auto_increment* column, you need to use ALTER TABLE statement in the following form:
ALTER TABLE my_table AUTO_INCREMENT=123
(Where "123" is the new next value)
If u are using postgresql u have following sql statement to alter sequence
ALTER SEQUENCE table_name_id_seq RESTART WITH 1
for example, if u have table called users, then u should do
ALTER SEQUENCE users_id_seq RESTART WITH 1
where 1 is the new sequence.

AUTOINCREMENT not automatically adding values in Mysql Workbench IDE

I am learning to use MYSQL-Workbench for inserting data into tables
My Problem::
I have two columns one for Sl_no and one for Name
Sl_no is the primary key which has auto increment checked as shown in
snapshot-1
If you look at Snapshot-2 auto_increment is not automatically
incremented when i keep adding the values
should i need to enable any option for this ?
Because Using the IDE should automatically add values(Since Sl_NO has
autoincrement enabled) but it is not doing so
I have to manually add it one after another
Snapshot-1
Snapshot-2
How can i resolve this ?
{EDIT}
I am using Windows-7 OS
As you can see i am clicking the marked area in the pic to apply
changes, but nothing is happening.
Should i need to enable any settings
UPDATE: It wasn't apparent right away that you're doing this from model and not directly in your db table.
To create your schema from model you need to do Forward Engineering and in the dialog choose Generate INSERT Statements for Tables.
Then go to the object browser in your MySQL connection, not your model. Fire select statement in query window or choose from context menu Select Rows - LIMIT 1000 and you'll see that your sl_no column is populated as expected.
BTW both mac and windows interfaces are identical.
Original answer:
You forgot to commit your changes by clicking Apply
And after reviewing the change script click Apply again.
After that you'll see your auto_increment column values populated

auto increment not starting from 1

I am inserting some data to a database and I have an id column which has auto increment. I updated my xampp server yesterday and now the auto increment is starting from 4, 3, 5 in different tables. It used to work fine before. I did not delete any rows from the table it just starts from those numbers. What is wrong?
ALTER TABLE tablename AUTO_INCREMENT = 1
This will reset your auto increment to start from 1
If you really want to reset this, in phpMyAdmin, open this table, go to Operations and change the value for AUTO_INCREMENT.
You can reset your auto increment ID from the following Command:
ALTER TABLE TABLE_NAME AUTO_INCREMENT = 1;
This will start from 1.
OR
go to LOCALHOST/PHPMYADMIN enter:
select your DB
after choose the specific table than go to Operations an set a number to the Auto increment.
If you really want to reset this, in phpMyAdmin, open this table, go to Operations and change the value for AUTO_INCREMENT.----- this is working for me, i have just done this,
This can be a consequence of using a Galera cluster. By default, it will automatically adjust the auto_increment_increment and auto_increment_offset variables according to the size of the cluster, and when the cluster size changes. This avoids replication conflicts due to auto_increment.
The short answer is not to depend on auto_increment numbers being subsequent, without gaps.