In phpMyAdmin under operations I can "Copy database to:" and select
Structure and data
CREATE DATABASE before copying
Add AUTO_INCREMENT value
I need to be able to do that without using phpMyAdmin.
I know how to create the database and user.
I have a source database that's a shell that I can work from so all I really need is the how to copy all the table structure and data part. (I know, the harder part)
system() & exec() are not options for me which rules out mysqldump. (I think)
How can I loop through each table and recreate it's structure and data?
Is it just looping through the results of
SHOW TABLES
then for each table looping through
DESCRIBE tablename
Then, is there an easy way for getting the data copied?
UPDATE:
So, I ended up going with:
SHOW TABLES;
for each table then I did
SHOW CREATE TABLE `tablename`;
and then
INSERT INTO `newBDname`.`tablename` SELECT * FROM `existingDBname`.`tablename`;
You can use mysql_fetch_assoc on SHOW TABLES and then for each result, store the output of mysql_fetch_result SHOW CREATE TABLE $table_name and then issue mysql_query on $show_create_table
Hope that helps ... more into helping than doing it for you. :)
Use SELECT INTO to accomplish this.
SELECT *
INTO destinationDB..newTable
FROM sourceDB..existingTable
Related
I am new with PHP development and just wondering if theres a existing function on PHP than duplicate the copy command on phpmyadmin, i know that the query sequence is below, but this is like a long query/code since the table has alot of columns. i mean if phpmyadmin has this feature maybe its calling a build in function?
SELECT * FROM table where id = X
INSERT INTO table (XXX)VALUES(XXX)
Where the information is based from the SELECT query
Note: The id is primary and auto increment.
Here is the copy command on phpmyadmin
i mean if phpmyadmin has this feature maybe its calling a build in function?
There is no built-in functionality in MySQL to duplicate a row other than an INSERT statement of the form: INSERT INTO tableName ( columns-specification ) SELECT columns-specification FROM tableName WHERE primaryKeyColumns = primaryKeyValue.
The problem is you need to know the names of the columns beforehand, you also need to exclude auto_increment columns, as well as primary-key columns, and know how to come up with "smart defaults" for non-auto_increment primary key columns, especially composite keys. You'll also need to consider if any triggers should be executed too - and how to handle any constraints and indexes that may be designed to prevent duplicate values that a "copy" operation might introduce.
You can still do it in PHP, or even pure-MySQL (inside a sproc, using Dynamic SQL) but you'll need to query information_schema to get metadata about your database - which may be more trouble than it's worth.
I need to generate a php script that will carry out a sequential backup and update/renaming a number of MySql tables. Can I do this in a single query or will I need to generate a query for each action?
I need the script to do the following in order
DROP TABLE backup2
RENAME TABLE backup1 TO backup2
RENAME TABLE main TO backup1
COPY TABLE incomingmain TO main
TRUNCATE TABLE incomingmain
In practice the TABLE incomingmain will be populated from an external import before the TABLE update sequence above is carried out.
Can any one advise please how I structure this after connecting to the database?
You are better off to use a mysqli::multi_query().
It also depends on the return values, meaning are you going to check for errors or blindly run them all at once? If I am you, I would code it sequentially, just because it will look much cleaner from a coding point of view.
I have a database that has a notice_current table and a notices_archive table. As part of a user logout process, I want to move all of their associated notices from the current table to the archive.
In my PHP application code I am currently making a transaction where I copy the notices over, and then delete the rows in the notices_current table if there were no errors in the copying. However, I am wondering if MySQL has some innate function or method for simply pushing notices from one table to another. If so, it would see that this would be more effective than my current implementation.
There's not a single built-in function for this, but if you're currently iterating over all of the rows, then something like this might be a lot more efficient:
BEGIN;
INSERT INTO notices_archive SELECT * FROM notice_current WHERE user_id=%;
DELETE FROM notice_current WHERE user_id=%;
COMMIT;
u can use phpmyadmin tools to do this.
goto phpmy admin then select your source db and click on operation tab and select copy and set the parameters then let phpmyadmin to do his job :D
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.
I have table called "users" and I want to make an exact copy as "users_2" as regard the structure only not the content.
I wanna do this using PHP only as I don't have an access to phpMyadmin or mysql console.
Do you have an idea how to do that ?
After connecting to your database appropriately in php (mysql_connect):
mysql_query("create TABLE tablename like SRCTABLE");
You can use the SQL SHOW CREATE TABLE users command, its result is a CREATE TABLE statement in which you can just replace users with users_2 and execute.