I have two amounts of dynamic entities. For example
B1, B2, B3, B4, B5,... I don't know how many B's i have but i will have a specific table in SQL for each B. On the other hand i have a dynamic amount of A's each represented as a single row in Table AEnum.
Now i want to have a Mapping. The mapping looks like that:
Each a can have each entity of B or not => 1/0 => boolean.
For example:
A1 => B1 (true), B2 (true), B3 (false), B4 (true)...
A2 => B1 (true), B2 (false), B3 (false), B4 (true)...
A3 => B1 (false), B2 (true), B3 (true), B4 (true)...
So for each B i made a column with B's name in the Table AHasB, just storing a tinyint. This is however not very dynamic. I want to make it as dynamic as possible.
Possible idea:
Table BEnum stores Bid and BName (the Tablename of B's instance), AEnum stays the same just being a number of rows storing stuff about each A. Then AHasB would look like:
A1 => "B1, B4, B12"
A2 => "B9, B10"
Just storing it as a single string which i parse on the logic side, not the persistence side. With that solution the following would be the case:
When adding a B: I only need to add the B table manually and update the BEnum table
When deleting a B: Delete the table, update the BEnum table accordingly and update AHasB to delete all B instances out of the strings that just have been deleted
When adding a A: Just insert a new row in AHasB
When deleting an A: Just remove that row
I think that would be the most dynamic approach. Or is there any better way to save this in a SQL database?
If anyone asks: B1, B2, B3,... are all names for quite different tables. For example: cars, hobbys, etc. A1, A2, A3, are persons and the relation can be read as "For person X i want to know which hobbies he has, what cars he drives, etc."
Someone any better ideas?
In general, you can represent a relation between any entity sets as a table containing a column for each entity set. For example:
persons (person PK, name, age, ...)
cars (car PK, make, model, ...)
car_owners (person PK/FK, car PK/FK)
In car_owners, including both columns in the PK ensures the pair is unique, meaning a many-to-many relation. Limiting the PK to the one or other means entities in that column are required to be unique, hence a one-to-many relation.
Related
On the R(ABCDE) table tuples if A and B are same, then C also same value. I need a trigger that prevents to violate this rule when update the table.
This is too long for a comment.
This question indicates a short-coming in the data model. If C is dependent on A and B, it should have its own table. I think the data model needs to be fixed.
You should have an AB table:
AB_ID A B C
In this table the combination A/B would be unique.
Your table would then remove the three columns A, B, and C and replace them with AB_ID.
Voila! You can model the data correctly and you don't need to use a trigger.
I have 2 tables named A1 and B1. And both the tables has commonly 3 columns but in different variable. Considering A1 as the master table I have to map B1 but the values in A1 (those particular ID's(3 columns)) will not be present in B1 and vice versa. And the ID's present in both table contains duplicates. I have to find the relationship (map) the two tables without adding any extra attribute in SQL and POWERBI
Here is the sample table:
Copy the ID from A1 table to a new table. (Use 'Add as new query' option)
Merge the ID from B1 table to the new column you just created.
Right click and remove duplicates from the new column. Now you have a master ID column without any duplicate values.
Map this column with ID in A1 and B1.
there is a parent table, he can add more than one children email ids(no. unknown)(primary key).
And a child can have one or 2 parent email ids(mother/father)
This will be a many to many relationship?
could you guide me on constructing this relationship in phpmyadmin.
A many to many relationship in a normal relational database, like MySQL, is normally constructed using a cross link table. That is a table that links to records in the other tables together.
For instance, when you've got tables A and B, you create a third table, C, which can look (very simplified) like this:
A B C
== == ====
A1 B1 A1B3
A2 B2 A2B1
A3 B3 A2B2
A3B2
So in this cross link table, you can see that record A2 is linked to two records in B, just as B2 is linked to two records in A. Of course, this schema is simplified. A1 is actually the keyfield of table A and A1B1 is a primary key that consists of two fields, one having a foreign key constraint to the key of table A and one having an FK to table B.
It doesn't always have to be a many to many relationship. For instance, you can say that a person has to parents, which also are persons. Since it's not very likely for people to have more that two biological parents, you can safely give a row in person a fatherid and a motherid, both of which refer to the same person table. So, like always, it depends on your exact needs.
Working in Oracle SQL Developer (3.2.20.09).
I have to create a view from two source views. For example, Fields 1-3 exist in View A and Fields 4-6 exist in View B. There is zero overlap. I cannot select both views and pull all their fields into a subsequent view because combined they have too many fields (the two source views have combined well over the total fields allowed in a single view).
Is it possible to tell my pl/sql package procedure: get this field from view A. If it's not there, go to view B?
Or is a better solution to use all_tab_columns or some other meta(?) solution, look to see who owns Field 1 and grab it from there?
I don't have any experience with either of the above two options so would appreciate a lot of guidance.
Or is there simply a better third option?
It's not possible to create a view with more than 1000 columns. If the combined number of columns of your view A and B is less, you can simply create a third view using SQL in a worksheet:
CREATE OR REPLACE VIEW view_3 (f1, f2, f3, f4, f5, f6)
AS
SELECT a1, a2, a3, NULL, NULL, NULL FROM view_a UNION ALL
SELECT null, null, null, b4, b5, b6 FROM view_b;
In SQL, Select into ... copies rows into a different (backup) table. Is this possible if the backup table has different structure (or different column names)? If not, what is the best way to achieve this?
Here is what I want to do: TableA has columns a1,a2,a3. I want to copy some rows from this table to another table TableB which has column b1,b2,b3,b4. Content of a1 to go into b1, a2 to b2 etc.
The column names do not matter at all, as long as data types match (or can be cast in the assignment).
If the data types of the columns don't match, try casting the values accordingly. Just try with small dummy tables. Be sure to list the target columns explicitly to avoid confusion. Like this:
INSERT INTO TableB (b1, b2, b3)
SELECT a1, a2, a3
FROM TableA
WHERE <some condition>;
More details in the SQLite manual here.