Nội dung
In this article, AZDIGI will show you to check and fix the error of Table Database missing Primary Key in Database with WP-CLI.
I. OVERVIEW
As you all know, all the data of a website will be stored in Database, Database is very important for the website and it is the property of the website. When your website fails, you can redesign it from anywhere, but Database is damaged or lost, it cannot be recovered if you don’t have a backup or the error is too heavy to handle.
As the picture below is an illustration of the error that there is no primary key for the column in the Database table, AZDIGI will show you how to check and solve this problem in today’s article.
When the primary key is lost, the “Edit”, “Copy”, “Delete” option buttons no longer appear and you cannot do anything on this table. Below is an illustration of my _option
table.
To check and fix the primary key loss error in the database table. Please follow the steps below.
II. IMPLEMENTATION GUIDE
Step 1: Check the structure of the Table Database for missing primary key
I have an error on wp_option
table in this article, so I will guide you with this table. With other tables, do the same.
Note: wp_ is called a prefix and can be changed, so it is not required to be wp_option but can be changed with any name.
Please open Terminal to check, your server must have WP-CLI installed. If you have not installed WP-CLI, please follow the quick installation below:
Use the cd
command to go to the directory where the source code runs with the database with the error table because wp-cli reads the wp-config.php
file to know which database is used. You can use the following command to check:
Note: –allow-root is used with root. If in user you do not need to add this option.
wp db query "DESCRIBE $(wp db prefix --allow-root)options" --allow-root
When I enter that command above to check, option_id
in the KEY section doesn’t have a primary key.
With a table with a primary key, it will display as follows:
Step 2: Update the primary key
Once the identified primary key is lost, update the primary key for the option_id
column with the wp-cli command as follows:
wp db query "ALTER TABLE $(wp db prefix --allow-root)options MODIFY option_id INT AUTO_INCREMENT PRIMARY KEY;" --allow-root wp db query "DESCRIBE $(wp db prefix --allow-root)options" --allow-root
After updating, check again and you will see that the option_id
column has the primary key with the PRI line. It’s completed. Now go back to phpMyAdmin and check if you can update Database operations.
This is my result after the implementation is complete.
III. Summary
So AZDIGI has completed the steps to resolve the error “Database table is missing a primary key”. Hopefully, the above methods will help you handle this error safely and effectively.
If you need support, you can contact AZDIGI’s Technical Department for the fastest support.