Entity Relationship Diagram: Composition of primary and foreign keys - foreign-keys

I have defined the following tables with their attributes:
Table A) Table B) Table C)
- ID - ID - ID1
- Name - xxx - ID2
- Address - yyy - zzz
- ...
- ...
I have two questions:
1) In Table A), ID is my primary key. The ID attribute in Table A) and Table B) are the same and I am not sure if in Table B) ID can be used as a primary and foreign key? Apparently, ID is also a primary key in Table B) but at the same time a foreign key for Table A). Is that allowed?
2) In Table C) I need both attributes ID1 and ID2. None of the two by itself is a primary but they ned the attribute zzz to be. However, the two attributes ID1 and ID2 are more or less interchangeable so that I have the possibility to create two different primary keys: 1) ID1 + zzz OR 2) ID2 + zzz. I am not sure if it matters which key to choose or if this scenario is allowed at all. Or is the primary key in this particular case the combination of all 3 attributes ID1, ID2 and zzz?

If ID in A and B is the same then you can merge the two tables and put same ID:
Table AB)
-ID
-name
-address
-xxx
-yyy
so ID should not be the same...
You can use the primary key as combination of ID1 and ID2

Related

PowerBI filter one table with multiple columns from another table

Here I have two tables:
Table A: col_A, col_B, col_C, metric_1, metric_2, metric_3
Table B: col_A, col_B, col_C, metric_X, metric_Y, metric_Z
I may need to put them in the report with col_A, col_B, col_C as shared filters. col_A, col_B, col_C are many to many relationship, for example, age, country, domain. How could I achieve this?
The solutions I may know are:
Pull column col_A, col_B, col_C as filters from table 1, but in this case table 2 doesn't have any relation with table 1 and the filter won't work for table 2. And if I add relation of table 2 with table 1 for col_A, then I couldn't next also add relation for col_B or col_C as only one relation could be added.
Another solution is that I would extract col_A, col_B, col_C as a new table for dimensions shared between table 1 and table 2. Then the filters may have better performance as there is less data. However, how could I apply the shared dimension table filter to table 1 and table 2? Or is there way like filter could achieve this?
ForAll(Table1, Collect(col_A, Filter(Table2, col_A in FullName).FullName))
Thanks.
In my opinion, you have two main choices here:
Calculate common dimension tables separately for each Col_A, Col_B, Col_C
Create a common key in each Table A, Table B and create one common dimension table
Common dimension tables for each column
For my simplified model of just two common columns, I have created one separate table per dimension:
The code to create Dim_A is just to use DISTINCT and UNION to get all the possible values from both tables:
Dim_A =
DISTINCT (
UNION (
DISTINCT ( 'Table A'[Col_A] ) ,
DISTINCT ('Table B'[Col_A] )
)
)
It is common to hide the key columns on the many-side of relationships, so I have done that in the model too.
This is the approach I would use if this were my data model, especially if Table A and Table B have columns that are somehow related to Col_A, Col_B, Col_C and give these important context. This approach leaves you with greater freedom to normalize your data model.
Common key in each table
In Power Query (or where you define your tables), add a column that concatenates the important columns into one key column. In Power Query you can simply add a new custom column and the formula will be:
[Col_A]&[Col_b]&[Col_C]
You need this common column in BOTH tables, since Power BI does not allow relationships formed by a combination of columns, only single-column relationships. Once this is done, you add a single table in the data model using the same style of DAX code as used previously, but you now also need more columns for it to make sense:
Dim_All =
DISTINCT (
UNION (
SELECTCOLUMNS (
'Table A' ,
"Key" , [Key] ,
"Col_A" , [Col_A] ,
"Col_B" , [Col_B]
),
SELECTCOLUMNS (
'Table B' ,
"Key" , [Key] ,
"Col_A" , [Col_A] ,
"Col_B" , [Col_B]
)
)
)
Connect your tables using the Key columns, but make sure to specify the correct 1-many setting, depending on your data it might create a 1-1 relationship instead, but you want to avoid this for this data model.
You need to create shared dimension tables and a star schema. When you create the appropriate 1-to-many relationships, the filter will propagate automatically for you.

Informatica Cloud Data Integration - find non matching rows

