ER-Diagram: Entity with unique attribute relative to another entity - entity-relationship

In an E-R diagram:
Let's say we have an entity owner and an entity pet.
A owner is related to a pet through the relationship adopts.
This relationship is one to many, i.e an owner can adopt many pets, but a pet can only be adopted by one owner.
Now each pet has a name that is unique among the pets owned by an owner, i.e an owner cannot have two pets named "Squiggles".
However, different owners can utilize the same pet names, i.e. Jack can have a pet named "Squiggles", and so can Jill.
How do I represent the pet's name in an E-R diagram?

It sounds like this is what you want:
http://en.wikipedia.org/wiki/Weak_entity
A pet would be unique identified by the owner id and its name.
The example in wikipedia sounds analogous to what you are looking for.
While this might not be exactly how you implement it in a DB this is how you would diagram it.

Related

Does Core Data take advantage of "Fetch Index(es)" containing a relationship when JOIN-ing behind the scenes?

Let's say I have a two Entity kinds in Core Data:
Pet
Owner
Assuming there's a one-to-many relationship between Owner and Pet (since each owner could have multiple pets)...
Is it better practice to define a Fetch Index on both the "name" and "owner" properties of the Pet Entity, or just the "name" property, if I wanted to fetch all Owners(s) with an Pet named "Charlie" for example?
This is assuming an Owner predicate like: pet.name == "Charlie"
(... I know the answer for SQLite, but since it's an abstraction on that, I wasn't clear on the expectation.)

Relationship in ER model

I am learning about ER modelling about Database systems. My problem is that there is a entity called books,enitity named user and I want to create a borrows relationship between user and book with attribute issue date. I modelled it as described but it was pointed that borrows cannot be a relationship because a same user can borrow a book twice. Can anybody explain me what this is as I am using issue data as an attribute so records in borrows relationship would not collide as I will use PK as userid,bookid and issue date. How can I model this accurately? I am a little confused in this.
In the ER model, entity relations consist of attributes of a single entity set, in which the PK identifies only one entity set. Relationship relations have a composite PK that represents two or more entity sets.
Your question uses a composite PK that represents two entity sets (userid and bookid) and a value set (issue date). Strictly speaking, it's neither an entity relation nor a relationship relation. It's a combination of a relationship relation (two entity keys) and a weak entity set (issue date functions similar to a weak key). If we want to be creative, we might call it a weak relationship.
If I was forced to draw an ER diagram for this, I might present it like this:
The ER model isn't a complete logical model (unlike the relational model) and there are some situations which aren't handled well or at all. This is one of those situations.
As per description, User and Book are the entities.
One user can borrow an instance of book.
Similary, one user can borrow multiple instances of book, whether It can be same
instance or various instances.
So every transaction between the User and Book has the Issue Date.
Neither the user nor the book has the Issue Date.
Here, the relationship between User and Book are Many to Many.
The Bridge table is Transaction. We can name it as Borrow also as per your interest.
Now, The user has one to many transactions.
Every Book has one to many transactions.
Every transaction is a combination of a User and Book.
Note: Since every user can have the same book multiple times and at the same day. So we can have a composite primary key of user_id, book_id and Issue_timestamp as there is a chance of redundancy in the Issue Date in the same combination.

Entity Relationship diagram: composite attributes vs entity

So I have this small problem, I am trying to make an ER Diagram and I have a student. For the student entity it says:
Record which school a student is in. A student must be attending one school. All schools contain at least one student.
You are to record the following information schools: their names(must be unique), # of students attending, and the principal name.
When I read this, it seems like "school" is a composite attribute or an entirely separate entity. Since it says "A student must be attending one school. All schools contain at least one student" should I make it an entity because I can't think of how to show this relationship via a composite attribute.
As I see it, school is a separate entity. A composite attribute would be something like a 'name' which consists of a 'first name' and a 'last name'. The school only consists of school and student only consists of student in your example.

ios Core Data Design Issue

