In SQL is there any way to find the last time a table had an insert and delete statement performed on it? I tried
select update_time from information.schema.tables where table_num="tablename";
but that did not work. I am also trying to look at deletions and insertions specifically. I am also working in MySQL Workbench.
Related
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.
I have an AWS RDS MySQL table which has a unique key constraint. Let us take the table name as user and field name as username for example. Suppose the table has a row username=admin. I am executing the below queries using Laravel.
delete from user where username='admin'
insert into user (username) values ('admin')
Once in a while, I can see Integrity Constraint Violation1062 Duplicate Entry in the logs. It seems like the row is not deleted by the time the code executes the insert query. It works most of the time. I can update the code with other logic but wondering why it is happening! Is there any AWS RDS specific scenarios related to this use case? I have not experienced this using own MySQL installation. Thanks for your help!
I have a table (MySql) that some rows need to be updated when a user desires.
i know the right way is just using Sql UPDATE statement and i don't speak about 'Which is faster? Delete and insert or just update!'. but as my table update operation needs more time to write a code (cause of table's relations) why i don't delete the old row and insert updated field?
Yes, you can delete and insert. but what keeps the record in your database if the program crash a moment before it can insert data to Database?
Update keeps this from happening. It keeps the data in your database and change the value that needed to be changed. Maybe it is complicated to use in your database, but you can certain that your record still safe.
finally i get the answer!
in a RDBMS system there are relations between records and one record might have some dependencies. in such situations you cannot delete and insert new record because foreign key constraint cause data lose. records dependent (ie user posts) to main record (ie an user record) will be deleted!
if there are situations that you don't have records dependencies (not as exceptions! but in data models nature) (like no-sql) and you have some problems in updating a record (ie file checking) you can use this approach.
Original Question
MySQL workbench allows one to define "inserts": rows to be inserted into the database on creation. It does this by adding lines such as
START TRANSACTION;
USE `someDB`;
INSERT INTO `someDB`.`countries` (`name`) VALUES ('South Africa');
COMMIT;
However, if the database, table and entry exists, this throws an error. Creation of tables does not, as workbench uses CREATE IF NOT EXISTS for those. Is there a way to get workbench to insert using INSERT...ON DUPLICATE KEY UPDATE?
Half Solution
Running the script with the force argument:
mysql user=xx password=xx --force < script.sql
Ignores such errors, and is thus a solution in my particular case. However, the actual question of modifying the type of INSERTS still stands (for interest)
See here
I have a query which basically "syncs" all the data from a table in one database, to a replicated table in another database.
Here is the simple query:
TRUNCATE TABLE [Database2].[dbo].[USER_SYNC]
INSERT INTO [Database2].[dbo].[USER_SYNC]
SELECT * FROM [Database1].[dbo].[USER]
Now, after some research, I had a look into using a trigger to do this, however, I read up that stored procedures and heavy queries such as this should not be used within a trigger.
Therefore, what is the best way in which I can automatically run this query from within SQL, whenever a record in database1 is inserted, amended or deleted?
And if what I read up about triggers was incorrect, then how would I go about creating one for my procedure? Thanks.
If you need to sync tables you do not need to truncate one every time on update, delete or insert.
Create identical copy of user table.
Create on update, on delete, on insert triggers on the original user table.
In the trigger update, delete or insert to the duplicate table only one row at a time - the one that was updated, deleted or inserted to the original user table. This will not be a heavy query.
UPDATE:
http://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx
http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html