How to get a lookup through multiple tables - ms-access

I got three tables that are dependent 1t:N after each other.
foo --> fooBar --> fooBarBaz
1:n 1:n
----------------------------
fooID | barID | BazID
Name | Name |
| fooID | barID
I can easily create those with the Lookup & Relationship dialog. In fooBar there is still the fooID, which is displayed with the foo.Name.
Is there now a possibility to display the foo.Nameand the fooBar.Name in the fooBarBaz table ?
Expressions do not allow me to use DLookup, so how can I display that nicely?

Related

Insert date into multiple table or create one table with more column and store at once

Suppose I have several user tables i.e user_table1, user_table2, user_table3, user_table..... . I have created this because if I create single-user table, it will create approx 150 columns. So I separated it with fewer columns table. In user_table1 user_id is set to primary key and rest of the table I set user_id as a foreign key.
user_table1
-------------------------------------------
| user_id | column1 | column2 | column.....|
-------------------------------------------
| 1 | value 1 | value 2 | value .....|
-------------------------------------------
user_table2
-----------------------------------------------
| user_id(fk) | column1 | column2 | column.....|
-----------------------------------------------
| 1 | value 1 | value 2 | value .....|
-----------------------------------------------
user_table3
-----------------------------------------------
| user_id(fk) | column1 | column2 | column.....|
-----------------------------------------------
------ ----- --------- ----- ------- -----
The first table is generally stored for login details and some other value when the user register. So my question is after registration when user edit their profile (profile details value will store into another table i.e user_table2, user_table3) how to insert to another table? Is this method is ok or I should create one table with 150 columns?
I can't think of a case where multiple tables that all have a 1:1 relationship to each other do anything other than add complexity to working with them since simple look-ups become joins, etc. If you really don't have duplicated information, one large table is probably easier to work with. If you are having data repeated across multiple users, then you should probably reevaluate your schema and set up tables that represent different types of information.

How can i simulate a distribution of foreign keys to multiple similar tables?

I am about to create an application, that stores information about products. There is a big list of products(with ID, Name, ...) and they have to be distributed to some grocery stores, represented by tables, with equal columns.
students:
+-------------------------+
| student_ID | Name | ... |
| ... | ... | ... |
store_1
+-----------------------------+
| student_ID | inventory| ... |
| .. | ... | ... |
store_2
+-----------------------------+
| student_ID | inventory| ... |
| .. | ... | ... |
store_3
...
Is there a way to do this better (Without creating 1000 tables for 1000 stores)? I know i could put everything in one table, but if there are 1000s of stores with 1000s of products in each store (=> 1.000.000s of records in one table) it would probably take too long to query. I also thought about creating a json/csv-file for every location, but then there are problems like: How do i sort/query it as fast as in mysql? How can i do sth like 'on delete cascade', ...
Can anybody help me with a better solution/some ideas?
Three tables would do the trick: One for the products and all their attributes (Product_ID, description, vendor codes, etc.); a second table listing the stores (Store_ID, location, sales tax rate, manager, etc.); and finally a table to link the two with a compound primary key of Product_ID, Store_ID that has columns for inventory, price (if different by store, and any other store/product specific attributes).

Copy a table and replace foreign surrogate key column with text column

