I have this table structure and simple relationships:
and sample data in the table:
Company
Company names are unique and should not be repeated:
+------------+---------------+
| Company_ID | Company_name |
+------------+---------------+
| 1 | Company_name1 |
+------------+---------------+
| 2 | Company_name2 |
+------------+---------------+
Location (Locations should be assigned to a specific company):
+-------------+------------+-------------------------+
| Location_ID | Company_ID | Location_name |
+-------------+------------+-------------------------+
| 1 | 1 | Company1_Location_name1 |
+-------------+------------+-------------------------+
| 2 | 1 | Company1_Location_name2 |
+-------------+------------+-------------------------+
| 3 | 2 | Company2_Location_name1 |
+-------------+------------+-------------------------+
| 4 | 2 | Company2_Location_name2 |
+-------------+------------+-------------------------+
Data The data in the table should depend on the selected company, and the locations should be only those that occur in the company:
+---------+-------------+------------+------------+------+
| Data_ID | Location_ID | Company_ID | data_value | date |
+---------+-------------+------------+------------+------+
| 1 | 1 | 1 | 5 | date |
+---------+-------------+------------+------------+------+
| 2 | 2 | 1 | 2 | date |
+---------+-------------+------------+------------+------+
| 3 | 3 | 2 | 3 | date |
+---------+-------------+------------+------------+------+
| 4 | 2 | 1 | 1 | date |
+---------+-------------+------------+------------+------+
| 5 | 4 | 2 | 6 | date |
+---------+-------------+------------+------------+------+
| 6 | 4 | 2 | 7 | date |
+---------+-------------+------------+------------+------+
The main dependencies that should be met:
- Company names should be unique and attempts to add the same company should be blocked
- Location names should be assigned to a specific company, but they may repeat and a location may appear in several companies but have a different
Location_ID
- Adding values to the date table should depend on:
- company (we choose a specific company for which we add values)
- locations (locations must depend on company)
For example:
When adding values for a company with Company_ID = 1
, I should only be able to add Location_ID
that occur under that company.
If I want to add a value in the data
table for Company_name1
then the only available values for the Location_ID
column in the data
table, should be: Company1_Location_name1
and Company1_Location_name2
and I can't have values there from another company (i.e. Company2_Location_name1
and Company2_Location_name2
)
At the moment it works badly:
when adding values to the data
table I can select a company, but then I have locations available and I can add values that do not make sense - for Company_name1
I can add a location from Company_name2
but it should be blocked.
How can I solve such a problem? Add some additional table which will be responsible for particular pairing?