Recursive Table How Reference To Load Data? - sql

I am planning on using a recursive table in my data model so that I can support an undetermined number of children that can live under a parent. My example is a Table of Contents where I don't know how deep my subsections will be under a chapter.
The issue I am stumbling over is what techniques do folks use to populate there DB once they have defined a recursive table? By that I mean if I have a list of items that refer to a chapter -> Section -> Subsection...when I load the Chapter, Section, and Subsection into the model I need to identity the lowest level of the hierarchy and assign that value to the Item I am loading (Foreign Key - I would assume) so that I can always get all of the info about that item.
So for example:
Item: 2A-GHI: Chapter: 2 Section: A SubSection: GHI
If I have my data loaded like
ID|TOCID|TOC_VALUE|PARENT_ID
1|Chapter|2|-1
2|Section|A|1
3|SubSection|GHI|2
How do i tie the item to GHI so that I can set the FK to the Recursive Table for that Item?
Do you use all three values as a Key and set that as another column in the table so that on load you can identity the lowest level?
Like So:
ID|TOCID|TOC_VALUE|PARENT_ID|Key
1|Chapter|2|-1|2
2|Section|A|1|2_A
3|SubSection|GHI|2|2_A_GHI
I can load the recursive table and I am using a CTE to recurse the data, but I am not sure what the best method is to load the recursive data and tie that data into the model so that my item has a FK to the Table of Contents data.

You need a column for the row's parent ID.
ID|TOCID|TOC_VALUE|parent
1|Chapter|2|0
2|Section|A|1
3|SubSection|GHI|2
a parentID of 0 or null means it's a root (chapter) node

Related

PostgreSQL reference multiple values?

I'm trying to create a small company database as a personal learning experience in PostgreSQL.
I'm trying to follow Derek Banas tutorial (which is really helpful) but there is something different how my products are processed.
So far I've made these tables as shown in the image Table list
In the components table I have a row 'where_used' where I wanted to put an reference INT to the product where it is used.
But as you can see, some components are used in multiple products. And I can only store one value?
So my question is:
Can someone point me in the right direction on how to get this working?
I tried googling for a solution,
But as I'm such a noob I do not know how to get the right keywords for the search...
In data modeling this technic of having multiple sub entities from a super entity is called Inheritage...
As an exemple, you can have a table that is called "Vehicle" and some child tables that are called "Planes", "Boats", "Car".
The main table (Vehicle) will have shared properties
The "Plane" table will have some properties specific to planes and so on...
Of course the Primary KEY of vehicle will be shared in an exclusive mode betwwen "Planes", "Boats" and "Cars"...
From a child point of view, the tables will have a PRIMARY KEY that is also a FOREIGN KEY.
So in your case you have a table 'COMPONENTS' that is linked to the table 'PRODUCTS'
You want to have a column in your table 'COMPONENTS' that you'll call 'where_used' that say if the component is used in x or y product.
There is different ways of doing it but this is what I would do :
Basically, you'll need 3 tables :
The first one is "COMPONENT" which store unique component values
The second one is "PRODUCT" which store every unique products
The third one is "COMPOSITION" (or whatever name you like) which is the associated table between "COMPONENT" and "PRODUCT" which will look like this :
COMPONENT_ID
PRODUCT_ID
1
1
1
2
2
3
2
1

One to many relationship data addition for multiple level not working properly in MS Access

I have a MS Access application with four levels of one to many relationships. The levels are as below:
Scenario ->> Attractiveness->> Metrics->> Parameters
I have a set of attractiveness, metrics and parameters list all the data are independent. We can map one attractiveness with any number of metrics and for that metrics I need to map parameters. The first higher level is scenario so I am going to create scenarios. So for a scenario we can have only two attractiveness. Then for that two attractiveness user can map any metrics available. In the next level user need to select the parameters for given attractiveness->>Metrics levels.
Please find the below image of my relationship between these tables.
I am using the datasheet view to enter data for each level in a sub form. Please find the below data entry sub form
Please find the below relationship keys:
Scenario Table PK-> (Scenario ID with no Duplicates) -> Attractiveness Table FK ->(Scenario ID with Duplicates)
Attractiveness Table PK ->(Attractiveness ID with no Duplicates) - > Metrics Table FK->(Attractiveness ID with Duplicates)
Metrics Table PK ->(AttractivenessMetrics ID with no Duplicates) - > Parameter Table FK->(AttractivenessMetrics ID with Duplicates)
I can able to add level by level (i.e adding attractiveness, Metrics then update the attID, MetricID, attMetricID in Metric table) then add parameter level is working fine. But if I add parameter without updating the previous metric level then all the parameter are added into the first metric. Since I have no AttractivenessMetrics ID combination in Metrics Table.
I fixed the above issue by using form timer control to keep track of the active control. So while users moving into parameter level updation form metrics level then it will update the metric tables fields this will fix the no parent issues in parameter level. It works well and easy to update the data.

Implement a multi level outline structure

