MySQL foreign key to table list - php

Lately I have been doing some work with MySQL and have come across the need to store table names as a field in one of my tables (which is not ideal). Instead I would like to reference a list of tables that currently exist in the database as a foreign key. I have found that the information_schema database stores a list of these tables however I cannot reference them as a foreign key as they are Memory tables. Is there another way I can reference the list of tables in the database as a foreign key.
For those interested, the reason I need to store the table names is because I am developing a basic PHP framework that exposes each table in the database as a class.

How dynamic is the list of tables in your database? One option is to create your own table and (periodically) fill it with the table names from information_schema.

Related

MySQL migrating data to a completely different database structure and keep relationships intact

I have a database that needs migrating into a completely different structure (we're going from a custom CMS to a slightly modified Wordpress DB).
I can pull the individual tables and map them onto the new structure easily enough, but I'm struggling to find a method to preserve the relationships between tables.
For example in the old db I have a customer and basket table joined 1 to many from the customer table. (IE one customer - many baskets). Mapping these to the new db is simple (user, usermeta and a custom table). However the keys in the new db are completely different to the keys in the old db, so I cannot use them to relate the new records together.
How can I re-establish this relationship? (even broad terms will be helpful)
The only thing that occurs to me is keep your old keys in "temporary" columns.
Create the customer table first, with new id column, keeping the old id column.
Now create the basket table (without
the foreign key constraint).
Run an update query, to update the new basket
key column with the new customer id, joining on the OLD keys (sorry, don't know MySQL update syntax well enough to give an example).
Recreate the foreign key constraint on the new columns
Drop the "old" columns
Hope that helps.

Doctrine find related records without foreign keys

I have two preexisting tables that were created by a CMS. I've mapped them in Symfony2 and I am trying to establish a relationship between two of the tables but I am now noticing that the CMS created these tables without a foreign key. This table table_uploads has a column that every other table is related to called column_table_name and column_record_num.
So assuming we have three tables... table_students, table_uploads, and table_teachers, the table table_uploads will have a value of either students or teachers in column_table_name to show its relation to the other two tables as well as a number in column_record_num that corresponds to the other table's primary key. Neither of the three tables actually have a defined foreign key however.
In Symfony, I'd like to call a table
$students = $this->getDoctrine()->getRepository('SchoolBundle:Students')->findAll();
and then load the related records from another table in such a way like (I know select_related doesn't actually exist, but this is essentially the functionality I'm looking to create)
$students->select_related()
Is this at all possible without the existence of a foreign key? The only way I can think of doing it is grabbing the column values from $students and running another query to other table with these values and merging the two results.
You could create a custom repository method that would join unrelated tables. DQL join between unrelated entities?

mysql input data with foreign key constrain

I have two simple table on Mysql Db with a foreign key constrain, let's say:
User (Userid,Name)
BorrowedBook (BookTitle, Userid)
I need to populate these table through a form, thus using Php.
What's the best practice to input consistent data on User and BorrowedBook?
I tried in several ways, including two separate INSERT INTO but I do have error message on Foreign Key constrain.

Multiple foreign keys to the same table in Cake PHP?

Cake has its conventions for automating some functionality interaction between it's models and the tables they reference. Foreign keys should be called people_id, if they reference a table called people. How do I handle a case where a row holds two people, and needs foreign keys for both people? It's obvious that I can't duplicate the column names, having two people_id columns.
Consider a competition where you have two competitors. Each competitor is represented by a row in the competitor's table. The competitions table needs to refer to both of those competitors. How can I do this wouldn't breaking the automatic functionality that Cake offers?
I found a link that describes how to do this perfectly.
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm

PHP MySQL query against changing database

I am developing a PHP web based data entry tool with MySQL as the database. However the database will undoubtedly change whilst the data entry is going on (there is a lot of it to be done so we have started it so that it runs in parallel to the other development).
I have constructed the SQL queries so that the php can automatically:
Determine what tables are in the database
List tables with a certain prefix so that only ones that data entry should use are listed
However, what I cant figure out (despite checking php, sql and mysql manuals and tutorials) is how to automatically pull tables that are connected by foreign key, so that data entry have a list of items to choose from for the given table. So in short, how do I - using php - determine:
Any foreign keys for the given table
The table name that the foreign key points to
WITHOUT hard-coding any table names into the SQL queries?
A quick way to list your Foreign Key references using the KEY_COLUMN_USAGE view:
SELECT CONCAT( table_name, '.',
column_name, ' -> ',
referenced_table_name, '.',
referenced_column_name ) AS list_of_fks
FROM information_schema.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_SCHEMA = (your schema name here)
AND REFERENCED_TABLE_NAME is not null
ORDER BY TABLE_NAME, COLUMN_NAME;
This query does assume that the constraints and all referenced and referencing tables are in the same schema.
For InnoDB tables, using the Comment field of SHOW TABLE STATUS is useful for extracting foreign key information for older versions of MySQL.
I am unaware of any other way than the above 2 methods.
Happy coding!

Categories