How to INSERT INTO one table and one of the columns automatically 'INSERT INTO' another table?

Viewed 16

I have created two tables one named 'A' and one named 'B'. They both share a column called ID.

CREATE TABLE A (
ID int NOT NULL,
random1 char(10),
random2 char(10)
);

CREATE TABLE B (
ID int NOT NULL,
random3 char(10),
random4 char(10)
);

I'm trying to make it so when I INSERT INTO Table A, it copies/inserts itself into Table B:

INSERT INTO A (ID, random1, random2)
VALUES (123, 'Value2', 'Value3');

SELECT * FROM TABLE A should return

| ID | random1 | random2 |
|:---- |:------:| -----:|
| 123  | Value2   | Value3 |

and SELECT * FROM TABLE B should return

| ID | random3 | random4 |
|:---- |:------:| -----:|
| 123  | NULL   | NULL |

I tried to use relationships/Primary-Foreign connections to achieve it but to no avail.

This is probably not the best way of making the table but I have already finished most of the other work and this is one of the last things to do.

0 Answers
Related