Loop through some databases searching for a table - php

I want to loop through some databases (the count of the databases are undefined) searching for a table and than I want to make SELECT and UPDATE queries in that DB where I found that table.

You can do a select against INFORMATION_SCHEMA.TABLES to locate the table by name:
SELECT
`TABLE_SCHEMA`,
`TABLE_NAME`,
`TABLE_ROWS`
FROM `INFORMATION_SCHEMA`.`TABLES`
WHERE `TABLE_NAME` like '%_assets'
Unless the desired table names might vary (different prefix, capitalization, etc.), use = 'name' instead of like '%_assets'

There are two solutions to your problem:
1). MySQL based using Federated Engine (as mentioned by you). You can go through this Blog post on Federated Engine
2). (As you are using PHP) Fetching data from one server, then either looping through the data and firing relevant queries to another server, or firing single aggregated query to another server based on the data fetched from first server.
Hope it will help you...

To list the various tables in a database you can query the database schema for the info you want.
select `table_name` as 'name'
from `information_schema`.`tables`
where `table_type`='base table' and `table_schema`=database();
The same approach can be used to find other items in the database such as events,triggers,stored procedures etc etc

Related

How to update all records of all tables in database?

I have a table with a lot of records (could be more than 500 000 or 1 000 000).
I want to update some common columns with the same field name in all tables throughout the database.
I know the traditional way to write separate queries to individual tables but not one query to update all records of all tables.
What is the most efficient way to do this in SQL, without using some dialect-specific features, so it works everywhere (Oracle, MSSQL, MySQL, Postgres etc.)?
ADDITIONAL INFO: There are no calculated fields. There are indexes. Used generated SQL statements that update the table row by row.
(This sounds like the classic case for normalizing that 'column'.)
Anyway... No. There is no single query to locate that column across all tables, then perform an UPDATE on each of the tables.
In MySQL, you can use the table information_schema.COLUMNS to locate all the tables containing a particular named column. With such a SELECT, you can generate (using CONCAT(), etc) the desired UPDATE statements. But then, you need to manually run them (via copy and paste).
Granted, you could probably write a Stored Procedure to wrap that into a single call, but that is too risky. What if some other table has the same column name, but should not be updated?
Example of building ALTERs to change tables' Engines: http://mysql.rjweb.org/doc.php/myisam2innodb#generating_alters
Example of using an SP to "pivot" rows to columns, complete with executing the generated code: http://mysql.rjweb.org/doc.php/pivot
As for common code across multiple vendors -- forget it! Virtually every syntax needs some amount of tweaking.

Copy all information from one table and insert it into another table on different server using PHP

I want to copy everything from a table on my local server and insert it into a table on a remote server.
Something like
INSERT INTO table2
SELECT * FROM table1;
How can I adapt this for 2 tables on different servers and databases?
Well, if you want to use PHP, then you could do something like querying everything from one table, simply like SELECT * FROM table, and then iterating over your table with a while loop, inserting one record at a time to the new table. I assume you know some PHP, for this answer to help you. You can't do it with one SQL statement atleast, that's for sure.
Please look at this StackOverflow thread: SQL Insert into … values ( SELECT … FROM … ). There are discussed some compatibility issues across various database engines, too.
It should answer your question quite good as long as it is within a single database.
For copies between different database instances have look at backup & restore, export & import mechanisms or at seperate copy scripts in php, python, etc. using either native or ODBC database drivers.

Querying for data from two tables in different databases of different servers?

I have one table t1 on database d1 of the server s1 and now another table t2 is in d2 of the server s2.Now i want some data of the table t1 and its related data from t2 table as resultant data.how can i make it possible?
FEDERATED Storage Engine in MySQL 5.0.related document i have seen any other option...what can i do to get data across two different server?
There are two solutions to your problem:
1). MySQL based using Federated Engine (as mentioned by you). You can go through this Blog post on Federated Engine
2). (As you are using PHP) Fetching data from one server, then either looping through the data and firing relevant queries to another server, or firing single aggregated query to another server based on the data fetched from first server.
Hope it will help you...

php do something for every record in the database

I have two tables in the database(videos and viewData) .
Im trying to build a script that runs for each record in the "videos" table and does something using the "videoID" field for that specific entry in the "videos" table. The does something part would be dumping some data into the viewData table.
Would I need to store all the records in an array before calling the loop? An example of a loop like this would be really helpful. Also in a way that could be potentially scalable that wouldn't hurt the server too much if there were a 1000+ records in the "videos" table.
Thanks,
Dave
Try to avoid the loop at all costs. Think set based processing, which means handle the entire set of rows within one SQL command.
I'm not entirely sure what you are attempting to do, as your question is a little vague. however, here are two possibly ways to handle what you are trying to do using set based thinking.
You can do a JOIN in an UPDATE, essentially selecting from the parent table and UPDATEing the child table for all rows in a single UPDATE command.
UPDATE c
SET Col1=p.Col1
FROM ParentTable p
INNER JOIN ChildTable c On p.ParentID=c.ParentID
WHERE ...
you can also INSERT based on a SELECT, so you would create one row from each row returned in the SELECT, like:
INSERT INTO ChildTable
(Col1, Col2, Col3, Col4)
SELECT
p.ColA, p.ColB, 'constant value', p.ColC-p.ColD
FROM ParentTable p
WHERE...
Working with a database in the loop isn't a good practice. It is good to select all table data into an array by one query and work with this array in future.
Do you have access by other means to MySQL tables? Like with MySQL Administrator or another tool, even by command line?
This is because it would be much more time, resources and everything else, doing that directly in the database, through a query or a database function.
I would do that this way.
But for the sake of clarity, unless you are storing the videos themselves inside database tables, 1000 records are not a problem. Maybe 10,000 would be.
General tip: do just what you need to do.
If you only need to operate upon data, do this on the database.
If you only need to check one field in one table, use SELECT your_field FROM your_table instead of SELECT * FROM your_table.

What is the cleanest way to sort "describe table" query results?

I'm working on "describe table" output to show a list of fields and their types, i want my primary keys to be at top of the list..
I think there's no way to sort describe's results using SQL (something like 'order by') rather than sorting it in PHP.
what do you think guys ?
thanks
When you do a SHOW COLUMNS or a DESCRIBE TABLE, you're really just using the builtin special database called INFORMATION_SCHEMA to pull information about a named table. Funny thing is, it seems to not return the information as a table, so it's impossible to get the data returned by those functions to act like a table (for sorting, subquerying, etc.).
Fortunately, you can set up your own query to perform the same lookup as SHOW or DESCRIBE:
select
COLUMN_NAME as "Field",
COLUMN_TYPE as "Type",
IS_NULLABLE as "Null",
COLUMN_KEY as "Key",
COLUMN_DEFAULT as "Default",
EXTRA as "Extra"
from
INFORMATION_SCHEMA.COLUMNS
where
TABLE_NAME = 'my table' and
TABLE_SCHEMA = 'my database'
-- add ordering --
order by COLUMN_TYPE;
You're correct. MySQL will always output columns in their actual order — that is, the order they're physically stored in the table's data.
You can physically move columns around within your table so that the primary keys are first, though this will require MySQL to lock the table and rewrite its entire data in the new order. That's almost never worthwhile.
If you're just interested in presenting nice output, regardless of how the table is actually structured, you can indeed sort the columns in PHP, perhaps using a simple usort() call.
Do not use describe but use the schema information tables which are provided by most dbms.
Then you can use simple selects to get your information.

Categories