What is the best way to do this with mysql :
I have two tables in the same database (a table : Gene, and a table Gcur).
In table Gene, I have a column last_updated. In table Gcur, I have a column last_modified.
I'd like to synchronize column last_modified with column last_updated.
For example, I made an update of column last_modified (from table Gcur), and automatically column last_updated (from table Gene) is updated. Two tables are linked by an ID key.
It should be possible with triggers ? An idea ?
Thanks !
Yes, it is possible with triggers, and fairly trivial. The result would look like
CREATE TRIGGER au_Gcur AFTER UPDATE ON Gcur
FOR EACH ROW
UPDATE Gene SET last_updated = NEW.last_modified WHERE id = NEW.id;
Related
I have a script that updates a table with infomation and I want to update another table with some of it.
In Table: products I have a column called last_updated and I want that info to be copied into Table: products_booze which also has a column called last_updated
Any help would be great!
following SQL will do the trick
update products_booze, products
SET products_booze.last_updated = products.last_updated
WHERE products_booze.prod_id = products.prod_id
How to add or insert specific column from one table to other ? I tried writing like this
ALTER TABLE info_apie_zaideja
ADD SELECT info_apie_match.Rank AFTER 'Nick'
FROM info_apie_match;
or this
UPDATE info_apie_zaideja
ADD COLUMN SELECT info_apie_match.Rank AFTER 'Nick'
FROM info_apie_match;
but that did not work. Oh, and the table where I want to insert column is view table if that helps somehow. All answers will be appreciated.
You need to do this in two steps. First alter the table to add the new column:
ALTER TABLE info_apie_zaideja
ADD COLUMN Rank INT AFTER Nick;
Then fill it in by copying from corresponding rows in the other table:
UPDATE info_apie_zaideja AS z
JOIN info_apie_match AS m ON z.id = m.zaideja_id
SET z.Rank = m.Rank
I had to guess at the column that relates the two tables. Correct the ON clause to match your actual table relations.
Also, consider whether you really need the column in both tables. With this redundancy, you'll need to make sure that whenever you update one table, the other one is updated as well. Instead, you could just use a JOIN whenever you need the value from the other table.
I have a scenario that requires a value in a row of a table to be updated automatically whenever a row has been added or deleted in another table. I'm not sure how to do it.BTW I'm using phpmyadmin in order to manage my database. Thanks in advance.
pages Table
------------
page_no
no_of_choices
choices Table
-------------
page_no
choice_no
When I add a choice with choice number 1 and page_no, then the table page which has the row, page_no=1 should be updated with no_of_choices=no_of_choices+1
You can use triggers.
For example:
CREATE TRIGGER `test1`
AFTER INSERT ON `tbl1`
FOR EACH ROW SET NEW.upd_fld = new_value
Similarly could be done for delete.
You can also create triggers from phpMyAdmin
TABLE A: page_no, no_of_choices
TABLE B: page_no, choice_no...
With a relational database you very rarely want to have duplicate data. If something breaks at some point, you won't know which to trust - the rows in Table B, or the no_of_choices in Table A. A better solution is to do one of the following (depending on which table you are querying):
SELECT COUNT(no_of_choices) FROM B WHERE page_no = 1
or
SELECT A.*, COUNT(choice_no) AS choice_no FROM A LEFT JOIN B USING(page_no)
You get the same result, but now you have one record to go off of, so you won't have inconsistent data.
I want use a stored procedure to update multiple tables in a db. Each table has a GUID as the PK and there are FK's between the tables.
For example, one table is "Tool" with a column ID (the guid) and another table is "Type" with ID as guid again. There is a column in Tool called "TYPE_ID" that is a FK to the table Type with Type's Guid stored in it. I want to first update the Tool table and then after, update the Type table based on that FK.
UPDATE Tool
SET Name=#Name, [Enabled]=#Enabled, TestMode=#TestMode, SerialNumber=#SerialNumber,
Andon=#Andon, ChimeZone=#ChimeZone, Number=#ToolNumber
WHERE ID=#ID
Update Type
SET Type=#Type
WHERE Tool.ID=#ID AND
Tool.TYPE_ID=Type.ID
I know that this code is incorrect for the second update, but this is the gist of how I would like to be able to do it. Is there a way to not have to SELECT the FK Guid, store it, and use it the next update? If that is the only way, how do I do that?
Write your second update like this, joining the Tool table to the Type table:
UPDATE ty
SET Type = #Type
FROM Tool to
INNER JOIN Type ty
ON to.TYPE_ID = ty.ID
WHERE to.ID = #ID
Actually you can UPDATE FROM clause:
UPDATE Type
SET Type=#Type
FROM Tool INNER JOIN TYPE
ON Tool.TYPE_ID=Type.ID
WHERE Tool.ID=#ID;
See http://msdn.microsoft.com/en-us/library/ms177523.aspx for full UPDATE syntax.
I have one table (TABLE A) in my mysql database which has the listed fields:
row1_id - PK
row2_id - PK
row3_data
What I would like to do is, every time a record is inserted or updated in table A, I would like to call a trigger to write me the new information of table A in another table, TABLE A_LOG, which has the following fields:
row0-modification-id - PK
row1_id (from table A) - PK
row2_id (from table A) - PK
row3_data (from table A)
the Table A_LOG should then have 3 PKeys, two from the new inserted/updated record of table A and other that indicates me the number of the modification (1, 2, 3..)
Thank you very much for your help.
Create TableA_Log to autoincrement row0-modification-id.
I bet you don't exactly need this table to have row0-modification-id AND row1_id and row2_id all as the PK.
CREATE TRIGGER dbo.trg_TableA_ins_upd
ON dbo.DatabaseName
AFTER INSERT, UPDATE
AS
BEGIN
INSERT INTO TableA_Log (row1_id, row2_id, row3_data)
SELECT row1_id, row2_id, row3_data FROM inserted
END