How do I display content for foreign keys in phpmyadmin - php

Whenever I try to insert a new record in my MySQL database using phpMyAdmin, if the column refers to a foreign key, i get a dropdown box with the valid IDs that i can use. The problem is when the database gets too big, the ID is meaningless for me. For example if the ID is the social security number of the employee, i don't know it, but i can probably remember the name. Is there a way to display the row contents next to the ID or at least one column that could be more meaningful to the user? I have seen that feature in phpPgAdmin.
I tried this in the config file:
$cfg['ForeignKeyDropdownOrder'] = array( 'content-id', 'id-content' );
But it didn't work.

phpMyAdmin has a function for this, but you must be using the InnoDB table type. You must also have pmadb configured.
Select the table that contains the foreign key (child).
Click the "Structure" tab.
Click "Relation view".
Under the foreign key constraints, click the "Choose column to display" drop down and make a choice.
The column value will now be shown next to the id. It will also be shown when hovering over the foreign key value after making a selection.
For anyone having trouble getting this setup, please see the #relation-view documentation which tells you exactly how to configure it with screenshots.

In addition to what #EternalHour says you also need to check the radio button under view options
that says "Relational display column" if you want the value to be displayed.
If not checked you will only see the Foreign Key value when you hover that specific id.

If you really want to keep using phpmyadmin for this, you can create a view with a join which shows you the name, id and other information in 1 table. Then you can just use the search function to search on a name.

Related

This table does not contain a unique column. Error

I have a table in my database, but when I want to change something with double click option phpMyAdmin keeps printing this error:
"This table does not contain a unique column. Features related to the
grid edit, checkbox, Edit, Copy and Delete links may not work after
saving."
But there is unique column(id) and primary column(id) as you can see in my picture below.
It is really confusing, because I don't know what is the problem.
Thank you.

CakePHP: inserting into a MySQL ARCHIVE engine table

I have a small ARCHIVE engine table that I want to submit logs into whenever someone does something. I am not giving a unique "id" to every row; instead my "id" column is the user's ID.
I use this->model->save() and the first insert is fine. The problem is Cake thinks I'm trying to UPDATE on the second time that someone does something, when really all I want is to create another record, with the same user's ID, with a different action or timestamp.
How do I force CakePHP to INSERT only, without using query()? And without having to make 5th column for "row ID"?
Always make sure to unset the primary key. For instance in the beforeValidate. This will trigger a save instead of an update.
See the documentation

CakePHP: How can I disable auto-increment on Model.id?

CakePHP 1.3.0, mysqli
I have a model, Manifest, whose ID should be the unique number from a printed form. However, with Manifest.id set as the primary key, CakePHP is helping me by setting up auto-increment on the field. Is there a way to flag the field via schema.php and/or elsewhere to disable auto-increment? I need just a plain, old primary key without it.
The only other solution I can imagine is adding on a separate manifest number field and changing foreign keys in a half dozen other tables. A bit wasteful and not as intuitive.
I just tested this out on my cake sandbox and it worked.
All you need to do is set the id field in the data you're saving. So, if you're saving post data, and you want the id to be 200 (arbitrary; you could use another table field or user input or anything else for this source).
$this->data['Manifest']['id'] = 200;
Is that what you're after, being able to set your own values for id's rather than auto incrementing them?
You should be able to include the ID field in the add form. Just make sure to override its default type or CakePHP will turn it into a hidden field.
echo $this->Form->input('id', array('type' => 'text'));
I am a bit curious about this. Of course you cannot save a record without the primary key (which is ID in your case) set. So if you don't want the ID to be incremented automatically, then you must be saving your own ID. And it shouldn't cause a problem.
Are you using a mysql database? If so, do you have the auto_increment set to true on the ID field? Then mysql itself will automatically increment the ID whenever you save a record won't it?
You can set your schema in the Manifest model: http://book.cakephp.org/view/442/_schema.

How can we re-use the deleted id from any MySQL-DB table?

