For an university project I need to do the following:
Let the user enter ingredients via HTML form, process it via PHP into my MySQL database. For Example:
The user enters EGG and QUANTITY and CALORIES.
Let the user make recipes out of these ingredients. So another form with recipename and ingredients.
Now my question is: How can I connect these tables in a proper manner? So that tablerecipe with recipename knows repiceid 1 consists of 3 eggs, 100g beef and 300g cheese?
I read about lookup tables, but I don't understand them. Any help would be appreciated.
My table structure looks like this so far:
If each recipe will have multiple ingredients, and each ingredient will belong to only one recipe, then you have a One-to-Many relationship. You should define your tables like this:
+----------------+ +------------------+
| recipes | | ingredients |
+----------------+ +------------------+
| recipe_id <----------------+ | ingredient_id |
| recipe_name | | | ingredient_name |
| | +----------------+ recipe_id |
| | | |
| | | |
| | | |
| | | |
| | | |
+----------------+ +------------------+
Notice how the ingredients table has a recipe_id that points to a recipe in the recipes table. When you want to query all the ingredients that belong to a recipe, you could query it like this:
SELECT
*
FROM
recipes r
INNER JOIN ingredients i WHERE i.recipe_id = r.recipe_id
However, this is probably not what you want because you might want to use an ingredient more than once. In this case, each ingredient can belong to more than one recipe and each recipe can have more than one ingredient. This is a Many-to-Many relationship. You should define your tables like this:
+----------------+ +------------------+
| recipes | | ingredients |
+----------------+ +------------------+
| recipe_id <---+ +--> ingredient_id |
| recipe_name | | | | ingredient_name |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
+----------------+ | | +------------------+
| |
| +--------------------+ |
| | recipe_ingredients | |
| +--------------------+ |
| | id | |
+--+ recipe_id | |
| ingredient_id +--+
| quantity |
| type |
| |
| |
| |
| |
+--------------------+
Notice the extra table. This is sometimes called a bridge table and it associates a recipe_id with an ingredient_id. In this way, more than one ingredient can be associated with a recipe, and more than one recipe can be associated with an ingredient. I also added two extra columns to the bridge table that adds more information to the relationship. I added a quantity which is the amount of that ingredient to use, and a type which can be used to define the type of measurement (i.e. cups, grams, teaspoons, etc). When you want to query all the ingredients that belong to a recipe in this case, you could query it like this:
SELECT
*
FROM
recipes r
INNER JOIN ingredients_recipes ir ON ir.recipe_id=r.recipe_id
INNER JOIN ingredients i ON i.ingredient_id=ir.ingredient_id
Related
I'm rewriting an old application and I'm using Eloquent to handle the db layer.
I have these two tables:
+----------------+
| core_users |
| |
| id name |
|----------------|
| 1 John Doe |
| 2 Jane Doe |
+----------------+
+---------------------+
| core_access |
| |
| id user_id access |
|---------------------|
| 1 1 users |
| |
| 2 1 stores |
| |
| 3 2 stores |
+---------------------+
This is a belongsToMany relation since a user can have many access roles and access roles can belong to many users. But the core_access table is not a true pivot table, which have proven cumbersome when I wan't to attach and detach access roles to users.
An alternative, that I hesitate from doing, is to alter the table structure with a true pivot table like this:
+------------------------+
| core_user_core_access |
| |
| id user_id access_id |
|------------------------|
| 1 1 1 |
| |
| 2 1 2 |
| |
| 3 2 2 |
+------------------------+
But rather is there a way to setup the current table structure with Eloquent Models with a belongsToMany approach?
I have an entity called books and another one users.
Each book can have many authors(users).
So when I add a book I select the users that are registered on the site.
But if the author of the book doesn't have an account on the site I want to type his name by hand.
+----+---------+-----------+--+
| id | book_id | author_id | |
+----+---------+-----------+--+
| 1 | 1 | 1 | |
| 2 | 1 | Mr.Paul | |
+----+---------+-----------+--+
See how the author_id is a string but also an reference key to an user?
Can I do this with doctrine or I will have to manage the inserts myself?
Edit:I have no idea what the right title should be
Edit2:A possible solution will be to make another table containing only authors that don't have an account on the website.
I would recommend following approach
You have a table books a table accounts and the following:
authors_books
+----+---------+-----------+--+
| id | book_id | author_id | |
+----+---------+-----------+--+
| 1 | 1 | 1 | |
| 2 | 1 | 2 | |
+----+---------+-----------+--+
authors
+----+---------+------------+--+
| id | name | account_id | |
+----+---------+------------+--+
| 1 | Mr. Paul | 1 | |
| 2 | Simon | (empty) | |
+----+---------+-------------+--+
the account_id can be empty
I have a question about a Junction table in MySQL.
In my Junction Table, I store EmployerID, which references my Employer Table and MeetingID, which references my Meetings Table.
Currently, my Junction table looks like this:
+------------+------------+
| EmployerID | MeetingID |
+------------+------------+
| Employer1 | Meeting1 |
| Employer2 | Meeting1 |
| Employer1 | Meeting2 |
| Employer2 | Meeting2 |
| Employer1 | Meeting3 |
| Employer2 | Meeting3 |
| Employer1 | Meeting4 |
| Emokoyer2 | Meeting4 |
+------------+------------+
However, based on my data I know that e.g. Employer1 attended only Meeting1 and Meeting4 and Employer2 attended only Meeting3 and Meeting4. However, because I have 4 meetings in total in my Meetings table, the junction table lists all EmployersID for all MeetingsID,
Is there a way to correct the Junction table, so that it will correctly display what meetings each employer attended?
Category Table
+----+-----------------------+
| id | category_name |
+----+-----------------------+
| 1 | Buy Book |
| 2 | Buy other thinks |
+----+-----------------------+
Buy Table
+----+-----------------------+----------+-------------+----------+--------+-------+
| id | identity | name | description | per_rate | bought | costs |
+----+-----------------------+----------+-------------+----------+--------+-------+
| 1 | PROJECT[1]CATEGORY[1] | BOOK | PHP BOOK | 10 | 50 | 5000 |
| 2 | PROJECT[1]CATEGORY[1] | BOOK | PHP BOOK | 10 | 40 | 4000 |
| 3 | PROJECT[2]CATEGORY[1] | BOOK | JS BOOK | 2 | 50 | 100 |
+----+-----------------------+----------+-------------+----------+--------+-------+
I Want to Select category name from Other table when I select this table.
identity: PROJECT[project_id]CATEGORY[category_id]
So There are any way to pick the category id and select category name from other table
I Want Like This Table
+----+---------------+-----------------------+----------+-------------+----------+--------+-------+
| id | category_name | identity | name | description | per_rate | bought | costs |
+----+---------------+-----------------------+----------+-------------+----------+--------+-------+
| 1 | Buy Book | PROJECT[1]CATEGORY[1] | BOOK | PHP BOOK | 10 | 50 | 5000 |
| 2 | Buy Book | PROJECT[1]CATEGORY[1] | BOOK | PHP BOOK | 10 | 40 | 4000 |
| 3 | Buy Book | PROJECT[2]CATEGORY[1] | BOOK | JS BOOK | 2 | 50 | 100 |
+----+---------------+-----------------------+----------+-------------+----------+--------+-------+
You have a really bad data structure. The project and category should be in their own columns, with numbers stored properly as numbers, and proper foreign key relationships. In MySQL, doing this might require a trigger, but it is worth it.
Sometimes, we are stuck with other people's bad decisions. You can do what you want using like:
select b.*, c.category_name
from buy b join
category c
on b.identity like concat('%CATEGORY[', c.id, ']');
However, you should probably put effort into fixing the broken data structure.
I have 14 tables (one for every year) with product code, firm name and invoice numbers. Main structure of table is identical (product code, ID), but there can be some variables in names of firms.
Table2011
| ID | productcode | firm1 | firm2 | firm3 | etc |
| 1 | G-00001 | 2;5;40| 32;67 | | 150 |
| 2 | G-00005 | | 50 | | |
|etc | | | | | |
Table2010
| ID | productcode | firm1 | firm2 | firm3 |etc |
| 1 | G-00001 | 1;10 | | 55 | |
| 2 | G-00003 | | 2 | | |
| 3 | G-00005 | | 50 | 40 | |
| etc| | | | | |
Table2009
...
Column Firm1 do not usually equals to same firm as firm 1 in other table
I am using table editor to work with tables (adding columns to table, editing values…).
I would like to know if it is possible to achieve result like below. It is above my PHP skills.
Product G-00001 page
…
<UL>
<LI>Year 2011: 150etc; 67firm2; 40firm1; 32firm2; 5firm1; 2firm1</LI>
<LI>Year 2010: 55firm3; 10firm1; 1firm1</LI>
<LI>Year 2009: ...</LI>
...
</UL>
…
Lemme begin with book recommendation : SQL Antipatterns. You will need it, doesn't matter if you caused this mess or ar just assigned to fix it.
If i was in your place, first thing would do would be to fix the database structure. This is madness. You do not need a new table for each year and new column for each company. Database is not a form of Excel spreadsheet.
Invoices Years Companies
----------------- ------------- ---------------
| product_code PK | | year_id PK | | company_id PK |
| company_id FK | | number | | title |
| amount | ------------- ---------------
| year_id FK |
-----------------
Where PK - primary key and FK - foreign key.
This structure would make the gathering of information much much much MUCH easier.
If you just want to display the data and not worry about the restructuring just yet you can use a JOIN to display the information from all the tables.
Although I would agree with teresko you really need to redesign that database. It is not maintainable the way it is.