Python Forum
mysql id auto increment not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mysql id auto increment not working
#1
I deleted ID 2 from mySQL db, but then I have the following to AUTO INCREMENT

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="password",
  database="Users"
)

mycursor = mydb.cursor()
SQL = "ALTER TABLE users MODIFY id INTEGER NOT NULL AUTO_INCREMENT"
mycursor.execute(SQL)
mydb.commit()
myresult = mycursor.fetchall()

for x in myresult:
    print(x)
But when I do this, it displays the database, but its not AUTO INCREMENT, ing. the ID goes from 1-3, it skips 2.
SQL = "SELECT * FROM users"
Output:
('Peter', 'Lowstreet 4', 1) ('Hannah', 'Mountain 21', 3) ('Michael', 'Valley 345', 4) ('Sandy', 'Ocean blvd 2', 5)
Reply
#2
From the documentation: https://www.sqlite.org/autoinc.html
Quote:On an INSERT, if the ROWID or INTEGER PRIMARY KEY column is not explicitly given a value, then it will be filled automatically with an unused integer, usually one more than the largest ROWID currently in use. This is true regardless of whether or not the AUTOINCREMENT keyword is used.

If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY, that changes the automatic ROWID assignment algorithm to prevent the reuse of ROWIDs over the lifetime of the database. In other words, the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs from previously deleted rows.
As you can see, the purpose of AUTOINCREMENT is to prevent reusing a key value, even for keys that are no longer in the table.
Reply
#3
ok, I thought AUTO_INCREMENT will 'auto increment'. Is that because I deleted the record by id, and not something like a name?

So there's no way to reset so it will be 1,2,3,4,5?
Reply
#4
Because I deleted it by name, WHERE name = 'Michael', and commit.

But now its deleted 4,

Output:
('Peter', 'Lowstreet 4', 1) ('Hannah', 'Mountain 21', 3) ('Sandy', 'Ocean blvd 2', 5)
Reply
#5
Why do you want to reassign id's when you delete a record? What is id supposed to represent? If you reassign id each time you modify the database it makes id meaningless.
Reply
#6
I'm learning mySQL, how do I reassign all the id keys so they're in order?
Reply
#7
Reassigning key values so they are sequential is not something anyone would do. The purpose of a key is to be a unique value that can be used to reference a paricular row, not to be a counter.
Reply
#8
ok thank you
Reply
#9
Everything might make more sense if you don't think of a table as something that has rows and columns. A table is a collection of records where each record has the same fields (row = record, column = field). The order of the records in the table is not meant to have any meaning.
Reply
#10
Thank you again, makes sense
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 692 Oct-03-2023, 10:25 PM
Last Post: lostintime
  help to increment a third list hermine 7 1,356 Nov-29-2022, 04:19 PM
Last Post: perfringo
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,143 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  Character Increment AnokhiRaaz 1 2,513 Apr-22-2021, 04:29 AM
Last Post: buran
  Increment text files output and limit contains Kaminsky 1 3,213 Jan-30-2021, 06:58 PM
Last Post: bowlofred
  Increment formula Kristenl2784 4 2,902 Jul-20-2020, 10:14 PM
Last Post: Kristenl2784
  [openpyxl] Increment cells being pasted into Template Kristenl2784 4 3,594 Jul-16-2020, 10:00 PM
Last Post: Kristenl2784
  How can I increment a List item with in a "for in" msteffes 4 3,580 Aug-14-2019, 08:55 AM
Last Post: DeaD_EyE
  How define iteration interval increment SriMekala 5 4,376 Jun-01-2019, 01:06 PM
Last Post: ichabod801
  SQlite3 quickly increment INT value? jmair 1 2,459 Mar-04-2019, 08:03 PM
Last Post: stranac

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020