Translating ER diagram to Relational model - database

I have a question about the ER diagram below.
Question: If there are 2 relationships born and live from actor to place entity.
Do they both have the same foreign key which is place_no? So does that mean I just put the foreign key in Actors relational model and that's it or do I have to make a table for Born?
Because an actor is born in a place and lives in a place... so how would it be possible to differentiate between 1 place_no FK?

You have to specify the arity of the relationships. You can map the conceptual 1:n relationships directly in relational model using just foreign keys. For n:m relationships you should use junction tables.
Since it seems that the relationships here are both n:1 since a person can be born in just one place and (based on your comments) we can also assume that a person can only live in one place at the time we can map these relationships as foreign keys.
As an example in SQL (assuming we have two relations actor and place, the code is in CREATE code of actor):
[...]
born int REFERENCES place(place_no),
lives int REFERENCES place(place_no),
[...]

Related

What's the relationship between the two entities?

The ER diagram for a database is given below:
Now, what's the relationship between planes and flights entities?
I'd say one to many, but that'd be wrong because while one plane can have more than one flights, many flights can't have one (single) plane simultaneously.
So, what exactly is the relationship?
I'm new to databases. Please tell me if I'm wrong.
First, your diagram isn't an ER diagram, it's a table diagram. ER diagrams must be able to represent the ER model, which supports ternary and higher relationships, weak entity sets and identifying relationships, and other concepts for which table diagrams don't have notation. Proper ER diagram notation is generally referred to as Chen's notation.
Now, ER relationships are easily identified by looking for entity domains (columns that identify entity sets) that appear in the same table. These are usually indicated by PK and/or FK, but they need not be.
When you have a binary relationship (two entity domains in the same table), look at the unique constraints on these domains:
One-to-one relationships require two separate unique constraints, one for each domain.
One-to-many relationships require a unique constraint on the domain on the many side.
Many-to-many relationships require a composite unique constraint on both domains together.
One-to-many relationships can be denormalized into the entity relation of the entity on the many side, since the unique constraint required for the relationship matches the PK for the entity relation. One-to-one relationships can be denormalized into either entity relation. A many-to-many relationship requires a composite key, and must be represented as a separate relation since the composite key doesn't match either entity relation's PK.
In your example, (flight_num, planeID) represents the relationship, and since only flight_num is uniquely constrained (due to being the PK of the flights relation), this is a many-to-one relationship: each flight is associated with exactly one plane, while each plane can be involved in many flights.
Here's a visual reference in which unique constraints are indicated with underlining:
Many people still use terminology and concepts from the old network data model, such as conflating relationships with FK constraints and entity sets with tables (which is why table diagrams are often mistakenly called ERDs). I highly recommend reading Codd's paper "A Relational Model of Data for Large Shared Data Banks" and Chen's paper "The Entity-Relationship Model - Toward a Unified View of Data".
As you said the relation is One-To-Many because if it is a Many-To-Many relation it must have a junction table between these 2 tables (Tbl_Plane_Flight) , another thing is that Plane_Id is referenced in Flights table.
But there must be a validation rule (or a constraint in flights table) for this relation, that a plane cannot have many flight as the same time

Converting relationship with attribute to relational schema?

Between two entities is a 1 to many relationship but this relationship has an attribute of its own.
When converting these entities to a relational schema what will happen to this attribute? Does it get joined into the entity on the many side of the relationship like a foreign key does? I don't think it should be another separate relation because that is like saying it's an associative entity which it's not.
Any help is greatly appreciated.
In ER as described by Chen, each entity relation and each relationship relation would map to a separate table (except weak entities/identifying relationships). However, one-to-one and one-to-many relationships commonly get denormalized to reduce the number of tables. In this case, attributes of the relationship become attributes of the determining entity.
Associative entities occur when a relationship is the subject of a relationship. Recording a relationship in a separate table doesn't make it an associative entity. Nor do attributes make a relationship into an entity. Attributes on relationships are quite normal in the ER model.

How many tables will the Relational Schema have for this ER Diagram?