I am working on Informatica Cloud Data
Integraion.I have 2 tables- Tab1 and Tab2.The joining column is id.I want to find all records in Tab1 that do not exist in Tab2.What transformations can I use to achieve this?
Tab1
id name
1 n1
2 n2
3 n3
Tab2
id
1
5
6
I want to get records with id 2 and 3 from tab1 as they do not exist in tab2
You can use database source qualifier overwrite sql
Select * from table1 where id not in ( select id from table2)
Or else you can use informatica like below.
Do a lookup on table2, on join condition on id.
In exp transformation, create a flag
out_flag= iif(isnull (:lkp(id)),'pass','fail')
Put a filter next and keep the condition as out_flag= 'pass'
Whole map should be like this
Lkp
|
Sq --exp|-----> fil---tgt

SQLite how to limit the number of records

I want to limit the number of records in my SQLite table to for example 100 records, and then when I INSERT the 101th record, the first record (the oldest) be removed from the table. In other word, I want to prevent the table from growing more than 100 records and always have the last 100 records. Is there any setting or query with SQLite or should I handle it manually?
thanks in advance
You can do it with a trigger.
Say that your table is this:
CREATE TABLE tablename (
id INTEGER PRIMARY KEY,
name TEXT,
inserted_at TEXT DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now'))
);
In the column inserted_at you will have the timestamp of the insertion of each row.
This is not necessary if you declared the column id as:
id INTEGER PRIMARY KEY AUTOINCREMENT
because in this case you could identify the 1st inserted row by the minimum value of the id.
Now create this trigger:
CREATE TRIGGER keep_100_rows AFTER INSERT ON tablename
WHEN (SELECT COUNT(*) FROM tablename) > 100
BEGIN
DELETE FROM tablename
WHERE id = (SELECT id FROM tablename ORDER BY inserted_at, id LIMIT 1);
-- or if you define id as AUTOINCREMENT
-- WHERE id = (SELECT MIN(id) FROM tablename);
END;
END;
Every time that you insert a new row, the trigger will check if the table has more than 100 rows and if it does it will delete the 1st inserted row.
See the demo (for max 3 rows).

Power BI inactive relationships

I have an active relationship between table A and B that can’t be deactivated. Now, I have a inactive relationship between the same two table and I need to use this relationship as a filter for both. What can I do?
I have tried everything and I always manage to filter only one table.
Thanks everyone in advance
Ed
Concatenate both column of each table on which relations can be created and create a new relationship on this newly created column this will help u to filter the report using both the columns
Eg:- Table 1 - Relation ship columns are C1 and C2
Table 2 - Relationship column are C3 and C4
create a new column in table 1 say RC1 using dax Concatenate(c1,c2) and in table 2 say RC2 using
Concatenate(c3,c4) now create a realtionship on RC1 and RC2 (both side relationship)

Django foreign key column replace

I have class A in database for example:
ID COL1 COL2 COL3
1 a b c
And column B in database:
ID COL1 COL2 FK_COL
1 d e 1
2 dd ee 1
3 ddd eee 1
class B have foreign key to class A. How can I create a query to get all objects from table B where foreign key is 1 but instead of ID from A to be COL1 from A.
ID COL1 COL2 FK_COL
1 d e a
2 dd ee a
3 ddd eee a
If I understand your question correctly, you can use ForeignKey.to_field. From the documentation:
ForeignKey.to_field
The field on the related object that the relation is to. By default, Django uses the primary key of the related object.
EDIT: For the additional question in the comment, you'd do something like:
b_obj = B.objects.filter(a=1).values('colX')
Or, if you're coming from the A object:
a_obj = A.objects.get(pk=1)
b_objs = a_obj.b_subset.values('colX')
colX is the column you want displayed, and b_subset will depend on the name of the related model. If you do dir(a_obj) it will be obvious which one is it, but it should be the name of the model plus "_set".
SECOND EDIT: values can also follow relations, so to answer what is ultimately your question, you would do something like:
b_objs = B.objects.filter(a=1).values('COl1', 'COL2', 'FK_COL__COL1')
This gets you the value of the COL1 from the A table (note the double underscore to follow the relation).