PHPMyAdmin - Deleting rows from table with no unique column - wordpress

This is really annoying, I've encountered this problem plenty of times when importing a database:
https://i.gyazo.com/8051625ceaa6f2e00212a134a96a485e.png
Because it has no unique column, I can't delete rows. Because I can't delete rows, I can't assign a unique column because I can't delete the rows with duplicate entries for that column (those rows with ID = 0).
I can't remember how I fixed this before. I have no idea how this problem even happens, I thought the wp_options table would have a unique key on the ID column by default.

Ah, sorry the solution was really simple. PHPMyAdmin just prevents you from deleting rows through the GUI, but the SQL query to delete the rows still works. I deleted those duplicate rows by going into the SQL tab and running DELETE FROM wp_options WHERE option_id = '0';

I've also encountered this problem, and thought the same thing (NOTE: It's NOT just the wp_options table - you're going to have issues on other tables as well!)
The issue is the way the export / import is processed.
There's a subtle "autoincrement" checkbox somewhere in the phpMyAdmin interface for the export under the "object creation" options:
NOTE: Once you've exported / imported, and find yourself dealing with this, the simplest way that I've come up with to solve the issue is:
Create a new column titled 'new_ID', make it AUTO_INCREMENT.
Then,
a. either run a query to update the existing ID column to the new_ID value, OR
b. Delete the existing ID column, and rename 'new_ID' to the correct name, and add a PRIMARY KEY index.

I figured out what caused the problem. When importing the SQL file, there was an error when importing one of the keys for one of the tables. As a result, it skipped the importing of every key after that, and the option_id unique key was one of the keys it skipped. So yeah you were right, the problem happened with a lot of other tables too. The solution was to import the rest of the keys and indexes and stuff from the .sql file.
What I mean is this:
--
-- Indexes for table wp_links
ALTER TABLE wp_links
ADD PRIMARY KEY (link_id), ADD KEY link_visible (link_visible);
--
-- Indexes for table wp_options
ALTER TABLE wp_options
ADD PRIMARY KEY (option_id), ADD UNIQUE KEY option_name (option_name), ADD KEY wpe_autoload_options_index (autoload);
I forgot to delete the wp_links table before importing the new database tables, so it couldn't create the link_id primary key since it already existed. As a result, every key that appeared in the file after that one got skipped.

Related

Tying table records together in SQLite3

