Mysql setting a record as deleted or archive - mysql

Is there any way to omit some records in mysql select statement and not deleting them?
We can easily add a column for example deleted and set it to 1 for deleted ones and keep them but the problem is that we have to put where deleted = 1 in all queries.
What is the best way to keep some records as an archive?

I don't know how many tables you have and how much data you want to store, but a solution could be this one:
You create a tblName_HIST table for each the tables (tblName) you want to keep the virtually deleted data
Optional: Add a DELETED_DATE column to keep track of the date the record was deleted.
You add a Trigger on the tblName tables that AFTER DELETE statement INSERT the record in the tblName_HIST table.
This will allow you to keep the Queries and the DB tables made since now without modify them that much.

Related

Why not to delete tho old row and insert updated row?

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.

how to limit my rows in a mysql table and overrite new rows to insert from 1st row?

e.g:i created a logtable and i want to keep limit of rows with 1000 records
...if i insert a record after 1000 rows my row should over ride from 1st row by deleteting or what ever it is.
is der any solution in mysql
anyone plz help
There is no super simple solution, but this is possible. Basically, you need to do two things.
The first is to keep track of the order that rows are inserted into the table. SQL tables represent unordered sets, so this ordering needs to be included explicitly. One simple method is an auto-incrementing id for the table.
The second is to add a trigger to insert. When a new row is inserted, then this trigger will delete a row. I would recommend that this trigger be an after insert trigger.
You can also do this at the application layer. For instance, if you insert new rows using a stored procedure, then the stored procedure can do the delete as well as the the insert.
Finally, if the goal is just to get rid of older data, there are other options. For instance, you might consider partitioning the table or scheduling a job (event) to periodically delete excess rows.

How to know if table has any deleted row?

I have a table with lots of brand names in it.
I want to control if it has any changes before my process.
So I use
select max(track_update_time) from brands limit 1
When I delete a record there is changes but I can't know it by asking update time.
I try to create an after delete trigger with:
update brands set max(track_update_time) = now()
but it locks table on delete and give error on trigger.
How can I learn if table has any updated or deleted record?
You can't access the update time any more because it is deleted. So you have either only MARK the row as deleted using an additional row (and still leaving it in the DB).
If only the latest time of deletion of any row is of relevance, you simply could store it in a separate, new table.
And if the same applies for updates, store these in this table, too, and delete the current update time row entirely (saving storage space)...

Augment and Prune a MySQL table

I need a little advice concerning a MySQL operation:
There is a database A wich yields several tables. With a query I selected a set of entries out of this database to copy these results into another table of database B.
Now the table in database B contains the results of my query on database A.
For instance the query is:
SELECT names.name,ages.age FROM A.names names A.ages ages WHERE ages.name = name.name;
And to copy these results into database B I would run:
INSERT INTO B.persons (SELECT name,age FROM A.names names A.age age WHERE age.name = name.name);
Here's my question: When the data of database A has changed I want to run an "update" on the table of database B.
So, the easy and dirty approach would be: Truncate the table in database B, re-run the query on database A and copy the result back to database B.
But isn't there a smarter way so that only new result rows of that query will be copied and those entries in database B which are not in database A anymore get deleted?
In short: Is there a way to "augment" the table of database B with new entries and "prune" old entries out?
Thanks for your help
I would do two things:
1) Ensure you have a primary key that's either an integer or a unique combination of columns at a minimum in database B
2) Use logical deletes instead of physical deletes i.e. have a boolean deleted column
Point 2 ensures you never have to delete and lose data, you just update the flag and in your queries put where deleted = 0 or where deleted is null.
When combined with a primary key it means everything can be handled easily by an INSERT ... WITH DUPLICATE KEY which will insert new rows and update existing ones - which means it can perform your 'deletes' at the same time too.
What you describe sounds like you want to replicate the table. There is no simple quick fix for what you describe. You could of course write some application logic to do it but it would not be so efficient as it would have to compare each entry in each table and then delete or update accordingly.
One solution would be to setup a foreign-key index between A and B and cascade updates and deletes to B. But this would only partly solve the problem. It would drop rows in B if they were deleted in A and it would update a key column in B if it were updated in A. But it would not update the other columns. Note also that this would require your table type to be INNODB.
Another would be to run inserts on B with A's values but use
INSERT ON DUPLICATE KEY UPDATE....
Again this would work fine for updates but not for Deletes.
You could try to setup actual MySQL replication but this is perhaps beyond the scope of your problem and is more involved.
Finally you could set up the foreign key index as described above and write a trigger that whenever an updates is applied to A then the corresponding key row in B is also updated. This seems like a plausible solution for you while not the cleanest I would admit.
It would seem that a small batch script run periodically on which ever environment your running on to duplicate the table would be the best to achieve what you are looking for.

Delete all fields in huge mysql table

I have a lot amount of db, now i need to delete some table, where is many data, about millions, but if i delete using sql syntaxis, phpmyadmin interface, or delete table, i still have some data after refreshing. How to delete clear all data in table?
The easiest way to ensure you get a table wiped is to use the TRUNCATE TABLE table_name statement. If you still have data in the table after that, it means something is constantly adding data to the table.