I can't imagine the relationships it takes to find the best way the two following tables:
I have a table called "Poeples". It contains "ID, name and sex".
And I have an another table called "Pairs". It contains "ID, name, father_id, mother_id".
A person can belong to several pairs. And a pair can only have one parent_id and mother_id
I don't know if I need to make a foreign key for mother_id and father_id. I would like mother_id to be a poeple and father_id also a poeple (from the poeples table).
Do you have an idea of how?
I plan to use the belongsTo and hasMany relationship, but since there are two foreign keys (I do not know if that's what I have to do), I do not know how to do it.
Thank you very much
You can't make one relation for two fields.
What you need to do is to define two relations in Pair model, one would be PeopleByMother and another PeopleByFather (maybe find better naming).
Therefore a Pair model has two hasOne relations, and People has one belongsTo relation that aims for Pair model
Related
I'm having trouble to develop my database model.
I have the tables TOUR and TRIP (both with GUID primary key) and both have some children tables in common. Like DETAIL, COMMENT, MEDIA, PRICE and like that.
I'm thinking in create in those children tables some "ParentId" column to link with TOUR or TRIP, but I need to know which parent table is. In this way I should to create a column "ParentType" where 1 is TOUR and 2 is TRIP or maybe create a TourDetail and a TripDetail to link each one with other table?
I'm looking for the best practice to do that.
In terms of design, the SQL convention is that each relationship should have its own column in the referencing table. That's the concept if foreign key columns.
So you need to create two columns in each child table (DETAIL, COMMENT, MEDIA, PRICE), like TOUR_ID and TRIP_ID that will contain the primary key of the correspondig records in, respectively, tables TOUR and TRIP. Of course if a children refers to only one parent and not the other, only one column is necessary.
This will then allow you to build proper JOIN queries like :
SELECT
...
FROM TRIP
JOIN DETAIL on DETAIL.TRIP_ID = TRIP.ID
...
WHERE
...
Let's say you have one cook that has one restaurant, and vice versa. So with a one-to-one relationship, you would have the primary key id in the cooks table and cook_id as the primary and foreign key in the restaurants table.
So how would you represent a relationship of one-to-many between the restaurant and its customers? Since the restaurant does not have its own ID, would the customers table have its own id and then contain foreign keys of cook_id?
Edit: I've thought of a better and more realistic example. Let's say you have a work order that only ever has one quote. You'll have the work order's id in the quotes table, since it's 1-to-1. Being a quote, it's bound to change, and that same particular quote gets revised. If you wanted to record the revisions made to a quote (or some sort of history log), you'd want something like a quote_revisions table. In this case, a work order only ever has one quote, and a quote can have many quote revisions. With what IDs do you link the quotes and quotes_revisions table?
Since you have a one-to-one relationship, the cook's id is the restaurant's id too. You can relate customers to restaurants by associating customer keys with cook/restaurant keys in a table (customers or another table). The one-to-many cardinality is enforced by placing a unique constraint on the customer's key so that they can't be associated with more than one restaurant/cook.
Using the Work_order example:
Work_order would have a PK of, say, wo_id, and it might be AUTO_INCREMENT.
Quotes would have a PK with the same wo_id, but not AUTO_INCREMENT.
Quote_revisions would have an INDEX(wo_id), but some other column(s) for the PK.
Work_order and Quotes are "1:1", as provided by wo_id.
Quotes and Quote_revisions are "1:N"; wo_id in both tables provides that relationship.
It is rarely useful to have 1:1, but your example might be a good use case. (One table is relatively large and static, the other is relatively small and frequently changed.)
I would instead have a restaurant_id field as the primary key in the restaurant table, along with cook_id as a foreign key. Yes, this structure would support a one-to-many relationship just as well as a one-to-one relationship, but I believe each entity should nevertheless have its own ID. If you like, you can put a unique constraint on the foreign key, to ensure that the relationship does remain one-to-one. Or you could simply have a single restaurant table that includes fields with information about its head chef.
My UNF is
database(
manager_id,
manager_name,
{supplier_id,
supplier_name,
{order_id,
order_quantity}}
{purchase_id,
purchase_date}
Here manager_name, supplier_id, order_id and purchase_id are primary key.
During normalization there will be 1 table called purchase. Is it necessary to make manager_name as a foreign key?
How can I normalize these database?
This is a part of my college project on database. Normalization is really confusing.
First consider splitting things out by things that naturally go together. In this case you have manager information, supplier information, order information and purchase information. I personally would want to know the difference between an order and a purchase because that is not clear to me.
So you have at least four tables for those separate pieces of information (although depending on the other fields you might need, suppliers and managers could be in the same table with an additional field such as person_type to distinguish them, in this case you would want a lookup table to grab the valid person type values from). Then you need to see how these things relate to each other. Are they in a one to one relationship or a one-to many or a many to many relationship? In a one-to one relationship, you need the FK to also have a unique constraint of index to maintain the uniqueness. In a many to many you will need an additional junction table that contains both ids.
Otherwise in the simplest case the child table of purchase would have FKs to the manager, supplier. and order tables.
Manager name should under no circumstances be a primary key. Many people have the same name. Use Manager ID as the key because it is unique where name is not. In general I prefer to separate out the names into First, middle and last so that you can sort on last name easily. However in some cultures this doesn't work so well.
I am using DBDesigner 4 for designing my database relations.
I have a users table and a recipes table. One user can own many recipes but one recipe cannot be owned by many users. This relationship is shown by the user_recipes relation in the picture. (A one-to-many relationship from users to recipes).
However, recipes can be liked by users. Many users can like many recipes. This is a many-to-many relationship between users and recipes and the pivot table for this is users_like_recipes.
But when I create this pivot table, I only need the users_id and recipes_id column. The recipes_users_id column is getting added on its own and I am not able to remove it. It says the third column has come from another Relation which is defined in the model. I guess its the user_recipes relation.
When I remove the user_recipes relation, I get the pivot table like I want to.
But I need the user_recipes relation too!
Please. Any help would be appreciated.
I would suggest removing user_id as a primary key from from the recipes table. Currently the combination if id and user_id provides identification for your recipes table. In this situation multiple user_id's can create the same recipe id because the combination has to be unique. user_id can just be a normal column in your table. If you REALLY want to, you can make an alternate key on (id, user_id) but you do not need it because the id is unique.
I have seen ,implementing the relation between "States" and "Districts" in two ways:
The relation between States and Districts is one to many relationship respectively..
First way:
In this implementaion,take two tables "States" and "Districts" and implement the one to many relationship between States to District as put the foreign key in Districts table.
In my "States" table the columns are: state_id(pk) & state_name.
In my "Districts" table the columns are: district_id(pk) district_name state_id(fk).
Second Way:
In this implementaion,take two tables "States" and "Districts" and implement the one to many relationship between States to District as creating the third table "state_district" and implementing as follows.
In my "States" table the columns are: state_id(pk) & state_name.
In my "Districts" table the columns are: district_id(pk) district_name .
The third table is "state_district",the columns are s_did(pk), district_id(fk),state_id(fk).
What is the difference betwen these two mechanisms.
The difference is that in the first case there can be only one state per district, wheras in the second there can be many states per district.
Which one you should use depends entirely on whether a district can be associated with multiple states or not. If they can then you have to use the second many-to-many model. If they cannot then while in practice you could use the second model, it would be incorrect to do so -- you should use the first one-to-many model.
for one to many relationship we use a table's primary key as foreign key in another table - Which is your first approach ans correct in this case
For many to many relationship we use a third table to store the relationship between first 2 tables- which is not required in your case as state to district has one to many relationship
What is the difference between these two mechanisms.
The difference is that the second method allows a district to be associated with more than one state. You can do this by just adding another row for a given district in the third table.
INSERT INTO state_district (district_id, state_id) VALUES
(1234, 49), (1234, 50);
Now you have the same district 1234 associated with both Alaska (49) and Hawaii (50).
I would assume you don't really need this. In fact, it would be better to ensure that each district belongs to exactly one state. You should have only a one-to-many relationship between states and districts. So you should use the first design.
Your second way should be done if there is a many to many relationship between states to districts.
You first way is correct and you should implement.
I would propose the following table structures:
States: --don't need extra metadata such as the sequence generated value
state_name varchar2(50) PRIMARY KEY
Districts: -- don't need extra metadata
district_name varchar2(100) PRIMARY KEY
State_Districts
state_name varchar2(50)
district_name varchar2(100)
primary key (state_name,district_name)
This ensures that you don't have duplicate district names which are real unique identifiers, regardless of if Wyoming and Pennsylvania have the same district name, the data is independent of each other. This also ensures that there will be no null values in any of the three tables, this is pretty important when we think about normalization techniques.
It would appear from your first table definition of the two tables that there is a State ID field in the District table. This indicates to me that there are one or more districts per state. In this case, a third table would be redundant.