I'd like to improve my understanding of cardinality constraints in ER diagrams.
I have two entities:
User
Location
But, I want the relationship between these two entities to be many-to-many (a user can be in many locations and a location can have many users).
To do this I need to introduce an association class UserLocation.
Is it correct to say I now have 3 entities?
If I were to draw an ER diagam of the above, would I draw in the UserLocation entity, and would the cardinality look like this?
User 1 ------ * User Location * ------ 1 Location
You do not need a third entity.
In the Entity world, this is modeled like this:
The entity User has a list of Locations.
The entity Location has a list of Users.
In the Relational world, this is modeled like this:
A table USER, with primary key USER_ID
A table LOCATION, with primary key LOCATION_ID
A table USER_LOCATION, with two foreign keys, one to each of the above tables.
I believe that in the Relational diagram the "intermediary" is not visible. So, I think you would need something like this:
User * ------- * Location
It all depends on the type and level of entitiy mapping you are doing.
You can express the relationship as it is
Entity 1 <> Entity 2
Such as using the 'Crows Feet' to represent the many relationship.
When normalising the map you would break up the many to many relationships with an intermediate table, this would normally contain the primary keys of both of the many tables
Entity 1 > Entity 3 < Entity 2
Where Entity 3 (your intermedite table) would hold PK's for both tables, and ocassionally any other data, such as a unique ID etc.
Related
I have been using Core Data to model my database. I have 2 entities that are in many to many relationship. Each person can have many addresses, and on each address multiple persons can live.
Now i would like to add property to this relationship. For example one person - address will have label home, other person - address can will have label mama's place.
I can't add this property on address entity, because same address will have different labels for different persons.
Since relationships are modeled like NSSets, I don't see a way to do what I want.
Can this be somehow done?
It is not possible to add attributes to a many-many relationship directly. The Apple-recommended approach (see "Modelling a relationship based on its semantics" in the CoreData Programming Guide) is to replace the many-many relationship with an intermediate entity, to which you add the attributes. Each of your existing entities will have a one-many relationship with the new entity.
In your case, you might have something like this:
Person <--->> PersonAddressDetails <<---> Address
You can then add the label attribute to the PersonAddressDetails entity.
In Core Data I have a House Entity which has a relationship to a Room entity. The Room entity itself has a many-to-many relationship with a Door entity.
Two rooms might share the same door, so here is what I need to know:
Is it possible to have two Room entities with a relationship to the same Door entity. And when updating the properties of the Door entity, it will be the same object and gets updated in both Room entities.
Is it possible to move the Door entity from one relationship to another, without copying it?
You have a many-to-many relationship between Room and Door. This means that those relationships behave as a Set, so you can call myDoor.doors.remove(room), or .insert() to manage the entity. These are reciprocal as well, like all Core Data relationships (see here for further details). Do note your naming scheme is a bit confusing, and for example I'd recommend switching to using myDoor.rooms
How can I represent my lookup tables in technical reports?
In other words, the ER model is used to represent a database,
but what about lookup tables?
To recover a conceptual model (entity sets, attributes and relationships) from a physical model (tables and columns), we first have to understand the logical model. This means understanding the domains and functional dependencies which are represented by the lookup table.
Lookup table is a common term which can mean different things. I generally understand it as a table which represents a domain with a surrogate key, and associates it with a name and/or a few other attributes. In the ER model, these would be simple entity relations, and leaves / terminal nodes in the graph of entity sets.
If a lookup table records facts about only one type of thing (represented by the key of the lookup table), then you can represent that type as an entity set (rectangle) with an attribute (oval) for each dependent column, and draw relationships (diamonds) to connect it to other entity sets as required. Look for foreign key columns / constraints in other tables to find these relationships.
For example, consider the following physical model:
CarMake and CarModel are examples of lookup tables. This isn't a very good model, since in the real world CarModelId determines CarMakeId, while the model treats them as independent elements in CarSales. However, since the point of the example is to focus on lookup tables, I'll use it as is.
In this case, CarMake and CarModel describe a single entity set each. Their functional dependencies are CarMakeId -> CarMakeName and CarModelId -> CarModelName. In CarSales, we've got CarSaleId -> RegNumber, Price, SoldOn (attributes) and CarSaleId -> CarMakeId, CarModelId (relationships).
In this case our ER model is similar to the physical model:
However, in some cases, you may find multiple types of things combined into one lookup table due to the similar physical structure. This doesn't affect the logical or conceptual models, but makes it more complicated to recover since we have to understand how the table is used to unpack it.
I'm trying to create a relation using Fluent API, but I can't get it to work. There are 2 tables: SecurityEntities and SecurityEntityRelations. The SecurityEntities table contains users and groups, the SecurityEntityRelation table is an association table that contains relations between the users and groups (it defines which groups contain which members). I already mamaged to relate the 2 entities within EntityTypeConfiguration:
HasMany(se => se.Groups).WithMany(g => g.Members).Map(seg =>
{
seg.MapLeftKey("ser_EntityName");
seg.MapRightKey("ser_MemberOf");
seg.ToTable("ser_SecurityEntityRelations");
});
This populates the SecurityEntity.Groups and Members properties. But one of the security groups is being concidered a "primary" group, which should be stored in a separate SecurityEntity.PrimaryGroup property. In SQL, this is implemented by a column in the SecurityEntityRelation table, which marks the relation as being "primary". (I know, a self-referencing foreign key in the SecurityEntity table would have been a better solution, but this was designed quite some time ago, and we're stuck with this implementation because our legacy code is also written this way) So, now my question is: how can I create an EF mapping like the one above, which specifies the same relation, but only when column "ser_IsPrimaryGroup" equals a fixed value of '1'?
In the example of using "intermediate join entity"
To find out who one person’s friends are, you have to aggregate all the friend destinations of the friends relationship, for example:
NSSet *personsFriends = [aPerson valueForKeyPath:#"friends.friend"];
Is above line of code getting a given person's MUTUAL friends? or just ONE-WAY friends, which means only getting "peoples who are treated by this given person as his friends"?
I am not certain, because "To find out who one person’s friends are" sounds like ONE-WAY friendship (that could be why there is a strange relationship befriendedBy represents those who count the source as their friend. FriendInfo represents information about one friendship, “in one direction.” .)
This is a really confusing example. There are two possibilities.
In most cases being "friends" is a mutual thing. In this case you would have a self-referencing many-to-many relationship of a Person, perhaps called friends. The relationship would be mutual.
You seem to be implying that it is possible to add another person as a friend even if that person is does not reciprocate. In order to lift the confusion give this many-to-many relationship another distinct name, e.g. contacts. This would be the Persons that have been added unilaterally. In Core Data, all relationships are best modelled as mutual, so you can use another relationship potentialFriends that is the inverse relationship of contacts. Maybe there are better names, but you get the idea.
The intermediate join entity is only necessary if you want to store additional attribute with a particular relationship, e.g. the date a contact request was made. In this case, you would have the join entity e.g. friendLink, which would have a to-one relationship to two distinct Persons. You can model the state of the link (unilateral or mutual) in this entity.