I have a table named myTable in MySQL. The myTable records will be truncated or inserted by some jobs. I want to create a View from myTable. But the view shouldn't get updated if myTable records are deleted or truncated. The View should refresh only if there is any insert in myTable. I am not sure if this is doable. Please advice.
Related
I have two similar table new data insert to table_1 with. i want write a trigger in phpMyAdmin to update old data in table_2 with new data that insert to table_1 and delete table_1 new data. ech row have unique id.
It look like simple but I do not have MySQL knowledge.
Thanks.
You can not update the table on which the trigger is executed. MySQL locks the table when inserting to it and does not allow updating it when it is locked (which may cause a deadlock)
I think the better solution for you is to use a stored procedure. And also it seems weird to me what you are trying to achieve , you can just update table_2 with new data which is more performance wise and makes sense.
I'm trying to, once a table is updated with some new information from an insert, update at the same time another specific column(s) of other table(s) on the same database.
Is like, ok, I save in table1 my age, my country and my name and I have to save at the same time my age in table2, field age2 and my name in table3, field name2. Something like that. But doing it automatically.
I read about the triggers but also read that with triggers you CAN'T specify the name of the table.
Can anyone please help me? I'm pretty lost.
You definitely can and must specify the name of the table when creating a trigger!
Triggers are defined for data modifications on tables.
You should use
CREATE TRIGGER `copy_on_new_data` AFTER INSERT ON `your_table_name` FOR EACH ROW BEGIN [...] END;
and insert the desired data to your other tables using the new qualifier, like
INSERT INTO `other_table`(name) values (new.name);
I'm using sql server 2008 as a backend for my project.
My database table not contains any date related field then how can i find which records are inserted / updated for particular date ?
I want to perform above operation on existing data from my table
Is it possible? If yes then please let me know.
For existing records in the table, I dont think you can find it. But for the new records, you could create a TRIGGER which will update another table with the primary key and a date field
You can enable Change Tracking option on this table.
Check Here
You could add INSERT and UDPATE triggers to your tables that write the ID of the record being updated and the current date/time to an additional audit table. Then you can query the audit table
CREATE TRIGGER RecordInsertTrigger ON MyTable FOR INSERT
AS
INSERT INTO
AuditTrail
(
RecordID,
ActionDate
)
SELECT
RecordID,
getdate()
FROM
inserted
And then create a similar trigger for UPDATE.
You can extend the AuditTrail table to include other relevant information live UserID etc.
I'm having an issue with finding and deleting duplicate records, I have a table with IDs called CallDetailRecordID which I need to scan and delete records, the reason there are duplicates is that I'm exporting data to special arching engine works with MySQL and it doesn't support indexing.
I tried using "Select DISTINCT" but it dosn't work, is there is another way? I'm hoping I can create a store procedure and have it run weekly to perform clean up.
your help is highly appreciated.
Thank you
CREATE TABLE tmp_table LIKE table
INSERT INTO tmp_table (SELECT * FROM table GROUP BY CallDetailRecordID)
RENAME table TO old_table
RENAME tmp_table to table
Drop the old table if you want, add a LOCK TABLES statement at the beginning to avoid lost inserts.
what if I wanted to update the records in the table by altering values in one of the columns?
I have records in the table that have one column empty(null values). I want to change these values and insert values from another table into those records.
Basically I have a table with one column empty. I do not want to append to the end of the table but start inserting from record 1.
For the existing records, you would have to use UPDATE to update that one column, WHERE thatColumn IS NULL.
Shouldn't the values in that column have some relation to the rest of the record? I could understand initializing the existing records to a non-null value, or using an UPDATE query to populate data from another table in that column, but all related to the original row...
UPDATE old SET old.badColumn = new.newData
FROM oldTable old
JOIN newTable new on old.someID = new.someID
This would find the related data in newTable matching oldTable, and update the badColumn to some data from newTable... let me know if you need more help.
See the "Using the UPDATE statement with information from another table" section from this page of SQL Server Books Online.