mysql inserting data from one schema to another - mysql

Is there a way to insert data from a table in schema1 to a table in schema2 in mysql.
Also , I assume there will be any access/privilege issues.
My environment is Joomla using Fabrik extension, PHP, MySQL
Kindly share some tips
Thanks in advance

This query does that:
INSERT INTO db2.table1 SELECT * FROM db1.table1;
Not tested but should do the job.
If you do this as root user, you will have no permission issues.
Backup your data first, though.

You can always preface the table name with the database name and as long as the user has the appropriate permission you can do:
insert into db1.users( first, middle, last )
select a.first, a.middle, a.last from db2.users a
See the following for the documentation insert .. select

Related

MySQL not allowing to create table

I'm learning about MySQL, I just made my first databank, but when I make a table called "pessoas", as shown in an Youtube tutorial, it showed these two atached error messages, and didn't let me see the table created. I've tried to put back accents surrounding the words, but didn't work. Can someone explain me what's going on?
Your SELECTa are gibberish for mysql that is why they fail
See you second select for example
SELECT
Db as scopeuserhostSelect_priv
AS Selectlnsert_priv
AS lnsertUpdate_priv
AS UpdateDelete_priv
AS DeleteCreate_priv
AS CreateDrop_priv
AS DropGrant_priv
AS GrantReferences_priv
AS Referenceslndex_priv
AS IndexAlter_priv
AS AlterCreate_View_priv
AS Create ViewShow_view_priv
AS Show viewTrigger_priv
AS TriggerDelete_versioning_rows_priv
AS Delete versioning rows
FROM mysql.db
WHERE cadastro2 likedb
A query looks something like this
SELECT `test12`.`id` as id,
`test12`.`usr_id` as usr_id,
`test12`.`category` as category,
`test12`.`comp_id` as comp_id,
`test12`.`position`as position,
`test12`.`description` as description,
`test12`.`country` as country,
`test12`.`state` as state,
`test12`.`city` as city,
`test12`.`start_date` as start_date,
`test12`.`end_date` end_date,
`test12`.`timestamp` as timestamo
FROM `testdb`.`test12`
WHERE `category` LIKE 'tesmeifyou can';
every column is separated by comma, an alias for a column needs an actual column, and LIKE needs to compare a string
Here you find some more information on SELECT
A.A.,
your CREATE TABLE runs successfully on MariaDB 10.4.13.
The actual error messages are related to queries that retrieve information about database users, I guess these queries come from the MySQL Workbench itself.
My best guess is that your database and Workbench have different versions. Personally I am not a big fan of the Workbench (only for query profiling), so maybe try out another client: MySQL commandline, HeidiSQL, phpMyAdmin, ...
Best regards,
Martin

How to copy values from one MySQL database to another

I have 2 databases with different structures.
I need to copy information from database A to database B.
Database A has 1 table while database B has 2 related ones.
It is a Q&A site so the old database (A) has a table that contains both the question and the answer.
In the new database these are separate and the answer must contain a field with the id of the question.
Please help me make a SQL request.
Something like
"INSERT INTO table1 (field1,field3,field9)
SELECT table2.field3,table2.field1,table2.field4
FROM table2"
One more thing .. some values in the new database are known (will be hardtyped .. not taken from the old database)
You can simply use the below and specify manually where needed or grab it from the old database/table you are copying from. Also helps for if the columns are named differently or not as many in the new database.
USE `old_database`;
INSERT INTO `new_database`.`new_table`(`column1`,`column2`,`column3`)
SELECT `old_table`.`column2`, `old_table`.`column7`, `old_table`.`column5`
FROM `old_table`
you need to specify database in statement...
insert into database1.table1
select from database2.table2

MySQL - Trigger or Replication is better?

I want to replicate certain table from one database into another database in the same server. This tables contain exactly the same fields.
I was considering to use MySQL Replication to replicate that table but some people said that it will increase IO so i find another way to create 3 Trigger (Insert, update and Delete) that will perform exactly the same thing like what i expect.
My Question is, which way is better? Is it using MySQL replication is better even though it's in the same server or using Trigger to replicate the data is better.
Thanks.
I don't know what is your goal, but I got mine getting use of the VIEW functionality.
I had two different applications with separate databases but in the same Mysql server. Application2 needed to get a few data from Application1. In general, this is a trivial situation that you can handle with USE DB1; or USE DB2; as your needing, but my programming framework does not work very well with multiple DBs.
So, lets see my solution...
Here is my select query to retrieve this data:
SELECT id, name FROM DB1.customers;
So, using DB2 as default schema, I've created a VIEW:
USE DB2;
CREATE VIEW app1_customers AS SELECT id, name FROM DB1.customers;
Now I can retrieve this data in DB2 as a regular table with a regular SELECT statement.
SELECT * FROM DB2.app1_customers;
Hope ts useful. BR
Assuming you have two databases on the same server i.e DB1 and DB2 and the table is called tbl1 and it is sitting in DB1 you can query the table like this:
USE DB1;
SELECT * FROM tbl1;
USE DB2;
SELECT * FROM DB1.tbl1;
This way you wont need to copy the data and worry about extra space and extra code. You can query a table in another database on the same server. Replication and triggers are not your answer here. You could also create a view to encapsulate the SQL statement.
Definitely triggers is the way to go. Having another server (slave) will need to spare several MB for installation, logs, cpu and memory usage.
I'd use triggers to keep both tables equal. If you want to create a table with the same columns definition and data use:
USE db2;
CREATE TABLE t1 AS SELECT * FROM db1.t1;
After that, go ahead and create the triggers for Update, Insert and Delete statemetns.
Also you could ALTER the new table to a different engine like MEMORY or add indexes to see if you can improve something.

How to find out how many and what are the tables in my database?

I want to know how many tables are there in my database server and what are they.
You can request the tables from a certain database by executing the following SQL commands:
USE <database-name>;
SHOW TABLES;
Hope that helps.
You can fire query as below
SELECT * from information_schema.tables;
-- In MS-SQL Server, Retrieve all TableName from Database.
SELECT * FROM sys.tables;

Interacting with Custom Database Tables in Wordpress

I have a custom wordpress table(my_table). And i am inserting,updating all that good stuff to it. When i try to use
$wpdb->get_results("SELECT * FROM $wpdb->my_table",ARRAY_A);
or
$wpdb->get_results("SELECT * FROM my_table",ARRAY_A);
I get this error
WordPress database error: [Table 'mydatabase.my_table' doesn't exist]
SELECT * FROM my_table
I have tried to add my_table to wp-db.php as well but i still receive this error.
Yes the table is in the database
There is data in the table
Is their a config option of i need to change or somewhere else where i need to add my table name? I cannot find it for some reason.
full error
WordPress database error Table 'mydatabase.mytable' doesn't exist for query SELECT * FROM mytable made by do_action, call_user_func_array, promos
Ok tiggles, here is what I would do in your situation if you are still stuck. Create a new table, but this time, by executing a SQL command from WordPress, using something like :
$wpdb->query('CREATE TABLE mynewtable (First_Name char(50), Last_Name char(50), ... etc.)');
Immediately check that you can query it. If so, then go to your databse and dump all data from mytable into this newly created table. Use this one instead.
If it was a user permission because the user that created the previous table was not the same as the one granted access to as WordPress, then you will be safe.
If your table my_table is, indeed, in the mydatabase database and if mydatabase is, indeed, WordPress' database, there is no reason why it should not work. If mydatabase is not WordPress database or if my_table is in another database, then it is normal that it does not work.