Home Website TutorialsWordPress Fix Database Table Missing Primary Key error on WordPress

Fix Database Table Missing Primary Key error on WordPress

by Thạch Phạm
Published: Last Updated on
A+A-
Reset

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.

Database Table Missing Primary Key

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.

Database Table Missing Primary Key

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.

AZDIGI Tutorial
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.

Database Table Missing Primary Key

With a table with a primary key, it will display as follows:

Database Table Missing Primary Key

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:

AZDIGI Tutorial
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
    
Screenshot 2020 11 01 at 10.14 2

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.

Screenshot 2020 11 15 at 12.38.46@2x

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.

Đánh giá

Tham gia nhóm hỗ trợ Server - Hosting

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Bài viết cùng chuyên mục

AZDIGI – Không chỉ là đơn vị hàng đầu trong lĩnh vực Web Hosting và Máy chủ, chúng tôi mong muốn mang lại những kiến thức bổ ích nhất và luôn cập nhật thường xuyên cho cộng đồng người đam mê thiết kế website, công nghệ,…

Vui lòng không sao chép nội dung nếu chưa xin phép. Designed and Developed by PenciDesign