Get logs of all update queries within database in mysql - 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.

Related

Insert multiple values in MYSQL database

I have a MySql database and need some of the tables to have pre-created values. Its like a user needs to select a species of the pet he owns and I give him values from table "pet_species", like ["Fish","Dog","Cat",...,"Lizard"].
Also I have a table like "pet_breed" with pet_species_id, pet_breed_id, pet_breed_name. So is it possible to add the values in MySql Workbench or somewhere else, so it will look for all constraints while I add values, and then generate a right sql script to create or update this tables.
Hope you understand what I mean.

Cant copy unique records from one database table to another?

Hi,
I am trying to copy unique records from a database table to another table of the same name but different database. The source database contains some records that are already present in the destination database, so those I dont need, only the other ones. Database destination is called "test" and the source database is "forums". The table name is store for both cases. I am using this query:
INSERT INTO test.store (cs_key, cs_value, cs_array, cs_updated,cs_rebuild)
SELECT DISTINCT cs_key, cs_value, cs_array, cs_updated,cs_rebuild
FROM forums.store
But I am getting many errors as I try to run this query. Why?
Thank you.

Merge two database into a third one

I have two MySQL database k_db1 and k_db2 on a single server.
In k_db1, I have k_db1.table1 and k_db1.table2.
In k_db2, I have k_db2.table3 and k_db2.table4.
I want to create a third database k_db3 where I copy/paste tables of others databases.
It will result in k_db3.db1-table1, k_db3.db1-table2, k_db3.db2-table3, k_db3.db2-table4. I want to transfer data, indexes etc... and I don't want to delete k_db1 and k_db2 tables in the process. It must duplicate datas.
Do you know a way to do this just with SQL command?
Thanks in advance for your help.
You can try something like this:
DROP TABLE IF EXISTS k_db3.db1_table_1;
CREATE TABLE k_db3.db1_table_1 AS
SELECT * FROM db1.table_1;
Then you can recreate the indexes on the new table via ALTER TABLE statements.
Also I would avoid using - in table names.

In MySQL is this possible to query the database in specify time using SQL statement?

For example, the data is like this:
[id][name]
at 10:00, the data is like this:
[1][John]
at 11:00, the user edit the data change to this:
[1][Johnson]
So, user use the alter command to change the data, but it is possible for the database query back the data on 10:00 in MySQL? Thanks.
What you are talking about is versioning. Having time stamp and version number would help but storing multiple records in same table with same id would cause a decrease in data integrity - what about a trigger on the table and insert into some form of audit table?
In general you have two options:
Add a colum version with a timestamp and expand the primary key to your currient PK and the version. To grep the data you just need to select the least version e.g. with max. Or add another colum with a bit value for the newest version. This should be faster to select.
Create a table with historical values. To implement this you need to add a trigger to the orginal table on update and copy the old value in your history table. So you can see all changes.

How to retrieve MYSQL records as an INSERT statement

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?