I need to setup and manage a multi level outline structure for requirements management in Microsoft Access 2013.
outline needs to work much like a word outline where each record can have only 1 parent, but can have 1 or more siblings (sibling order does matter) and 0 to many children.
Need to support as many outline levels as possible, current data exceeds 15 levels but is being reduced where possible.
need to be able to determine if a record has children, has siblings, and what is its parent.
need to be able to delete all children of a record when a record is deleted.
need to be able to add new records either after or below a record in the outline structure
need to be able to renumber the outline when adds, deletes, copies, and moves are done within the outline structure.
current approach is a table which contains a unique ID, 15 numeric fields for each individual level # of the outline #, and using a sort of the numeric fields to organize into the outline order.
running into several question related to this situation:
is this the best data structure approach or should I move outline info to a separate table?
given that I am in a query with an outline order sort, can I write a VBA function as a calculated column to determine a record's parent, if it has siblings, and if it has children.
is it possible/practical to perform an sql select in vba to query the same table/query I am in to get a records parent, children, siblings, as a calculated column?
is it possible to use data triggers to support any management of this outline structure.
Example of the outline structure:
1
1.1
1.1.1
1.1.2
1.1.2.1
1.1.2.2
1.1.3
2
2.1
2.2
2.3
2.3.1
2.3.1.1
2.3.1.1.1
This ain't going to be at all trivial in Access. For maximum flexibility in the number of levels I would not use separate fields for the level numbers but would instead have a self-referencing table with columns ID, NodeNumber, OutlineNumber, ParentID (NodeNumber would be a single number indicating which sibling this record is under a given parent - 1, 2, 3 - and OutlineNumber would be the full Outline reference for this record - 1.2.1, 1.2.2, 1.2.3). You would use the ParentID to identify a record's parent; query all records with the same ParentID to find siblings; query records where the ParentID = current record's ID to find children. You're going to have to write some serious VBA code to control inserts, updates and deletes, and definitely don't let people add data manually into the table. Unfortunately Access does not have data triggers.
[EDIT] Apparently I'm wrong! Access does have an equivalent to SQL-Server "trigger" functionality called data macro, but I've never used it. Maybe this could help you?

How to Create a Hierarchy in SSAS when duplicate attributes exist at the Root

I have a Dimensional table structure which resembles the following:
Ideally the hierarchial representation should be
CodeClassDesc --> CodeDesc
So A would be a Parent to A and B; B would be a Parent to A, B and C in my Analysis Server Cube. The problem is that because CodeClassDesc has multiple entries in the table it produces multiple duplicate Parents in my Cube with a single corresponding Child Element per Parent which is not what I'd consider a true Hierarchy. Or at least not what I am looking for the expected results.
I believe this is possible in SSAS without having to manipulate the data within the table via a VIEW but I don't know what I'm missing.
I've tried defining the CodeClassDesc and CodeDesc fields as a composite key but that doesn't work, yet I am almost certain there is a way to do this.
After attempting every conceivable permutation of methods to acquire resolution for this, I concluded that normalization of the required attribute was the only way to resolve the issue of having multiple entries of the Parent for every corresponding Child element.
So I created a VIEW of the table using a DISTINCT SELECT of only the CodeClass and CodeClassDesc fields in my DSV (DataSource View) in the Cube. Then I set the CodeClass field as a logical Primary Key and created a relationship between it and the CodeClass field of the main table.
I then used the CodeClassDesc field of the VIEW to create the top-level parent in my Dimension, which gave me only 1 distinct record for each value; and added the CodeDesc fields from the Table to create the Child Relationships. Works like a charm so I guess the answer would have to be that you cannot create a Parent Hierarchy consisting of a single Value per Parent if the source has multiple records.
In the dimension structure, you should change the property KeyColumn of the attribute "CodeClassDesc" to a composite key containing both "CodeClassDesc" and "CodeDesc" then change the NameColumn property to show itself

QTreeView and QSqlQueryModel -- loading children as needed from sql database

I have a following problem.
I'm trying to implement a model for my QTreeView that would load dynamically data from sql table.
Table looks like this:
CREATE TABLE xcmObjects
(
id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
id_parent INT DEFAULT 0 NOT NULL ,
title TEXT
);
id_parent contains id of a parent record - so they form a structure.
I'd like my model to load data from this table only when needed. In other words I don't want to load the full structure into memory, instead I wan't to read children of only those nodes that have been opened by the user.
QSqlQueryModel and QSqlTableModel seem to work only for flat tables.
I think that one solution to this problem would be to implement custom QAbastractItemModel class and inside store seperate QSqlQueryModel instances for each open node (including the top level invisible parent). And then rewrite each method and forward requests to apropriate models.
Maybe there is some simpler solution ? :-)
Thanks for help.
I don't think it would be too difficult to work through a subclass of QAbstractItemModel to do this. Your top level would be all items in the table where parent_id is 0. Store the ID of each item as the internal data for the QModelIndex classes, and then you can use the parent index passed in to various functions to construct new queries for the data.