How to retrieve MYSQL records as an INSERT statement - mysql

I'm trying come up with the best method of synchronizing particular rows of 2 different database tables. So, for example there's 2 product tables in different databases as such...
Origin Database
product{
merchant_id,
product_id,
... additional fields
}
Destination Database
product{
merchant_id
product_id
... additional fields
}
So, the database schema is the same for both. However I'm looking to select records with a particular merchant_id, remove all records from the destination table that have that merchant_id and replace those records with records from the origin database of the same merchant_id.
My first thought was using mysqldump, parsing out the create table statements, and only running the Insert Statements. Seems like a pain though. So I was wondering if there is a better technique to do this.
I would think mysql has some method of creating INSERT statements as output from a SELECT statement, so you can define how to insert specific record information into a new db.
Any help would be appreciated, thank you much.

phpMyAdmin has part of this capability: You can run a query and then export the results of that query into a file containing CREATE statements.
Update: And mysqldump has it too: Link
mysqldump -u username -p --where="id='merchant_id'" databasename
In regards to replacing merchant IDs, that part I don't entirely understand yet. You may be better off doing a manual search+replace on them. Can you make a real life example of two such records?

Related

Get logs of all update queries within database in mysql

I am looking to get all the update statements with old and new values within a database into one table.
For an example :
I have database name "users".
It contains four tables "primary_info","address_info","avtars","audit_logs"
Now, Whichever update statements executes on primary_info,address_info and avtars table that i need to log into audit logs table with below way.
ID, Table Name, Field_name,Old_value,New_value,Current Datetime
I know we can create triggers to manage such things.But i have database which contains more than 90 tables.So it won't help me to achieve by making trigger (update before) .
So is there any other way which i missed here ?
Thanks in advance.

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.

MySQL store checksum of tables in another table

CONTEXT:
we have big databases with loads of tables. Most of them (99%) are using innodb.
we want to have a daily process that monitors which table has been modified. As they use innodb the value of Update_time from
SHOW table STATUS from information_schema;
is null.
For that reason we want to create a daily procedure that will store the checksum (and other stuffs for that matters) of each table somewhere (preferably another table). On that, we will do different checks.
PROBLEM:
I'm trying to use
checksum table from db_schema.table_name;
which returns a resultset-table with 2 columns: "table","checksum".
It gives me the value I want but I'm not able to use it in a select or insert statement.
I tried a lot of things like:
select `checksum` from (checksum table from db_schema.table_name);
or other similar queries. But I'm not able to extract the data from the resultset.
Is there a way I can do that?
Thanks in advance for your help.
EDIT: in the end what I want is to build a more complex resultset having different informations in it (table schema, table name, count, checksum, datetime:now()...)
Then I'll use this resultset to compare with the values of yesterday and draw my own statistics. That's why I want to get the checksum from that resultset.
There is no possibility to save the result of CHECKSUM TABLE directly using SQL. Neither can you use prepared statements or cursors in stored procedures to use the checksum result.
You best make a script around it, or download some popular tools doing it for you.
For MyISAM tables using the CHECKSUM=1 table argument, you can simply use INFORMATION_SCHEMA like this:
SELECT TABLE_NAME,CHECKSUM FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'test' AND ENGINE='MyISAM'
AND CHECKSUM IS NOT NULL;