0

I'm kind of new to MySQL, truncate, and id ... and I'm trying to learn it.

I have a table with 4 objects/rows. I list all of their ID like this.

I wish them to have the id of : 1,2,3,4. But this is what I have right now :

enter image description here

I tried to clear out my table and re-construct it back, but my ID doens't seem to start from 1,2,3 and so forth.

How can I make them start again from 1 every time the database is clear ?

Is there anything that I can tweak on my table, MySQL, or even within my Laravel application to achieve something like this ?

code-8
  • 54,650
  • 106
  • 352
  • 604
  • 2
    `truncate table_name` should do the job – Abhik Chakraborty May 15 '15 at 16:03
  • 1
    http://stackoverflow.com/questions/8923114/how-to-reset-auto-increment-in-mysql you need to reset the auto increment. Less drastic that rebuilding from scratch. – Doon May 15 '15 at 16:03
  • Where/When should I do `truncate table_name` ? – code-8 May 15 '15 at 16:04
  • Use truncate to completely wipe the table. If you just delete a bunch of rows, the index won't reset. And make sure your id is set to Auto Increment. Edit: That's good. – Tim Lewis May 15 '15 at 16:04
  • 1
    @TimLewis : I had mine set to auto increment. – code-8 May 15 '15 at 16:04
  • `ALTER TABLE tablename AUTO_INCREMENT = 1` Is that right ? If I run this, will it wipe out my data ? – code-8 May 15 '15 at 16:06
  • @rangerover.js... just running that will most likely cause conflicts if you haven't removed the rows... – Devon Bessemer May 15 '15 at 16:09
  • 2
    On a side note, try not to worry too much about your indexes... Yes everyone likes 1, 2, 3, 4, etc instead of 1, 3, 6, 9, etc, but in a database, you should just worry about the actual data and not it's indexes (unless you somehow get two of the same index, then there`s a **real** problem). – Tim Lewis May 15 '15 at 16:14
  • Just as @AbhikChakraborty, you should use `truncate table`, docs: https://dev.mysql.com/doc/refman/5.1/en/truncate-table.html – Hackerman May 15 '15 at 16:15
  • If you care about the values, then something has gone very wrong somewhere!!!! – Strawberry May 15 '15 at 16:23

0 Answers0