Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phpmyadmin export issue

My shared host does not allow SSH access. I am trying to export a database using phpmyadmin and import onto a new server. I keep getting this error and i am not sure how to fix it. Any help is appreciated.

Error
SQL query:
--
-- Indexes for dumped tables
--
--
-- Indexes for table `EWRporta_blocks`
--
ALTER TABLE  `EWRporta_blocks` ADD PRIMARY KEY (  `block_id` ) ,
ADD KEY  `title` (  `title` ) ;
MySQL said: Documentation
#1068 - Multiple primary key defined 
like image 634
TheShadyOneHD Avatar asked May 15 '15 10:05

TheShadyOneHD


1 Answers

I have run into this issue multiple times, and modonoghue has one valid way of handling it by dropping your tables and completely recreating them.

Problem & a General Solution

Basically what is happening is that you are trying to run INSERT statements that are inserting values into primary keys that already exist - thereby giving you an error of duplicate keys. The database has no clue how to handle having multiple entries with the same key, as SQL logic is based around every 'row' having a primary key that is completely unique.

What you want to do is to save all the values to your exported sql file, in a query that, when you import the file again, deletes all the existing values (assuming you want to restore it to a certain point and aren't worrying about data saved between your export date and your import date!) and inserts all the exported values ... or somehow else avoids trying to add a new entry with an existing key (see following).

One method to export a specific database (TRUNCATE):

  1. In other words, when you are using PHPMYADMIN to export your sql file, click "Custom - display all possible options".
  2. Under "Format-Specific Options" make sure "structure and data" is selected (otherwise you may end up dropping your tables and not having the data to restore them with!!!)
  3. Under "Data Creation Options" select "Truncate table before insert" -- this will delete all the existing data in the tables.

When you import, all the existing data will be deleted from each table (TRUNCATE) and all the exported data will be written back to the tables (INSERT) but the tables themselves won't be deleted (DROP).

IGNORE vs TRUNCATE (Alternative Route)

You should be able to skip Step 3 above (TRUNCATE) and instead select the checkbox "Instead of INSERT Statements Use ..." "INSERT IGNORE statements"

Basically, "IGNORE" will just skip over duplicates in your exported data, and prevents you from having to delete your existing data. This is good if you want to just add back lost data, without deleting data that's been changed / added since the last export.

Superuser (ON DUPLICATE KEY UPDATE)

There is also an INSERT INTO ... ON DUPLICATE KEY UPDATE ... that allows you to tell the query exactly what to do if there is a duplicate key. This prevents you from just ignoring two entries with identical keys that may not be identical entries. It is more complicated to setup properly, however.

like image 70
SRDC Avatar answered Oct 19 '22 19:10

SRDC