Question

I am currently testing out something, and encountering an issue when trying to insert some values in a table.

I have 3 tables as following; Devices, Outdoor, indoor.

**outdoor**             --->          **devices**             <----- **indoor**

net_id(pk)                    net_id(fk)               net_id(pk)

I wanted to have a relationship so that a device can be either outdoor or indoor, by having either the net_id of indoor or outdoor. Thus I did the following:

ALTER TABLE devices
ADD CONSTRAINT o_relationship
FOREIGN KEY (net_id)
REFERENCES outdoor (net_id);

ALTER TABLE devices
ADD CONSTRAINT i_relationship
FOREIGN KEY (net_id)
REFERENCES indoor (net_id);

Now my problem is, when i am trying to insert a value in devices, i am unable to do so unless the net_id values belongs to both indoor and outdoor table. This means that let's say "net_001" exists in indoor and outdoor table it allows me to insert it in devices, but if "net_001"exists in indoor and "net_002" exists in outdoor and let's say I want to add "net_001" or "net_002" in devices it won't allow me to do so. Thus I want to be able to do insertion in devices table that as explained above.

Hope i was clear enough to explain the issue. Thank you in advance!

Was it helpful?

Solution 2

Maintain a relation table among devices and in & out devices.

Solution 1:

For Device_Relation Table

+------------------+--------------+
| Of Table         | Column       |
+------------------+--------------+
| Indoor           | net_id       | -- <--- pk
| Outdoor          | net_id       | -- <--- pk
| Devices          | device_id    | -- <--- pk
| Device_relations | d.device_id, | -- <--- pk
|                  | od.net_id,   | -- <--- fk
|                  | id.net_id    | -- <--- fk
+------------------+--------------+

Solution 2:

For In_Out_Relation Table

+------------------+--------------+
| Of Table         | Column       |
+------------------+--------------+
| Indoor           | net_id       | -- <--- fk
| Outdoor          | net_id       | -- <--- fk
| in_out_id        | in_out_id    | -- <--- pk
+------------------+--------------+

For Devices Table

+------------------+--------------+
| Of Table         | Column       |
+------------------+--------------+
| Devices          | device_id    | -- <--- pk
| In_Out_Relation  | io.in_out_id | -- <--- fk
+------------------+--------------+

OTHER TIPS

Use two columns in table device, one for indoor and one for outdoor, otherwise you can't distinguish which is indoor and which is outdoor because they may have same id as you describe.

ALTER TABLE devices
ADD CONSTRAINT o_relationship
FOREIGN KEY (net_indoor_id)
REFERENCES indoor (net_id);

ALTER TABLE devices
ADD CONSTRAINT o_relationship
FOREIGN KEY (net_outdoor_id)
REFERENCES outdoor (net_id);

Of course according to what you're telling the engine, the ID must belong to both tables.

What you're trying to do is modelling inheritance on a relational database, which is widely studied and is a classical design question when it becomes to choosing the method. In your case, device is the parent class, and indoor and outdoor are child classes.

As mentioned here, you need to choose from one of the methods, as I quote from the referenced link:

But you can do different approach, making that devices contains primary key, and indoor and outdoor references it...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top