1

I have two tables. Device and Device_Config. Device table has the following columns:

device_id
config_id
bla
bla2
foo
bar

And Device_config has the following columns

id
foo
bar

As you can predict, config_id on my Device table is a foreign key referencing id column on the Device_config So I have added this constraint on my Device table.

ALTER TABLE device
ADD CONSTRAINT device_config_id_fk FOREIGN KEY (config_id)
REFERENCES device_config (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE;

But this way, when a row in Device_config is deleted, the corresponding row on the Device table is deleted. However I want the opposite. When a device is deleted, I want the corresponding entry in device_config to be deleted. How can I achieve this?

2
  • 3
    So several different devices share the same config? I might not understand your business case, but I would have expected the foreign key to point from device_config to device, i.e. a device_id in the device_config table, not the other way round Commented May 10, 2013 at 13:43
  • 3
    Please add the real DDLS. IMHO most people read sql faster than your shorthand pseudocode. Commented May 10, 2013 at 14:57

1 Answer 1

4

then change the logic. Device table wont have *config_id* and device_config will have *device_id*. Relation is one device may have many device_configs.

Then

ALTER TABLE device_config
ADD CONSTRAINT device_config_fk FOREIGN KEY (device_id)
REFERENCES config (device_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.