问题
I have 2 tables and was wondering what the best relationship between them was. I know there is a relationship between them but I get so confused with one to many, many to one, many to many, unidirectional, bidirectional, multidirectional etc.
So this is the basic, displayed, structure:
Traveler Table:
+------------------------------------------+
| Name | Family Name | National ID No. |
+------------------------------------------+
| Dianne | Herbert | 579643 |
| Francine | Jackson | 183432 |
| Oprah | Dingle | 269537 |
+------------------------------------------+
Journeys Table
+------------------------------------------------------------------------------------------------------+
| Start Station | End Station | Start Time | End Time | Travelers |
+------------------------------------------------------------------------------------------------------+
| Hull | Leeds | 13:50 | 14:50 | Francine Jackson, Oprah Dingle |
| Newcastle | Manchester | 16:30 | 19:00 | Dianne Herbert, Francine Jackson |
| Hull | Manchester | 10:00 | 13:00 | Dianne Herbert, Francine Jackson, Oprah Dingle |
+------------------------------------------------------------------------------------------------------+
The travelers table is okay, it makes sense:
CREATE TABLE Travelers (
Name VARCHAR(50) NOT NULL,
Family_Name VARCHAR(50) NOT NULL,
National_ID_Number INT(6) NOT NULL PRIMARY KEY
)
But I am unsure about how to do the journeys table. Especially with Travelers:
CREATE TABLE Journeys (
Start_Station VARCHAR(50) NOT NULL,
End_Station VARCHAR(50) NOT NULL,
Start_Time VARCHAR(50) NOT NULL,
End_Time VARCHAR(50) NOT NULL,
Travelers ???????
)
Obviously I have "Travelers" as a column inside my 2nd table. So there is a relationship there with the first table. But what is it? I think I need to make a Foreign Key somehow?
回答1:
You are looking for a junction/association table. The tables should look like this:
create table Journeys (
Journey_Id int auto_increment primary key,
Start_Station VARCHAR(50) NOT NULL,
End_Station VARCHAR(50) NOT NULL,
Start_Time VARCHAR(50) NOT NULL,
End_Time VARCHAR(50) NOT NULL
)
create table TravelerJourneys (
traveler_journey_id int auto_increment primary key,
traveler_id int(6),
journey_id int,
foreign key (traveler_id) references travelers(National_ID_Number),
foreign key (journey_id) references Journeys (journey_id)
);
回答2:
I Relational • Pre-Requisite Explanation
There is an awful lot of misinformation; disinformation in the "literature" produced by the "theoreticians" and all the authors that follow them. Of course that is very confusing and leads to primitive, pre-relational Record Filing Systems with none of the Integrity; Power; and Speed of Relational Systems. Second, while newbies try hard to answer questions here, due to the above, they are also badly confused.
I can't provide a tutorial, this is not-so-short explanation of the issues that you need to understand before diving in to the Question.
1 Relationship
I get so confused with one to many, many to one, many to many, unidirectional, bidirectional, multidirectional etc.
unidirectional, bidirectional, multidirectional
Please delete those from your mind, they are not Relational terms (the "theoreticians" and newbies love to invent new things, they have no value other than to add confusion).- There is no direction in a relationship. It always consists of:
- a Primary Key: thing that is referenced, the parent end, and
- a Foreign Key: the child end, thing that is referencing the parent PK
- At the SQL code level, DML, you could perceive a "direction", parent-to-child, or child-to-parent. It is a matter of perception (not storage) and relevant only to the requirement of the code, the "way to get from this data to that data".
At the physical level, SQL DDL, there is only one type of relationship Parent::Child, and that is all we have ever needed. No Cardinality yet, because that is controlled by other means. As with the natural world, the parent is the thing that is referenced, the child is the thing that references the parent.
At the bare bones level, that is not a Relational database, but a 1960's Record Filing System, the relationship is Referenced:: Referencing, and God only knows what each thing is.
The child can have only one parent, and the parent can have many children, therefore the one-and-only relationship at the physical level is:
one [parent] to 0-to-many [children]
A Relational database is made up of things (rows, the main symbol, with either square or round corners)) and relationships between things (the lines, either Identifying or Non-Identifying). A thing is a Fact, each row is a Fact, the relationships are relationships between Facts.
In the Relational Model, each thing must be uniquely Identified, each logical row (not record!) must be unique. That is the Primary Key, which must be made up from the data (
INT; GUID; UUID;
etc are not data, they are additions, in the system, the user does not see them).Of course,
IDENTITY or AUTOINCREMENT
are fine for prototypes and trials, they are not permitted in Production.There are many differences between Relational databases and the pre-relation, 1960's Record Filing Systems that the "theoreticians" use. Such primitive systems use physical pointers, such as
Record ID
(INT; GUID; UUID;
etc). If I had to declare just one, the fundamental difference is:- whereas the RFS is physical, the Relational Model is Logical
- therefore, whereas in the RFS physical records are referenced by their physical pointer, in the RDb logical rows (nor records!) are referenced by their logical Key
The relationship is established as follows:
ALTER TABLE child_table ADD CONSTRAINT constraint_name FOREIGN KEY ( foreign_key_column_list ) REFERENCES parent_table ( primary_key_column_list )
Beware, some "theoreticians", and some newbies, do not understand SQL. If I tell you that Sally is Fred's daughter, from the single Fact you will know that Fred is Sally's father. There is no need for the second statement, it is obviously the first statement in reverse. Likewise in SQL, it is not stupid. There is only one relationship definition. But those darlings add a second "relationship", the above in reverse. That is
(a) totally redundant, and
(b) interferes with administration of the tables. Probably, those types are the ones that use weird and wonderful directional terms.
2 Cardinality
That is controlled firstly by implementing an Index, and secondly by additional by other means. The additional is not relevant here.
one [parent]
Each row is unique, by virtue of the Primary Key, expressed as:ALTER TABLE table ADD CONSTRAINT constraint_name PRIMARY KEY ( column_list )
one [parent] to many [children]
Because each parent row is unique, we know that the reference [to the parent] in the child will reference just one rowALTER TABLE child_table ADD CONSTRAINT constraint_name FOREIGN KEY ( foreign_key_column_list ) -- local child REFERENCES parent_table ( primary_key_column_list ) -- referenced parent
Example
- All my data models are rendered in IDEF1X, the Standard for modelling Relational databases since 1993. Refer to IDEF1X Introduction,.
ALTER TABLE Customer ADD CONSTRAINT Customer_pk PRIMARY KEY ( CustomerCode ) ALTER TABLE OrderSale ADD CONSTRAINT OrderSale_pk PRIMARY KEY ( CustomerCode, OrderSaleNo ) ALTER TABLE Order ADD CONSTRAINT Customer_Issues_Orders_fk FOREIGN KEY ( CustomerCode ) -- local child REFERENCES Customer ( CustomerCode ) -- referenced parent
many to one
There is no such thing. It is simply reading a one-to-many relationship in reverse, and doing so without understanding. In the example, reading the data model explicitly, or translating it to text:Each Customer issues 0-to-n OrderSales
the reverse is (refer again to the one-to-many):
Each OrderSale is issued by 1 Customer
Again, beware, newbies may implement a duplicate relationship, that will (a) confuse you, and (b) stuff things up royally.
many to many
We have been using diagrammatic modelling tools since the early 1980's. Even IDEF1X was available for modelling long before it was elevated to a NIST Standard. Modelling is an iterative process: whereas redrawing is very cheap, re-implementing SQL is expensive. We start at the Logical level with no concern for the physical (tables, platform specifics), with only entities, progress to logical Keys, Normalising as we go. Finally, still at the logical level, we would finalise each table, and check that the datatypes are correctly set.
If and when the logical model is (a) stable, and (b) signed off, then we progress to the Physical: creating the datatypes; tables; foreign keys; etc. It is a simple matter of translating the data model to SQL DDL. If you use a modelling tool, that is one click, and the tool does it for you.
The point is, there is progression, and a distinction between the Logical and Physical levels.- At the physical level, as can be understood from the fact that there is one and only one type of relationship in SQL, there is no such thing as a many-to-many relationship. Notice that it can't be expressed even in text form, in a single statement, we need two statements.
- Such a relationship exists only at the logical modelling level: when we determine that there is such a relationship between two Facts (rows in a table), we draw it.
- At the point when the data model is stable, and we move from teh Logical to the Physical, the n-to-n relationship is translated into an Associative Table and a relationship to each parent.
- Refer to this unrelated document for an Example
- Notice the many-to-many relationship
Favours
in the Logical Requirement - Notice the translation to an Associative table and tw relationships in Implementation (Right side only)
Each User favours 0-to-n ProductPreferences Each Product is favoured in 0-to-n ProductPreferences
- Now notice this sagely: that Implementation model can be read Logically:
Each User favours 0-to-n Products (via ProductPreference) Each Product is favoured by 0-to-n Users (via ProductPreference)
- Notice the many-to-many relationship
Additionally, you might find this document helpful (section 1 Implementation: Relationship only).
II Your Question
Now we can deal with your question.
1 The Obstacle
Your quandary is due to:
- not progressing through the formal stages, due to lack of education in the subject matter (hopefully mitigated by the above explanations)
- having an idea at the Logical level ... but not formally
- of the views required in the app, as opposed to the perceiving the data independent of the app
- diving into the Physical tables ... with nothing in-between
- not asking specific questions, due either to shyness or inability to identify the particular point that you do not understand
and thus you are stuck, as per your original post.
2 The Quandary
Your quandary is:
you have this at the logical level (Data model, Entity-Relationship level):
- and of course, your
CREATE TABLE
commands at the physical level.
I hope my explanations above are enough to understand the great gap in what you have:
- the logical vs the physical
- that the physical is far too premature
- that we need at least some data modelling (not formal, not possible in this medium) to work things out.
The Logical data model is simply not progressed enough, let alone resolved, in order to create stable tables, let alone correct ones.
3 Journey Progressed
Let's take your Journey
thingamajig first. What is a Journey ?
- It is definitely not an Independent thing. We do not go walking in the heath and heather after the dew; nor the quietened beach at sunset, and suddenly, out of nowhere ... find a Journey, sitting there, all by itself. No. It can't stand up.
- A Journey is Dependent (at least) on a starting and finishing point.
- And what are those points exactly ? Railway stations.
- Railway stations are Independent, they do stand alone.
- And then a Journey is Dependent on a Railway station. In two separate relationships: start; end.

