Synchronization of tables PhpMyAdmin

0

I needed a help in PHP My Admin about synchronizing fields in different tables and in the same database. For example:

Database: Test
Test Table1
columns:
- col1
- col2

Test table2
columns:
- col1
- col3

It needed that, when values were inserted in table 1, table 2 would have column 3 null and columns 1 would be equal.


Note: I use PHP My Admin version 4.7.4

    
asked by anonymous 15.02.2018 / 20:05

1 answer

0

You can use a TRIGGER for this, see below:

Table 1:

CREATE TABLE Teste1(
   ID INT AUTO_INCREMENT PRIMARY KEY,
   Nome NVARCHAR(30) NOT NULL
);

Table 2:

CREATE TABLE Teste2(
   Teste1ID INT PRIMARY KEY,
   Algumacoisa NVARCHAR(50) DEFAULT NULL,

   CONSTRAINT FK_Teste1_em_Teste2
   FOREIGN KEY (Teste1ID)
   REFERENCES Teste1.ID
);

Trigger used to insert new record after an INSERT in Table1:

CREATE TRIGGER Gatilho_Teste1
AFTER INSERT
ON Teste1 FOR EACH ROW
INSERT INTO Teste2(Teste1ID) VALUES(NEW.ID);

See Working:

After each record is inserted in table1 the trigger is triggered and it inserts the msm id in table 2, already in column3 you requested null, then only declare DEFAULT NULL in the table construction.

    
15.02.2018 / 21:43