I am building a database / application in MySQL. I am trying to create a Stored Procedure that returns a table of all children that are currently linked to a parent, for display.
The children table is going to be populated with up to 100,000 records.
I want the returned table to be a copy of the child table, except the foreign key column linking the children to the parent (current and previous) should be replaced by a text column containing the parents name, (I don't want to return a surrogate key for display)
These are my two tables
Parent
PARENTID | PARENTNAME
-------------------------
1 | NAME1
2 | NAMETWO
3 | ANOTHERNAME
Child
CHILDNAME | CURRENTPARENTID | PREVIOUSPARENTID | OTHERDATA COLUMNS...
-----------------------------------------------------------------------
123ABC | 2 | 3 | ..
124ABC | 2 | 1 | ..
125ABC | 1 | 2 | ..
And when I call the stored procedure to return all children with currentparentID = 2, for instance, I would like the table returned to be
CHILDNAME | CURRENTPAR_NAME| PREVIOUSPAR_NAME | OTHERDATA COLUMNS...
-----------------------------------------------------------------------
123ABC | NAMETWO | ANOTHERNAME | ..
224ABC | NAMETWO | NAME1 | ..
I can't figure how the INSERT INTO statement would be made
Would it be easier / more efficent to just return the raw children table filtered to currentparentid = 2, and do the assignment on the application side?
Cheers
How about an insert statement like this:
INSERT INTO NewTable(CHILDNAME, CURRENTPAR_NAME, PREVIOUSPAR_NAME)
SELECT c.CHILDNAME, p1.PARENTNAME, p2.PARENTNAME
FROM Child c
JOIN Parent p1 ON (p1.PARENTID = c.CURRENTPARENTID)
JOIN Parent p2 ON (p2.PARENTID = c.PREVIOUSPARENTID)
;
Depending on the structure of the child table, whether it is active, etc, you could tack on a WHERE clause to do the insert in chunks.

MySQL Multiple references between tables

This question is probably quite easy to answer, but since I haven't got much experience in database design, I'm stuck. I don't even know how to google this because I don't know the terminology ....
I have a mysql database with two tables and in the first table i need to make MULTIPLE references to the second table. What should I use? Can I select multiple matches with Enum? Or should I just use a comma separated list and varchar?
|MAIN TABLE
|==========================================
| id (primary index)
| date (tstamp)
| product name (varchar)
| componentids (int, enum, varchar ???)
|===========================================
|COMPONENTS TABLE
|===========================================
| componentid (int)
| name (varchar)
| info (varchar)
|===========================================
so a possible scenario would be this:
|MAIN TABLE
|=====================================================
| id | 1 | 2 |
| date | 34958734958 | 349587123138 |
| product name | A test product | A second product |
| componentids | 2,3 | 1,2 |
|=====================================================
|COMPONENTS TABLE
|========================================================
| componentid | 1 | 2 | 3 |
| name | Component 1 | Component 2 | Component 1 |
| info | info. text | info. text | info. text |
|========================================================
how do I achieve this in an effective way?
thank you very much for your help!
What you're after is a many-to-many relationship. Each component can belong to multiple products, and each product can have multiple components. I'd strongly recommend using a third table for this, maybe called product_components.
Your main table has (id, date, name)
Your components table has (id, name, info)
Your product_components table has (product_id, component_id). Each of these is a foreign key that references the main table and component table respectively.
This maintains "referential integrity" which means that it becomes impossible to have a product referring to a component that doesn't exist (e.g. the database will throw an error if you try).
And yes, you can select the multiple components associated with one product in one go this way.
SELECT components.*
FROM components
JOIN product_components
ON components.id = product_components.component_id
WHERE product_components.product_id = <some product id>
No comma-separated lists or varchar. That's not the relational way.
How should it go? Are there many rows in the main table for every one in component, or visa versa?
A one-to-many relationship means adding a foreign key to the many table and JOINing the two:
select *
from main
join component
on main.componentid = component.componentid
This will match all the rows in the main table with their component counterpart.

Variable-Length Array in MySQL

I'm writing an application in PHP that uses a MySQL database to store information. One of the pieces of information that it has to store is an array of names, but the array has no set length. It could range from one to many items. Is there a way to store this array in MySQL and be able to retrieve it by specifying just one item from the array?
For example, I want to be able to store something like this in a table:
Foo Bar
-------------------
[baz,car] fiz
[abc,def] ghi
Then, I want to be able to tell MySQL just to search for car and SELECT the first row in the table above.
Is there a feasible way to implement this?
The way to implement it is to "normalized" the schema. There are several related ideas which comprise schema normalization, but the critical one here is that your data should be represented with two tables and a relationship between. The relationship is called, "one to many", since for one "Bar" there are one-or-more "Foo".
A necessary step, then, is to add unique identifier columns to your schema (easiest just to use auto-incrementing integers), and then query using the JOIN mechanism to relate your data.
this is why we call MySQL (and many others) a "relational" database.
Bar
+----+----------+
| id | name |
+----+----------+
| 01 | fiz |
+----+----------+
| 02 | ghi |
+----+----------+
Foo
+----+--------+----------+
| id | bar_id | name |
+----+--------+----------+
| 01 | 01 | baz |
+----+--------+----------+
| 02 | 01 | car |
+----+--------+----------+
| 03 | 02 | abc |
+----+--------+----------+
| 04 | 03 | def |
+----+--------+----------+
And here is what the join looks like to select the "fiz" record based on "car" in the Foo relation
SELECT
Bar.*
FROM Bar
JOIN Foo ON Bar.id = Foo.bar_id
WHERE Foo.name = "car"
And if you want the entire list of Foo relations for each bar with a matching Foo:
SELECT
Bar.*,
GROUP_CONCAT(Foo.name)
FROM Bar
JOIN Foo ON Bar.id = Foo.bar_id
WHERE Foo.name = "car"
GROUP BY Bar.id
Either
SELECT Bar FROM tbl
WHERE FIND_IN_SET('car', Foo)