I wanted to know how to represent a relationship between two entities which means "atleast".
Eg- A song must be part of atleast one album.
If "A song can be part of one or more than one album" means a one to many relationship between song and album, then what would the ER diagram for the Eg given above be?
It's represented on an ERD by a double line (called a participation constraint).
If one song can be on many albums, you need a junction table, Like so:
album -+------< album_song >------+- song
Related
I have following entities with a many-to-many relationships.
i'm making a small playlist app where the entity named Entity is for the playlists and the songs entity is for the songs. at the moment i have a viewcontroller where you can add playlists to the Entity and a viewcontroller where you can add songs to the song entity. The problem is that all the videos are shown in every playlists. How can i hook this relationship up between these 2 entities?
Do i need to make a attribute in Songs which hold a playlist id? or how will the Entity know which song belong in which Entity object?
First of all, I would suggest to choose slightly different names for the entities
and relationships, in particular the singular form (Song, Playlist) for the entity
name, and the plural form of the destination (songs, playlists) for to-many relationships:
Now the objects are "hooked up" by setting the relationship between them.
Assume that you have a song and a playlist:
Song *songA;
Playlist *playlistB;
Now you can either call
[songA addPlaylistsObject:playlistB]; // (1)
or
[playlistB addSongsObject:songA]; // (2)
to establish a relationship between these two objects.
It does not matter whether which one you call because "songs" and "playlists"
are inverse relationships. songA is added to playlistB.songs,
and playlistB is added to songA.playlists.
Check this
This is picture of the answer! You will need to use a table to couple the to tables together.
I'm developing a simple iOS application with the following database setup. There is an Athlete entity which has a many to many relationship with Workout. Workout has a to many relationship with Workout Scores. Athlete<<->>Workout<->>Workout_Scores. I think i've set up my model incorrectly though. I was thinking athletes can share workouts (e.g. multiple athletes have the same workout object), or, an exercise may be exclusive to one person. However, the exercise score is strictly for one athlete, not shared. You can have up to 1 score for each workout. 2 athletes can have the same workout, but their score should be separate. Did I set up my model correctly? Should the score entity be related to athlete, not workout?
It sounds like you want to use Workout_Scores as a sort of join table (although that of terminology isn't appropriate for an object-graph framework like CoreData). Your Workout_Scores entity should have two to-one relationships to Athlete and Workout. Athlete should have a to-many relationship to Workout_Score (an athlete may have many workout scores), and Workout should have a to-many relationship to WorkoutScore (a workout may have many workout scores that originated from a single or from many different athletes).
Here's what I propose for your data model:
I am trying to understand the concept of ER modelling, but I do not yet succeed. I have designed the ER model about movie database, but I do not know wheather it is a good design and how to connect the entities:
between Actor and Film i want to say "actor can play in each film only once" and at the same time "many actors can play in many movies" -- is it 1 to 1 relation or many to many?
and HOW do we need to think about entities ans relations between them? relations to one user, one film, one actor, one director, or in general?
UPDATE: new question : should the relation between Director and Film be 1 to many or many to many? I want to say : "one director can have many films && many directors can have may films" ??
Think about it like this: There are many movies. There are many actors. It makes sense that you would only want to include each actor in a particular movie once, but otherwise you want to be able to "mix and match" the movies and actors to express the relationship.
Looking at your diagram, you don't seem to have any fields which express the relationship between Film and Actor - those lines need to match actual fields. Read up on foreign keys: http://en.wikipedia.org/wiki/Foreign_key
The relationship between Actor and Movie that you want is actually many-to-many. You can express this with a "join table" (you'd need to add this to your diagram).
Something like this would work:
FilmActor
-------
uidFilm
uidActor
And put a unique constraint on those two fields together so it can't be duplicated (i.e. the same Actor can't appear in a Film twice)
In Core Data of Xcode 4.3.2, an Ordered To-Many relationship is modelled with NSOrderedSet. It works well until I found the need to have repeated items in the relationship; it should really be modelled in a NSArray.
For example, in a music app, I have the following songs: SongA, SongB, SongC ,
I may want a party play list where people insert songs in any order and could be repeated. The list may look something like:
[SongC, SongC, SongA, SongC]
The way Core Data currently works, the list would become:
[SongC, SongA]
I.e., all repeated items are dropped, as it is modelled with sets.
So, coming back to my question: what is a good way to model repeated items in order in a relationship in Core Data?
Relationships are sets (and ordered relationships are ordered sets, but still sets); sets by definition contain unique objects. So you can't put duplicate objects into a relationship either way.
Whether you use the ordered-relation feature or not, you'll want to go back to the abstract ER model to find another way to turn your conceptual relationships into a Core Data model... it might help to think about how you'd do it in a plain SQL (or SQL-like) database and then come back to what Core Data does beyond SQL.
It sounds like you're making something akin to iTunes playlists, no? A model that might work for that would go something like:
Playlist <--->> PlaylistEntry
PlaylistEntry <<---> Song
The PlaylistEntry entity represents one instance of a Song's inclusion in one Playlist. You can have multiple PlaylistEntrys that reference the same Song in a single Playlist, and you can add other attributes to the PlaylistEntry to keep track of other things (like song order, if you're not using an ordered relationship). As a bonus, you can use that to add other features if you like -- say, to make a playlist that plays three different snippets out of one long track.
As rickster said, relationships as managed by Core Data uniquely associate entities between them. Even if Lion's Core Data (is supposed to) supports ordered relationships (supposed to, because in practice it won't work, the feature is buggy, barely usable), they still are relationships that follow the relational database model.
And so you have to manage the association by yourself, and you most certainly have to manage the ordered part of the association by yourself too.
Score <->> ScoreSong
ScoreSong <<-> Song
With ScoreSong having the following properties:
ScoreSong:
- score: -> Score
- song: -> Song
- order: integer, indexed
Then you have to use a Fetch Request with a sort descriptor for the key order, which will return an ordered NSArray of ScoreSong. You can ask the fetch request to prefetch the songs, then you can create the songs array, still properly ordered, with a single call to valueForKey: #"song".
Of course you have to create different ScoreSong for a single Song when you need to include than song more than once in your Score. That's the whole point of the added indirection.
I have the following scenario.I need to recommend artists to users. How should this be modeled? I am looking for suggestions on this.
I was thinking. Based on following criterias:
When a user listens to a song, the songs genre gets recorded somewhere with +1
When a user "likes" a song, the songs genre gets recorded somewhere with +2
This way I could list all artists based on the genre that has the highest points.
Even still, should I have a new model called "Recommendation" and have it embedded into user?
I am open to all kinds of suggestions.
A User could have many Votes. A Vote could be an up-vote or down-vote (using single table inheritance). You could then have a Recommendation model that determines what a user might like depending on up and down-votes (find related artists from up-votes and subtract artists related to down-votes.) A User then could have many Recommendations.