Many-to-many Relations
The SaveToDB add-in has a special mode to edit data of many-to-many relations.
For example, you have three tables: employees, territories, and employee_territories.
The employee_territories table contains two columns of foreign keys: employee_id and territory_id.
When you get data from the employee_territories table without filtered values on the ribbon, you get actual data like
employee_id | territory_id | _State_ |
---|---|---|
Employee 1 | Territory 1 | ✓ |
Employee 1 | Territory 2 | ✓ |
Employee 2 | Territory 3 | ✓ |
The add-in adds the checkbox _State_ column used to add or delete relations.
You can create a pivot table using the Excel data table as a data source.
Place the foreign key columns to axes and the SUM of the _State_ column to cells.
You will see a table like
Territory 1 | Territory 2 | Territory 3 | |
---|---|---|---|
Employee 1 | ✓ | ✓ | |
Employee 2 | ✓ | ||
Employee 3 |
The add-in adds validation lists of foreign key columns as axis items.
Moreover, you can double-click in the pivot table to change the data of the underlying table and save changes from the pivot worksheet!
Returning to the table, you can select a value on the ribbon.
For example, if you select Territory 1, you will have the following table:
employee_id | territory_id | _State_ |
---|---|---|
Employee 1 | Territory 1 | ✓ |
Employee 2 | Territory 1 | x |
Employee 3 | Territory 1 | x |
So, you can double-click on the _State_ column value to add or delete the relation.
If you select Employee 1, you will have a table like this
employee_id | territory_id | _State_ |
---|---|---|
Employee 1 | Territory 1 | ✓ |
Employee 1 | Territory 2 | ✓ |
Employee 1 | Territory 3 | x |
To implement these features, the add-in creates the following automatic configuration, reading the database metadata:
ID | TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | EVENT_NAME | HANDLER_SCHEMA | HANDLER_NAME | HANDLER_TYPE | HANDLER_CODE |
---|---|---|---|---|---|---|---|---|
s07 | employee_territories | employee_id | ValidationList | s07 | employees | TABLE | [employee_id],[last_name] | |
s07 | employee_territories | territory_id | ValidationList | s07 | territories | TABLE | [territory_id],[territory_description] | |
s07 | employee_territories | employee_id | ManyToMany | ATTRIBUTE | ||||
s07 | employee_territories | territory_id | ManyToMany | ATTRIBUTE | ||||
s07 | employee_territories | AddStateColumn | ATTRIBUTE |
It uses the ManyToMany event types for foreign key columns.
It uses the AddStateColumn event type to add the _State_ column.
Also, it creates validation lists using the primary key tables.
You can create such configuration for other objects too. For example, you can load data from stored procedures or views.
You can also disable adding automatic configurations using the DoNotAddManyToMany event type in the xls.handlers table.