How to model a recursive relationship in a relational model? - database

Is it possible to model a recursive relationship (such as seen in the photo) using the relational model? I find it hard to believe that the relation user would receive a foreign key from itself to signify the 1:n relationship it has with itself in the relationship "is_referred_by".
An entity in an EER Diagram with a recursive relationship

Related

How do you deal with Recursive Relationships N:M, in Database Relational Schema

When we have a N:M Recursive Relationship what is the best way to create the relational schema?
In various books I searched they analyze recursive relationships of 1:1 and 1:N but for N:M there is almost nothing.
Should I treat it like a new Relationship like we do in traditional N:M non-recursive relationships?
For example in this recursive relationship is it better to:
A.Create a new relationship
INVITE(InviterId,InviteeId,AcceptanceDate,InvitationDate) - in bold the Primary Keys . In this case they are also foreign keys.
////
B. Include this relationship in the Person Entity
PERSON(Id,Password,InviterId,InviteeId,AcceptanceDate,InvitationDate).
///
Definitions are recursive, not relationships/associations. You seem to be talking about FK (foreign key) cycles or the special case of that where a FK references its own table. A FK constraint says values appear elsewhere as PK/UNIQUE. Alternatively, that values that satisfy one relationship satisfy another in just one way. There's nothing special about this from a relational design standpoint. Most SQL DBMSs are needlessly poor when the explicit/declared FK graph is not a tree.
Whether you could or should combine your tables depends on the information modeling & database design method you are using plus relational database design principles. There are many different "traditions". Find & follow a published academic textbook on information modeling, the relational model & database design. (Dozens are free online, also slides & courses.) PS Information & manuals on a tool to manage designs does not constitute an introduction on how to design.
The diagram you give is a Chen original true pure ER (entity relationship) diagram. Under that method you can't combine your entity & relationship into a relationship because an entity needs to have its own box & table. But you could in the relational model & in pseudo-ER methods & products that don't use diamonds in their diagrams & in methods that allow more choice in mapping from Chen diagrams.

ERD Inheritance & Identifying Relationship simultaneously

Let's assume we have 2 ERD entities connected with 1 relationship. Visual Paradigm allows to mark such a relationship as "Subtype" and "Identifying" at the same time. And I just fail to imagine any case where such a combination could make sense.
Is that actually legal? If yes I would appreciate some example and a procedure of translating it to the relational model (what are the changes in comparison to only-"Subtype" or only-"Identifying" case?).
What Visual Paradigm calls an ERD is in fact a table diagram. Diagrams that don't use shapes for relationships don't support attributes and relationships on relationships, nor ternary and higher order relationships. If a diagramming notation doesn't support all the concepts of the Entity-Relationship model, we can't call its diagrams Entity-Relationship diagrams.
From an ER point of view, all subtype relationships are identifying relationships, since the parent entity set's identity is a component of the child entity set's identity. However, all identifying relationships aren't subtype relationships. The difference between a subtype and a weak entity set is that the latter uses an additional weak key component to distinguish multiple children of a parent. Subtypes depend only on the supertype for identity and so each instance of the supertype can have only instance per subtype.
For example, an entity set Person (identified by person_id) may have a subtype Employee (which is also identified by person_id). Compare this with an Invoice (identified by invoice_id) and LineItem (identified by invoice_id and line_number).
The ERD (conceptual model) translates into the following tables (physical model):

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.

Resources