Now i'm sure this is an amateur question and that i'm going about this the wrong way, that's why i'm asking here! So please go easy.
I'm trying to create a database in Access 2013 that will store orders for a POS system (It isn't a commercial product, it's a computing project in C#). It contains several tables.
My issue is that I can't have multiple menuitems for every orderitem. Is there a way to do this?
Thank you in advance!
Create a MenuOrderItem table that goes between OrderItems and MenuItems. It will have a 1-to-Many relationship to both OrderItems and MenuItems. That's how Many-to-Many relationships are built in relational dbs...with an intermediary table.
In your case, to do this, remove the relationship between OrderItems and MenuItems. Create table called MenuOrderItem (or something similar). It should have just two columns in it: a column named OrderItemId, and a column named MenuItemId. These columns will be used to make the two new relationships: the first between MenuOrderItem and OrderItems and the second between MenuOrderItem and MenuItems.
One way would be to put a table between orderItems and menuItems with 2 columns. orderItemID and menuItemID. Then create your relationships. This would allow you to have one orderItem with many menuItems.
Related
Is it possible to assign several rows to a single row of a different table? Doing purchase order I have a table for Suppliers then under that I have items that are in the different table. Giving me headache for over a week now...
Not sure what you really want, but my first guess us that you have a many-to-many situation. Am I correct?
If my assumption is correct, the default solution is creating a junction-table. If for instance an Order can contain many Products, and a Product can be associated with many Orders, you usually create something like an OrderDetail table. And that would look something like this:
OrderDetail:
- OrderId
- ProductId
- Quantity
If you wish, you can even force that the combination of OrderId and ProductId should be unique with a unique key contraint (or unique index, which is usually the same).
I've been used to this kind of convention before starting to work with Entity Framework/MVC:
tblMyItem (id, id_lookup1, val2, val3, val4...) - single, generic table
tblkLookup1 (id, val1, val2,...) - one to many relation table
tblmMyItemLookup2 (id, id_myitem, id_lookup2) - many to many relations table.
Recently I've found somewhere on the web that it's not good to create id column in tblmMyItemLookup2 when using Entity Framework, but I couldn't find more information on that. Could anyone please explain, why this is so significant?
When designing many-2-many relationship(i.e Student - Teacher) based on intermediate table (Student2Teacher) with only 2 foreign key columns, you'll end up having entities with navigation properties without intermediate table(no entity will be created for that table in context at all):
Student.Teachers <- EntityCollection<Teacher>
Teacher.Students <- EntityCollection<Student>
Meanwhile, if you add any extra field to your intermediate table, you'll have navigation properties pointing to intermediate entity:
Student.Student2Teacher
Teacher.Student2Teacher
making your querying uselessly more complex
In general we use lookup tables to macth associated records in two different tables that have a many to many relationship. For instance, let we have three tables: Students, Professors and StudentsProfessors. Since, a student can atten lesson that are serviced by many professors and a professor can teach more than one leasons then this is clearly a many to many relationship. So the lookup table called StudentsProfessors is used to match a student to her/his professors and vice versa. Now, the use of an id for each record of this lookup table is meaningless. We are not going to use this number anywhere. We just need to know that Student with studentId=10 is associated with professors with ids in (1,2,4,9). Just that and not the id of this record in the lookup table.
I'm trying to design a snackbar automation system (to answer the first question - No, it's not a homework, it's for learning purposes) and I have an issue with with the recipes and how to present them in the database. I have two options:
Option 1:
[Ingredients] -> [IngrID, IngrName]
[Recipe] -> [RecipeID, RecipeName]
[IngRecipe] -> [IngrID, RecipeID]
In this case the third table is a typical many-to-many table, the model looks correct and it's a piece of cake to manipulate with the data using Entity Framework. But I want to keep track of the amounts as well. Generally I use Ingredients as a table to insert the new purcheses. If the ingredient exist, just update the amount.
Option 2
Now if I add column "amount" as a column to IngRecipe, the whole idea of many-to-many table vanishes and I no longer can use the entity model to fill the fields automatically. But I can't seem to find a more apropriete place for this column. Where and how will I say "Well, get me 100 gr of chicken breast and add it to whatever recipe"
Any help is appreciated. Thanks in advance!
It's a solid model start, consider:
RecipieIngredients -> Recipe (FK), Ingredient (FK), IngredientQuantity
Key over (Recipe, Ingredient)
Note that it is still a M-M relationship (the quantity is not part of the PK nor involved in a FK), just with more relevant data for this relationship pair. The names can be changed, but at some point, this must be represented as a M-M relationship in a normalized relational model.
Don't let the framework ruin a good normalized design - and I hope EF can cope with such trivial scenarios; even LINQ2SQL can.
In my app I use ORM Designer which supports only 1:1 relation. I have Items, Products, Shipping tables: Item has ProductId and ShippingId attributes which are foreign keys.
I need to upload data from xml file into my SQL DB, so I gonna use SqlBulkCopy class and I need to create DataTable objects: itemsTable, productsTable, shippingTable to load data from xml into them.
So, do I need to create DataRelations: ItemsProducts and ItemsShipping? If so, itemsTable would contain productsTable and shippingTable?
Thanks!
Edits:
And is it possible to write to server the content from 3 dataTables simultaneously?
This isn't a direct answer to your question, but it's entirely possible to have a one-to-many relationship in a .dbml file. See the screenshot below, which is simply the properties sidebar you can get to by right-clicking on an association and choosing "Properties".
If you use SqlBulkCopy, you have to do it using one table/entity at a time. It will work if the entities already have the foreign key values. BTW I created a wrapper object for SqlBulkCopy that can do a bulk insert for a collection of objects.
It may help you, you can find it here:
http://www.codeproject.com/Articles/354094/Bulk-Insert-in-NET-applications-Part-1
In SQL I have 2 tables.
Sport Athlete
SportId AthleteId
SportName AthleteName
Their relation is many to many. So there is a third table that join them.
AthleteSport
AthleteId
SprortId
If i create an entity data model using the wizard the third table is disapeared and only creates the 2 main tables with the many to many relation.
how can I perform a query to find out what kind of sports athlete 'A' does? How to change them or add a new sport?
I want to use linq and c#.
Thank you.
In your Sport entity, there will be a so called "navigation property" Athletes that contains all Athletes that belong to that Sport instance.
The same is true the other way around.
Can't you do A.Sports and get that list?