I have a doubt about this. I have 2 tables
Ordered_products
ordered_product_id
product_id
quantity
price_charged
Ordered_items
ordered_item_id
item_id
unit_of_measure
box_type
Each ordered product can be composed by one or more items and one item has only one ordered product.
Which solution is better:
Sol 1: Just add as foreign key the ordered_product_id into ordered_items table. (http://cl.ly/image/0g3G2J231U0P)
Sol 2: Create a new table with ordered_product_id and ordered_items keys. (http://cl.ly/image/0Z1D1C1g0R3t)
Please give me any advice
Solution one is better. The only time you need seperate table is for many to many relationships or when that table needs to store information specific to the relationship, like when it starts and ends.
With a one to many relationship, just add a column to the many table to point to its parent. It makes joins easy and clear.
Solution 1. Don't create unnecessary tables.
Association tables are only used for many-to-many relations.
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
...
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 two simple tables "items" and "orders". For the sake of simplicity lets assume that one item can only be in one order or one order can only contain one item.
Now as this can be implemented using simple one to one relationship I can do following:
I can add the primary key of the orders table to the items table like below
//Table Items
item_id, item_name, order_id
1, shoes, 1
2, watch, 2
//Table Orders
order_id, customer
1, James
2, Rick
or I can add the primary key of the items table to the orders table like below
//Table Items
item_id, item_name
1, shoes
2, watch
//Table Orders
order_id, customer, item_id
1, James, 1
2, Rick, 2
Which one is correct and why? Are there any guide lines to decide which key goes where? Sure common sense will work in simple examples as above but in complex examples how do we decide?
One-to-One relationships should be generally merged simply into one table. If there aren't any contradictions, the One-to-One relationship might be a sign of an unconsidered decision.
And If You really want to use this kind of relationship, it's totally up to You where to place FK. You might want to take optionality into consideration when applying FK. However, in MySQL, it still won't be a true One-to-One relationship because deferred keys are not supported there.
I have a problem in designing a database for a "business". I have some database for different types of products( for example car parts, groceries and some other stuff). Now if I want to make a business, meaning that I want to sell these items, I am thinking that I am going to need a table like Products which will contain a product foreign key, a stock and a price. My question is how can I use foreign keys in my Products table from my existing tables? I was thinking of using a combination of foreign keys (i.e in the Products table I would have a column for each product type), but I don't think it's such a good idea. What if I would want to expand my business to other types of products? Any ideas?
Consider inheritance:
You can use a table products which will be supertype of the products you have and refer to this table though the foreign key. If you want more info about how to implement inheritance click here