How can we re-use the deleted id from any MySQL-DB table?
If I want to rollback the deleted ID , can we do it anyhow?
It may be possible by finding the lowest unused ID and forcing it, but it's terribly bad practice, mainly because of referential integrity: It could be, for example, that relationships from other tables point to a deleted record, which would not be recognizable as "deleted" any more if IDs were reused.
Bottom line: Don't do it. It's a really bad idea.
Related reading: Using auto_increment in the mySQL manual
Re your update: Even if you have a legitimate reason to do this, I don't think there is an automatic way to re-use values in an auto_increment field. If at all, you would have to find the lowest unused value (maybe using a stored procedure or an external script) and force that as the ID (if that's even possible.).
You shouldn't do it.
Don't think of it as a number at all.
It is not a number. It's unique identifier. Think of this word - unique. No record should be identified with the same id.
1.
As per your explanation provided "#Pekka, I am tracking the INsert Update and delete query..." I assume you just some how want to put your old data back to the same ID.
In that case you may consider using a delete-flag column in your table.
If the delete-flag is set for some row, you shall consider program to consider it deleted. Further you may make it available by setting the delete-flat(false).
Similar way is to move whole row to some temporary table and you can bring it back when required with the same data and ID.
Prev. idea is better though.
2.
If this is not what you meant by your explanation; and you want to delete and still use all the values of ID(auto-generated); i have a few ideas you may implement:
- Create a table (IDSTORE) for storing Deleted IDs.
- Create a trigger activated on row delete which will note the ID and store it to the table.
- While inserting take minimum ID from IDSTORE and insert it with that value. If IDSTORE is empty you can pass NULL ID to generate Auto Incremented number.
Of course if you have references / relations (FK) implemented, you manually have to look after it, as your requirement is so.
Further Read:
http://www.databasejournal.com/features/mysql/article.php/10897_2201621_3/Deleting-Duplicate-Rows-in-a-MySQL-Database.htm
Here is the my case for mysql DB:
I had menu table and the menu id was being used in content table as a foreign key. But there was no direct relation between tables (bad table design, i know but the project was done by other developer and later my client approached me to handle it). So, one day my client realised that some of the contents are not showing up. I looked at the problem and found that one of the menu is deleted from menu table, but luckily the menu id exist in cotent table. I found the menu id from content table that was deleted and run the normal insert query for menu table with same menu id along with other fields. (Id is primary key) and it worked.
insert into tbl_menu(id, col1, col2, ...) values(12, val1, val2, ...)

Extract primary key from MySQL in PHP

I have created a PHP script and I am lacking to extract the primary key, I have given flow below, please help me in how can i modify to get primary key
I am using MySQL DB, working for Joomla, My requirement is tracking the activity like insert/update/delete on any table and store it in another audit table using triggers, i.e. I am doing Auditing. DB's table structure: Few tables dont have any PK nor auto increment key
Flow of my script is :
I fetch out all table from DB.
I check whether the table have any trigger or not.
If yes then it moves to check for next table and so on.
If it does'nt find any trigger then it creates the triggers for the table, such that,
it first checks if the table has any primary key or not(for inserting in Tracking audit table for every change made)
if it has the primary key then it uses it further in creation of trigger.
if it doesnt find any PK then it proceeds further in creating the trigger without inserting any id in audit table
Now here, My problem is I need the PK every time so that I can record the id of any particular table in which the insert/update/delete is performed, so that further i can use this audit track table to replicate in production DB..
Now as I haave mentioned earlier that I am not available with PK/auto-incremented in some table, then what should I do get the particular id in which change is done?
please guide me...GEEKS!!!
If I understand your question right, you need a unique identifier for table rows that have no primary key and no other kind of unique identifier. That's not easy to do as far as I can see. Other databases have unique Row IDs, but mySQL does not. You could use the value of every column to try and identify the row, but that is far from duplicate-safe - there could be two or more rows containing the exact same values. So I'd say, without a unique identifier, this is something that simply cannot be done.
Some ideas in this SO question:
MySQL: is there something like an internal record identifier for every record in a MySQL table?

Categories