What's the relationship between the two entities? - database

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

Related

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 to determine relationship (1:1, 1:n, n:m) between tables when reverse engineering

I have some tables already created in my database and now I need to draw ER diagram for these tables.
Identified the primary key and foreign key between the
tables
Determined pk-fk relationship using the keys
Now I need to identify the cardinality between the tables. How do I
do this?? Please let me know if there is any set of rules which I
need to consider while evaluating 1:1, 1:M and M:M relationships.
Let me take an example of two tables where am struck at:
Table A has a composite key made of pid and identitytype.
Table B has a composite key made of pid and maritalid.
Table A and Table B are associated with each other using pid and pid
is not null in both the tables.
Let me know what could be the relationship type between Table A and Table B whether it is 1:1, 1:M or M:M. Also, please let me know the sequence of steps that you followed in arriving at type of relationship conclusion.
Thanks,
Dex.
Relationships in the entity-relationship model are very different from what you've got in mind. Relationships are not represented by foreign key constraints - that's the old network data model, and it's limited to binary relationships. The entity-relationship model represents n-ary relationships between entity sets in tables, not between tables.
Foreign key constraints restrict the values of a set of columns to be a subset of the values of another set of columns. They are effectively used to enforce entity sets (domains) - for example, to ensure that every person_id column is a subset of the one that represents all known persons in the system. FK constraints are only used during updates - you could delete all FKs from a database and your SELECT queries and JOINs would work exactly as before, further demonstrating that they don't represent relationships.
A relationship is an association among two or more entity sets, each represented by a suitable key. Relationship instances are always recorded in rows of a table. For example:
A 1:1 relationship between a driver's license and a person would be represented by having the license key and person key together as two columns of a table, and both (separately) uniquely constrained. Whether this is in a license table, a person table, or a separate driver's license table is an implementation detail.
A 1:N relationship between cars and their owners would be represented by having the car key and person key together as two columns of a table, and the entity set on the many side uniquely constrained. This is often implemented in the table recording the attributes of the entity on the many side.
The preceding relationships are simple enough that we can denormalize them and don't need to record the relationship in a separate table. For higher relationships, though, we need separate tables:
An M:N relationship between students and subjects would be represented by having the student key and the subject key together as two columns of a table, and the combination of the two uniquely constrained.
An M:N:P relationship between suppliers, products and regions would be represented by having the supplier key, product key and region key together as three columns of a table, and the combination of the three uniquely constrained.
An M:N:1 relationship between regions, products and suppliers (a sole mandate) would be represented by having the region key, product key and supplier key together as three columns of a table, and the combination of region and product keys uniquely constrained.
See the pattern?
Every role/component of a relationship can have a foreign key constraint defined if the characteristic predicate of that entity set (its required attributes) is represented in a different table. That means a single n-ary relationship can require n different FK constraints.
To determine the cardinality of a relationship from an existing table:
Determine the entity sets represented in the table. Not all columns represent entity sets - some represent value sets, meaning the values have meaning themselves as labels or measures.
Determine which combination of entity sets are uniquely constrained together. These are the many sides of the relationship, and we'll give them variables like M, N, P, etc.
Every other entity set is dependent on the previous combination and represented by a 1 in cardinality.
It's not quite that simple. It's possible for a table's key to involve value sets in addition to entity sets. In these cases, we've got a weak entity/identifying relationship/subtyping situation. These are usually (but not always) 1:N relationships in which the child entity's key partially overlaps with the parent entity's key.
For more information, I recommend Peter Chen's paper The Entity-Relationship Model - Toward a Unified View of Data.
You should look at this SO question:
postgresql-describe-table
In reality, without looking at the schema definition, without a contra-positive example, you have no way of knowing if the table is a 1:1, 1:n, or n:m relation if n = m = 1.
You can do a scan, but it is the constraints that determine that relationship.
If you don't have that data, then you can only demonstrate 1:n and n:m with examples, but it cannot be proven that 1:1 is not n:m without the constraint definitions.
A 1:1 relationship will look like this:
PK - PK
this can only by one-to-(zero or one), as both tables can only have unique keys, 1:n and n:m are not allowed. This would have to be constrained by software on some level to ensure the PK = PK for the separate tables, or more usually, if you really want 1:1 the data is stored, normalized, in the same table. Otherwise, you need to ensure key coordination by a transactional insert, or whatnot. Auto-generated keys are not advised.
A 1:n relationship will look like this:
PK - FK
the FK (foreign key) defines it to be constrained to a primary key in another table, but can be in multiplicity.
An n:m relationship will look like this:
PK - FK|FK - PK
where there are three tables. Two normalized tables with primary keys (the PKs) and a joining table with FK relationships defining the mapping n:m between the tables.
Of course, all of this could be constrained by code using the database, and hence, the table constraints are the only reliable definition of the data schema.
Foreign Keys must point to Primary Keys in another table, so you can't have FK:FK relationships, and there really is no PK:PK relationship defined by the database. That has to be constrained by transactional insert via code. The usual convention is to store data that is PK:PK in the same table, per a normalized data format.
Okay, so, to add a comment, directed at tables A and B; all you can say for certain is you have primary keys consisting of pid:identitytype and pid:maritalid, and if that is the case, for the sake of discussion, say identitytype and maritalid are ints, then you have int:int and int:int, and it tells you nothing. if identitytype has overlap with maritalid, then there is no way to reliably tell them apart. If you are only going to match on pid, then you have an N:M relationship as you have pid:N-pid:M different possibilities which would lead to an N:M relationship.

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.