Predicate
I have given some of the Predicates, those relevant right now, so that they are explicit, so that you can check them carefully.
All the Predicates can be read directly from the model.
In the normal case, you have to read them from the diagram (it is rich with specific detail), and check that it is correct
that provides a valuable feedback loop:
modelling --> Predicate --> check --> more modelling.
4 Traveller Progressed
Now for your Traveller
thingee. What is a Traveller ?
- A Traveller is a person who has travelled on at least 1 journey
- Therefore Journey is Dependent on Person
Person is Independent, it can stand alone
5 Journey Resolved
Now we can finalise Journey.

5 Requirement
Now we have a decent chance of answering your Question.
- I have chosen Relational Keys that throw themselves at us, no thinking necessary.
- What makes a
Journey
unique is( NationalID, StationStart, DateTimeStart )
not( NationalID, StationStart )
. Anything more would be superfluous. Person
needs an additional Key, called an Alternate Key, on( NameFamily, Name )
. This prevents dupes on those values.
RoleName
In the first instance, the column name for a PK in used unchanged wherever it is an FK
Except:
- to make it even more meaningful, eg.
TravellerID
, or- to differentiate, when there is more than one FK to the same parent, eg.
StationStart, StationEnd
.
6 Traveller ???
So what exactly is Traveller???
(the concept in your mind, it is not in the Requirement) ?
- One possibility is:
- a Person who travels on a Journey is a Traveller.
- That is already available above, in the single
Person
sense.
- But there is more. I get the idea that it is a group of people who took a journey together. But that too, is available from the above:
SELECT * FROM Journey WHERE (condition...) GROUP BY StationStart, DateTimeStart, StationEnd
- But that will give you the whole train, not a group of people who have an intended common purpose.
- What I can figure out is, that you mean a group of people who have some common purpose, such as taking a trip together. That marks an intent, before the fact of the Journey. It could be a loose Group, or and Excursion, etc. Something smaller than a train-load.
- I will give you two options. It is for you to contemplate them, and to specify (if it is long, edit your Question; if it is short, post a Comment).
7 Group Option
This is a simple structure, for groups that travel together. This assumes that (because it is group travel) tickets for the Journey
are purchased in a block, for all the Members
of the Group
, and we don't track individual Person
purchases.