I have the following as my ER Diagram. I am still learning DBMS, and trying to translate this ER Diagram to a Relational Schema. I know that each entity in the ER Diagram will have a separate table. However, I am not sure what to do about the relationship for this particular ER Diagram. We were told that each relationship between the entities will have a table too. Therefore, do I need to make separate for relationships in this ER Diagram as well? But, there are no attributes of the relationship. Also, I am having a confusion in what sort of relationship is this exactly? Is this one to many?
I am attaching the link to the picture of the ER Diagram. Please guide me in the right direction. Thanks!
Your diagram isn't an ER diagram in the original sense of that term. In the entity-relationship model, relationships are associations among entity sets and were meant to be implemented as tables. For example, your AUTHOR_BOOK, CAST and PURCHASE tables are relationship tables that associate two entity sets each (keep in mind that relationships aren't limited to just two entity sets). Note how the relationships are represented using the keys of the entity sets e.g. (actorID, inventID). The same pattern can be found in some of your other tables, i.e. (inventID, publisher), (inventID, director), (inventoryID, genre), (inventoryID, supplier), (receiptID, inventID) and (receiptID, customerID). These are your relationships - not the crow's foot lines which are just foreign key constraints. In Chen's original notation, the relationships would be indicated using diamond shapes between and connected to the two entity types. Also, Chen would've made a separate relationship table (aka junction table) for each of these relationships.
Your table diagram shows 14 tables. Following Chen's method, there would be 19 tables:
Your title references a relational schema. Note that relational schemas are not restricted to the entity-relationship model, but can represent any set of normalized tables (1NF or higher). The number of tables would partially depend on the level of normalization.
But, there are no attributes of the relationship.
This is not correct. Your Purchase relationship shows two attributes - quantity and amountPaid. Note that an attribute is a mapping from an entity or relationship set to a value set. Thus, I'm not counting the entity keys as attributes of the relationship. I also modeled Book's pubYear as an attribute of the relationship between Book and Publisher.
In practice, I would probably denormalize relations with the same determinant, which give a physical schema similar to your original diagram, though implementing every relationship table separately does have some advantage in easing schema changes when relationship cardinalities change.

Convert 1 to 1 relation in ER to a Table

If we have two entities in an ER model.
Sport and Organization.
We have a relationship between these two. it is 1 to 1 relationship.
One sport belongs to one Org. one Org. handles one sport.
If i want to covert this to relations, I will have one Sport relation one ORG. relation.
How should i create a relation for the 1 to 1 relationship?
1 to many is easy as is many to many.
Passing a "1 to 1" relationship from ER model to Relational means that you have to create a relation putting a foreign key in either the first table or the second one. You can choose the table that makes more sense to have the foreign key and it will depend on the particular usage.
In these cases, I used to put the foreign key in the table that looks more concrete. For example you could have the foreign key in the sports table. If in the future the requirements changes (you relationship to "1 to N") and it is possible that an organisation could handle several sports, you do not have to change anything as you already have the foreign key in the sports table.

Difference between one-to-one and one-to-many relationship in a database

When having a one-to-one relationship in a database the other table has a foreign key ID (in this example). And in a one-to-many relationship the table contains many foreign keys.
But does the database know whether this is a one-to-one or one-to-many relationship? Are the relationships that I make in an ER-Diagram only to indicate where there should be foreign keys when making the actual tables?
What is the difference between one-to-one and one-to-many relationship in a database?
In a sense, all the relationships we talk about are not known to the database, they are constructs we have invented to better understand how to design the tables.
The big difference in terms of table structure between one-to-one and one-to-many is that in one-to-one it is possible (but not necessary) to have a bidirectional relationship, meaning table A can have a foreign key into table B, and table B can have a foreign key into the associated record in table A. This is not possible with a one-to-many relationship.
One-to-one relationships associate one record in one table with a single record in the other table. One-to-many relationships associate one record in one table with many records in the other table.
To enable one-to-one relationship you need to add unique constraint to foreign key. It is not possible to have two foreign keys for each table as it will be impossible to create records.
Im having trouble understanding what the actual question is.
Your analysis is for the most part correct, in that if you have a 2 tables, and table2 has a foreign key to table one, it could be either a one-to-one or a many-to-one.
Your sentence "And in a one-to-many relationship the table contains many foreign keys."
The table of the 'many' side still contains one column that is a foreign key, its just that more than one row can have the same foreign key value (many rows point to one parent).
Also note that you can put the foreign key on the parent table, to the child, instead of the other way around. In this way, you can prevent one-to-many if you want to do that. Also note that in this way, more than one parent can share a child, which might or might not be what you want.
The database-level equivalent of a 1:1 vs. 1:m is having a unique index on the foreign key column. Note that this will only work for 1:1, NOT 1:0..1, as null is considered when evaluating uniqueness. There are workarounds for this restriction, but that's it at the basic level.
Similarly by example, a product has only one product code, so it's one-to-one relationship (product <-> ABC123), but a customer can purchase more than one product, so it's one-to-many relationship (person <->>>product).
well, you are right, this relation is important for you, but not for db itself. When you have two tables, one with your basic information, and another one with your detailed information.. for both tables you are you, so it is one-to-one relation, you can not map your data to somebody else.
Now add third table "cities" and one of your information points to city you live in - this is example of one-to-many (one city can be used, and should be used for many people).
one-to-many / one-to-one just show how your tables interact. And all the time, you want to "save" rows/columns in table not duplicating them you will use one-to-many relation with another table. Or many-to-many :)
Let's assume you have a table with two attributes A and B. If A is a candidate key and B is not then the relationship between A and B is 1 to many. If both A and B are candidate keys then the relationship is 1 to 1.
Given table A and B if
A and B have a strict 1 to 1 relationship
For every B instance, there will always be an A instance
The best approach is to make the primary key of B also a foreign key referencing A. This is also called "Table per Type Inheritance" and the "is a" relationship. There are other ways to enforce a unique foreign key, but using the primary key makes the relationship clear in the schema and in ER diagrams.
Of course there are always other scenarios, and if your design doesn't meet both of the criteria above, you'll have to use another approach.

Resources