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?
Related
I am trying to delete the same id between two tables. And deleting a storage file by getting the image file value.
For example procedure:
user_id(table1)->user_id(table2)->avatar->file name-> delete images-> delete all data from same user_id between first table and second table
I have two tables,
*user* table
| user_id | Full_name |
| -------- | -------------- |
| 1 | Steve Jobs |
| 2 | Bill Gates |
| 3 | Elon Musk |
*users_option* table (with both column foreign keys)
| user_id | user_option | user_value |
| ------------ | -------------- | -------------- |
| 1 | email | abc#abc.com |
| 1 | avatar | 'big.jpg','sml.png' |
| 1 | username | stevejobs |
| 2 | email | def#def.com |
| 2 | avatar | 'big.png','sml.jpeg' |
| 2 | username | billgates |
I am hardly writing this code with ORM MySQL
ORM::table($config['db']['pre'].'user')
ORM::table($config['db']['pre'].'user_option')
Simply that, I just want completely delete all the data from the same user_id.
I just want completely delete all the data from the same user_id.
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 a small database:
+-----------+-----------+------------------------+
| Name | Number | Hobby |
+-----------+-----------+------------------------+
| Alex | 2, 3 | Game, Shopping |
+-----------+------------------------------------+
It's mean Number 2 is Game and Number 3 is Shopping.
How can I show above data like this table
+-----------+-----------+
| 2 | Game |
+-----------+-----------+
| 3 | Shopping |
+-----------+------------
Your database is not normalized. You need a third table that will be what's usually called a join table.
The people table. The primary key is id
+-----------+-----------+
| Id | Name |
+-----------+-----------+
| 1 | Alex |
| 2 | Thor |
| 3 | Iron Man |
| 4 | Dr Stange |
| 5 | Thanos |
+-----------+------------
The hobbies Table
+-----------+-----------+
| Id | Name |
+-----------+-----------+
| 1 | Game |
| 2 | Shopping |
| 3 | Fighting |
+-----------+-----------+
Join table called (for example) people_hobbies
+-----------+-----------+
| person_id | hobby_id |
+-----------+-----------+
| 1 | 1 |
| 1 | 2 |
+-----------+-----------+
This people_hobbies table will use person_id and hobby_id to create a multi field primary key. This will ensure that you will not be able to add the same combination twice... which should not even make sense.
person_id is a foreign key that references the id from the people table.
hobby_id is a foreign key that references the id from the hobbies table.
Having foreign keys will let you avoid having a key in the people_hobbies table that do not exist in both the people and the hobbies table.
The example in the table below shows that the person id 1 has two hobbies (1 and 2). For a human, that translates to Alex's hobbies are Game and Shopping.
The above structure will let you manage your DB the way most people do.
Just keep a few things in mind:
You cannot add anything in people_hobbies before they exist in both people and hobbies tables
You must have the CASCADE UPDATE and CASCADE DELETE to the foreign key definitions so that when you delete a person or a hobby from your tables, it will remove the relationship from the people_hobbies table.
SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+
SELECT * FROM bad_schema;
+------+--------+----------------+
| name | number | hobby |
+------+--------+----------------+
| Alex | 2, 3 | Game, Shopping |
+------+--------+----------------+
CREATE TABLE better_schema AS
SELECT DISTINCT name
, SUBSTRING_INDEX(SUBSTRING_INDEX(number,',',i+1),',',-1) + 0 number
, TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(hobby,',',i+1),',',-1)) hobby
FROM bad_schema
, ints;
SELECT * FROM better_schema;
+------+--------+----------+
| name | number | hobby |
+------+--------+----------+
| Alex | 2 | Game |
| Alex | 3 | Shopping |
+------+--------+----------+
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
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.