8 Excursion Option
An excursion is one outing for the group, with different members each outing. This assumes that the Journey
for each Person
is tracked (booked personally, at different times).
The Fact that each
Member
has reserved theirJourney
(or not) is simply a matter of joiningExcursion::Member::Journey
.- Which is eminently possible due to the Relational Keys (impossible in an RFS). Refer to this Example. Please ask if you need code.
The Identifier for a
Group
(above) and anExcursion
(below) is quite different:- I have set up
Group
to be a somewhat permanent affair, with a home, and an assumption that they go on several outings together. The groups you have given (in yourJourneys.Travellers
) would be three different groups, due to the membership. Excursion
is a single event, the group is the list of Passengers.MemberID
andPassengerID
are RoleNames forNationalID
, that is, the Role thePerson
plays in the subject table.
- I have set up
It also allows
Journeys
that aPerson
takes alone (without anExcursion
) to be tracked.
Please feel free to ask specific questions. Or else update your original post.
回答3:
Firstly understand what each relationships are, I am explaining very few basics which are widely used.
One to One
A One-to-One relationship means that you have two tables that have a relationship, but that relationship only exists in such a way that any given row from Table A can have at most one matching row in Table B.
Ex: A Student has unique rollnumber to unique student which means one student can have only one rollnumber
Many to Many
A good design for a Many-to-Many relationship makes use of something called a join table. The term join table is just a fancy way of describing a third SQL table that only holds primary keys.
Ex- Many Students can have many subjects.
One to Many
a one-to-many relationship is a type of cardinality that refers to the relationship between two entities A and B in which an element of A may be linked to many elements of B, but a member of B is linked to only one element of A.
For instance, think of A as books, and B as pages. A book can have many pages, but a page can only be in one book.
While in your case Travelers column make it as foreign key,the primary key of Traveler table.
Reason: One Traveller can have many journeys. So here relationship is One to Many
回答4:
As you have a n To n relations. You need to create an intermediate table.
In this case you will have To create a unique id to the journey table to identify the row easily.
CREATE TABLE TRAVELERS_IN_JOURNEY (
National_of,
Journey_id
)
As a column cannot contains multiple keys, you ca also remove the Travelers column from you Journey table.
CREATE TABLE Journeys (
Journey_id INT AUTO_INCREMENT PRIMARY KEY,
Start_Station VARCHAR(50) NOT NULL,
End_Station VARCHAR(50) NOT NULL,
Start_Time VARCHAR(50) NOT NULL,
End_Time VARCHAR(50) NOT NULL
)
来源:https://stackoverflow.com/questions/57288792/what-kind-of-relationship-do-these-2-tables-require