Translating ER diagram to Relational model

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),
[...]

What is the purpose of data modeling cardinality?

I understand what cardinality is, so please don't explain that ;-)
I would like to know, what the purpose of doing cardinality is in data modeling, and why i should care.
Example: In an ER model you make relations and ad the cardinality to the relations.
When am i going to use the cardinality further in the development process? Why should i care about the cardinality?
How, when and where do i use the cardinalities after i finish an ER model for example.
Thanks :-)
Cardinalities tell you something important about table design. A 1:m relationship requires a foreign key column in the child table pointing back to the parent primary key column. A many-to-many relationship means a JOIN table with foreign keys pointing back to the two participants.
How, when and where do i use the cardinalities after i finish an ER model for example.
When physically creating the database, the direction, NULL-ability and number of FKs depends on the cardinalities on both endpoints of the relationship in the ER diagram. It may even "add" or "remove" some tables and keys.
For example:
A "1:N" relationship is represented as a NOT NULL FK from the "N" table to "1" table. You cannot do it in the opposite direction and retain the same meaning.
A "0..1:N" relationship is represented as a NULL-able FK from "N" to "0..1" table.
A "1:1" relationship is represented by two NOT NULL FKs (that are also keys) forming a circular reference1 or by merging two entities into a single physical table.
A "0..1:1" relationship is represented by two FKs, one of which is NULL-able (also under keys).
A "0..1:0..1" relationship is represented by two FKs, both NULL-able and under keys, or by a junction table with specially crafted keys.
An "M:N" relationship requires an additional (so called "junction" or "link") table. A key of that table is a combination of migrated keys from child tables.
Not all cardinalities can be (easily) represented declaratively in the physical database, but fortunately those that can tend to be most useful...
1 Which presents a chicken-and-egg problem when inserting new data, which is typically resolved by deferring constraint checking to the end of the transaction.
Cardinality is a vital piece of information of a relation between two entites. You need them for later models when the actual table architecture is being modelled. Without knowing the relationship cardinality, one cannot model the tables and key restriction between them.
For example, a car must have exactly 4 wheels and those wheels must be attached to exactly one car. Without cardinality, you could have a car with 3, 1, 0, 12, etc... wheels, which moreover could be shared among other cars. Of course, depending on the context, this can make sense, but it usually doesn't.
A data model is a set of constraints; without constraints, anything would be possible. Cardinality is a (special kind of) constraint. In most cultures, a marriage is a relation between exactly two persons. (In some cultures these persons must have different gender.)
The problem with data modelling is that you have to specify the constraints you wish to impose on the data. Some constraints (unique, foreign key) are more important, and less dependent on the problem domain as others ("salary < 100000"). In most cases Cardinality will be somewhere in between crucial and bogus.
If you are creating the data layer of an application and you decided to use an ORM, maybe it's entity framework.
There's a point when you need to create your models and your model maps. At that point you would be able to pull out your ERD, review the cardinality you put on your diagram and create the correct relationships so your data layer shape matched your database shape.

Resources