Copy VARIOUS fields from one table to ONE field from another in MYSQL [closed]

-4

Good morning, I just registered and first of all I want to thank the willingness of this community to answer the questions we know least about. What I need is if someone can help me by informing me if it is possible to copy one or more fields of a table to a single field of another table. The idea is that when you change a field of a record in a table, as many records as changed fields are generated in the destination table. The purpose of the destination table is specifically to record the history of changes of a record in the source table .--- I would appreciate it if you can illustrate me with an example .--- I am quite new to mysql, but very self-taught. Already GRATEFUL!

if ALTER TABLE Tabla1(campo1, campo2,...) INSERT INTO Tabla2.campo1

I do not know if I explain myself .---

    
asked by DieWalever 10.09.2018 в 13:23
source

1 answer

-1

If I did not understand wrong, from what I read in the comments, you are trying to get a log. for that you would have to create a Trigger.

The trigger can be complicated to enterder if you have little experience in DB's.

What you have to understand in practice, is that the Trigger defines the action that the database has to exercise when an event is verified.

Trigger at line level: this trigger is executed once per line (row) for each DML instruction. is declared in the creation of the trigger indicating the clause "for each row".

Trigger at the institution level: it is executed only once per DML action, eg you make an insert of 20 lines, the trigger at institutional level comes into operation only once and not 20, so this type of level does not come used very frequently.

BEFORE and AFTER This attribute tells the trigger when it has to start working.

before: it is executed before the DML code, it is usually used for the normalization of the inserted data, that is, you add a line and in the name you put something like: "pLUTO" but the name you want with the initial capital, if you use the before, you can modify the data and normalize it: "Pluto" before it is inserted in the database.

after: it is executed after the DML code, this is usually used to create logs.

how to create a trigger:

create [or replace] trigger [nombre del trigger]
[before | after] [update | delete | insert] on [tabla de origen]
[clausula] // es decir for each row, ya que es a nivel instutucional de default
begin
   //tu codigo 
   // aca harias el insert en la tabla de destino
   // si se utiliza el trigger After update (el que te sirve para el log)
   // para alcanzar los datos y ver los cambios tendrias que hace de la siguente manera: 
   //:old.nombreCampo  para obtener el dato antes de la modifica y
   //:new.nombreCampo para obtener el dato despues de la modifica, osea el valor actual.
end; 

I hope this can help you ☺

    
answered by 10.09.2018 / 14:41
source