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)...
Related
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.
So I have timestamp column in table that I wanted to update every time that row is accessed, including INSERT, SELECT & UPDATE statements.
(Note that not just when the row is modified)
Is there built-in feature for MySQL to achieve this?
There is a feature that does this! In MySQL they are called Triggers. With Triggers you can run additional MySQL code when performing an action (i.e.: when INSERTING, SELECTING, UPDATING, DELETING, etc. a table).
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.
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.
i've been looking though different tutorials online,but i can't seem to find what i need.
I need to copy a record into a history table, every time it is updated.
Is there a way to do it with triggers without having to type out all of my data fields?
It would help if you posted a schema of your tables, and what exactly you want to be inserting into your history table, but for now I'll make some assumptions about the table you're updating, and what you want in your history table. As an aside, this trigger will not work for a copy-paste... as each subsequent update to a record will be unable to be inserted into your history table because of primary key violations.
CREATE TRIGGER trg_History AFTER UPDATE ON my_table
FOR EACH ROW INSERT INTO history_table VALUES (NEW.col1, NEW.col2, OLD.col1, OLD.col2 ... etc)
the NEW keyword refers to all the data being inserted/updated and OLD refers to, well, the old data before being overwritten.
Again, please be more specific with the information you need to be inserting, and what you've tried so far, as we can only help you with general syntax at this point in time.