WPDB WP Core Database SQL index, primary keys fix script

Lost your WordPress table indexes, keys and auto_increment fields ? How to re add your primary keys? Lost all that when moving databases?

You could use some searching on the web and and up with a whole lot of manual work copying from pages with outdated database queries. Including guessing/estimating of your next autoincrement value..

We wrote a script that parses the build-in schedule.php from the most current version of WordPress ( @see wp-admin/includes/schema.php ) . We translate it in realtime all to some queries that are up to date. So the output will be up to date for all WordPress versions. It is not a fixed script output for a specific version, it will show the latest version of WP Core.
And .. we even have fixes for you to delete rows where your primary key is lost and the “primary key” values are zero (0). The script will remove those corrupted rows.

The script will give you DELETE and MODIFY/ALTER table queries. It will also enable auto increments.
Warning: this is tested on mysql and mariadb for a single WP Core site (not a multisite), it might not work on your site, so make a backup first.
This will only fix your WordPress Core tables (users, posts etc..) it will not fix any other non-WP-Core tables (such as GravityForms or any other plugin or custom tables).

Call the free fixer script here. You only have to rename the database prefix to wp_ or whatever your table prefix is (see you wp-config.php for that).

Or buy the fixer script here to run it on your WordPress server, that will automatically set your database prefix to YOUR own prefix and it will do an extra check and show you all the tables without any keys.

This script will fix the following database symptoms and errors based on a missing primary key:

  1. PHP errors in your error log: “WordPress database error: [Duplicate entry ‘0’ for key ‘PRIMARY’]”
  2. You trying to re-add the primary key with increment resulting in “#1075 – Incorrect table definition; there can be only one auto column and it must be defined as a key”
  3. Not able to create new pages, all pages have the Publish button replaced with Submit for review. (See Dutch screenshot “Inzenden voor beoordeling”)
    No permalink, just “?preview=true”

26 replies on “WPDB WP Core Database SQL index, primary keys fix script”

Thank you very much. This fixed my media library. I couldn’t upload. setting wp_posts ID field and wp_postmeta meta ID field to auto increment

you are a superhero – this worked like a dream for me after hours researching i came across your comment in stackexchange with this link. Thank you a million time

Thank you soooo much !!!
I’ve lost so much time to find where was the problem, you help me to fix it so quickly.

In Notepad, Ctrl + F, select the prefix and remplace with your database prefix to save more time again.

Have a good day !

Wow – thank you so much. I just wasted hours looking for a solution and was finally able to find this link.
I ran this query using phpmyadmin and it fixed the problem completely – I was not able to upload pictures or create new posts. Thanks again for this solution – I really appreciate it.

P.S. I used Word to find/replace the prefix with wp_

Hi, thanks for your post. Can you tell me is there any diff between your method to fix indexes and

`define( “WP_ALLOW_REPAIR”, true ); // in wp-config.php

and repairing it with native WP repair?

Hi @kreso,

using WP_ALLOW_REPAIR is not the same.

Using WP_ALLOW_REPAIR will (database-whise) only do a mysql ” REPAIR TABLE $table ” , to fix the corrupt files of the database table itself.

Using REPAIR TABLE or even OPTIMIZE TABLE has no knowledge of missing keys nor duplicate zero (0) keys that prevent adding new primary keys or uniques. It will only try to fix
broken files that actually hold your table data on the server disk.

Reference : https://core.svn.wordpress.org/tags/5.5.1/wp-admin/maint/repair.php
Look for these 2 lines and read beyond:
// Loop over the tables, checking and repairing as needed.
foreach ( $tables as $table ) {

If you compare it with the free script: https://wpindexfixer.tools.managedwphosting.nl/wpindexfixer/ you will note a hughe difference.

Hi all,

First thank you for the tips.
Can any one tell how to add them to my website ? i have the same problem but dont know how to resolve it (where to put the queries) please

@Mood Contact your host where to add the queries. Most of the time it will be your WP database wich will be accessible via phpmyadmin.

Thank you for your reply.
I have access to the Myphpadmin platform could you tell me at which level I should make the modification.
My host is very slow and unhelpful.

Thank you so much!

In case it’s useful to anyone .. phpMyAdmin has a config setting to ignore errors. Useful if some of the tables already have some indexes (I had put a few in manually).

I get query errors running the script in PhpMyAdmin; #1067 – Foutieve standaard waarde voor ‘user_registered’.
Is there a fix for this? I would like to use this solution to fix our WP.

Hello/hallo Niels,

this is a database date field, and has nothing to do with the indexes. However, you need to focus on the default values AND the values allready in the database for all dates in the user table, and possibly also in the posts table (4 times).

After you fix that, you will be able to run the index fixer script.

Hello, thanks for the quick reply. Is there a tutorial available for the action you mentioned? I changed some values and got different errors. Sorry, i have no idea what settings to change to what value.

If you manage to set it to 1970-01-01 00:00 or something the like I would guess you will be fine

When we try to activate the plugin, WordPress returns a message saying that it can’t be activated because it caused a fatal error – but there’s nothing in error logs. Any idea?

Did you mean the paid version? You must read the instructions on the site carefully. It is not intended for normal plugin usage, it is a mu-plugin. Instructions are on the site, consult them with your webmaster.

Sorry to come back to this.
It all worked and soleved my issue (blank page when trying to add post/page). However my site is very slow (take a lot of time) when adding a new post, image, or saving settings.

What could be the possible cause of it, and how to go about it???

Slow WP can have many many sources of origin .. You might ask your webhost if the server is having a high serverload for instance.

That’s an interesting article and work.. I have about 350MB of a WordPress DB, consists of 1.6m rows. More than 30k posts.. A big blog.

And although everything works ok, I’d still love to have my WordPress/SQL improved in terms of performance. So, would your re-indexing suggestion adds an extra performance to overall WordPress?

Thank you!

No, this fixer will only repair/add broken Core indexes. Add the ones that should be present in WP Core and perform some other fixes. Every site needs some basic indexes for it to work at all.

Your issue is a *performance* issue. You are better off reducing the amount of plugins or changing theme.
You might even look a different hosting. If you like we can make a demo/test copy of your site at our servers, ask for a free demo at https://www.managedwphosting.nl/jouw-wordpress-website-kan-sneller/

Leave a Reply to damian dauti Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.