Strategy to map multiple lookup tables with system tables? - mysql

I have a bunch of lookup tables:
Religion
Country
City
nationality
Currency
etc... approx 80-100 lookup tables.
Now I have system tables where i am centralizing all the fields and values so i can ID all fields, centralize them for reporting and add multi-language to my lookups. So these are tables like:
Form (all forms on system)
Field (all fields on system)
Value (all values for all fields on system)
Form_Field_value (mapping them together)
Translation (maps value and field to multi language)
but the question is how to get all the 80-100 tables data into these Field / Value tables? So it will be like this:
Field table:
id 1 Natioality
id 2 Country
1d 3 city
...
Value table
1d 1 american
id 2 chinese
id 3 rusian
...
field_value
id 1, field_id1, value_id1
id 2, field_id1, value_id2
id 3, field_id1, value_id3
...
Ofcourse i can manually do it but then it defeats the purpose of having those lookup tables. So ideal is to keep these tables in sync. next question is which tables to use for user forms? The lookup tables or the form_fields table?

union select http://dev.mysql.com/doc/refman/5.0/en/union.html

Related

Multiple values in column MYSQL

How my system works is that a user may be assigned to several things.
A column is called group and they may be assigned to several groups, e.g 1,5,8 etc... Is there a way to store them in the same column?
Don't store relations in single column. Normalize your data and introduce new table which will hold references for user group relations like
Table user_groups
user_id group_id
1 1
1 5
1 8
Also have a look at Is storing a delimited list in a database column really that bad?

Database Schema for Restaurant Table Merging and Unmerging

I need help to design a table for Restaurant Point of Sales System particularly in merging and unmerging restaurants table.
For example, i want to merge tables 1-3 as Table 1,
-Table 1
-Table 2
-Table 3
Table 4
Table 5
Then it would be something like this,
Table 1
-Table 1
-Table 2
-Table 3
Table 4
Table 5
After a transaction the tables can be unmerge. Anyone who has a solution to this?
To expand on what zerkms suggests, you will want a two-tiered system to support this approach. Link table information (server, order, timing, etc) to a Seating. Seating will have a one-to-many relationship with a Table relation.
When a guest is assigned to a single table, one Table will be linked to one Seating. If a group of guests is assigned to multiple tables, multiple Tables will be linked to a single Seating.
When a group of guests leaves, unlink their Tables from their Seating. To count unused tables, simply COUNT entries from Table.
- Seating 1
- Table 1
- Seating 2
- Table 2
- Table 3
- Table 4
Note: Table entries will be static for the lifetime of the restaurant (unless more physical tables are added). Seating entries should be dynamically created and removed as necessary.

sql get data from multiple tables and add the result

My MySQL db has a table called catmaster. It contains various "categories", each with a unique id like CAT12. Each "category" is made up of underlying "keywords" and has its own table with 1 column called key_id. So the table CAT12 might have 4 records (keywords) like the following: KEY1 KEY2 KEY3 KEY4. Each of these keywords also has its own table with the same names. Each of these tables has 2 columns, 1 with a date and the other with an integer value, call it inventory.
Is there a way I can write a query that pulls the SUM of the inventory for the underlying keywords (however many there might be) in a given category for a given date?
or is it best to just have my application loop through the records and pull the value for each keyword then sum it itself?
THANKS!

Setup SQL relationship so that Excel can link 3 tables

I need to establish a relationship in an existing database containing 3 tables for 3 measurement devices A, B, C that will populate these 3 tables independently:
[table1] [table2] [table3]
id id id
mesA_1 mesB_1 mesC_1
mesA_2 mesB_2 mesC_2
Each sample is assigned a unique id, and that's how we can associates the 3 sets of measurements that correspond to the same sample.
So I have to create the 3 tables and the relationship between the three id keys.
In the end I want to be able to query the database from Excel to display in a single line the measurements of the 3 devices.
I followed tutorials but most of the time they're for 2 tables and not 3. Basically I set every id key as PRIMARY but then I do not know if the relationship is two-way or not, if must chain the 3 tables or define a master and two childs. In short I'm clueless.

Store a unique reference to a Mysql Table

I am creating a site that is sort of ecommerce-ish. I want to give my users a perfect search ability using specific attributes that differ from product to product. I plan to create 1 products table storing the basic information that is shared among products i.e Name, Description, Price and a few others. Then I plan to create several "details" table say categories_computers with columns Processor, HDD, RAM, etc and another table say table_shoes with columns MATERIAL, SIZE, GENDER, etc.
I am new to Mysql but not to the concept of Databases. I don't think I will have a problem storing this data to each table. My issue comes about from reads. It won't be hard to query a product id but I think it would be extremely wasteful to query all details tables to get the details of the product since 1 product can only have 1 details.
So my question is how can I store a reference to a table in a column so that a product has say ID, Name, Description, Price, Details_Table_ID or something similar to save on queries. Do tables have unique ids in Mysql? Or how does the Stackoverflow community suggest I go about this? Thanks.
EDIT
Silly me, I have just remembered that every table name is uniques so I can just use that, so my question changes to how I can write a query that contains one cell in a table A to be used as a reference to a Table name.
Don't use separate details tables for each category, use a generic details table that can store any attribute. Its columns would be:
Product_ID INT (FK to Products)
Attribute VARCHAR
Value VARCHAR
The unique key of this table would be (Product_ID, Attribute).
So if Product_ID = 1 is a computer, you would have rows like:
1 Processor Xeon
1 RAM 4GB
1 HDD 1TB
And if Product_ID = 2 is shoes:
2 Material Leather
2 Size 6
2 Gender F
If you're worried about the space used for all those attribute strings, you can add a level of indirection to reduce it. Create another table Attributes that contains all the attribute names. Then use AttributeID in the Details table. This will slow down some queries because you'll need to do an additional join, but could save lots of space
Think about just having a single ProductDetails table like this:
ProductDetailID (PK)
ProductID (foreign key to your Products table)
DetailType
DetailValue
this way you do not have to create new columns every time you add a new product detail type. and you'll have many ProductDetail rows for each productid, which is fine and will query ok. Just be sure to put an index on ProductDetails.ProductID !
Since this is an application so you must be generating the queries. So lets generate it in 2 steps. I assume you can add a column product_type_id in your Product table that will tell you which child table to user. Next create another table Product_type which contains columns product_type_id and query. This query can be used as the base query for creating the final query e.g.
Product_type_id | Query
1 | SELECT COMPUTERS.* FROM COMPUTERS JOIN PRODUCT ON COMPUTERS.PRODUCT_ID = PRODUCT.PRODUCT_ID
2 | SELECT SHOES.* FROM SHOES JOIN PRODUCT ON COMPUTERS.PRODUCT_ID = PRODUCT.PRODUCT_ID
Based on the product_id entered by the user lookup this table to build the base query. Next append your where clause to the query returned.