I am trying to create a Core Data model for my application. I have having issues in defining the attributes and relationships. Here's the scenario
I have a Contract Management Application. So the basic entity is Contract. It has a Tenant but can have multiple Landlords. Tenant and Landlords can either be a Person or a Company type.
I have defined the entities as shown in the table below.
I only created Landlord and Tenant to reference them easily from Contract. I feel that Contract should have a To-Many relationship with a name of landlords as a Contract can have multiple landlords. The problem I have is that each Landlord could either be a Person or a Company. Same goes with the tenant, though it would always be one for a Contract.
Here's the updated Snapshot of Landlord
& Tenant Entities
Can you please help me define what relations should I create from Contract to Landlord && Tenant ( along with reverse relations if any ) ?
Here's the ER Diagram that I could think of my head ! ;)
Can you please help me define what relations should I create from Contract to Landlord && Tenant ( along with reverse relations if any ) with each of them being either a Company or a Person ?
Thanks
I've set up a few data structures similar to yours.
What works for me in this situation is to set up an Entity that defines the roles that each person plays in a contract and another Entity that defines type of role.
So in your case I'd think about using this structure...
Delete the Entities Landlord and Tenant;
Add a new Entity called ContractRole with Relationships contract, company, person and contractRoleType.
Add a new Entity called ContractRoleType with Attribute type and Relationship contractRole;
to the existing Entities Contract, Company and Person, add a new Relationship contractRole;
the Entity ContractRoleType will have at least two types "Landlord" and "Tenant".
So for each Contract you will enter data for at least two ContractRoles, one with ContractRoleType "Landlord" and one with ContractRoleType "Tenant".
I find this type of data structure very flexible.

What is a many to many relationship?

I'm a bit confused on what a many to many relationship is. I'm wondering if the following is a many to many relationship:
A student at a school has many clubs. A club at a school has many students. Let's say that the student has many attributes: firstname, lastname, phone, age, email, etc. A club only has one attribute: a name.
When I make a new club, I want to be able to give the club a name and one or more students. Upon making the club, I want that club to be associated with those students and those students to be associated with that club.
When I make a new student, I want to be able to give the student a firstname, last name, etc, and one or more clubs. Upon making the student, I want that student to be associated with those clubs and those clubs to be associated with that student.
I also want to display a club's students and a student's clubs on their show pages.
I've read that a many to many relationship is when you have a join table that lets you access common attributes of the resulting students and clubs, but there are no common attributes in my case.
Do I have a many to many relationship here? If so, do I use a HABTM or has_many, through relationship?
Actually yes you DO have common attributes.
You stated yourself that a Student has many Clubs
And a Club has many Students.
What is in common? Students and Clubs.
What now follows is to define what a Student and a Club actually are, which you already did.
A Student is a combination of firstname, last name, etc... What you have not specified is what makes a Student UNIQUE. A club also must be defined as to what will make it UNIQUE. While for academic purposes, you could say the name is what makes it unique, in real live, that would probably not be the best solution.
Usually for performance purposes, each student is given a unique Autoincrement ID (which is a number).
Same thing can be done with the Club.
You create a 3rd table which is what creates the Many to Many relation.
In that 3rd table, you have 2 columns. One with the Unique Index for the Student, and the other column with the Unique Index for the Club. You simply add an entry on that table in which you wish to relate a student to a club.
Since you can have many students assigned to the same club, and you can have many clubs assigned to the same student, you have a many to many relation.
Edit: As mentioned in another answer, your 3rd table should also declare the combined indexes as unique, so that you don't add the same entry multiple times.
You have a many to many
Create an id for each table that is unique for that table typically an auto incrementing int.
Then a third table that is a junction/intersect table call it X.
Put a row in X with the student id and club id if the student has the club and vice versa. It would have a unique composite key in table X across both id's in it.
The composite would guarantee no duplicate rows in X
Yes indeed there is a many-to-many relationship here, use HABTM. Also, why do you say that there are no attributes in common? Club names and student names are definitely common attributes in this case.

Resources