0

Is there a way to populate a table dynamically from another table?

That is I have 2 table

  1. Customer which has field regionName
  2. Region which has field Name.

On populating customer table,it should populate the distinct values of customer table regionName into the Region table Name field automatically.

Is it possible? Then how?If not How to populate the value in both table from a single webservices?

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101

1 Answers1

0

That depends on what you mean with "populating".

If you want to do this only once, use a statement like this:

INSERT INTO Region(Name) SELECT DISTINCT regionName FROM customer;

If you want to do this whenever a new customer is added, you need a trigger:

CREATE TRIGGER add_customer_region
AFTER INSERT ON customer
FOR EACH ROW
BEGIN
    INSERT OR IGNORE INTO Region(Name) VALUES(NEW.regionName);
END;

This trigger requires a UNIQUE constraint on the Region.Name column.

CL.
  • 173,858
  • 17
  • 217
  • 259