I am currently working on a database structure in SQLite Studio (not sure whether that's in itself important, but might as well mention), and error messages are making me wonder whether I'm just going at it the wrong way or there's some subtlety I'm missing.
Assume two tables, people-basics (person-ID, person-NAME, person-GENDER) and people-stats (person-ID, person-NAME, person-SIZE). What I'm looking into achieving is "Every record in people-basics corresponds to a single record in people-stats.", ideally with the added property that person-ID and person-NAME in people-stats reflect the associated person-ID and person-NAME in people-basics.
I've been assuming up to now that one would achieve this with Foreign Keys, but I've also been unable to get this to work.
When I add a person in people-basics, it works fine, but then when I go over to people-stats no corresponding record exists and if I try to create one and fill the Foreign Key column with corresponding data, I get this message: "Cannot edit this cell. Details: Error while executing SQL query on database 'People': no such column: people-basics.person" (I think the message is truncated).
The DDL I currently have for my tables (auto-generated by SQLite Studio based on my GUI operations):
CREATE TABLE [people-basics] (
[person-ID] INTEGER PRIMARY KEY AUTOINCREMENT
UNIQUE
NOT NULL,
[person-NAME] TEXT UNIQUE
NOT NULL,
[person-GENDER] TEXT
);
CREATE TABLE [people-stats] (
[person-NAME] TEXT REFERENCES [people-basics] ([person-NAME]),
[person-SIZE] NUMERIC
);
(I've removed the person-ID column from people-stats for now as it seemed like I should only have one foreign key at a time, not sure whether that's true.)
Alright, that was a little silly.
The entire problem was solved by removing hyphens from table names and column names. (So: charBasics instead of char-basics, etc.)
Ah well.

Removing SQLite column of table of which there are foreign constraints

I needed to delete a column from a table that other tables had a foreign constraint on. I guess ALTER TABLE doesn't let you remove columns, so I had to create a new table without the column and copy the data over and rename them appropriately. Though now it won't let me delete the old table with the extra column because of foreign table constraints still I guess pointing to the old table instead of the new one... even though the new one now has the correct name. What is the recommended practice for making the foreign keys point to the right table now that I did the switch?
I don't think that you can. You might have to recreate all the other tables with the updated foreign key, because you cannot change foreign key constraint conditions or at least as far as I know.

SQLite Find ROWID of most recent or next ROWID

So I have a table with data about an image. The table looks something like this...
ROWID|title|description|file_path
The file path contains the name of the image. I want to rename the image to match the ROWID.
How do I get the latest ROWID? I need to also account for rows that have been deleted as I am using this as an autoincremented primary key. Because, if a row within the table has been deleted it is possible for the table to look like this...
1|title A|description A|..\fileA.jpg
2|title B|description B|..\fileB.jpg
5|title E|description E|..\fileE.jpg
7|title G|description G|..\fileG.jpg
On top of that there could be one or more rows that have been deleted so the next ROWID could be 10 for all I know.
I also need to account for an fresh new table or a table that has had all data deleted and the next ROWID could be 1000.
In summary, I guess the real question is; Is there a way to find out what the next ROWID will be?
If you have specified AUTOINCREMENT in primary key field and table is not empty this query will return latest ROWID for table MY_TABLE:
SELECT seq
FROM sqlite_sequence
WHERE name = 'MY_TABLE'
What language? Looks like the c API has the following function:
sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
http://www.sqlite.org/c3ref/last_insert_rowid.html
You could also just do:
select MAX(rowid) from [tablename];
Unfortunately neither of these methods completely worked the way I needed them to, but what i did end up doing was....
insert data into table with the fields I needed the rowid for filled with 'aaa'
then updated the rows with the data.
This seemed to solve my current issue. Hopefully it doesn't cause another issue down the road.
I think last_insert_rowid is what you want, usually.
Note that the rowid behavior is different depending on the autoincrement flag - either it will monotonically increase, or it will assume any free id. This will not usually affect any smaller use cases though.

SQLLite: How to renumber a auto-increment field?

I have two different CSV files which I have merged and imported into a single table in a SQLite3 database. Each CSV file contained a column called ID. Since, some of the ID's are duplicates (across the CSV files) and this is a primary key field, I need a way to completely renumber the ID field for each row in the table.
The ID field is also an auto-increment field.
So, what I would like to do is to run a SQL command or some other method where the ID for each row of the table would be reset to ensure uniqueness. For example, the ID field for the first row will be set to 1 the next to 2 and so on.
Note, it is not so important that it begin with 1. Ensuring primary key uniqueness is the goal here. It doesn't matter what number it starts at. There are also no foreign key relations so that is not an issue.
Any suggestions much appreciated.
Okay, in my case, I figured out that it was easiest to not import the ID column. Rather, I imported everything else and then added an ID field of type auto-increment. Once I did that, everything was re-numbered as I wanted.

How to make sure SQLite never create a row with the same ID twice?

I have a SQLite table that looks like this:
CREATE TABLE Cards (id INTEGER PRIMARY KEY, name TEXT)
So each time I create a new row, SQLite is going to automatically assign it a unique ID.
However, if I delete a row and then create a new row, the new row is going to have the ID of the previously deleted row.
How can I make sure it doesn't happen? Is it possible to somehow force SQLite to always give really unique IDs, that are even different from previously deleted rows?
I can do it in code but I'd rather let SQLite do it if it's possible. Any idea?
Look at autoincrement (INTEGER PRIMARY KEY AUTOINCREMENT). It will guarantee this and if the request can't be honored it will fail with SQLITE_FULL.

Resources